foreign.c 37 KB

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