foreign.c 41 KB

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