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