1 /* Copyright (C) 2010 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
29 #include "libguile/_scm.h"
30 #include "libguile/bytevectors.h"
31 #include "libguile/instructions.h"
32 #include "libguile/foreign.h"
36 SCM_SYMBOL (sym_void
, "void");
37 SCM_SYMBOL (sym_float
, "float");
38 SCM_SYMBOL (sym_double
, "double");
39 SCM_SYMBOL (sym_uint8
, "uint8");
40 SCM_SYMBOL (sym_int8
, "int8");
41 SCM_SYMBOL (sym_uint16
, "uint16");
42 SCM_SYMBOL (sym_int16
, "int16");
43 SCM_SYMBOL (sym_uint32
, "uint32");
44 SCM_SYMBOL (sym_int32
, "int32");
45 SCM_SYMBOL (sym_uint64
, "uint64");
46 SCM_SYMBOL (sym_int64
, "int64");
47 SCM_SYMBOL (sym_int
, "int");
48 SCM_SYMBOL (sym_long
, "long");
49 SCM_SYMBOL (sym_unsigned_int
, "unsigned-int");
50 SCM_SYMBOL (sym_unsigned_long
, "unsigned-long");
51 SCM_SYMBOL (sym_size_t
, "size_t");
53 /* that's for pointers, you know. */
54 SCM_SYMBOL (sym_asterisk
, "*");
56 SCM_SYMBOL (sym_null
, "%null-pointer");
57 SCM_SYMBOL (sym_null_pointer_error
, "null-pointer-error");
59 /* The cell representing the null pointer. */
60 static SCM null_pointer
;
62 #if SIZEOF_VOID_P == 4
63 # define scm_to_uintptr scm_to_uint32
64 # define scm_from_uintptr scm_from_uint32
65 #elif SIZEOF_VOID_P == 8
66 # define scm_to_uintptr scm_to_uint64
67 # define scm_from_uintptr scm_from_uint64
69 # error unsupported pointer size
73 /* Raise a null pointer dereference error. */
75 null_pointer_error (const char *func_name
)
77 scm_error (sym_null_pointer_error
, func_name
,
78 "null pointer dereference", SCM_EOL
, SCM_EOL
);
82 static SCM
cif_to_procedure (SCM cif
, SCM func_ptr
);
85 static SCM pointer_weak_refs
= SCM_BOOL_F
;
88 register_weak_reference (SCM from
, SCM to
)
90 scm_hashq_set_x (pointer_weak_refs
, from
, to
);
94 pointer_finalizer_trampoline (GC_PTR ptr
, GC_PTR data
)
96 scm_t_pointer_finalizer finalizer
= data
;
97 finalizer (SCM_POINTER_VALUE (PTR2SCM (ptr
)));
100 SCM_DEFINE (scm_make_pointer
, "make-pointer", 1, 1, 0,
101 (SCM address
, SCM finalizer
),
102 "Return a foreign pointer object pointing to @var{address}. "
103 "If @var{finalizer} is passed, it should be a pointer to a "
104 "one-argument C function that will be called when the pointer "
105 "object becomes unreachable.")
106 #define FUNC_NAME s_scm_make_pointer
109 scm_t_uintptr c_address
;
111 c_address
= scm_to_uintptr (address
);
112 if (SCM_UNBNDP (finalizer
))
116 SCM_VALIDATE_POINTER (2, finalizer
);
117 c_finalizer
= SCM_POINTER_VALUE (finalizer
);
120 return scm_from_pointer ((void *) c_address
, c_finalizer
);
125 scm_from_pointer (void *ptr
, scm_t_pointer_finalizer finalizer
)
129 if (ptr
== NULL
&& finalizer
== NULL
)
135 type
= scm_tc7_pointer
| (finalizer
? (1 << 16UL) : 0UL);
136 ret
= scm_cell (type
, (scm_t_bits
) ptr
);
140 /* Register a finalizer for the newly created instance. */
141 GC_finalization_proc prev_finalizer
;
142 GC_PTR prev_finalizer_data
;
143 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret
),
144 pointer_finalizer_trampoline
,
147 &prev_finalizer_data
);
154 SCM_DEFINE (scm_pointer_address
, "pointer-address", 1, 0, 0,
156 "Return the numerical value of @var{pointer}.")
157 #define FUNC_NAME s_scm_pointer_address
159 SCM_VALIDATE_POINTER (1, pointer
);
161 return scm_from_uintptr ((scm_t_uintptr
) SCM_POINTER_VALUE (pointer
));
165 SCM_DEFINE (scm_pointer_to_bytevector
, "pointer->bytevector", 2, 2, 0,
166 (SCM pointer
, SCM len
, SCM offset
, SCM uvec_type
),
167 "Return a bytevector aliasing the @var{len} bytes pointed\n"
168 "to by @var{pointer}.\n\n"
169 "The user may specify an alternate default interpretation for\n"
170 "the memory by passing the @var{uvec_type} argument, to indicate\n"
171 "that the memory is an array of elements of that type.\n"
172 "@var{uvec_type} should be something that\n"
173 "@code{uniform-vector-element-type} would return, like @code{f32}\n"
175 "When @var{offset} is passed, it specifies the offset in bytes\n"
176 "relative to @var{pointer} of the memory region aliased by the\n"
177 "returned bytevector.")
178 #define FUNC_NAME s_scm_pointer_to_bytevector
182 size_t boffset
, blen
;
183 scm_t_array_element_type btype
;
185 SCM_VALIDATE_POINTER (1, pointer
);
186 ptr
= SCM_POINTER_VALUE (pointer
);
188 if (SCM_UNLIKELY (ptr
== NULL
))
189 null_pointer_error (FUNC_NAME
);
191 if (SCM_UNBNDP (uvec_type
))
192 btype
= SCM_ARRAY_ELEMENT_TYPE_VU8
;
196 for (i
= 0; i
<= SCM_ARRAY_ELEMENT_TYPE_LAST
; i
++)
197 if (scm_is_eq (uvec_type
, scm_i_array_element_types
[i
]))
201 case SCM_ARRAY_ELEMENT_TYPE_VU8
:
202 case SCM_ARRAY_ELEMENT_TYPE_U8
:
203 case SCM_ARRAY_ELEMENT_TYPE_S8
:
204 case SCM_ARRAY_ELEMENT_TYPE_U16
:
205 case SCM_ARRAY_ELEMENT_TYPE_S16
:
206 case SCM_ARRAY_ELEMENT_TYPE_U32
:
207 case SCM_ARRAY_ELEMENT_TYPE_S32
:
208 case SCM_ARRAY_ELEMENT_TYPE_U64
:
209 case SCM_ARRAY_ELEMENT_TYPE_S64
:
210 case SCM_ARRAY_ELEMENT_TYPE_F32
:
211 case SCM_ARRAY_ELEMENT_TYPE_F64
:
212 case SCM_ARRAY_ELEMENT_TYPE_C32
:
213 case SCM_ARRAY_ELEMENT_TYPE_C64
:
217 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, uvec_type
,
218 "uniform vector type");
222 if (SCM_UNBNDP (offset
))
225 boffset
= scm_to_size_t (offset
);
227 blen
= scm_to_size_t (len
);
229 ret
= scm_c_take_typed_bytevector (ptr
+ boffset
, blen
, btype
);
230 register_weak_reference (ret
, pointer
);
235 SCM_DEFINE (scm_bytevector_to_pointer
, "bytevector->pointer", 1, 1, 0,
236 (SCM bv
, SCM offset
),
237 "Return a pointer pointer aliasing the memory pointed to by\n"
238 "@var{bv} or @var{offset} bytes after @var{bv} when @var{offset}\n"
240 #define FUNC_NAME s_scm_bytevector_to_pointer
246 SCM_VALIDATE_BYTEVECTOR (1, bv
);
247 ptr
= SCM_BYTEVECTOR_CONTENTS (bv
);
249 if (SCM_UNBNDP (offset
))
252 boffset
= scm_to_unsigned_integer (offset
, 0,
253 SCM_BYTEVECTOR_LENGTH (bv
) - 1);
255 ret
= scm_from_pointer (ptr
+ boffset
, NULL
);
256 register_weak_reference (ret
, bv
);
261 SCM_DEFINE (scm_set_pointer_finalizer_x
, "set-pointer-finalizer!", 2, 0, 0,
262 (SCM pointer
, SCM finalizer
),
263 "Arrange for the C procedure wrapped by @var{finalizer} to be\n"
264 "called on the pointer wrapped by @var{pointer} when @var{pointer}\n"
265 "becomes unreachable. Note: the C procedure should not call into\n"
266 "Scheme. If you need a Scheme finalizer, use guardians.")
267 #define FUNC_NAME s_scm_set_pointer_finalizer_x
270 GC_finalization_proc prev_finalizer
;
271 GC_PTR prev_finalizer_data
;
273 SCM_VALIDATE_POINTER (1, pointer
);
274 SCM_VALIDATE_POINTER (2, finalizer
);
276 c_finalizer
= SCM_POINTER_VALUE (finalizer
);
278 SCM_SET_CELL_WORD_0 (pointer
, SCM_CELL_WORD_0 (pointer
) | (1 << 16UL));
280 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (pointer
),
281 pointer_finalizer_trampoline
,
284 &prev_finalizer_data
);
286 return SCM_UNSPECIFIED
;
291 scm_i_pointer_print (SCM pointer
, SCM port
, scm_print_state
*pstate
)
293 scm_puts ("#<pointer 0x", port
);
294 scm_uintprint (scm_to_uintptr (scm_pointer_address (pointer
)), 16, port
);
295 scm_putc ('>', port
);
299 /* Non-primitive helpers functions. These procedures could be
300 implemented in terms of the primitives above but would be inefficient
301 (heap allocation overhead, Scheme/C round trips, etc.) */
303 SCM_DEFINE (scm_dereference_pointer
, "dereference-pointer", 1, 0, 0,
305 "Assuming @var{pointer} points to a memory region that\n"
306 "holds a pointer, return this pointer.")
307 #define FUNC_NAME s_scm_dereference_pointer
309 SCM_VALIDATE_POINTER (1, pointer
);
311 return scm_from_pointer (* (void **) SCM_POINTER_VALUE (pointer
), NULL
);
315 SCM_DEFINE (scm_string_to_pointer
, "string->pointer", 1, 0, 0,
317 "Return a foreign pointer to a nul-terminated copy of\n"
318 "@var{string} in the current locale encoding. The C\n"
319 "string is freed when the returned foreign pointer\n"
320 "becomes unreachable.\n\n"
321 "This is the Scheme equivalent of @code{scm_to_locale_string}.")
322 #define FUNC_NAME s_scm_string_to_pointer
324 SCM_VALIDATE_STRING (1, string
);
326 /* XXX: Finalizers slow down libgc; they could be avoided if
327 `scm_to_string' & co. were able to use libgc-allocated memory. */
329 return scm_from_pointer (scm_to_locale_string (string
), free
);
333 SCM_DEFINE (scm_pointer_to_string
, "pointer->string", 1, 0, 0,
335 "Return the string representing the C nul-terminated string\n"
336 "pointed to by @var{pointer}. The C string is assumed to be\n"
337 "in the current locale encoding.\n\n"
338 "This is the Scheme equivalent of @code{scm_from_locale_string}.")
339 #define FUNC_NAME s_scm_pointer_to_string
341 SCM_VALIDATE_POINTER (1, pointer
);
343 return scm_from_locale_string (SCM_POINTER_VALUE (pointer
));
349 SCM_DEFINE (scm_alignof
, "alignof", 1, 0, 0, (SCM type
),
350 "Return the alignment of @var{type}, in bytes.\n\n"
351 "@var{type} should be a valid C type, like @code{int}.\n"
352 "Alternately @var{type} may be the symbol @code{*}, in which\n"
353 "case the alignment of a pointer is returned. @var{type} may\n"
354 "also be a list of types, in which case the alignment of a\n"
355 "@code{struct} with ABI-conventional packing is returned.")
356 #define FUNC_NAME s_scm_alignof
358 if (SCM_I_INUMP (type
))
360 switch (SCM_I_INUM (type
))
362 case SCM_FOREIGN_TYPE_FLOAT
:
363 return scm_from_size_t (alignof (float));
364 case SCM_FOREIGN_TYPE_DOUBLE
:
365 return scm_from_size_t (alignof (double));
366 case SCM_FOREIGN_TYPE_UINT8
:
367 return scm_from_size_t (alignof (scm_t_uint8
));
368 case SCM_FOREIGN_TYPE_INT8
:
369 return scm_from_size_t (alignof (scm_t_int8
));
370 case SCM_FOREIGN_TYPE_UINT16
:
371 return scm_from_size_t (alignof (scm_t_uint16
));
372 case SCM_FOREIGN_TYPE_INT16
:
373 return scm_from_size_t (alignof (scm_t_int16
));
374 case SCM_FOREIGN_TYPE_UINT32
:
375 return scm_from_size_t (alignof (scm_t_uint32
));
376 case SCM_FOREIGN_TYPE_INT32
:
377 return scm_from_size_t (alignof (scm_t_int32
));
378 case SCM_FOREIGN_TYPE_UINT64
:
379 return scm_from_size_t (alignof (scm_t_uint64
));
380 case SCM_FOREIGN_TYPE_INT64
:
381 return scm_from_size_t (alignof (scm_t_int64
));
383 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
386 else if (scm_is_eq (type
, sym_asterisk
))
388 return scm_from_size_t (alignof (void*));
389 else if (scm_is_pair (type
))
391 return scm_alignof (scm_car (type
));
393 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
397 SCM_DEFINE (scm_sizeof
, "sizeof", 1, 0, 0, (SCM type
),
398 "Return the size of @var{type}, in bytes.\n\n"
399 "@var{type} should be a valid C type, like @code{int}.\n"
400 "Alternately @var{type} may be the symbol @code{*}, in which\n"
401 "case the size of a pointer is returned. @var{type} may also\n"
402 "be a list of types, in which case the size of a @code{struct}\n"
403 "with ABI-conventional packing is returned.")
404 #define FUNC_NAME s_scm_sizeof
406 if (SCM_I_INUMP (type
))
408 switch (SCM_I_INUM (type
))
410 case SCM_FOREIGN_TYPE_FLOAT
:
411 return scm_from_size_t (sizeof (float));
412 case SCM_FOREIGN_TYPE_DOUBLE
:
413 return scm_from_size_t (sizeof (double));
414 case SCM_FOREIGN_TYPE_UINT8
:
415 return scm_from_size_t (sizeof (scm_t_uint8
));
416 case SCM_FOREIGN_TYPE_INT8
:
417 return scm_from_size_t (sizeof (scm_t_int8
));
418 case SCM_FOREIGN_TYPE_UINT16
:
419 return scm_from_size_t (sizeof (scm_t_uint16
));
420 case SCM_FOREIGN_TYPE_INT16
:
421 return scm_from_size_t (sizeof (scm_t_int16
));
422 case SCM_FOREIGN_TYPE_UINT32
:
423 return scm_from_size_t (sizeof (scm_t_uint32
));
424 case SCM_FOREIGN_TYPE_INT32
:
425 return scm_from_size_t (sizeof (scm_t_int32
));
426 case SCM_FOREIGN_TYPE_UINT64
:
427 return scm_from_size_t (sizeof (scm_t_uint64
));
428 case SCM_FOREIGN_TYPE_INT64
:
429 return scm_from_size_t (sizeof (scm_t_int64
));
431 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
434 else if (scm_is_eq (type
, sym_asterisk
))
436 return scm_from_size_t (sizeof (void*));
437 else if (scm_is_pair (type
))
441 while (scm_is_pair (type
))
443 off
= ROUND_UP (off
, scm_to_size_t (scm_alignof (scm_car (type
))));
444 off
+= scm_to_size_t (scm_sizeof (scm_car (type
)));
445 type
= scm_cdr (type
);
447 return scm_from_size_t (off
);
450 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
455 /* return 1 on success, 0 on failure */
457 parse_ffi_type (SCM type
, int return_p
, long *n_structs
, long *n_struct_elts
)
459 if (SCM_I_INUMP (type
))
461 if ((SCM_I_INUM (type
) < 0 )
462 || (SCM_I_INUM (type
) > SCM_FOREIGN_TYPE_LAST
))
464 else if (SCM_I_INUM (type
) == SCM_FOREIGN_TYPE_VOID
&& !return_p
)
469 else if (scm_is_eq (type
, sym_asterisk
))
476 len
= scm_ilength (type
);
481 if (!parse_ffi_type (scm_car (type
), 0, n_structs
, n_struct_elts
))
484 type
= scm_cdr (type
);
492 fill_ffi_type (SCM type
, ffi_type
*ftype
, ffi_type
***type_ptrs
,
495 if (SCM_I_INUMP (type
))
497 switch (SCM_I_INUM (type
))
499 case SCM_FOREIGN_TYPE_FLOAT
:
500 *ftype
= ffi_type_float
;
502 case SCM_FOREIGN_TYPE_DOUBLE
:
503 *ftype
= ffi_type_double
;
505 case SCM_FOREIGN_TYPE_UINT8
:
506 *ftype
= ffi_type_uint8
;
508 case SCM_FOREIGN_TYPE_INT8
:
509 *ftype
= ffi_type_sint8
;
511 case SCM_FOREIGN_TYPE_UINT16
:
512 *ftype
= ffi_type_uint16
;
514 case SCM_FOREIGN_TYPE_INT16
:
515 *ftype
= ffi_type_sint16
;
517 case SCM_FOREIGN_TYPE_UINT32
:
518 *ftype
= ffi_type_uint32
;
520 case SCM_FOREIGN_TYPE_INT32
:
521 *ftype
= ffi_type_sint32
;
523 case SCM_FOREIGN_TYPE_UINT64
:
524 *ftype
= ffi_type_uint64
;
526 case SCM_FOREIGN_TYPE_INT64
:
527 *ftype
= ffi_type_sint64
;
529 case SCM_FOREIGN_TYPE_VOID
:
530 *ftype
= ffi_type_void
;
533 scm_wrong_type_arg_msg ("pointer->procedure", 0, type
,
537 else if (scm_is_eq (type
, sym_asterisk
))
540 *ftype
= ffi_type_pointer
;
547 len
= scm_ilength (type
);
550 ftype
->alignment
= 0;
551 ftype
->type
= FFI_TYPE_STRUCT
;
552 ftype
->elements
= *type_ptrs
;
553 *type_ptrs
+= len
+ 1;
555 for (i
= 0; i
< len
; i
++)
557 ftype
->elements
[i
] = *types
;
559 fill_ffi_type (scm_car (type
), ftype
->elements
[i
],
561 type
= scm_cdr (type
);
563 ftype
->elements
[i
] = NULL
;
567 /* Return a "cif" (call interface) for the given RETURN_TYPE and
570 make_cif (SCM return_type
, SCM arg_types
, const char *caller
)
571 #define FUNC_NAME caller
574 long i
, nargs
, n_structs
, n_struct_elts
;
578 ffi_type
**type_ptrs
;
581 nargs
= scm_ilength (arg_types
);
582 SCM_ASSERT (nargs
>= 0, arg_types
, 3, FUNC_NAME
);
583 /* fixme: assert nargs < 1<<32 */
584 n_structs
= n_struct_elts
= 0;
586 /* For want of talloc, we're going to have to do this in two passes: first we
587 figure out how much memory is needed for all types, then we allocate the
588 cif and the types all in one block. */
589 if (!parse_ffi_type (return_type
, 1, &n_structs
, &n_struct_elts
))
590 scm_wrong_type_arg (FUNC_NAME
, 1, return_type
);
591 for (walk
= arg_types
; scm_is_pair (walk
); walk
= scm_cdr (walk
))
592 if (!parse_ffi_type (scm_car (walk
), 0, &n_structs
, &n_struct_elts
))
593 scm_wrong_type_arg (FUNC_NAME
, 3, scm_car (walk
));
595 /* the memory: with space for the cif itself */
596 cif_len
= sizeof (ffi_cif
);
598 /* then ffi_type pointers: one for each arg, one for each struct
599 element, and one for each struct (for null-termination) */
600 cif_len
= (ROUND_UP (cif_len
, alignof(void*))
601 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*));
603 /* then the ffi_type structs themselves, one per arg and struct element, and
604 one for the return val */
605 cif_len
= (ROUND_UP (cif_len
, alignof(ffi_type
))
606 + (nargs
+ n_struct_elts
+ 1)*sizeof(ffi_type
));
608 mem
= scm_gc_malloc_pointerless (cif_len
, "foreign");
609 cif
= (ffi_cif
*) mem
;
611 /* reuse cif_len to walk through the mem */
612 cif_len
= ROUND_UP (sizeof (ffi_cif
), alignof(void*));
613 type_ptrs
= (ffi_type
**)(mem
+ cif_len
);
614 cif_len
= ROUND_UP (cif_len
615 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*),
617 types
= (ffi_type
*)(mem
+ cif_len
);
619 /* whew. now knit the pointers together. */
620 cif
->rtype
= types
++;
621 fill_ffi_type (return_type
, cif
->rtype
, &type_ptrs
, &types
);
622 cif
->arg_types
= type_ptrs
;
624 for (walk
= arg_types
, i
= 0; scm_is_pair (walk
); walk
= scm_cdr (walk
), i
++)
626 cif
->arg_types
[i
] = types
++;
627 fill_ffi_type (scm_car (walk
), cif
->arg_types
[i
], &type_ptrs
, &types
);
630 /* round out the cif, and we're done. */
631 cif
->abi
= FFI_DEFAULT_ABI
;
636 if (FFI_OK
!= ffi_prep_cif (cif
, FFI_DEFAULT_ABI
, cif
->nargs
, cif
->rtype
,
638 SCM_MISC_ERROR ("ffi_prep_cif failed", SCM_EOL
);
644 SCM_DEFINE (scm_pointer_to_procedure
, "pointer->procedure", 3, 0, 0,
645 (SCM return_type
, SCM func_ptr
, SCM arg_types
),
646 "Make a foreign function.\n\n"
647 "Given the foreign void pointer @var{func_ptr}, its argument and\n"
648 "return types @var{arg_types} and @var{return_type}, return a\n"
649 "procedure that will pass arguments to the foreign function\n"
650 "and return appropriate values.\n\n"
651 "@var{arg_types} should be a list of foreign types.\n"
652 "@code{return_type} should be a foreign type.")
653 #define FUNC_NAME s_scm_pointer_to_procedure
657 SCM_VALIDATE_POINTER (2, func_ptr
);
659 cif
= make_cif (return_type
, arg_types
, FUNC_NAME
);
661 return cif_to_procedure (scm_from_pointer (cif
, NULL
), func_ptr
);
667 /* Pre-generate trampolines for less than 10 arguments. */
669 #ifdef WORDS_BIGENDIAN
670 #define OBJCODE_HEADER 0, 0, 0, 8, 0, 0, 0, 40
671 #define META_HEADER 0, 0, 0, 32, 0, 0, 0, 0
673 #define OBJCODE_HEADER 8, 0, 0, 0, 40, 0, 0, 0
674 #define META_HEADER 32, 0, 0, 0, 0, 0, 0, 0
679 /* 0 */ scm_op_assert_nargs_ee, 0, nreq, /* assert number of args */ \
680 /* 3 */ scm_op_object_ref, 0, /* push the pair with the cif and the function pointer */ \
681 /* 5 */ scm_op_foreign_call, nreq, /* and call (will return value as well) */ \
682 /* 7 */ scm_op_nop, \
683 /* 8 */ META (3, 7, nreq)
685 #define META(start, end, nreq) \
687 /* 0 */ scm_op_make_eol, /* bindings */ \
688 /* 1 */ scm_op_make_eol, /* sources */ \
689 /* 2 */ scm_op_make_int8, start, scm_op_make_int8, end, /* arity: from ip N to ip N */ \
690 /* 6 */ scm_op_make_int8, nreq, /* the arity is N required args */ \
691 /* 8 */ scm_op_list, 0, 3, /* make a list of those 3 vals */ \
692 /* 11 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */ \
693 /* 14 */ scm_op_load_symbol, 0, 0, 4, 'n', 'a', 'm', 'e', /* `name' */ \
694 /* 22 */ scm_op_object_ref, 1, /* the name from the object table */ \
695 /* 24 */ scm_op_cons, /* make a pair for the properties */ \
696 /* 25 */ scm_op_list, 0, 4, /* pack bindings, sources, and arities into list */ \
697 /* 28 */ scm_op_return, /* and return */ \
698 /* 29 */ scm_op_nop, scm_op_nop, scm_op_nop \
703 scm_t_uint64 dummy
; /* ensure 8-byte alignment; perhaps there's a better way */
704 const scm_t_uint8 bytes
[10 * (sizeof (struct scm_objcode
) + 8
705 + sizeof (struct scm_objcode
) + 32)];
709 CODE (0), CODE (1), CODE (2), CODE (3), CODE (4),
710 CODE (5), CODE (6), CODE (7), CODE (8), CODE (9)
716 #undef OBJCODE_HEADER
720 (defun generate-objcode-cells (n)
721 "Generate objcode cells for up to N arguments"
726 (format " { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + %d) },\n"
727 (* (+ 4 4 8 4 4 32) i)))
728 (insert " { SCM_BOOL_F, SCM_PACK (0) },\n")
731 #define STATIC_OBJCODE_TAG \
732 SCM_PACK (scm_tc7_objcode | (SCM_F_OBJCODE_IS_STATIC << 8))
736 scm_t_uint64 dummy
; /* alignment */
737 scm_t_cell cells
[10 * 2]; /* 10 double cells */
740 /* C-u 1 0 M-x generate-objcode-cells RET */
742 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 0) },
743 { SCM_BOOL_F
, SCM_PACK (0) },
744 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 56) },
745 { SCM_BOOL_F
, SCM_PACK (0) },
746 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 112) },
747 { SCM_BOOL_F
, SCM_PACK (0) },
748 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 168) },
749 { SCM_BOOL_F
, SCM_PACK (0) },
750 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 224) },
751 { SCM_BOOL_F
, SCM_PACK (0) },
752 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 280) },
753 { SCM_BOOL_F
, SCM_PACK (0) },
754 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 336) },
755 { SCM_BOOL_F
, SCM_PACK (0) },
756 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 392) },
757 { SCM_BOOL_F
, SCM_PACK (0) },
758 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 448) },
759 { SCM_BOOL_F
, SCM_PACK (0) },
760 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 504) },
761 { SCM_BOOL_F
, SCM_PACK (0) }
765 static const SCM objcode_trampolines
[10] = {
766 SCM_PACK (objcode_cells
.cells
+0),
767 SCM_PACK (objcode_cells
.cells
+2),
768 SCM_PACK (objcode_cells
.cells
+4),
769 SCM_PACK (objcode_cells
.cells
+6),
770 SCM_PACK (objcode_cells
.cells
+8),
771 SCM_PACK (objcode_cells
.cells
+10),
772 SCM_PACK (objcode_cells
.cells
+12),
773 SCM_PACK (objcode_cells
.cells
+14),
774 SCM_PACK (objcode_cells
.cells
+16),
775 SCM_PACK (objcode_cells
.cells
+18),
779 cif_to_procedure (SCM cif
, SCM func_ptr
)
783 SCM objcode
, table
, ret
;
785 c_cif
= (ffi_cif
*) SCM_POINTER_VALUE (cif
);
786 nargs
= c_cif
->nargs
;
789 objcode
= objcode_trampolines
[nargs
];
791 scm_misc_error ("make-foreign-function", "args >= 10 currently unimplemented",
794 table
= scm_c_make_vector (2, SCM_UNDEFINED
);
795 SCM_SIMPLE_VECTOR_SET (table
, 0, scm_cons (cif
, func_ptr
));
796 SCM_SIMPLE_VECTOR_SET (table
, 1, SCM_BOOL_F
); /* name */
797 ret
= scm_make_program (objcode
, table
, SCM_BOOL_F
);
802 /* Set *LOC to the foreign representation of X with TYPE. */
804 unpack (const ffi_type
*type
, void *loc
, SCM x
)
809 *(float *) loc
= scm_to_double (x
);
811 case FFI_TYPE_DOUBLE
:
812 *(double *) loc
= scm_to_double (x
);
815 *(scm_t_uint8
*) loc
= scm_to_uint8 (x
);
818 *(scm_t_int8
*) loc
= scm_to_int8 (x
);
820 case FFI_TYPE_UINT16
:
821 *(scm_t_uint16
*) loc
= scm_to_uint16 (x
);
823 case FFI_TYPE_SINT16
:
824 *(scm_t_int16
*) loc
= scm_to_int16 (x
);
826 case FFI_TYPE_UINT32
:
827 *(scm_t_uint32
*) loc
= scm_to_uint32 (x
);
829 case FFI_TYPE_SINT32
:
830 *(scm_t_int32
*) loc
= scm_to_int32 (x
);
832 case FFI_TYPE_UINT64
:
833 *(scm_t_uint64
*) loc
= scm_to_uint64 (x
);
835 case FFI_TYPE_SINT64
:
836 *(scm_t_int64
*) loc
= scm_to_int64 (x
);
838 case FFI_TYPE_STRUCT
:
839 memcpy (loc
, SCM_POINTER_VALUE (x
), type
->size
);
841 case FFI_TYPE_POINTER
:
842 *(void **) loc
= SCM_POINTER_VALUE (x
);
849 /* Return a Scheme representation of the foreign value at LOC of type TYPE. */
851 pack (const ffi_type
* type
, const void *loc
)
856 return SCM_UNSPECIFIED
;
858 return scm_from_double (*(float *) loc
);
859 case FFI_TYPE_DOUBLE
:
860 return scm_from_double (*(double *) loc
);
862 return scm_from_uint8 (*(scm_t_uint8
*) loc
);
864 return scm_from_int8 (*(scm_t_int8
*) loc
);
865 case FFI_TYPE_UINT16
:
866 return scm_from_uint16 (*(scm_t_uint16
*) loc
);
867 case FFI_TYPE_SINT16
:
868 return scm_from_int16 (*(scm_t_int16
*) loc
);
869 case FFI_TYPE_UINT32
:
870 return scm_from_uint32 (*(scm_t_uint32
*) loc
);
871 case FFI_TYPE_SINT32
:
872 return scm_from_int32 (*(scm_t_int32
*) loc
);
873 case FFI_TYPE_UINT64
:
874 return scm_from_uint64 (*(scm_t_uint64
*) loc
);
875 case FFI_TYPE_SINT64
:
876 return scm_from_int64 (*(scm_t_int64
*) loc
);
877 case FFI_TYPE_STRUCT
:
879 void *mem
= scm_gc_malloc_pointerless (type
->size
, "foreign");
880 memcpy (mem
, loc
, type
->size
);
881 return scm_from_pointer (mem
, NULL
);
883 case FFI_TYPE_POINTER
:
884 return scm_from_pointer (*(void **) loc
, NULL
);
892 scm_i_foreign_call (SCM foreign
, const SCM
*argv
)
894 /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
905 cif
= SCM_POINTER_VALUE (SCM_CAR (foreign
));
906 func
= SCM_POINTER_VALUE (SCM_CDR (foreign
));
908 /* Argument pointers. */
909 args
= alloca (sizeof (void *) * cif
->nargs
);
911 /* Compute the worst-case amount of memory needed to store all the argument
912 values. Note: as of libffi 3.0.9 `cif->bytes' is undocumented and is zero,
913 so it can't be used for that purpose. */
914 for (i
= 0, arg_size
= 0; i
< cif
->nargs
; i
++)
915 arg_size
+= cif
->arg_types
[i
]->size
+ cif
->arg_types
[i
]->alignment
- 1;
917 /* Space for argument values, followed by return value. */
918 data
= alloca (arg_size
+ cif
->rtype
->size
919 + max (sizeof (void *), cif
->rtype
->alignment
));
921 /* Unpack ARGV to native values, setting ARGV pointers. */
924 off
= (scm_t_uint8
*) args
[i
] - data
+ cif
->arg_types
[i
]->size
,
927 /* Suitably align the storage area for argument I. */
928 args
[i
] = (void *) ROUND_UP ((scm_t_uintptr
) data
+ off
,
929 cif
->arg_types
[i
]->alignment
);
930 assert ((scm_t_uintptr
) args
[i
] % cif
->arg_types
[i
]->alignment
== 0);
931 unpack (cif
->arg_types
[i
], args
[i
], argv
[i
]);
934 /* Prepare space for the return value. On some platforms, such as
935 `armv5tel-*-linux-gnueabi', the return value has to be at least
936 word-aligned, even if its type doesn't have any alignment requirement as is
937 the case with `char'. */
938 rvalue
= (void *) ROUND_UP ((scm_t_uintptr
) data
+ off
,
939 max (sizeof (void *), cif
->rtype
->alignment
));
942 ffi_call (cif
, func
, rvalue
, args
);
944 return pack (cif
->rtype
, rvalue
);
948 /* Function pointers aka. "callbacks" or "closures". */
952 /* Trampoline to invoke a libffi closure that wraps a Scheme
955 invoke_closure (ffi_cif
*cif
, void *ret
, void **args
, void *data
)
958 SCM proc
, *argv
, result
;
960 proc
= PTR2SCM (data
);
962 argv
= alloca (cif
->nargs
* sizeof (*argv
));
964 /* Pack ARGS to SCM values, setting ARGV pointers. */
965 for (i
= 0; i
< cif
->nargs
; i
++)
966 argv
[i
] = pack (cif
->arg_types
[i
], args
[i
]);
968 result
= scm_call_n (proc
, argv
, cif
->nargs
);
970 unpack (cif
->rtype
, ret
, result
);
973 SCM_DEFINE (scm_procedure_to_pointer
, "procedure->pointer", 3, 0, 0,
974 (SCM return_type
, SCM proc
, SCM arg_types
),
975 "Return a pointer to a C function of type @var{return-type}\n"
976 "taking arguments of types @var{arg-types} (a list) and\n"
977 "behaving as a proxy to procedure @var{proc}. Thus\n"
978 "@var{proc}'s arity, supported argument types, and return\n"
979 "type should match @var{return-type} and @var{arg-types}.\n")
980 #define FUNC_NAME s_scm_procedure_to_pointer
985 void *closure
, *executable
;
987 cif
= make_cif (return_type
, arg_types
, FUNC_NAME
);
989 closure
= ffi_closure_alloc (sizeof (ffi_closure
), &executable
);
990 err
= ffi_prep_closure_loc ((ffi_closure
*) closure
, cif
,
991 invoke_closure
, SCM2PTR (proc
),
995 ffi_closure_free (closure
);
996 SCM_MISC_ERROR ("`ffi_prep_closure_loc' failed", SCM_EOL
);
999 if (closure
== executable
)
1000 pointer
= scm_from_pointer (executable
, ffi_closure_free
);
1003 /* CLOSURE needs to be freed eventually. However, since
1004 `GC_all_interior_pointers' is disabled, we can't just register
1005 a finalizer for CLOSURE. Instead, we create a pointer object
1006 for CLOSURE, with a finalizer, and register it as a weak
1007 reference of POINTER. */
1010 pointer
= scm_from_pointer (executable
, NULL
);
1011 friend = scm_from_pointer (closure
, ffi_closure_free
);
1013 register_weak_reference (pointer
, friend);
1020 #endif /* FFI_CLOSURES */
1025 scm_init_foreign (void)
1027 #ifndef SCM_MAGIC_SNARFER
1028 #include "libguile/foreign.x"
1030 scm_define (sym_void
, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID
));
1031 scm_define (sym_float
, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT
));
1032 scm_define (sym_double
, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE
));
1033 scm_define (sym_uint8
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8
));
1034 scm_define (sym_int8
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8
));
1035 scm_define (sym_uint16
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16
));
1036 scm_define (sym_int16
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16
));
1037 scm_define (sym_uint32
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
));
1038 scm_define (sym_int32
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
));
1039 scm_define (sym_uint64
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
));
1040 scm_define (sym_int64
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
));
1042 scm_define (sym_int
,
1044 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1045 #elif SIZEOF_INT == 4
1046 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1048 # error unsupported sizeof (int)
1052 scm_define (sym_unsigned_int
,
1053 #if SIZEOF_UNSIGNED_INT == 8
1054 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1055 #elif SIZEOF_UNSIGNED_INT == 4
1056 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1058 # error unsupported sizeof (unsigned int)
1062 scm_define (sym_long
,
1063 #if SIZEOF_LONG == 8
1064 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1065 #elif SIZEOF_LONG == 4
1066 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1068 # error unsupported sizeof (long)
1072 scm_define (sym_unsigned_long
,
1073 #if SIZEOF_UNSIGNED_LONG == 8
1074 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1075 #elif SIZEOF_UNSIGNED_LONG == 4
1076 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1078 # error unsupported sizeof (unsigned long)
1082 scm_define (sym_size_t
,
1083 #if SIZEOF_SIZE_T == 8
1084 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1085 #elif SIZEOF_SIZE_T == 4
1086 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1088 # error unsupported sizeof (size_t)
1092 null_pointer
= scm_cell (scm_tc7_pointer
, 0);
1093 scm_define (sym_null
, null_pointer
);
1097 scm_register_foreign (void)
1099 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
1101 (scm_t_extension_init_func
)scm_init_foreign
,
1103 pointer_weak_refs
= scm_make_weak_key_hash_table (SCM_UNDEFINED
);