foreign.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /* Copyright 2010-2016,2018,2024
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <alignof.h>
  19. #include <alloca.h>
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <ffi.h>
  24. #ifdef HAVE_COMPLEX_H
  25. #include <complex.h>
  26. #endif
  27. #include "boolean.h"
  28. #include "bytevectors.h"
  29. #include "dynwind.h"
  30. #include "eq.h"
  31. #include "eval.h"
  32. #include "extensions.h"
  33. #include "finalizers.h"
  34. #include "gsubr.h"
  35. #include "instructions.h"
  36. #include "intrinsics.h"
  37. #include "keywords.h"
  38. #include "list.h"
  39. #include "modules.h"
  40. #include "numbers.h"
  41. #include "pairs.h"
  42. #include "ports.h"
  43. #include "stacks.h"
  44. #include "symbols.h"
  45. #include "threads.h"
  46. #include "weak-table.h"
  47. #include "version.h"
  48. #include "foreign.h"
  49. /* Return the first integer greater than or equal to LEN such that
  50. LEN % ALIGN == 0. Return LEN if ALIGN is zero. */
  51. #define ROUND_UP(len, align) \
  52. ((align) ? (((len) - 1UL) | ((align) - 1UL)) + 1UL : (len))
  53. SCM_SYMBOL (sym_void, "void");
  54. SCM_SYMBOL (sym_float, "float");
  55. SCM_SYMBOL (sym_double, "double");
  56. SCM_SYMBOL (sym_complex_float, "complex-float");
  57. SCM_SYMBOL (sym_complex_double, "complex-double");
  58. SCM_SYMBOL (sym_uint8, "uint8");
  59. SCM_SYMBOL (sym_int8, "int8");
  60. SCM_SYMBOL (sym_uint16, "uint16");
  61. SCM_SYMBOL (sym_int16, "int16");
  62. SCM_SYMBOL (sym_uint32, "uint32");
  63. SCM_SYMBOL (sym_int32, "int32");
  64. SCM_SYMBOL (sym_uint64, "uint64");
  65. SCM_SYMBOL (sym_int64, "int64");
  66. SCM_SYMBOL (sym_short, "short");
  67. SCM_SYMBOL (sym_int, "int");
  68. SCM_SYMBOL (sym_long, "long");
  69. SCM_SYMBOL (sym_unsigned_short, "unsigned-short");
  70. SCM_SYMBOL (sym_unsigned_int, "unsigned-int");
  71. SCM_SYMBOL (sym_unsigned_long, "unsigned-long");
  72. SCM_SYMBOL (sym_size_t, "size_t");
  73. SCM_SYMBOL (sym_ssize_t, "ssize_t");
  74. SCM_SYMBOL (sym_ptrdiff_t, "ptrdiff_t");
  75. SCM_SYMBOL (sym_intptr_t, "intptr_t");
  76. SCM_SYMBOL (sym_uintptr_t, "uintptr_t");
  77. /* that's for pointers, you know. */
  78. SCM_SYMBOL (sym_asterisk, "*");
  79. SCM_SYMBOL (sym_null, "%null-pointer");
  80. SCM_SYMBOL (sym_null_pointer_error, "null-pointer-error");
  81. /* The cell representing the null pointer. */
  82. static SCM null_pointer;
  83. /* Raise a null pointer dereference error. */
  84. static void
  85. null_pointer_error (const char *func_name)
  86. {
  87. scm_error (sym_null_pointer_error, func_name,
  88. "null pointer dereference", SCM_EOL, SCM_EOL);
  89. }
  90. static SCM cif_to_procedure (SCM cif, SCM func_ptr, int with_errno);
  91. static SCM pointer_weak_refs = SCM_BOOL_F;
  92. static void
  93. register_weak_reference (SCM from, SCM to)
  94. {
  95. scm_weak_table_putq_x (pointer_weak_refs, from, to);
  96. }
  97. static void
  98. pointer_finalizer_trampoline (void *ptr, void *data)
  99. {
  100. scm_t_pointer_finalizer finalizer = data;
  101. finalizer (SCM_POINTER_VALUE (SCM_PACK_POINTER (ptr)));
  102. }
  103. SCM_DEFINE (scm_pointer_p, "pointer?", 1, 0, 0,
  104. (SCM obj),
  105. "Return @code{#t} if @var{obj} is a pointer object, "
  106. "@code{#f} otherwise.\n")
  107. #define FUNC_NAME s_scm_pointer_p
  108. {
  109. return scm_from_bool (SCM_POINTER_P (obj));
  110. }
  111. #undef FUNC_NAME
  112. SCM_DEFINE (scm_make_pointer, "make-pointer", 1, 1, 0,
  113. (SCM address, SCM finalizer),
  114. "Return a foreign pointer object pointing to @var{address}. "
  115. "If @var{finalizer} is passed, it should be a pointer to a "
  116. "one-argument C function that will be called when the pointer "
  117. "object becomes unreachable.")
  118. #define FUNC_NAME s_scm_make_pointer
  119. {
  120. void *c_finalizer;
  121. uintptr_t c_address;
  122. c_address = scm_to_uintptr_t (address);
  123. if (SCM_UNBNDP (finalizer))
  124. c_finalizer = NULL;
  125. else
  126. {
  127. SCM_VALIDATE_POINTER (2, finalizer);
  128. c_finalizer = SCM_POINTER_VALUE (finalizer);
  129. }
  130. return scm_from_pointer ((void *) c_address, c_finalizer);
  131. }
  132. #undef FUNC_NAME
  133. void *
  134. scm_to_pointer (SCM pointer)
  135. #define FUNC_NAME "scm_to_pointer"
  136. {
  137. SCM_VALIDATE_POINTER (1, pointer);
  138. return SCM_POINTER_VALUE (pointer);
  139. }
  140. #undef FUNC_NAME
  141. SCM
  142. scm_from_pointer (void *ptr, scm_t_pointer_finalizer finalizer)
  143. {
  144. SCM ret;
  145. if (ptr == NULL && finalizer == NULL)
  146. ret = null_pointer;
  147. else
  148. {
  149. ret = scm_cell (scm_tc7_pointer, (scm_t_bits) ptr);
  150. if (finalizer)
  151. scm_i_set_finalizer (SCM2PTR (ret), pointer_finalizer_trampoline,
  152. finalizer);
  153. }
  154. return ret;
  155. }
  156. SCM_DEFINE (scm_pointer_address, "pointer-address", 1, 0, 0,
  157. (SCM pointer),
  158. "Return the numerical value of @var{pointer}.")
  159. #define FUNC_NAME s_scm_pointer_address
  160. {
  161. SCM_VALIDATE_POINTER (1, pointer);
  162. return scm_from_uintptr_t ((uintptr_t) SCM_POINTER_VALUE (pointer));
  163. }
  164. #undef FUNC_NAME
  165. SCM_DEFINE (scm_pointer_to_scm, "pointer->scm", 1, 0, 0,
  166. (SCM pointer),
  167. "Unsafely cast @var{pointer} to a Scheme object.\n"
  168. "Cross your fingers!")
  169. #define FUNC_NAME s_scm_pointer_to_scm
  170. {
  171. SCM_VALIDATE_POINTER (1, pointer);
  172. return SCM_PACK ((scm_t_bits) SCM_POINTER_VALUE (pointer));
  173. }
  174. #undef FUNC_NAME
  175. SCM_DEFINE (scm_scm_to_pointer, "scm->pointer", 1, 0, 0,
  176. (SCM scm),
  177. "Return a foreign pointer object with the @code{object-address}\n"
  178. "of @var{scm}.")
  179. #define FUNC_NAME s_scm_scm_to_pointer
  180. {
  181. SCM ret;
  182. ret = scm_from_pointer ((void*) SCM_UNPACK (scm), NULL);
  183. if (SCM_HEAP_OBJECT_P (ret))
  184. register_weak_reference (ret, scm);
  185. return ret;
  186. }
  187. #undef FUNC_NAME
  188. SCM_DEFINE (scm_pointer_to_bytevector, "pointer->bytevector", 2, 2, 0,
  189. (SCM pointer, SCM len, SCM offset, SCM uvec_type),
  190. "Return a bytevector aliasing the @var{len} bytes pointed\n"
  191. "to by @var{pointer}.\n\n"
  192. "The user may specify an alternate default interpretation for\n"
  193. "the memory by passing the @var{uvec_type} argument, to indicate\n"
  194. "that the memory is an array of elements of that type.\n"
  195. "@var{uvec_type} should be something that\n"
  196. "@code{uniform-vector-element-type} would return, like @code{f32}\n"
  197. "or @code{s16}.\n\n"
  198. "When @var{offset} is passed, it specifies the offset in bytes\n"
  199. "relative to @var{pointer} of the memory region aliased by the\n"
  200. "returned bytevector.")
  201. #define FUNC_NAME s_scm_pointer_to_bytevector
  202. {
  203. SCM ret;
  204. int8_t *ptr;
  205. size_t boffset, blen;
  206. scm_t_array_element_type btype;
  207. SCM_VALIDATE_POINTER (1, pointer);
  208. ptr = SCM_POINTER_VALUE (pointer);
  209. if (SCM_UNLIKELY (ptr == NULL))
  210. null_pointer_error (FUNC_NAME);
  211. if (SCM_UNBNDP (uvec_type))
  212. btype = SCM_ARRAY_ELEMENT_TYPE_VU8;
  213. else
  214. {
  215. int i;
  216. for (i = 0; i <= SCM_ARRAY_ELEMENT_TYPE_LAST; i++)
  217. if (scm_is_eq (uvec_type, scm_i_array_element_types[i]))
  218. break;
  219. switch (i)
  220. {
  221. case SCM_ARRAY_ELEMENT_TYPE_VU8:
  222. case SCM_ARRAY_ELEMENT_TYPE_U8:
  223. case SCM_ARRAY_ELEMENT_TYPE_S8:
  224. case SCM_ARRAY_ELEMENT_TYPE_U16:
  225. case SCM_ARRAY_ELEMENT_TYPE_S16:
  226. case SCM_ARRAY_ELEMENT_TYPE_U32:
  227. case SCM_ARRAY_ELEMENT_TYPE_S32:
  228. case SCM_ARRAY_ELEMENT_TYPE_U64:
  229. case SCM_ARRAY_ELEMENT_TYPE_S64:
  230. case SCM_ARRAY_ELEMENT_TYPE_F32:
  231. case SCM_ARRAY_ELEMENT_TYPE_F64:
  232. case SCM_ARRAY_ELEMENT_TYPE_C32:
  233. case SCM_ARRAY_ELEMENT_TYPE_C64:
  234. btype = i;
  235. break;
  236. default:
  237. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, uvec_type,
  238. "uniform vector type");
  239. }
  240. }
  241. if (SCM_UNBNDP (offset))
  242. boffset = 0;
  243. else
  244. boffset = scm_to_size_t (offset);
  245. blen = scm_to_size_t (len);
  246. ret = scm_c_take_typed_bytevector ((signed char *) ptr + boffset,
  247. blen, btype, pointer);
  248. return ret;
  249. }
  250. #undef FUNC_NAME
  251. SCM_DEFINE (scm_bytevector_to_pointer, "bytevector->pointer", 1, 1, 0,
  252. (SCM bv, SCM offset),
  253. "Return a pointer pointer aliasing the memory pointed to by\n"
  254. "@var{bv} or @var{offset} bytes after @var{bv} when @var{offset}\n"
  255. "is passed.")
  256. #define FUNC_NAME s_scm_bytevector_to_pointer
  257. {
  258. SCM ret;
  259. signed char *ptr;
  260. size_t boffset;
  261. SCM_VALIDATE_BYTEVECTOR (1, bv);
  262. ptr = SCM_BYTEVECTOR_CONTENTS (bv);
  263. if (SCM_UNBNDP (offset))
  264. boffset = 0;
  265. else
  266. boffset = scm_to_unsigned_integer (offset, 0,
  267. SCM_BYTEVECTOR_LENGTH (bv) - 1);
  268. ret = scm_from_pointer (ptr + boffset, NULL);
  269. register_weak_reference (ret, bv);
  270. return ret;
  271. }
  272. #undef FUNC_NAME
  273. SCM_DEFINE (scm_set_pointer_finalizer_x, "set-pointer-finalizer!", 2, 0, 0,
  274. (SCM pointer, SCM finalizer),
  275. "Arrange for the C procedure wrapped by @var{finalizer} to be\n"
  276. "called on the pointer wrapped by @var{pointer} when @var{pointer}\n"
  277. "becomes unreachable. Note: the C procedure should not call into\n"
  278. "Scheme. If you need a Scheme finalizer, use guardians.")
  279. #define FUNC_NAME s_scm_set_pointer_finalizer_x
  280. {
  281. SCM_VALIDATE_POINTER (1, pointer);
  282. SCM_VALIDATE_POINTER (2, finalizer);
  283. scm_i_add_finalizer (SCM2PTR (pointer), pointer_finalizer_trampoline,
  284. SCM_POINTER_VALUE (finalizer));
  285. return SCM_UNSPECIFIED;
  286. }
  287. #undef FUNC_NAME
  288. void
  289. scm_i_pointer_print (SCM pointer, SCM port, scm_print_state *pstate)
  290. {
  291. scm_puts ("#<pointer 0x", port);
  292. scm_uintprint (scm_to_uintptr_t (scm_pointer_address (pointer)), 16, port);
  293. scm_putc ('>', port);
  294. }
  295. /* Non-primitive helpers functions. These procedures could be
  296. implemented in terms of the primitives above but would be inefficient
  297. (heap allocation overhead, Scheme/C round trips, etc.) */
  298. SCM_DEFINE (scm_dereference_pointer, "dereference-pointer", 1, 0, 0,
  299. (SCM pointer),
  300. "Assuming @var{pointer} points to a memory region that\n"
  301. "holds a pointer, return this pointer.")
  302. #define FUNC_NAME s_scm_dereference_pointer
  303. {
  304. void **ptr;
  305. SCM_VALIDATE_POINTER (1, pointer);
  306. ptr = SCM_POINTER_VALUE (pointer);
  307. if (SCM_UNLIKELY (ptr == NULL))
  308. null_pointer_error (FUNC_NAME);
  309. return scm_from_pointer (*ptr, NULL);
  310. }
  311. #undef FUNC_NAME
  312. SCM_DEFINE (scm_string_to_pointer, "string->pointer", 1, 1, 0,
  313. (SCM string, SCM encoding),
  314. "Return a foreign pointer to a nul-terminated copy of\n"
  315. "@var{string} in the given @var{encoding}, defaulting to\n"
  316. "the current locale encoding. The C string is freed when\n"
  317. "the returned foreign pointer becomes unreachable.\n\n"
  318. "This is the Scheme equivalent of @code{scm_to_stringn}.")
  319. #define FUNC_NAME s_scm_string_to_pointer
  320. {
  321. SCM_VALIDATE_STRING (1, string);
  322. /* XXX: Finalizers slow down libgc; they could be avoided if
  323. `scm_to_string' & co. were able to use libgc-allocated memory. */
  324. if (SCM_UNBNDP (encoding))
  325. return scm_from_pointer (scm_to_locale_string (string), free);
  326. else
  327. {
  328. char *enc;
  329. SCM ret;
  330. SCM_VALIDATE_STRING (2, encoding);
  331. enc = scm_to_locale_string (encoding);
  332. scm_dynwind_begin (0);
  333. scm_dynwind_free (enc);
  334. ret = scm_from_pointer
  335. (scm_to_stringn (string, NULL, enc,
  336. scm_i_default_string_failed_conversion_handler ()),
  337. free);
  338. scm_dynwind_end ();
  339. return ret;
  340. }
  341. }
  342. #undef FUNC_NAME
  343. SCM_DEFINE (scm_pointer_to_string, "pointer->string", 1, 2, 0,
  344. (SCM pointer, SCM length, SCM encoding),
  345. "Return the string representing the C string pointed to by\n"
  346. "@var{pointer}. If @var{length} is omitted or @code{-1}, the\n"
  347. "string is assumed to be nul-terminated. Otherwise\n"
  348. "@var{length} is the number of bytes in memory pointed to by\n"
  349. "@var{pointer}. The C string is assumed to be in the given\n"
  350. "@var{encoding}, defaulting to the current locale encoding.\n\n"
  351. "This is the Scheme equivalent of @code{scm_from_stringn}.")
  352. #define FUNC_NAME s_scm_pointer_to_string
  353. {
  354. size_t len;
  355. SCM_VALIDATE_POINTER (1, pointer);
  356. if (SCM_UNBNDP (length)
  357. || scm_is_true (scm_eqv_p (length, scm_from_int (-1))))
  358. len = (size_t)-1;
  359. else
  360. len = scm_to_size_t (length);
  361. if (SCM_UNBNDP (encoding))
  362. return scm_from_locale_stringn (SCM_POINTER_VALUE (pointer), len);
  363. else
  364. {
  365. char *enc;
  366. SCM ret;
  367. SCM_VALIDATE_STRING (3, encoding);
  368. enc = scm_to_locale_string (encoding);
  369. scm_dynwind_begin (0);
  370. scm_dynwind_free (enc);
  371. ret = scm_from_stringn (SCM_POINTER_VALUE (pointer), len, enc,
  372. scm_i_default_string_failed_conversion_handler ());
  373. scm_dynwind_end ();
  374. return ret;
  375. }
  376. }
  377. #undef FUNC_NAME
  378. SCM_DEFINE (scm_alignof, "alignof", 1, 0, 0, (SCM type),
  379. "Return the alignment of @var{type}, in bytes.\n\n"
  380. "@var{type} should be a valid C type, like @code{int}.\n"
  381. "Alternately @var{type} may be the symbol @code{*}, in which\n"
  382. "case the alignment of a pointer is returned. @var{type} may\n"
  383. "also be a list of types, in which case the alignment of a\n"
  384. "@code{struct} with ABI-conventional packing is returned.")
  385. #define FUNC_NAME s_scm_alignof
  386. {
  387. if (SCM_I_INUMP (type))
  388. {
  389. switch (SCM_I_INUM (type))
  390. {
  391. case SCM_FOREIGN_TYPE_FLOAT:
  392. return scm_from_size_t (alignof_type (float));
  393. case SCM_FOREIGN_TYPE_DOUBLE:
  394. return scm_from_size_t (alignof_type (double));
  395. case SCM_FOREIGN_TYPE_COMPLEX_FLOAT:
  396. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  397. return scm_from_size_t (alignof_type (float _Complex));
  398. #else
  399. return scm_from_size_t (alignof_type (float));
  400. #endif
  401. case SCM_FOREIGN_TYPE_COMPLEX_DOUBLE:
  402. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  403. return scm_from_size_t (alignof_type (double _Complex));
  404. #else
  405. return scm_from_size_t (alignof_type (double));
  406. #endif
  407. case SCM_FOREIGN_TYPE_UINT8:
  408. return scm_from_size_t (alignof_type (uint8_t));
  409. case SCM_FOREIGN_TYPE_INT8:
  410. return scm_from_size_t (alignof_type (int8_t));
  411. case SCM_FOREIGN_TYPE_UINT16:
  412. return scm_from_size_t (alignof_type (uint16_t));
  413. case SCM_FOREIGN_TYPE_INT16:
  414. return scm_from_size_t (alignof_type (int16_t));
  415. case SCM_FOREIGN_TYPE_UINT32:
  416. return scm_from_size_t (alignof_type (uint32_t));
  417. case SCM_FOREIGN_TYPE_INT32:
  418. return scm_from_size_t (alignof_type (int32_t));
  419. case SCM_FOREIGN_TYPE_UINT64:
  420. return scm_from_size_t (alignof_type (uint64_t));
  421. case SCM_FOREIGN_TYPE_INT64:
  422. return scm_from_size_t (alignof_type (int64_t));
  423. default:
  424. scm_wrong_type_arg (FUNC_NAME, 1, type);
  425. }
  426. }
  427. else if (scm_is_eq (type, sym_asterisk))
  428. /* a pointer */
  429. return scm_from_size_t (alignof_type (void*));
  430. else if (scm_is_pair (type))
  431. {
  432. /* TYPE is a structure. Section 3-3 of the i386, x86_64, PowerPC,
  433. and SPARC P.S. of the System V ABI all say: "Aggregates
  434. (structures and arrays) and unions assume the alignment of
  435. their most strictly aligned component." */
  436. size_t max;
  437. for (max = 0; scm_is_pair (type); type = SCM_CDR (type))
  438. {
  439. size_t align;
  440. align = scm_to_size_t (scm_alignof (SCM_CAR (type)));
  441. if (align > max)
  442. max = align;
  443. }
  444. return scm_from_size_t (max);
  445. }
  446. else
  447. scm_wrong_type_arg (FUNC_NAME, 1, type);
  448. }
  449. #undef FUNC_NAME
  450. SCM_DEFINE (scm_sizeof, "sizeof", 1, 0, 0, (SCM type),
  451. "Return the size of @var{type}, in bytes.\n\n"
  452. "@var{type} should be a valid C type, like @code{int}.\n"
  453. "Alternately @var{type} may be the symbol @code{*}, in which\n"
  454. "case the size of a pointer is returned. @var{type} may also\n"
  455. "be a list of types, in which case the size of a @code{struct}\n"
  456. "with ABI-conventional packing is returned.")
  457. #define FUNC_NAME s_scm_sizeof
  458. {
  459. if (SCM_I_INUMP (type))
  460. {
  461. switch (SCM_I_INUM (type))
  462. {
  463. case SCM_FOREIGN_TYPE_FLOAT:
  464. return scm_from_size_t (sizeof (float));
  465. case SCM_FOREIGN_TYPE_DOUBLE:
  466. return scm_from_size_t (sizeof (double));
  467. case SCM_FOREIGN_TYPE_COMPLEX_FLOAT:
  468. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  469. return scm_from_size_t (sizeof (float _Complex));
  470. #else
  471. return scm_from_size_t (2 * sizeof (float));
  472. #endif
  473. case SCM_FOREIGN_TYPE_COMPLEX_DOUBLE:
  474. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  475. return scm_from_size_t (sizeof (double _Complex));
  476. #else
  477. return scm_from_size_t (2 * sizeof (double));
  478. #endif
  479. case SCM_FOREIGN_TYPE_UINT8:
  480. return scm_from_size_t (sizeof (uint8_t));
  481. case SCM_FOREIGN_TYPE_INT8:
  482. return scm_from_size_t (sizeof (int8_t));
  483. case SCM_FOREIGN_TYPE_UINT16:
  484. return scm_from_size_t (sizeof (uint16_t));
  485. case SCM_FOREIGN_TYPE_INT16:
  486. return scm_from_size_t (sizeof (int16_t));
  487. case SCM_FOREIGN_TYPE_UINT32:
  488. return scm_from_size_t (sizeof (uint32_t));
  489. case SCM_FOREIGN_TYPE_INT32:
  490. return scm_from_size_t (sizeof (int32_t));
  491. case SCM_FOREIGN_TYPE_UINT64:
  492. return scm_from_size_t (sizeof (uint64_t));
  493. case SCM_FOREIGN_TYPE_INT64:
  494. return scm_from_size_t (sizeof (int64_t));
  495. default:
  496. scm_wrong_type_arg (FUNC_NAME, 1, type);
  497. }
  498. }
  499. else if (scm_is_eq (type, sym_asterisk))
  500. /* a pointer */
  501. return scm_from_size_t (sizeof (void*));
  502. else if (scm_is_pair (type))
  503. {
  504. /* a struct */
  505. size_t off = 0;
  506. size_t align = scm_to_size_t (scm_alignof(type));
  507. while (scm_is_pair (type))
  508. {
  509. off = ROUND_UP (off, scm_to_size_t (scm_alignof (scm_car (type))));
  510. off += scm_to_size_t (scm_sizeof (scm_car (type)));
  511. type = scm_cdr (type);
  512. }
  513. return scm_from_size_t (ROUND_UP(off, align));
  514. }
  515. else
  516. scm_wrong_type_arg (FUNC_NAME, 1, type);
  517. }
  518. #undef FUNC_NAME
  519. /* return 1 on success, 0 on failure */
  520. static int
  521. parse_ffi_type (SCM type, int return_p, long *n_structs, long *n_struct_elts)
  522. {
  523. if (SCM_I_INUMP (type))
  524. {
  525. if ((SCM_I_INUM (type) < 0 )
  526. || (SCM_I_INUM (type) > SCM_FOREIGN_TYPE_LAST))
  527. return 0;
  528. else if (SCM_I_INUM (type) == SCM_FOREIGN_TYPE_VOID && !return_p)
  529. return 0;
  530. #ifndef FFI_TARGET_HAS_COMPLEX_TYPE
  531. /* The complex types are always defined so they can be used when
  532. accessing data, but some targets don't support them as
  533. arguments or return values. */
  534. else if (SCM_I_INUM (type) == SCM_FOREIGN_TYPE_COMPLEX_FLOAT
  535. || SCM_I_INUM (type) == SCM_FOREIGN_TYPE_COMPLEX_DOUBLE)
  536. return 0;
  537. #endif
  538. else
  539. return 1;
  540. }
  541. else if (scm_is_eq (type, sym_asterisk))
  542. /* a pointer */
  543. return 1;
  544. else
  545. {
  546. long len;
  547. len = scm_ilength (type);
  548. if (len < 1)
  549. return 0;
  550. while (len--)
  551. {
  552. if (!parse_ffi_type (scm_car (type), 0, n_structs, n_struct_elts))
  553. return 0;
  554. (*n_struct_elts)++;
  555. type = scm_cdr (type);
  556. }
  557. (*n_structs)++;
  558. return 1;
  559. }
  560. }
  561. static void
  562. fill_ffi_type (SCM type, ffi_type *ftype, ffi_type ***type_ptrs,
  563. ffi_type **types)
  564. {
  565. if (SCM_I_INUMP (type))
  566. {
  567. switch (SCM_I_INUM (type))
  568. {
  569. case SCM_FOREIGN_TYPE_FLOAT:
  570. *ftype = ffi_type_float;
  571. return;
  572. case SCM_FOREIGN_TYPE_DOUBLE:
  573. *ftype = ffi_type_double;
  574. return;
  575. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  576. case SCM_FOREIGN_TYPE_COMPLEX_FLOAT:
  577. *ftype = ffi_type_complex_float;
  578. return;
  579. case SCM_FOREIGN_TYPE_COMPLEX_DOUBLE:
  580. *ftype = ffi_type_complex_double;
  581. return;
  582. #endif
  583. case SCM_FOREIGN_TYPE_UINT8:
  584. *ftype = ffi_type_uint8;
  585. return;
  586. case SCM_FOREIGN_TYPE_INT8:
  587. *ftype = ffi_type_sint8;
  588. return;
  589. case SCM_FOREIGN_TYPE_UINT16:
  590. *ftype = ffi_type_uint16;
  591. return;
  592. case SCM_FOREIGN_TYPE_INT16:
  593. *ftype = ffi_type_sint16;
  594. return;
  595. case SCM_FOREIGN_TYPE_UINT32:
  596. *ftype = ffi_type_uint32;
  597. return;
  598. case SCM_FOREIGN_TYPE_INT32:
  599. *ftype = ffi_type_sint32;
  600. return;
  601. case SCM_FOREIGN_TYPE_UINT64:
  602. *ftype = ffi_type_uint64;
  603. return;
  604. case SCM_FOREIGN_TYPE_INT64:
  605. *ftype = ffi_type_sint64;
  606. return;
  607. case SCM_FOREIGN_TYPE_VOID:
  608. *ftype = ffi_type_void;
  609. return;
  610. default:
  611. scm_wrong_type_arg_msg ("pointer->procedure", 0, type,
  612. "foreign type");
  613. }
  614. }
  615. else if (scm_is_eq (type, sym_asterisk))
  616. /* a pointer */
  617. {
  618. *ftype = ffi_type_pointer;
  619. return;
  620. }
  621. else
  622. {
  623. long i, len;
  624. len = scm_ilength (type);
  625. ftype->size = 0;
  626. ftype->alignment = 0;
  627. ftype->type = FFI_TYPE_STRUCT;
  628. ftype->elements = *type_ptrs;
  629. *type_ptrs += len + 1;
  630. for (i = 0; i < len; i++)
  631. {
  632. ftype->elements[i] = *types;
  633. *types += 1;
  634. fill_ffi_type (scm_car (type), ftype->elements[i],
  635. type_ptrs, types);
  636. type = scm_cdr (type);
  637. }
  638. ftype->elements[i] = NULL;
  639. }
  640. }
  641. /* Return a "cif" (call interface) for the given RETURN_TYPE and
  642. ARG_TYPES. */
  643. static ffi_cif *
  644. make_cif (SCM return_type, SCM arg_types, const char *caller)
  645. #define FUNC_NAME caller
  646. {
  647. SCM walk;
  648. long i, nargs, n_structs, n_struct_elts;
  649. size_t cif_len;
  650. char *mem;
  651. ffi_cif *cif;
  652. ffi_type **type_ptrs;
  653. ffi_type *types;
  654. nargs = scm_ilength (arg_types);
  655. SCM_ASSERT (nargs >= 0, arg_types, 3, FUNC_NAME);
  656. /* fixme: assert nargs < 1<<32 */
  657. n_structs = n_struct_elts = 0;
  658. /* For want of talloc, we're going to have to do this in two passes: first we
  659. figure out how much memory is needed for all types, then we allocate the
  660. cif and the types all in one block. */
  661. if (!parse_ffi_type (return_type, 1, &n_structs, &n_struct_elts))
  662. scm_wrong_type_arg (FUNC_NAME, 1, return_type);
  663. for (walk = arg_types; scm_is_pair (walk); walk = scm_cdr (walk))
  664. if (!parse_ffi_type (scm_car (walk), 0, &n_structs, &n_struct_elts))
  665. scm_wrong_type_arg (FUNC_NAME, 3, scm_car (walk));
  666. /* the memory: with space for the cif itself */
  667. cif_len = sizeof (ffi_cif);
  668. /* then ffi_type pointers: one for each arg, one for each struct
  669. element, and one for each struct (for null-termination) */
  670. cif_len = (ROUND_UP (cif_len, alignof_type (void *))
  671. + (nargs + n_structs + n_struct_elts)*sizeof(void*));
  672. /* then the ffi_type structs themselves, one per arg and struct element, and
  673. one for the return val */
  674. cif_len = (ROUND_UP (cif_len, alignof_type (ffi_type))
  675. + (nargs + n_struct_elts + 1)*sizeof(ffi_type));
  676. mem = scm_gc_malloc_pointerless (cif_len, "foreign");
  677. /* ensure all the memory is initialized, even the holes */
  678. memset (mem, 0, cif_len);
  679. cif = (ffi_cif *) mem;
  680. /* reuse cif_len to walk through the mem */
  681. cif_len = ROUND_UP (sizeof (ffi_cif), alignof_type (void *));
  682. type_ptrs = (ffi_type**)(mem + cif_len);
  683. cif_len = ROUND_UP (cif_len
  684. + (nargs + n_structs + n_struct_elts)*sizeof(void*),
  685. alignof_type (ffi_type));
  686. types = (ffi_type*)(mem + cif_len);
  687. /* whew. now knit the pointers together. */
  688. cif->rtype = types++;
  689. fill_ffi_type (return_type, cif->rtype, &type_ptrs, &types);
  690. cif->arg_types = type_ptrs;
  691. type_ptrs += nargs;
  692. for (walk = arg_types, i = 0; scm_is_pair (walk); walk = scm_cdr (walk), i++)
  693. {
  694. cif->arg_types[i] = types++;
  695. fill_ffi_type (scm_car (walk), cif->arg_types[i], &type_ptrs, &types);
  696. }
  697. /* round out the cif, and we're done. */
  698. cif->abi = FFI_DEFAULT_ABI;
  699. cif->nargs = nargs;
  700. cif->bytes = 0;
  701. cif->flags = 0;
  702. if (FFI_OK != ffi_prep_cif (cif, FFI_DEFAULT_ABI, cif->nargs, cif->rtype,
  703. cif->arg_types))
  704. SCM_MISC_ERROR ("ffi_prep_cif failed", SCM_EOL);
  705. return cif;
  706. }
  707. #undef FUNC_NAME
  708. static SCM
  709. pointer_to_procedure (SCM return_type, SCM func_ptr, SCM arg_types,
  710. int with_errno)
  711. #define FUNC_NAME "pointer->procedure"
  712. {
  713. ffi_cif *cif;
  714. SCM_VALIDATE_POINTER (2, func_ptr);
  715. cif = make_cif (return_type, arg_types, FUNC_NAME);
  716. return cif_to_procedure (scm_from_pointer (cif, NULL), func_ptr,
  717. with_errno);
  718. }
  719. #undef FUNC_NAME
  720. SCM
  721. scm_pointer_to_procedure (SCM return_type, SCM func_ptr, SCM arg_types)
  722. {
  723. return pointer_to_procedure (return_type, func_ptr, arg_types, 0);
  724. }
  725. SCM
  726. scm_pointer_to_procedure_with_errno (SCM return_type, SCM func_ptr,
  727. SCM arg_types)
  728. {
  729. return pointer_to_procedure (return_type, func_ptr, arg_types, 1);
  730. }
  731. SCM_KEYWORD (k_return_errno, "return-errno?");
  732. SCM_INTERNAL SCM scm_i_pointer_to_procedure (SCM, SCM, SCM, SCM);
  733. SCM_DEFINE (scm_i_pointer_to_procedure, "pointer->procedure", 3, 0, 1,
  734. (SCM return_type, SCM func_ptr, SCM arg_types, SCM keyword_args),
  735. "Make a foreign function.\n\n"
  736. "Given the foreign void pointer @var{func_ptr}, its argument and\n"
  737. "return types @var{arg_types} and @var{return_type}, return a\n"
  738. "procedure that will pass arguments to the foreign function\n"
  739. "and return appropriate values.\n\n"
  740. "@var{arg_types} should be a list of foreign types.\n"
  741. "@code{return_type} should be a foreign type.\n"
  742. "If the @code{#:return-errno?} keyword argument is provided and\n"
  743. "its value is true, then the returned procedure will return two\n"
  744. "values, with @code{errno} as the second value.")
  745. #define FUNC_NAME s_scm_i_pointer_to_procedure
  746. {
  747. SCM return_errno = SCM_BOOL_F;
  748. scm_c_bind_keyword_arguments (FUNC_NAME, keyword_args, 0,
  749. k_return_errno, &return_errno,
  750. SCM_UNDEFINED);
  751. return pointer_to_procedure (return_type, func_ptr, arg_types,
  752. scm_to_bool (return_errno));
  753. }
  754. #undef FUNC_NAME
  755. static const uint32_t *
  756. get_foreign_stub_code (unsigned int nargs, int with_errno)
  757. {
  758. size_t i;
  759. size_t code_len = with_errno ? 4 : 5;
  760. uint32_t *ret, *code;
  761. if (nargs >= (1 << 24) + 1)
  762. scm_misc_error ("make-foreign-function", "too many arguments: ~a",
  763. scm_list_1 (scm_from_uint (nargs)));
  764. ret = scm_i_alloc_primitive_code_with_instrumentation (code_len, &code);
  765. i = 0;
  766. code[i++] = SCM_PACK_OP_24 (assert_nargs_ee, nargs + 1);
  767. code[i++] = SCM_PACK_OP_12_12 (foreign_call, 0, 1);
  768. code[i++] = SCM_PACK_OP_24 (handle_interrupts, 0);
  769. if (!with_errno)
  770. code[i++] = SCM_PACK_OP_24 (reset_frame, 1);
  771. code[i++] = SCM_PACK_OP_24 (return_values, 0);
  772. return ret;
  773. }
  774. static SCM
  775. cif_to_procedure (SCM cif, SCM func_ptr, int with_errno)
  776. {
  777. ffi_cif *c_cif;
  778. SCM ret;
  779. scm_t_bits nfree = 2;
  780. scm_t_bits flags = SCM_F_PROGRAM_IS_FOREIGN;
  781. c_cif = (ffi_cif *) SCM_POINTER_VALUE (cif);
  782. ret = scm_words (scm_tc7_program | (nfree << 16) | flags, nfree + 2);
  783. SCM_SET_CELL_WORD_1 (ret, get_foreign_stub_code (c_cif->nargs, with_errno));
  784. SCM_PROGRAM_FREE_VARIABLE_SET (ret, 0, cif);
  785. SCM_PROGRAM_FREE_VARIABLE_SET (ret, 1, func_ptr);
  786. return ret;
  787. }
  788. /* Set *LOC to the foreign representation of X with TYPE. */
  789. static void
  790. unpack (const ffi_type *type, void *loc, SCM x, int return_value_p)
  791. #define FUNC_NAME "foreign-call"
  792. {
  793. switch (type->type)
  794. {
  795. case FFI_TYPE_FLOAT:
  796. *(float *) loc = scm_to_double (x);
  797. break;
  798. case FFI_TYPE_DOUBLE:
  799. *(double *) loc = scm_to_double (x);
  800. break;
  801. /* no FFI_TYPE_xxx_COMPLEX or (FFI_TYPE_COMPLEX_xxx) :-| */
  802. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  803. case FFI_TYPE_COMPLEX:
  804. {
  805. double re = scm_to_double (scm_real_part(x));
  806. double im = scm_to_double (scm_imag_part(x));
  807. if (sizeof (float _Complex) == type->size)
  808. *(float _Complex *) loc = (float)re + _Complex_I * (float)im;
  809. else if (sizeof (double _Complex) == type->size)
  810. *(double _Complex *) loc = re + _Complex_I * im;
  811. else
  812. abort();
  813. }
  814. break;
  815. #endif
  816. /* For integer return values smaller than `int', libffi expects the
  817. result in an `ffi_arg'-long buffer. */
  818. case FFI_TYPE_UINT8:
  819. if (return_value_p)
  820. *(ffi_arg *) loc = scm_to_uint8 (x);
  821. else
  822. *(uint8_t *) loc = scm_to_uint8 (x);
  823. break;
  824. case FFI_TYPE_SINT8:
  825. if (return_value_p)
  826. *(ffi_arg *) loc = scm_to_int8 (x);
  827. else
  828. *(int8_t *) loc = scm_to_int8 (x);
  829. break;
  830. case FFI_TYPE_UINT16:
  831. if (return_value_p)
  832. *(ffi_arg *) loc = scm_to_uint16 (x);
  833. else
  834. *(uint16_t *) loc = scm_to_uint16 (x);
  835. break;
  836. case FFI_TYPE_SINT16:
  837. if (return_value_p)
  838. *(ffi_arg *) loc = scm_to_int16 (x);
  839. else
  840. *(int16_t *) loc = scm_to_int16 (x);
  841. break;
  842. case FFI_TYPE_UINT32:
  843. if (return_value_p)
  844. *(ffi_arg *) loc = scm_to_uint32 (x);
  845. else
  846. *(uint32_t *) loc = scm_to_uint32 (x);
  847. break;
  848. case FFI_TYPE_SINT32:
  849. if (return_value_p)
  850. *(ffi_arg *) loc = scm_to_int32 (x);
  851. else
  852. *(int32_t *) loc = scm_to_int32 (x);
  853. break;
  854. case FFI_TYPE_UINT64:
  855. *(uint64_t *) loc = scm_to_uint64 (x);
  856. break;
  857. case FFI_TYPE_SINT64:
  858. *(int64_t *) loc = scm_to_int64 (x);
  859. break;
  860. case FFI_TYPE_STRUCT:
  861. SCM_VALIDATE_POINTER (1, x);
  862. memcpy (loc, SCM_POINTER_VALUE (x), type->size);
  863. break;
  864. case FFI_TYPE_POINTER:
  865. SCM_VALIDATE_POINTER (1, x);
  866. *(void **) loc = SCM_POINTER_VALUE (x);
  867. break;
  868. case FFI_TYPE_VOID:
  869. /* Do nothing. */
  870. break;
  871. default:
  872. abort ();
  873. }
  874. }
  875. #undef FUNC_NAME
  876. /* Return a Scheme representation of the foreign value at LOC of type
  877. TYPE. When RETURN_VALUE_P is true, LOC is assumed to point to a
  878. return value buffer; otherwise LOC is assumed to point to an
  879. argument buffer. */
  880. static SCM
  881. pack (const ffi_type * type, const void *loc, int return_value_p)
  882. {
  883. switch (type->type)
  884. {
  885. case FFI_TYPE_VOID:
  886. return SCM_UNSPECIFIED;
  887. case FFI_TYPE_FLOAT:
  888. return scm_from_double (*(float *) loc);
  889. case FFI_TYPE_DOUBLE:
  890. return scm_from_double (*(double *) loc);
  891. /* no FFI_TYPE_xxx_COMPLEX or (FFI_TYPE_COMPLEX_xxx) :-| */
  892. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  893. case FFI_TYPE_COMPLEX:
  894. {
  895. double re, im;
  896. if (sizeof (float _Complex) == type->size)
  897. {
  898. re = crealf(*(float _Complex *) loc);
  899. im = cimagf(*(float _Complex *) loc);
  900. }
  901. else if (sizeof (double _Complex) == type->size)
  902. {
  903. re = creal(*(double _Complex *) loc);
  904. im = cimag(*(double _Complex *) loc);
  905. }
  906. else
  907. abort ();
  908. return scm_make_rectangular (scm_from_double (re), scm_from_double (im));
  909. }
  910. #endif
  911. /* For integer return values smaller than `int', libffi stores the
  912. result in an `ffi_arg'-long buffer, of which only the
  913. significant bits must be kept---hence the pair of casts below.
  914. See <http://thread.gmane.org/gmane.comp.lib.ffi.general/406>
  915. for details. */
  916. case FFI_TYPE_UINT8:
  917. if (return_value_p)
  918. return scm_from_uint8 ((uint8_t) *(ffi_arg *) loc);
  919. else
  920. return scm_from_uint8 (* (uint8_t *) loc);
  921. case FFI_TYPE_SINT8:
  922. if (return_value_p)
  923. return scm_from_int8 ((int8_t) *(ffi_arg *) loc);
  924. else
  925. return scm_from_int8 (* (int8_t *) loc);
  926. case FFI_TYPE_UINT16:
  927. if (return_value_p)
  928. return scm_from_uint16 ((uint16_t) *(ffi_arg *) loc);
  929. else
  930. return scm_from_uint16 (* (uint16_t *) loc);
  931. case FFI_TYPE_SINT16:
  932. if (return_value_p)
  933. return scm_from_int16 ((int16_t) *(ffi_arg *) loc);
  934. else
  935. return scm_from_int16 (* (int16_t *) loc);
  936. case FFI_TYPE_UINT32:
  937. if (return_value_p)
  938. return scm_from_uint32 ((uint32_t) *(ffi_arg *) loc);
  939. else
  940. return scm_from_uint32 (* (uint32_t *) loc);
  941. case FFI_TYPE_SINT32:
  942. if (return_value_p)
  943. return scm_from_int32 ((int32_t) *(ffi_arg *) loc);
  944. else
  945. return scm_from_int32 (* (int32_t *) loc);
  946. case FFI_TYPE_UINT64:
  947. return scm_from_uint64 (*(uint64_t *) loc);
  948. case FFI_TYPE_SINT64:
  949. return scm_from_int64 (*(int64_t *) loc);
  950. case FFI_TYPE_STRUCT:
  951. {
  952. void *mem = scm_gc_malloc_pointerless (type->size, "foreign");
  953. memcpy (mem, loc, type->size);
  954. return scm_from_pointer (mem, NULL);
  955. }
  956. case FFI_TYPE_POINTER:
  957. return scm_from_pointer (*(void **) loc, NULL);
  958. default:
  959. abort ();
  960. }
  961. }
  962. #define MAX(A, B) ((A) >= (B) ? (A) : (B))
  963. SCM
  964. scm_i_foreign_call (SCM cif_scm, SCM pointer_scm, int *errno_ret,
  965. const union scm_vm_stack_element *argv)
  966. {
  967. /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
  968. objtable. */
  969. ffi_cif *cif;
  970. void (*func) (void);
  971. uint8_t *data;
  972. void *rvalue;
  973. void **args;
  974. unsigned i;
  975. size_t arg_size;
  976. ptrdiff_t off;
  977. cif = SCM_POINTER_VALUE (cif_scm);
  978. func = SCM_POINTER_VALUE (pointer_scm);
  979. /* Argument pointers. */
  980. args = alloca (sizeof (void *) * cif->nargs);
  981. /* Compute the worst-case amount of memory needed to store all the argument
  982. values. Note: as of libffi 3.0.9 `cif->bytes' is undocumented and is zero,
  983. so it can't be used for that purpose. */
  984. for (i = 0, arg_size = 0; i < cif->nargs; i++)
  985. arg_size += cif->arg_types[i]->size + cif->arg_types[i]->alignment - 1;
  986. /* Space for argument values, followed by return value. */
  987. data = alloca (arg_size + cif->rtype->size
  988. + MAX (sizeof (void *), cif->rtype->alignment));
  989. /* Unpack ARGV to native values, setting ARGV pointers. */
  990. for (i = 0, off = 0;
  991. i < cif->nargs;
  992. off = (uint8_t *) args[i] - data + cif->arg_types[i]->size,
  993. i++)
  994. {
  995. /* Suitably align the storage area for argument I. */
  996. args[i] = (void *) ROUND_UP ((uintptr_t) data + off,
  997. cif->arg_types[i]->alignment);
  998. assert ((uintptr_t) args[i] % cif->arg_types[i]->alignment == 0);
  999. unpack (cif->arg_types[i], args[i], argv[cif->nargs - i - 1].as_scm, 0);
  1000. }
  1001. /* Prepare space for the return value. On some platforms, such as
  1002. `armv5tel-*-linux-gnueabi', the return value has to be at least
  1003. word-aligned, even if its type doesn't have any alignment requirement as is
  1004. the case with `char'. */
  1005. rvalue = (void *) ROUND_UP ((uintptr_t) data + off,
  1006. MAX (sizeof (void *), cif->rtype->alignment));
  1007. /* off we go! */
  1008. errno = 0;
  1009. ffi_call (cif, func, rvalue, args);
  1010. *errno_ret = errno;
  1011. return pack (cif->rtype, rvalue, 1);
  1012. }
  1013. /* Function pointers aka. "callbacks" or "closures". */
  1014. #ifdef FFI_CLOSURES
  1015. /* Trampoline to invoke a libffi closure that wraps a Scheme
  1016. procedure. */
  1017. static void
  1018. invoke_closure (ffi_cif *cif, void *ret, void **args, void *data)
  1019. {
  1020. size_t i;
  1021. SCM proc, *argv, result;
  1022. proc = SCM_PACK_POINTER (data);
  1023. argv = alloca (cif->nargs * sizeof (*argv));
  1024. /* Pack ARGS to SCM values, setting ARGV pointers. */
  1025. for (i = 0; i < cif->nargs; i++)
  1026. argv[i] = pack (cif->arg_types[i], args[i], 0);
  1027. result = scm_call_n (proc, argv, cif->nargs);
  1028. unpack (cif->rtype, ret, result, 1);
  1029. }
  1030. SCM_DEFINE (scm_procedure_to_pointer, "procedure->pointer", 3, 0, 0,
  1031. (SCM return_type, SCM proc, SCM arg_types),
  1032. "Return a pointer to a C function of type @var{return_type}\n"
  1033. "taking arguments of types @var{arg_types} (a list) and\n"
  1034. "behaving as a proxy to procedure @var{proc}. Thus\n"
  1035. "@var{proc}'s arity, supported argument types, and return\n"
  1036. "type should match @var{return_type} and @var{arg_types}.\n")
  1037. #define FUNC_NAME s_scm_procedure_to_pointer
  1038. {
  1039. SCM cif_pointer, pointer;
  1040. ffi_cif *cif;
  1041. ffi_status err;
  1042. void *closure, *executable;
  1043. cif = make_cif (return_type, arg_types, FUNC_NAME);
  1044. closure = ffi_closure_alloc (sizeof (ffi_closure), &executable);
  1045. err = ffi_prep_closure_loc ((ffi_closure *) closure, cif,
  1046. invoke_closure, SCM_UNPACK_POINTER (proc),
  1047. executable);
  1048. if (err != FFI_OK)
  1049. {
  1050. ffi_closure_free (closure);
  1051. SCM_MISC_ERROR ("`ffi_prep_closure_loc' failed", SCM_EOL);
  1052. }
  1053. /* CIF points to GC-managed memory and it should remain as long as
  1054. POINTER (see below) is live. Wrap it in a Scheme pointer to then
  1055. hold a weak reference on it. */
  1056. cif_pointer = scm_from_pointer (cif, NULL);
  1057. if (closure == executable)
  1058. {
  1059. pointer = scm_from_pointer (executable, ffi_closure_free);
  1060. register_weak_reference (pointer,
  1061. scm_list_2 (proc, cif_pointer));
  1062. }
  1063. else
  1064. {
  1065. /* CLOSURE needs to be freed eventually. However, since
  1066. `GC_all_interior_pointers' is disabled, we can't just register
  1067. a finalizer for CLOSURE. Instead, we create a pointer object
  1068. for CLOSURE, with a finalizer, and register it as a weak
  1069. reference of POINTER. */
  1070. SCM friend;
  1071. pointer = scm_from_pointer (executable, NULL);
  1072. friend = scm_from_pointer (closure, ffi_closure_free);
  1073. register_weak_reference (pointer,
  1074. scm_list_3 (proc, cif_pointer, friend));
  1075. }
  1076. return pointer;
  1077. }
  1078. #undef FUNC_NAME
  1079. #endif /* FFI_CLOSURES */
  1080. static void
  1081. scm_init_foreign (void)
  1082. {
  1083. #ifndef SCM_MAGIC_SNARFER
  1084. #include "foreign.x"
  1085. #endif
  1086. scm_define (sym_void, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID));
  1087. scm_define (sym_float, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT));
  1088. scm_define (sym_double, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE));
  1089. scm_define (sym_complex_float, scm_from_uint8 (SCM_FOREIGN_TYPE_COMPLEX_FLOAT));
  1090. scm_define (sym_complex_double, scm_from_uint8 (SCM_FOREIGN_TYPE_COMPLEX_DOUBLE));
  1091. scm_define (sym_uint8, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8));
  1092. scm_define (sym_int8, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8));
  1093. scm_define (sym_uint16, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16));
  1094. scm_define (sym_int16, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16));
  1095. scm_define (sym_uint32, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32));
  1096. scm_define (sym_int32, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32));
  1097. scm_define (sym_uint64, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64));
  1098. scm_define (sym_int64, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64));
  1099. scm_define (sym_short,
  1100. #if SIZEOF_SHORT == 8
  1101. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1102. #elif SIZEOF_SHORT == 4
  1103. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1104. #elif SIZEOF_SHORT == 2
  1105. scm_from_uint8 (SCM_FOREIGN_TYPE_INT16)
  1106. #else
  1107. # error unsupported sizeof (short)
  1108. #endif
  1109. );
  1110. scm_define (sym_unsigned_short,
  1111. #if SIZEOF_SHORT == 8
  1112. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1113. #elif SIZEOF_SHORT == 4
  1114. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1115. #elif SIZEOF_SHORT == 2
  1116. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16)
  1117. #else
  1118. # error unsupported sizeof (short)
  1119. #endif
  1120. );
  1121. scm_define (sym_int,
  1122. #if SIZEOF_INT == 8
  1123. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1124. #elif SIZEOF_INT == 4
  1125. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1126. #else
  1127. # error unsupported sizeof (int)
  1128. #endif
  1129. );
  1130. scm_define (sym_unsigned_int,
  1131. #if SIZEOF_UNSIGNED_INT == 8
  1132. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1133. #elif SIZEOF_UNSIGNED_INT == 4
  1134. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1135. #else
  1136. # error unsupported sizeof (unsigned int)
  1137. #endif
  1138. );
  1139. scm_define (sym_long,
  1140. #if SIZEOF_LONG == 8
  1141. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1142. #elif SIZEOF_LONG == 4
  1143. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1144. #else
  1145. # error unsupported sizeof (long)
  1146. #endif
  1147. );
  1148. scm_define (sym_unsigned_long,
  1149. #if SIZEOF_UNSIGNED_LONG == 8
  1150. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1151. #elif SIZEOF_UNSIGNED_LONG == 4
  1152. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1153. #else
  1154. # error unsupported sizeof (unsigned long)
  1155. #endif
  1156. );
  1157. scm_define (sym_size_t,
  1158. #if SIZEOF_SIZE_T == 8
  1159. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1160. #elif SIZEOF_SIZE_T == 4
  1161. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1162. #else
  1163. # error unsupported sizeof (size_t)
  1164. #endif
  1165. );
  1166. scm_define (sym_ssize_t,
  1167. #if SIZEOF_SIZE_T == 8
  1168. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1169. #elif SIZEOF_SIZE_T == 4
  1170. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1171. #else
  1172. # error unsupported sizeof (ssize_t)
  1173. #endif
  1174. );
  1175. scm_define (sym_ptrdiff_t,
  1176. #if SCM_SIZEOF_SCM_T_PTRDIFF == 8
  1177. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1178. #elif SCM_SIZEOF_SCM_T_PTRDIFF == 4
  1179. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1180. #else
  1181. # error unsupported sizeof (ptrdiff_t)
  1182. #endif
  1183. );
  1184. scm_define (sym_intptr_t,
  1185. #if SCM_SIZEOF_INTPTR_T == 8
  1186. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1187. #elif SCM_SIZEOF_INTPTR_T == 4
  1188. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1189. #else
  1190. # error unsupported sizeof (intptr_t)
  1191. #endif
  1192. );
  1193. scm_define (sym_uintptr_t,
  1194. #if SCM_SIZEOF_UINTPTR_T == 8
  1195. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1196. #elif SCM_SIZEOF_UINTPTR_T == 4
  1197. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1198. #else
  1199. # error unsupported sizeof (uintptr_t)
  1200. #endif
  1201. );
  1202. null_pointer = scm_cell (scm_tc7_pointer, 0);
  1203. scm_define (sym_null, null_pointer);
  1204. }
  1205. void
  1206. scm_register_foreign (void)
  1207. {
  1208. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1209. "scm_init_foreign",
  1210. (scm_t_extension_init_func)scm_init_foreign,
  1211. NULL);
  1212. pointer_weak_refs = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  1213. }