1 /* Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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.
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.
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
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"
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");
57 /* that's for pointers, you know. */
58 SCM_SYMBOL (sym_asterisk
, "*");
60 SCM_SYMBOL (sym_null
, "%null-pointer");
61 SCM_SYMBOL (sym_null_pointer_error
, "null-pointer-error");
63 /* The cell representing the null pointer. */
64 static SCM null_pointer
;
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
73 # error unsupported pointer size
77 /* Raise a null pointer dereference error. */
79 null_pointer_error (const char *func_name
)
81 scm_error (sym_null_pointer_error
, func_name
,
82 "null pointer dereference", SCM_EOL
, SCM_EOL
);
86 static SCM
cif_to_procedure (SCM cif
, SCM func_ptr
);
89 static SCM pointer_weak_refs
= SCM_BOOL_F
;
93 register_weak_reference (SCM from
, SCM to
)
95 scm_weak_table_putq_x (pointer_weak_refs
, from
, to
);
99 pointer_finalizer_trampoline (GC_PTR ptr
, GC_PTR data
)
101 scm_t_pointer_finalizer finalizer
= data
;
102 finalizer (SCM_POINTER_VALUE (SCM_PACK_POINTER (ptr
)));
105 SCM_DEFINE (scm_pointer_p
, "pointer?", 1, 0, 0,
107 "Return @code{#t} if @var{obj} is a pointer object, "
108 "@code{#f} otherwise.\n")
109 #define FUNC_NAME s_scm_pointer_p
111 return scm_from_bool (SCM_POINTER_P (obj
));
115 SCM_DEFINE (scm_make_pointer
, "make-pointer", 1, 1, 0,
116 (SCM address
, SCM finalizer
),
117 "Return a foreign pointer object pointing to @var{address}. "
118 "If @var{finalizer} is passed, it should be a pointer to a "
119 "one-argument C function that will be called when the pointer "
120 "object becomes unreachable.")
121 #define FUNC_NAME s_scm_make_pointer
124 scm_t_uintptr c_address
;
126 c_address
= scm_to_uintptr (address
);
127 if (SCM_UNBNDP (finalizer
))
131 SCM_VALIDATE_POINTER (2, finalizer
);
132 c_finalizer
= SCM_POINTER_VALUE (finalizer
);
135 return scm_from_pointer ((void *) c_address
, c_finalizer
);
140 scm_from_pointer (void *ptr
, scm_t_pointer_finalizer finalizer
)
144 if (ptr
== NULL
&& finalizer
== NULL
)
148 ret
= scm_cell (scm_tc7_pointer
, (scm_t_bits
) ptr
);
152 /* Register a finalizer for the newly created instance. */
153 GC_finalization_proc prev_finalizer
;
154 GC_PTR prev_finalizer_data
;
155 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret
),
156 pointer_finalizer_trampoline
,
159 &prev_finalizer_data
);
166 SCM_DEFINE (scm_pointer_address
, "pointer-address", 1, 0, 0,
168 "Return the numerical value of @var{pointer}.")
169 #define FUNC_NAME s_scm_pointer_address
171 SCM_VALIDATE_POINTER (1, pointer
);
173 return scm_from_uintptr ((scm_t_uintptr
) SCM_POINTER_VALUE (pointer
));
177 SCM_DEFINE (scm_pointer_to_scm
, "pointer->scm", 1, 0, 0,
179 "Unsafely cast @var{pointer} to a Scheme object.\n"
180 "Cross your fingers!")
181 #define FUNC_NAME s_scm_pointer_to_scm
183 SCM_VALIDATE_POINTER (1, pointer
);
185 return SCM_PACK ((scm_t_bits
) SCM_POINTER_VALUE (pointer
));
189 SCM_DEFINE (scm_scm_to_pointer
, "scm->pointer", 1, 0, 0,
191 "Return a foreign pointer object with the @code{object-address}\n"
193 #define FUNC_NAME s_scm_scm_to_pointer
197 ret
= scm_from_pointer ((void*) SCM_UNPACK (scm
), NULL
);
199 register_weak_reference (ret
, scm
);
205 SCM_DEFINE (scm_pointer_to_bytevector
, "pointer->bytevector", 2, 2, 0,
206 (SCM pointer
, SCM len
, SCM offset
, SCM uvec_type
),
207 "Return a bytevector aliasing the @var{len} bytes pointed\n"
208 "to by @var{pointer}.\n\n"
209 "The user may specify an alternate default interpretation for\n"
210 "the memory by passing the @var{uvec_type} argument, to indicate\n"
211 "that the memory is an array of elements of that type.\n"
212 "@var{uvec_type} should be something that\n"
213 "@code{uniform-vector-element-type} would return, like @code{f32}\n"
215 "When @var{offset} is passed, it specifies the offset in bytes\n"
216 "relative to @var{pointer} of the memory region aliased by the\n"
217 "returned bytevector.")
218 #define FUNC_NAME s_scm_pointer_to_bytevector
222 size_t boffset
, blen
;
223 scm_t_array_element_type btype
;
225 SCM_VALIDATE_POINTER (1, pointer
);
226 ptr
= SCM_POINTER_VALUE (pointer
);
228 if (SCM_UNLIKELY (ptr
== NULL
))
229 null_pointer_error (FUNC_NAME
);
231 if (SCM_UNBNDP (uvec_type
))
232 btype
= SCM_ARRAY_ELEMENT_TYPE_VU8
;
236 for (i
= 0; i
<= SCM_ARRAY_ELEMENT_TYPE_LAST
; i
++)
237 if (scm_is_eq (uvec_type
, scm_i_array_element_types
[i
]))
241 case SCM_ARRAY_ELEMENT_TYPE_VU8
:
242 case SCM_ARRAY_ELEMENT_TYPE_U8
:
243 case SCM_ARRAY_ELEMENT_TYPE_S8
:
244 case SCM_ARRAY_ELEMENT_TYPE_U16
:
245 case SCM_ARRAY_ELEMENT_TYPE_S16
:
246 case SCM_ARRAY_ELEMENT_TYPE_U32
:
247 case SCM_ARRAY_ELEMENT_TYPE_S32
:
248 case SCM_ARRAY_ELEMENT_TYPE_U64
:
249 case SCM_ARRAY_ELEMENT_TYPE_S64
:
250 case SCM_ARRAY_ELEMENT_TYPE_F32
:
251 case SCM_ARRAY_ELEMENT_TYPE_F64
:
252 case SCM_ARRAY_ELEMENT_TYPE_C32
:
253 case SCM_ARRAY_ELEMENT_TYPE_C64
:
257 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, uvec_type
,
258 "uniform vector type");
262 if (SCM_UNBNDP (offset
))
265 boffset
= scm_to_size_t (offset
);
267 blen
= scm_to_size_t (len
);
269 ret
= scm_c_take_typed_bytevector (ptr
+ boffset
, blen
, btype
, pointer
);
275 SCM_DEFINE (scm_bytevector_to_pointer
, "bytevector->pointer", 1, 1, 0,
276 (SCM bv
, SCM offset
),
277 "Return a pointer pointer aliasing the memory pointed to by\n"
278 "@var{bv} or @var{offset} bytes after @var{bv} when @var{offset}\n"
280 #define FUNC_NAME s_scm_bytevector_to_pointer
286 SCM_VALIDATE_BYTEVECTOR (1, bv
);
287 ptr
= SCM_BYTEVECTOR_CONTENTS (bv
);
289 if (SCM_UNBNDP (offset
))
292 boffset
= scm_to_unsigned_integer (offset
, 0,
293 SCM_BYTEVECTOR_LENGTH (bv
) - 1);
295 ret
= scm_from_pointer (ptr
+ boffset
, NULL
);
296 register_weak_reference (ret
, bv
);
301 SCM_DEFINE (scm_set_pointer_finalizer_x
, "set-pointer-finalizer!", 2, 0, 0,
302 (SCM pointer
, SCM finalizer
),
303 "Arrange for the C procedure wrapped by @var{finalizer} to be\n"
304 "called on the pointer wrapped by @var{pointer} when @var{pointer}\n"
305 "becomes unreachable. Note: the C procedure should not call into\n"
306 "Scheme. If you need a Scheme finalizer, use guardians.")
307 #define FUNC_NAME s_scm_set_pointer_finalizer_x
310 GC_finalization_proc prev_finalizer
;
311 GC_PTR prev_finalizer_data
;
313 SCM_VALIDATE_POINTER (1, pointer
);
314 SCM_VALIDATE_POINTER (2, finalizer
);
316 c_finalizer
= SCM_POINTER_VALUE (finalizer
);
318 SCM_SET_CELL_WORD_0 (pointer
, SCM_CELL_WORD_0 (pointer
) | (1 << 16UL));
320 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (pointer
),
321 pointer_finalizer_trampoline
,
324 &prev_finalizer_data
);
326 return SCM_UNSPECIFIED
;
331 scm_i_pointer_print (SCM pointer
, SCM port
, scm_print_state
*pstate
)
333 scm_puts ("#<pointer 0x", port
);
334 scm_uintprint (scm_to_uintptr (scm_pointer_address (pointer
)), 16, port
);
335 scm_putc ('>', port
);
339 /* Non-primitive helpers functions. These procedures could be
340 implemented in terms of the primitives above but would be inefficient
341 (heap allocation overhead, Scheme/C round trips, etc.) */
343 SCM_DEFINE (scm_dereference_pointer
, "dereference-pointer", 1, 0, 0,
345 "Assuming @var{pointer} points to a memory region that\n"
346 "holds a pointer, return this pointer.")
347 #define FUNC_NAME s_scm_dereference_pointer
349 SCM_VALIDATE_POINTER (1, pointer
);
351 return scm_from_pointer (* (void **) SCM_POINTER_VALUE (pointer
), NULL
);
355 SCM_DEFINE (scm_string_to_pointer
, "string->pointer", 1, 1, 0,
356 (SCM string
, SCM encoding
),
357 "Return a foreign pointer to a nul-terminated copy of\n"
358 "@var{string} in the given @var{encoding}, defaulting to\n"
359 "the current locale encoding. The C string is freed when\n"
360 "the returned foreign pointer becomes unreachable.\n\n"
361 "This is the Scheme equivalent of @code{scm_to_stringn}.")
362 #define FUNC_NAME s_scm_string_to_pointer
364 SCM_VALIDATE_STRING (1, string
);
366 /* XXX: Finalizers slow down libgc; they could be avoided if
367 `scm_to_string' & co. were able to use libgc-allocated memory. */
369 if (SCM_UNBNDP (encoding
))
370 return scm_from_pointer (scm_to_locale_string (string
), free
);
376 SCM_VALIDATE_STRING (2, encoding
);
378 enc
= scm_to_locale_string (encoding
);
379 scm_dynwind_begin (0);
380 scm_dynwind_free (enc
);
382 ret
= scm_from_pointer
383 (scm_to_stringn (string
, NULL
, enc
,
384 scm_i_get_conversion_strategy (SCM_BOOL_F
)),
394 SCM_DEFINE (scm_pointer_to_string
, "pointer->string", 1, 2, 0,
395 (SCM pointer
, SCM length
, SCM encoding
),
396 "Return the string representing the C string pointed to by\n"
397 "@var{pointer}. If @var{length} is omitted or @code{-1}, the\n"
398 "string is assumed to be nul-terminated. Otherwise\n"
399 "@var{length} is the number of bytes in memory pointed to by\n"
400 "@var{pointer}. The C string is assumed to be in the given\n"
401 "@var{encoding}, defaulting to the current locale encoding.\n\n"
402 "This is the Scheme equivalent of @code{scm_from_stringn}.")
403 #define FUNC_NAME s_scm_pointer_to_string
407 SCM_VALIDATE_POINTER (1, pointer
);
409 if (SCM_UNBNDP (length
)
410 || scm_is_true (scm_eqv_p (length
, scm_from_int (-1))))
413 len
= scm_to_size_t (length
);
415 if (SCM_UNBNDP (encoding
))
416 return scm_from_locale_stringn (SCM_POINTER_VALUE (pointer
), len
);
422 SCM_VALIDATE_STRING (3, encoding
);
424 enc
= scm_to_locale_string (encoding
);
425 scm_dynwind_begin (0);
426 scm_dynwind_free (enc
);
428 ret
= scm_from_stringn (SCM_POINTER_VALUE (pointer
), len
, enc
,
429 scm_i_get_conversion_strategy (SCM_BOOL_F
));
440 SCM_DEFINE (scm_alignof
, "alignof", 1, 0, 0, (SCM type
),
441 "Return the alignment of @var{type}, in bytes.\n\n"
442 "@var{type} should be a valid C type, like @code{int}.\n"
443 "Alternately @var{type} may be the symbol @code{*}, in which\n"
444 "case the alignment of a pointer is returned. @var{type} may\n"
445 "also be a list of types, in which case the alignment of a\n"
446 "@code{struct} with ABI-conventional packing is returned.")
447 #define FUNC_NAME s_scm_alignof
449 if (SCM_I_INUMP (type
))
451 switch (SCM_I_INUM (type
))
453 case SCM_FOREIGN_TYPE_FLOAT
:
454 return scm_from_size_t (alignof (float));
455 case SCM_FOREIGN_TYPE_DOUBLE
:
456 return scm_from_size_t (alignof (double));
457 case SCM_FOREIGN_TYPE_UINT8
:
458 return scm_from_size_t (alignof (scm_t_uint8
));
459 case SCM_FOREIGN_TYPE_INT8
:
460 return scm_from_size_t (alignof (scm_t_int8
));
461 case SCM_FOREIGN_TYPE_UINT16
:
462 return scm_from_size_t (alignof (scm_t_uint16
));
463 case SCM_FOREIGN_TYPE_INT16
:
464 return scm_from_size_t (alignof (scm_t_int16
));
465 case SCM_FOREIGN_TYPE_UINT32
:
466 return scm_from_size_t (alignof (scm_t_uint32
));
467 case SCM_FOREIGN_TYPE_INT32
:
468 return scm_from_size_t (alignof (scm_t_int32
));
469 case SCM_FOREIGN_TYPE_UINT64
:
470 return scm_from_size_t (alignof (scm_t_uint64
));
471 case SCM_FOREIGN_TYPE_INT64
:
472 return scm_from_size_t (alignof (scm_t_int64
));
474 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
477 else if (scm_is_eq (type
, sym_asterisk
))
479 return scm_from_size_t (alignof (void*));
480 else if (scm_is_pair (type
))
482 /* TYPE is a structure. Section 3-3 of the i386, x86_64, PowerPC,
483 and SPARC P.S. of the System V ABI all say: "Aggregates
484 (structures and arrays) and unions assume the alignment of
485 their most strictly aligned component." */
488 for (max
= 0; scm_is_pair (type
); type
= SCM_CDR (type
))
492 align
= scm_to_size_t (scm_alignof (SCM_CAR (type
)));
497 return scm_from_size_t (max
);
500 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
504 SCM_DEFINE (scm_sizeof
, "sizeof", 1, 0, 0, (SCM type
),
505 "Return the size of @var{type}, in bytes.\n\n"
506 "@var{type} should be a valid C type, like @code{int}.\n"
507 "Alternately @var{type} may be the symbol @code{*}, in which\n"
508 "case the size of a pointer is returned. @var{type} may also\n"
509 "be a list of types, in which case the size of a @code{struct}\n"
510 "with ABI-conventional packing is returned.")
511 #define FUNC_NAME s_scm_sizeof
513 if (SCM_I_INUMP (type
))
515 switch (SCM_I_INUM (type
))
517 case SCM_FOREIGN_TYPE_FLOAT
:
518 return scm_from_size_t (sizeof (float));
519 case SCM_FOREIGN_TYPE_DOUBLE
:
520 return scm_from_size_t (sizeof (double));
521 case SCM_FOREIGN_TYPE_UINT8
:
522 return scm_from_size_t (sizeof (scm_t_uint8
));
523 case SCM_FOREIGN_TYPE_INT8
:
524 return scm_from_size_t (sizeof (scm_t_int8
));
525 case SCM_FOREIGN_TYPE_UINT16
:
526 return scm_from_size_t (sizeof (scm_t_uint16
));
527 case SCM_FOREIGN_TYPE_INT16
:
528 return scm_from_size_t (sizeof (scm_t_int16
));
529 case SCM_FOREIGN_TYPE_UINT32
:
530 return scm_from_size_t (sizeof (scm_t_uint32
));
531 case SCM_FOREIGN_TYPE_INT32
:
532 return scm_from_size_t (sizeof (scm_t_int32
));
533 case SCM_FOREIGN_TYPE_UINT64
:
534 return scm_from_size_t (sizeof (scm_t_uint64
));
535 case SCM_FOREIGN_TYPE_INT64
:
536 return scm_from_size_t (sizeof (scm_t_int64
));
538 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
541 else if (scm_is_eq (type
, sym_asterisk
))
543 return scm_from_size_t (sizeof (void*));
544 else if (scm_is_pair (type
))
548 while (scm_is_pair (type
))
550 off
= ROUND_UP (off
, scm_to_size_t (scm_alignof (scm_car (type
))));
551 off
+= scm_to_size_t (scm_sizeof (scm_car (type
)));
552 type
= scm_cdr (type
);
554 return scm_from_size_t (off
);
557 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
562 /* return 1 on success, 0 on failure */
564 parse_ffi_type (SCM type
, int return_p
, long *n_structs
, long *n_struct_elts
)
566 if (SCM_I_INUMP (type
))
568 if ((SCM_I_INUM (type
) < 0 )
569 || (SCM_I_INUM (type
) > SCM_FOREIGN_TYPE_LAST
))
571 else if (SCM_I_INUM (type
) == SCM_FOREIGN_TYPE_VOID
&& !return_p
)
576 else if (scm_is_eq (type
, sym_asterisk
))
583 len
= scm_ilength (type
);
588 if (!parse_ffi_type (scm_car (type
), 0, n_structs
, n_struct_elts
))
591 type
= scm_cdr (type
);
599 fill_ffi_type (SCM type
, ffi_type
*ftype
, ffi_type
***type_ptrs
,
602 if (SCM_I_INUMP (type
))
604 switch (SCM_I_INUM (type
))
606 case SCM_FOREIGN_TYPE_FLOAT
:
607 *ftype
= ffi_type_float
;
609 case SCM_FOREIGN_TYPE_DOUBLE
:
610 *ftype
= ffi_type_double
;
612 case SCM_FOREIGN_TYPE_UINT8
:
613 *ftype
= ffi_type_uint8
;
615 case SCM_FOREIGN_TYPE_INT8
:
616 *ftype
= ffi_type_sint8
;
618 case SCM_FOREIGN_TYPE_UINT16
:
619 *ftype
= ffi_type_uint16
;
621 case SCM_FOREIGN_TYPE_INT16
:
622 *ftype
= ffi_type_sint16
;
624 case SCM_FOREIGN_TYPE_UINT32
:
625 *ftype
= ffi_type_uint32
;
627 case SCM_FOREIGN_TYPE_INT32
:
628 *ftype
= ffi_type_sint32
;
630 case SCM_FOREIGN_TYPE_UINT64
:
631 *ftype
= ffi_type_uint64
;
633 case SCM_FOREIGN_TYPE_INT64
:
634 *ftype
= ffi_type_sint64
;
636 case SCM_FOREIGN_TYPE_VOID
:
637 *ftype
= ffi_type_void
;
640 scm_wrong_type_arg_msg ("pointer->procedure", 0, type
,
644 else if (scm_is_eq (type
, sym_asterisk
))
647 *ftype
= ffi_type_pointer
;
654 len
= scm_ilength (type
);
657 ftype
->alignment
= 0;
658 ftype
->type
= FFI_TYPE_STRUCT
;
659 ftype
->elements
= *type_ptrs
;
660 *type_ptrs
+= len
+ 1;
662 for (i
= 0; i
< len
; i
++)
664 ftype
->elements
[i
] = *types
;
666 fill_ffi_type (scm_car (type
), ftype
->elements
[i
],
668 type
= scm_cdr (type
);
670 ftype
->elements
[i
] = NULL
;
674 /* Return a "cif" (call interface) for the given RETURN_TYPE and
677 make_cif (SCM return_type
, SCM arg_types
, const char *caller
)
678 #define FUNC_NAME caller
681 long i
, nargs
, n_structs
, n_struct_elts
;
685 ffi_type
**type_ptrs
;
688 nargs
= scm_ilength (arg_types
);
689 SCM_ASSERT (nargs
>= 0, arg_types
, 3, FUNC_NAME
);
690 /* fixme: assert nargs < 1<<32 */
691 n_structs
= n_struct_elts
= 0;
693 /* For want of talloc, we're going to have to do this in two passes: first we
694 figure out how much memory is needed for all types, then we allocate the
695 cif and the types all in one block. */
696 if (!parse_ffi_type (return_type
, 1, &n_structs
, &n_struct_elts
))
697 scm_wrong_type_arg (FUNC_NAME
, 1, return_type
);
698 for (walk
= arg_types
; scm_is_pair (walk
); walk
= scm_cdr (walk
))
699 if (!parse_ffi_type (scm_car (walk
), 0, &n_structs
, &n_struct_elts
))
700 scm_wrong_type_arg (FUNC_NAME
, 3, scm_car (walk
));
702 /* the memory: with space for the cif itself */
703 cif_len
= sizeof (ffi_cif
);
705 /* then ffi_type pointers: one for each arg, one for each struct
706 element, and one for each struct (for null-termination) */
707 cif_len
= (ROUND_UP (cif_len
, alignof(void*))
708 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*));
710 /* then the ffi_type structs themselves, one per arg and struct element, and
711 one for the return val */
712 cif_len
= (ROUND_UP (cif_len
, alignof(ffi_type
))
713 + (nargs
+ n_struct_elts
+ 1)*sizeof(ffi_type
));
715 mem
= scm_gc_malloc_pointerless (cif_len
, "foreign");
716 /* ensure all the memory is initialized, even the holes */
717 memset (mem
, 0, cif_len
);
718 cif
= (ffi_cif
*) mem
;
720 /* reuse cif_len to walk through the mem */
721 cif_len
= ROUND_UP (sizeof (ffi_cif
), alignof(void*));
722 type_ptrs
= (ffi_type
**)(mem
+ cif_len
);
723 cif_len
= ROUND_UP (cif_len
724 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*),
726 types
= (ffi_type
*)(mem
+ cif_len
);
728 /* whew. now knit the pointers together. */
729 cif
->rtype
= types
++;
730 fill_ffi_type (return_type
, cif
->rtype
, &type_ptrs
, &types
);
731 cif
->arg_types
= type_ptrs
;
733 for (walk
= arg_types
, i
= 0; scm_is_pair (walk
); walk
= scm_cdr (walk
), i
++)
735 cif
->arg_types
[i
] = types
++;
736 fill_ffi_type (scm_car (walk
), cif
->arg_types
[i
], &type_ptrs
, &types
);
739 /* round out the cif, and we're done. */
740 cif
->abi
= FFI_DEFAULT_ABI
;
745 if (FFI_OK
!= ffi_prep_cif (cif
, FFI_DEFAULT_ABI
, cif
->nargs
, cif
->rtype
,
747 SCM_MISC_ERROR ("ffi_prep_cif failed", SCM_EOL
);
753 SCM_DEFINE (scm_pointer_to_procedure
, "pointer->procedure", 3, 0, 0,
754 (SCM return_type
, SCM func_ptr
, SCM arg_types
),
755 "Make a foreign function.\n\n"
756 "Given the foreign void pointer @var{func_ptr}, its argument and\n"
757 "return types @var{arg_types} and @var{return_type}, return a\n"
758 "procedure that will pass arguments to the foreign function\n"
759 "and return appropriate values.\n\n"
760 "@var{arg_types} should be a list of foreign types.\n"
761 "@code{return_type} should be a foreign type.")
762 #define FUNC_NAME s_scm_pointer_to_procedure
766 SCM_VALIDATE_POINTER (2, func_ptr
);
768 cif
= make_cif (return_type
, arg_types
, FUNC_NAME
);
770 return cif_to_procedure (scm_from_pointer (cif
, NULL
), func_ptr
);
776 /* Pre-generate trampolines for less than 10 arguments. */
778 #ifdef WORDS_BIGENDIAN
779 #define OBJCODE_HEADER 0, 0, 0, 8, 0, 0, 0, 40
780 #define META_HEADER 0, 0, 0, 32, 0, 0, 0, 0
782 #define OBJCODE_HEADER 8, 0, 0, 0, 40, 0, 0, 0
783 #define META_HEADER 32, 0, 0, 0, 0, 0, 0, 0
788 /* 0 */ scm_op_assert_nargs_ee, 0, nreq, /* assert number of args */ \
789 /* 3 */ scm_op_object_ref, 0, /* push the pair with the cif and the function pointer */ \
790 /* 5 */ scm_op_foreign_call, nreq, /* and call (will return value as well) */ \
791 /* 7 */ scm_op_nop, \
792 /* 8 */ META (3, 7, nreq)
794 #define META(start, end, nreq) \
796 /* 0 */ scm_op_make_eol, /* bindings */ \
797 /* 1 */ scm_op_make_eol, /* sources */ \
798 /* 2 */ scm_op_make_int8, start, scm_op_make_int8, end, /* arity: from ip N to ip N */ \
799 /* 6 */ scm_op_make_int8, nreq, /* the arity is N required args */ \
800 /* 8 */ scm_op_list, 0, 3, /* make a list of those 3 vals */ \
801 /* 11 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */ \
802 /* 14 */ scm_op_load_symbol, 0, 0, 4, 'n', 'a', 'm', 'e', /* `name' */ \
803 /* 22 */ scm_op_object_ref, 1, /* the name from the object table */ \
804 /* 24 */ scm_op_cons, /* make a pair for the properties */ \
805 /* 25 */ scm_op_list, 0, 4, /* pack bindings, sources, and arities into list */ \
806 /* 28 */ scm_op_return, /* and return */ \
807 /* 29 */ scm_op_nop, scm_op_nop, scm_op_nop \
812 scm_t_uint64 dummy
; /* ensure 8-byte alignment; perhaps there's a better way */
813 const scm_t_uint8 bytes
[10 * (sizeof (struct scm_objcode
) + 8
814 + sizeof (struct scm_objcode
) + 32)];
818 CODE (0), CODE (1), CODE (2), CODE (3), CODE (4),
819 CODE (5), CODE (6), CODE (7), CODE (8), CODE (9)
825 #undef OBJCODE_HEADER
829 (defun generate-objcode-cells (n)
830 "Generate objcode cells for up to N arguments"
835 (format " { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + %d) },\n"
836 (* (+ 4 4 8 4 4 32) i)))
837 (insert " { SCM_BOOL_F, SCM_PACK (0) },\n")
840 #define STATIC_OBJCODE_TAG \
841 SCM_PACK (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0))
845 scm_t_uint64 dummy
; /* alignment */
846 scm_t_cell cells
[10 * 2]; /* 10 double cells */
849 /* C-u 1 0 M-x generate-objcode-cells RET */
851 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 0) },
852 { SCM_BOOL_F
, SCM_PACK (0) },
853 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 56) },
854 { SCM_BOOL_F
, SCM_PACK (0) },
855 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 112) },
856 { SCM_BOOL_F
, SCM_PACK (0) },
857 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 168) },
858 { SCM_BOOL_F
, SCM_PACK (0) },
859 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 224) },
860 { SCM_BOOL_F
, SCM_PACK (0) },
861 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 280) },
862 { SCM_BOOL_F
, SCM_PACK (0) },
863 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 336) },
864 { SCM_BOOL_F
, SCM_PACK (0) },
865 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 392) },
866 { SCM_BOOL_F
, SCM_PACK (0) },
867 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 448) },
868 { SCM_BOOL_F
, SCM_PACK (0) },
869 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 504) },
870 { SCM_BOOL_F
, SCM_PACK (0) }
874 static const SCM objcode_trampolines
[10] = {
875 SCM_PACK (objcode_cells
.cells
+0),
876 SCM_PACK (objcode_cells
.cells
+2),
877 SCM_PACK (objcode_cells
.cells
+4),
878 SCM_PACK (objcode_cells
.cells
+6),
879 SCM_PACK (objcode_cells
.cells
+8),
880 SCM_PACK (objcode_cells
.cells
+10),
881 SCM_PACK (objcode_cells
.cells
+12),
882 SCM_PACK (objcode_cells
.cells
+14),
883 SCM_PACK (objcode_cells
.cells
+16),
884 SCM_PACK (objcode_cells
.cells
+18),
888 cif_to_procedure (SCM cif
, SCM func_ptr
)
892 SCM objcode
, table
, ret
;
894 c_cif
= (ffi_cif
*) SCM_POINTER_VALUE (cif
);
895 nargs
= c_cif
->nargs
;
898 objcode
= objcode_trampolines
[nargs
];
900 scm_misc_error ("make-foreign-function", "args >= 10 currently unimplemented",
903 table
= scm_c_make_vector (2, SCM_UNDEFINED
);
904 SCM_SIMPLE_VECTOR_SET (table
, 0, scm_cons (cif
, func_ptr
));
905 SCM_SIMPLE_VECTOR_SET (table
, 1, SCM_BOOL_F
); /* name */
906 ret
= scm_make_program (objcode
, table
, SCM_BOOL_F
);
911 /* Set *LOC to the foreign representation of X with TYPE. */
913 unpack (const ffi_type
*type
, void *loc
, SCM x
)
914 #define FUNC_NAME "scm_i_foreign_call"
919 *(float *) loc
= scm_to_double (x
);
921 case FFI_TYPE_DOUBLE
:
922 *(double *) loc
= scm_to_double (x
);
925 *(scm_t_uint8
*) loc
= scm_to_uint8 (x
);
928 *(scm_t_int8
*) loc
= scm_to_int8 (x
);
930 case FFI_TYPE_UINT16
:
931 *(scm_t_uint16
*) loc
= scm_to_uint16 (x
);
933 case FFI_TYPE_SINT16
:
934 *(scm_t_int16
*) loc
= scm_to_int16 (x
);
936 case FFI_TYPE_UINT32
:
937 *(scm_t_uint32
*) loc
= scm_to_uint32 (x
);
939 case FFI_TYPE_SINT32
:
940 *(scm_t_int32
*) loc
= scm_to_int32 (x
);
942 case FFI_TYPE_UINT64
:
943 *(scm_t_uint64
*) loc
= scm_to_uint64 (x
);
945 case FFI_TYPE_SINT64
:
946 *(scm_t_int64
*) loc
= scm_to_int64 (x
);
948 case FFI_TYPE_STRUCT
:
949 SCM_VALIDATE_POINTER (1, x
);
950 memcpy (loc
, SCM_POINTER_VALUE (x
), type
->size
);
952 case FFI_TYPE_POINTER
:
953 SCM_VALIDATE_POINTER (1, x
);
954 *(void **) loc
= SCM_POINTER_VALUE (x
);
965 /* Return a Scheme representation of the foreign value at LOC of type
966 TYPE. When RETURN_VALUE_P is true, LOC is assumed to point to a
967 return value buffer; otherwise LOC is assumed to point to an
970 pack (const ffi_type
* type
, const void *loc
, int return_value_p
)
975 return SCM_UNSPECIFIED
;
977 return scm_from_double (*(float *) loc
);
978 case FFI_TYPE_DOUBLE
:
979 return scm_from_double (*(double *) loc
);
981 /* For integer return values smaller than `int', libffi stores the
982 result in an `ffi_arg'-long buffer, of which only the
983 significant bits must be kept---hence the pair of casts below.
984 See <http://thread.gmane.org/gmane.comp.lib.ffi.general/406>
989 return scm_from_uint8 ((scm_t_uint8
) *(ffi_arg
*) loc
);
991 return scm_from_uint8 (* (scm_t_uint8
*) loc
);
994 return scm_from_int8 ((scm_t_int8
) *(ffi_arg
*) loc
);
996 return scm_from_int8 (* (scm_t_int8
*) loc
);
997 case FFI_TYPE_UINT16
:
999 return scm_from_uint16 ((scm_t_uint16
) *(ffi_arg
*) loc
);
1001 return scm_from_uint16 (* (scm_t_uint16
*) loc
);
1002 case FFI_TYPE_SINT16
:
1004 return scm_from_int16 ((scm_t_int16
) *(ffi_arg
*) loc
);
1006 return scm_from_int16 (* (scm_t_int16
*) loc
);
1007 case FFI_TYPE_UINT32
:
1009 return scm_from_uint32 ((scm_t_uint32
) *(ffi_arg
*) loc
);
1011 return scm_from_uint32 (* (scm_t_uint32
*) loc
);
1012 case FFI_TYPE_SINT32
:
1014 return scm_from_int32 ((scm_t_int32
) *(ffi_arg
*) loc
);
1016 return scm_from_int32 (* (scm_t_int32
*) loc
);
1017 case FFI_TYPE_UINT64
:
1018 return scm_from_uint64 (*(scm_t_uint64
*) loc
);
1019 case FFI_TYPE_SINT64
:
1020 return scm_from_int64 (*(scm_t_int64
*) loc
);
1022 case FFI_TYPE_STRUCT
:
1024 void *mem
= scm_gc_malloc_pointerless (type
->size
, "foreign");
1025 memcpy (mem
, loc
, type
->size
);
1026 return scm_from_pointer (mem
, NULL
);
1028 case FFI_TYPE_POINTER
:
1029 return scm_from_pointer (*(void **) loc
, NULL
);
1037 scm_i_foreign_call (SCM foreign
, const SCM
*argv
)
1039 /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
1042 void (*func
) (void);
1050 cif
= SCM_POINTER_VALUE (SCM_CAR (foreign
));
1051 func
= SCM_POINTER_VALUE (SCM_CDR (foreign
));
1053 /* Argument pointers. */
1054 args
= alloca (sizeof (void *) * cif
->nargs
);
1056 /* Compute the worst-case amount of memory needed to store all the argument
1057 values. Note: as of libffi 3.0.9 `cif->bytes' is undocumented and is zero,
1058 so it can't be used for that purpose. */
1059 for (i
= 0, arg_size
= 0; i
< cif
->nargs
; i
++)
1060 arg_size
+= cif
->arg_types
[i
]->size
+ cif
->arg_types
[i
]->alignment
- 1;
1062 /* Space for argument values, followed by return value. */
1063 data
= alloca (arg_size
+ cif
->rtype
->size
1064 + max (sizeof (void *), cif
->rtype
->alignment
));
1066 /* Unpack ARGV to native values, setting ARGV pointers. */
1067 for (i
= 0, off
= 0;
1069 off
= (scm_t_uint8
*) args
[i
] - data
+ cif
->arg_types
[i
]->size
,
1072 /* Suitably align the storage area for argument I. */
1073 args
[i
] = (void *) ROUND_UP ((scm_t_uintptr
) data
+ off
,
1074 cif
->arg_types
[i
]->alignment
);
1075 assert ((scm_t_uintptr
) args
[i
] % cif
->arg_types
[i
]->alignment
== 0);
1076 unpack (cif
->arg_types
[i
], args
[i
], argv
[i
]);
1079 /* Prepare space for the return value. On some platforms, such as
1080 `armv5tel-*-linux-gnueabi', the return value has to be at least
1081 word-aligned, even if its type doesn't have any alignment requirement as is
1082 the case with `char'. */
1083 rvalue
= (void *) ROUND_UP ((scm_t_uintptr
) data
+ off
,
1084 max (sizeof (void *), cif
->rtype
->alignment
));
1087 ffi_call (cif
, func
, rvalue
, args
);
1089 return pack (cif
->rtype
, rvalue
, 1);
1093 /* Function pointers aka. "callbacks" or "closures". */
1097 /* Trampoline to invoke a libffi closure that wraps a Scheme
1100 invoke_closure (ffi_cif
*cif
, void *ret
, void **args
, void *data
)
1103 SCM proc
, *argv
, result
;
1105 proc
= SCM_PACK_POINTER (data
);
1107 argv
= alloca (cif
->nargs
* sizeof (*argv
));
1109 /* Pack ARGS to SCM values, setting ARGV pointers. */
1110 for (i
= 0; i
< cif
->nargs
; i
++)
1111 argv
[i
] = pack (cif
->arg_types
[i
], args
[i
], 0);
1113 result
= scm_call_n (proc
, argv
, cif
->nargs
);
1115 unpack (cif
->rtype
, ret
, result
);
1118 SCM_DEFINE (scm_procedure_to_pointer
, "procedure->pointer", 3, 0, 0,
1119 (SCM return_type
, SCM proc
, SCM arg_types
),
1120 "Return a pointer to a C function of type @var{return-type}\n"
1121 "taking arguments of types @var{arg-types} (a list) and\n"
1122 "behaving as a proxy to procedure @var{proc}. Thus\n"
1123 "@var{proc}'s arity, supported argument types, and return\n"
1124 "type should match @var{return-type} and @var{arg-types}.\n")
1125 #define FUNC_NAME s_scm_procedure_to_pointer
1130 void *closure
, *executable
;
1132 cif
= make_cif (return_type
, arg_types
, FUNC_NAME
);
1134 closure
= ffi_closure_alloc (sizeof (ffi_closure
), &executable
);
1135 err
= ffi_prep_closure_loc ((ffi_closure
*) closure
, cif
,
1136 invoke_closure
, SCM_UNPACK_POINTER (proc
),
1140 ffi_closure_free (closure
);
1141 SCM_MISC_ERROR ("`ffi_prep_closure_loc' failed", SCM_EOL
);
1144 if (closure
== executable
)
1145 pointer
= scm_from_pointer (executable
, ffi_closure_free
);
1148 /* CLOSURE needs to be freed eventually. However, since
1149 `GC_all_interior_pointers' is disabled, we can't just register
1150 a finalizer for CLOSURE. Instead, we create a pointer object
1151 for CLOSURE, with a finalizer, and register it as a weak
1152 reference of POINTER. */
1155 pointer
= scm_from_pointer (executable
, NULL
);
1156 friend = scm_from_pointer (closure
, ffi_closure_free
);
1158 register_weak_reference (pointer
, friend);
1165 #endif /* FFI_CLOSURES */
1170 scm_init_foreign (void)
1172 #ifndef SCM_MAGIC_SNARFER
1173 #include "libguile/foreign.x"
1175 scm_define (sym_void
, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID
));
1176 scm_define (sym_float
, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT
));
1177 scm_define (sym_double
, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE
));
1178 scm_define (sym_uint8
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8
));
1179 scm_define (sym_int8
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8
));
1180 scm_define (sym_uint16
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16
));
1181 scm_define (sym_int16
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16
));
1182 scm_define (sym_uint32
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
));
1183 scm_define (sym_int32
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
));
1184 scm_define (sym_uint64
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
));
1185 scm_define (sym_int64
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
));
1187 scm_define (sym_short
,
1188 #if SIZEOF_SHORT == 8
1189 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1190 #elif SIZEOF_SHORT == 4
1191 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1192 #elif SIZEOF_SHORT == 2
1193 scm_from_uint8 (SCM_FOREIGN_TYPE_INT16
)
1195 # error unsupported sizeof (short)
1199 scm_define (sym_unsigned_short
,
1200 #if SIZEOF_SHORT == 8
1201 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1202 #elif SIZEOF_SHORT == 4
1203 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1204 #elif SIZEOF_SHORT == 2
1205 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16
)
1207 # error unsupported sizeof (short)
1211 scm_define (sym_int
,
1213 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1214 #elif SIZEOF_INT == 4
1215 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1217 # error unsupported sizeof (int)
1221 scm_define (sym_unsigned_int
,
1222 #if SIZEOF_UNSIGNED_INT == 8
1223 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1224 #elif SIZEOF_UNSIGNED_INT == 4
1225 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1227 # error unsupported sizeof (unsigned int)
1231 scm_define (sym_long
,
1232 #if SIZEOF_LONG == 8
1233 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1234 #elif SIZEOF_LONG == 4
1235 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1237 # error unsupported sizeof (long)
1241 scm_define (sym_unsigned_long
,
1242 #if SIZEOF_UNSIGNED_LONG == 8
1243 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1244 #elif SIZEOF_UNSIGNED_LONG == 4
1245 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1247 # error unsupported sizeof (unsigned long)
1251 scm_define (sym_size_t
,
1252 #if SIZEOF_SIZE_T == 8
1253 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1254 #elif SIZEOF_SIZE_T == 4
1255 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1257 # error unsupported sizeof (size_t)
1261 null_pointer
= scm_cell (scm_tc7_pointer
, 0);
1262 scm_define (sym_null
, null_pointer
);
1266 scm_register_foreign (void)
1268 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
1270 (scm_t_extension_init_func
)scm_init_foreign
,
1272 pointer_weak_refs
= scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY
);