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/foreign.h"
37 SCM_SYMBOL (sym_void
, "void");
38 SCM_SYMBOL (sym_float
, "float");
39 SCM_SYMBOL (sym_double
, "double");
40 SCM_SYMBOL (sym_uint8
, "uint8");
41 SCM_SYMBOL (sym_int8
, "int8");
42 SCM_SYMBOL (sym_uint16
, "uint16");
43 SCM_SYMBOL (sym_int16
, "int16");
44 SCM_SYMBOL (sym_uint32
, "uint32");
45 SCM_SYMBOL (sym_int32
, "int32");
46 SCM_SYMBOL (sym_uint64
, "uint64");
47 SCM_SYMBOL (sym_int64
, "int64");
48 SCM_SYMBOL (sym_short
, "short");
49 SCM_SYMBOL (sym_int
, "int");
50 SCM_SYMBOL (sym_long
, "long");
51 SCM_SYMBOL (sym_unsigned_short
, "unsigned-short");
52 SCM_SYMBOL (sym_unsigned_int
, "unsigned-int");
53 SCM_SYMBOL (sym_unsigned_long
, "unsigned-long");
54 SCM_SYMBOL (sym_size_t
, "size_t");
56 /* that's for pointers, you know. */
57 SCM_SYMBOL (sym_asterisk
, "*");
59 SCM_SYMBOL (sym_null
, "%null-pointer");
60 SCM_SYMBOL (sym_null_pointer_error
, "null-pointer-error");
62 /* The cell representing the null pointer. */
63 static SCM null_pointer
;
65 #if SIZEOF_VOID_P == 4
66 # define scm_to_uintptr scm_to_uint32
67 # define scm_from_uintptr scm_from_uint32
68 #elif SIZEOF_VOID_P == 8
69 # define scm_to_uintptr scm_to_uint64
70 # define scm_from_uintptr scm_from_uint64
72 # error unsupported pointer size
76 /* Raise a null pointer dereference error. */
78 null_pointer_error (const char *func_name
)
80 scm_error (sym_null_pointer_error
, func_name
,
81 "null pointer dereference", SCM_EOL
, SCM_EOL
);
85 static SCM
cif_to_procedure (SCM cif
, SCM func_ptr
);
88 static SCM pointer_weak_refs
= SCM_BOOL_F
;
91 register_weak_reference (SCM from
, SCM to
)
93 scm_hashq_set_x (pointer_weak_refs
, from
, to
);
97 pointer_finalizer_trampoline (GC_PTR ptr
, GC_PTR data
)
99 scm_t_pointer_finalizer finalizer
= data
;
100 finalizer (SCM_POINTER_VALUE (PTR2SCM (ptr
)));
103 SCM_DEFINE (scm_make_pointer
, "make-pointer", 1, 1, 0,
104 (SCM address
, SCM finalizer
),
105 "Return a foreign pointer object pointing to @var{address}. "
106 "If @var{finalizer} is passed, it should be a pointer to a "
107 "one-argument C function that will be called when the pointer "
108 "object becomes unreachable.")
109 #define FUNC_NAME s_scm_make_pointer
112 scm_t_uintptr c_address
;
114 c_address
= scm_to_uintptr (address
);
115 if (SCM_UNBNDP (finalizer
))
119 SCM_VALIDATE_POINTER (2, finalizer
);
120 c_finalizer
= SCM_POINTER_VALUE (finalizer
);
123 return scm_from_pointer ((void *) c_address
, c_finalizer
);
128 scm_from_pointer (void *ptr
, scm_t_pointer_finalizer finalizer
)
132 if (ptr
== NULL
&& finalizer
== NULL
)
138 type
= scm_tc7_pointer
| (finalizer
? (1 << 16UL) : 0UL);
139 ret
= scm_cell (type
, (scm_t_bits
) ptr
);
143 /* Register a finalizer for the newly created instance. */
144 GC_finalization_proc prev_finalizer
;
145 GC_PTR prev_finalizer_data
;
146 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret
),
147 pointer_finalizer_trampoline
,
150 &prev_finalizer_data
);
157 SCM_DEFINE (scm_pointer_address
, "pointer-address", 1, 0, 0,
159 "Return the numerical value of @var{pointer}.")
160 #define FUNC_NAME s_scm_pointer_address
162 SCM_VALIDATE_POINTER (1, pointer
);
164 return scm_from_uintptr ((scm_t_uintptr
) SCM_POINTER_VALUE (pointer
));
168 SCM_DEFINE (scm_pointer_to_bytevector
, "pointer->bytevector", 2, 2, 0,
169 (SCM pointer
, SCM len
, SCM offset
, SCM uvec_type
),
170 "Return a bytevector aliasing the @var{len} bytes pointed\n"
171 "to by @var{pointer}.\n\n"
172 "The user may specify an alternate default interpretation for\n"
173 "the memory by passing the @var{uvec_type} argument, to indicate\n"
174 "that the memory is an array of elements of that type.\n"
175 "@var{uvec_type} should be something that\n"
176 "@code{uniform-vector-element-type} would return, like @code{f32}\n"
178 "When @var{offset} is passed, it specifies the offset in bytes\n"
179 "relative to @var{pointer} of the memory region aliased by the\n"
180 "returned bytevector.")
181 #define FUNC_NAME s_scm_pointer_to_bytevector
185 size_t boffset
, blen
;
186 scm_t_array_element_type btype
;
188 SCM_VALIDATE_POINTER (1, pointer
);
189 ptr
= SCM_POINTER_VALUE (pointer
);
191 if (SCM_UNLIKELY (ptr
== NULL
))
192 null_pointer_error (FUNC_NAME
);
194 if (SCM_UNBNDP (uvec_type
))
195 btype
= SCM_ARRAY_ELEMENT_TYPE_VU8
;
199 for (i
= 0; i
<= SCM_ARRAY_ELEMENT_TYPE_LAST
; i
++)
200 if (scm_is_eq (uvec_type
, scm_i_array_element_types
[i
]))
204 case SCM_ARRAY_ELEMENT_TYPE_VU8
:
205 case SCM_ARRAY_ELEMENT_TYPE_U8
:
206 case SCM_ARRAY_ELEMENT_TYPE_S8
:
207 case SCM_ARRAY_ELEMENT_TYPE_U16
:
208 case SCM_ARRAY_ELEMENT_TYPE_S16
:
209 case SCM_ARRAY_ELEMENT_TYPE_U32
:
210 case SCM_ARRAY_ELEMENT_TYPE_S32
:
211 case SCM_ARRAY_ELEMENT_TYPE_U64
:
212 case SCM_ARRAY_ELEMENT_TYPE_S64
:
213 case SCM_ARRAY_ELEMENT_TYPE_F32
:
214 case SCM_ARRAY_ELEMENT_TYPE_F64
:
215 case SCM_ARRAY_ELEMENT_TYPE_C32
:
216 case SCM_ARRAY_ELEMENT_TYPE_C64
:
220 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, uvec_type
,
221 "uniform vector type");
225 if (SCM_UNBNDP (offset
))
228 boffset
= scm_to_size_t (offset
);
230 blen
= scm_to_size_t (len
);
232 ret
= scm_c_take_typed_bytevector (ptr
+ boffset
, blen
, btype
);
233 register_weak_reference (ret
, pointer
);
238 SCM_DEFINE (scm_bytevector_to_pointer
, "bytevector->pointer", 1, 1, 0,
239 (SCM bv
, SCM offset
),
240 "Return a pointer pointer aliasing the memory pointed to by\n"
241 "@var{bv} or @var{offset} bytes after @var{bv} when @var{offset}\n"
243 #define FUNC_NAME s_scm_bytevector_to_pointer
249 SCM_VALIDATE_BYTEVECTOR (1, bv
);
250 ptr
= SCM_BYTEVECTOR_CONTENTS (bv
);
252 if (SCM_UNBNDP (offset
))
255 boffset
= scm_to_unsigned_integer (offset
, 0,
256 SCM_BYTEVECTOR_LENGTH (bv
) - 1);
258 ret
= scm_from_pointer (ptr
+ boffset
, NULL
);
259 register_weak_reference (ret
, bv
);
264 SCM_DEFINE (scm_set_pointer_finalizer_x
, "set-pointer-finalizer!", 2, 0, 0,
265 (SCM pointer
, SCM finalizer
),
266 "Arrange for the C procedure wrapped by @var{finalizer} to be\n"
267 "called on the pointer wrapped by @var{pointer} when @var{pointer}\n"
268 "becomes unreachable. Note: the C procedure should not call into\n"
269 "Scheme. If you need a Scheme finalizer, use guardians.")
270 #define FUNC_NAME s_scm_set_pointer_finalizer_x
273 GC_finalization_proc prev_finalizer
;
274 GC_PTR prev_finalizer_data
;
276 SCM_VALIDATE_POINTER (1, pointer
);
277 SCM_VALIDATE_POINTER (2, finalizer
);
279 c_finalizer
= SCM_POINTER_VALUE (finalizer
);
281 SCM_SET_CELL_WORD_0 (pointer
, SCM_CELL_WORD_0 (pointer
) | (1 << 16UL));
283 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (pointer
),
284 pointer_finalizer_trampoline
,
287 &prev_finalizer_data
);
289 return SCM_UNSPECIFIED
;
294 scm_i_pointer_print (SCM pointer
, SCM port
, scm_print_state
*pstate
)
296 scm_puts ("#<pointer 0x", port
);
297 scm_uintprint (scm_to_uintptr (scm_pointer_address (pointer
)), 16, port
);
298 scm_putc ('>', port
);
302 /* Non-primitive helpers functions. These procedures could be
303 implemented in terms of the primitives above but would be inefficient
304 (heap allocation overhead, Scheme/C round trips, etc.) */
306 SCM_DEFINE (scm_dereference_pointer
, "dereference-pointer", 1, 0, 0,
308 "Assuming @var{pointer} points to a memory region that\n"
309 "holds a pointer, return this pointer.")
310 #define FUNC_NAME s_scm_dereference_pointer
312 SCM_VALIDATE_POINTER (1, pointer
);
314 return scm_from_pointer (* (void **) SCM_POINTER_VALUE (pointer
), NULL
);
318 SCM_DEFINE (scm_string_to_pointer
, "string->pointer", 1, 0, 0,
320 "Return a foreign pointer to a nul-terminated copy of\n"
321 "@var{string} in the current locale encoding. The C\n"
322 "string is freed when the returned foreign pointer\n"
323 "becomes unreachable.\n\n"
324 "This is the Scheme equivalent of @code{scm_to_locale_string}.")
325 #define FUNC_NAME s_scm_string_to_pointer
327 SCM_VALIDATE_STRING (1, string
);
329 /* XXX: Finalizers slow down libgc; they could be avoided if
330 `scm_to_string' & co. were able to use libgc-allocated memory. */
332 return scm_from_pointer (scm_to_locale_string (string
), free
);
336 SCM_DEFINE (scm_pointer_to_string
, "pointer->string", 1, 0, 0,
338 "Return the string representing the C nul-terminated string\n"
339 "pointed to by @var{pointer}. The C string is assumed to be\n"
340 "in the current locale encoding.\n\n"
341 "This is the Scheme equivalent of @code{scm_from_locale_string}.")
342 #define FUNC_NAME s_scm_pointer_to_string
344 SCM_VALIDATE_POINTER (1, pointer
);
346 return scm_from_locale_string (SCM_POINTER_VALUE (pointer
));
352 SCM_DEFINE (scm_alignof
, "alignof", 1, 0, 0, (SCM type
),
353 "Return the alignment of @var{type}, in bytes.\n\n"
354 "@var{type} should be a valid C type, like @code{int}.\n"
355 "Alternately @var{type} may be the symbol @code{*}, in which\n"
356 "case the alignment of a pointer is returned. @var{type} may\n"
357 "also be a list of types, in which case the alignment of a\n"
358 "@code{struct} with ABI-conventional packing is returned.")
359 #define FUNC_NAME s_scm_alignof
361 if (SCM_I_INUMP (type
))
363 switch (SCM_I_INUM (type
))
365 case SCM_FOREIGN_TYPE_FLOAT
:
366 return scm_from_size_t (alignof (float));
367 case SCM_FOREIGN_TYPE_DOUBLE
:
368 return scm_from_size_t (alignof (double));
369 case SCM_FOREIGN_TYPE_UINT8
:
370 return scm_from_size_t (alignof (scm_t_uint8
));
371 case SCM_FOREIGN_TYPE_INT8
:
372 return scm_from_size_t (alignof (scm_t_int8
));
373 case SCM_FOREIGN_TYPE_UINT16
:
374 return scm_from_size_t (alignof (scm_t_uint16
));
375 case SCM_FOREIGN_TYPE_INT16
:
376 return scm_from_size_t (alignof (scm_t_int16
));
377 case SCM_FOREIGN_TYPE_UINT32
:
378 return scm_from_size_t (alignof (scm_t_uint32
));
379 case SCM_FOREIGN_TYPE_INT32
:
380 return scm_from_size_t (alignof (scm_t_int32
));
381 case SCM_FOREIGN_TYPE_UINT64
:
382 return scm_from_size_t (alignof (scm_t_uint64
));
383 case SCM_FOREIGN_TYPE_INT64
:
384 return scm_from_size_t (alignof (scm_t_int64
));
386 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
389 else if (scm_is_eq (type
, sym_asterisk
))
391 return scm_from_size_t (alignof (void*));
392 else if (scm_is_pair (type
))
394 return scm_alignof (scm_car (type
));
396 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
400 SCM_DEFINE (scm_sizeof
, "sizeof", 1, 0, 0, (SCM type
),
401 "Return the size of @var{type}, in bytes.\n\n"
402 "@var{type} should be a valid C type, like @code{int}.\n"
403 "Alternately @var{type} may be the symbol @code{*}, in which\n"
404 "case the size of a pointer is returned. @var{type} may also\n"
405 "be a list of types, in which case the size of a @code{struct}\n"
406 "with ABI-conventional packing is returned.")
407 #define FUNC_NAME s_scm_sizeof
409 if (SCM_I_INUMP (type
))
411 switch (SCM_I_INUM (type
))
413 case SCM_FOREIGN_TYPE_FLOAT
:
414 return scm_from_size_t (sizeof (float));
415 case SCM_FOREIGN_TYPE_DOUBLE
:
416 return scm_from_size_t (sizeof (double));
417 case SCM_FOREIGN_TYPE_UINT8
:
418 return scm_from_size_t (sizeof (scm_t_uint8
));
419 case SCM_FOREIGN_TYPE_INT8
:
420 return scm_from_size_t (sizeof (scm_t_int8
));
421 case SCM_FOREIGN_TYPE_UINT16
:
422 return scm_from_size_t (sizeof (scm_t_uint16
));
423 case SCM_FOREIGN_TYPE_INT16
:
424 return scm_from_size_t (sizeof (scm_t_int16
));
425 case SCM_FOREIGN_TYPE_UINT32
:
426 return scm_from_size_t (sizeof (scm_t_uint32
));
427 case SCM_FOREIGN_TYPE_INT32
:
428 return scm_from_size_t (sizeof (scm_t_int32
));
429 case SCM_FOREIGN_TYPE_UINT64
:
430 return scm_from_size_t (sizeof (scm_t_uint64
));
431 case SCM_FOREIGN_TYPE_INT64
:
432 return scm_from_size_t (sizeof (scm_t_int64
));
434 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
437 else if (scm_is_eq (type
, sym_asterisk
))
439 return scm_from_size_t (sizeof (void*));
440 else if (scm_is_pair (type
))
444 while (scm_is_pair (type
))
446 off
= ROUND_UP (off
, scm_to_size_t (scm_alignof (scm_car (type
))));
447 off
+= scm_to_size_t (scm_sizeof (scm_car (type
)));
448 type
= scm_cdr (type
);
450 return scm_from_size_t (off
);
453 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
458 /* return 1 on success, 0 on failure */
460 parse_ffi_type (SCM type
, int return_p
, long *n_structs
, long *n_struct_elts
)
462 if (SCM_I_INUMP (type
))
464 if ((SCM_I_INUM (type
) < 0 )
465 || (SCM_I_INUM (type
) > SCM_FOREIGN_TYPE_LAST
))
467 else if (SCM_I_INUM (type
) == SCM_FOREIGN_TYPE_VOID
&& !return_p
)
472 else if (scm_is_eq (type
, sym_asterisk
))
479 len
= scm_ilength (type
);
484 if (!parse_ffi_type (scm_car (type
), 0, n_structs
, n_struct_elts
))
487 type
= scm_cdr (type
);
495 fill_ffi_type (SCM type
, ffi_type
*ftype
, ffi_type
***type_ptrs
,
498 if (SCM_I_INUMP (type
))
500 switch (SCM_I_INUM (type
))
502 case SCM_FOREIGN_TYPE_FLOAT
:
503 *ftype
= ffi_type_float
;
505 case SCM_FOREIGN_TYPE_DOUBLE
:
506 *ftype
= ffi_type_double
;
508 case SCM_FOREIGN_TYPE_UINT8
:
509 *ftype
= ffi_type_uint8
;
511 case SCM_FOREIGN_TYPE_INT8
:
512 *ftype
= ffi_type_sint8
;
514 case SCM_FOREIGN_TYPE_UINT16
:
515 *ftype
= ffi_type_uint16
;
517 case SCM_FOREIGN_TYPE_INT16
:
518 *ftype
= ffi_type_sint16
;
520 case SCM_FOREIGN_TYPE_UINT32
:
521 *ftype
= ffi_type_uint32
;
523 case SCM_FOREIGN_TYPE_INT32
:
524 *ftype
= ffi_type_sint32
;
526 case SCM_FOREIGN_TYPE_UINT64
:
527 *ftype
= ffi_type_uint64
;
529 case SCM_FOREIGN_TYPE_INT64
:
530 *ftype
= ffi_type_sint64
;
532 case SCM_FOREIGN_TYPE_VOID
:
533 *ftype
= ffi_type_void
;
536 scm_wrong_type_arg_msg ("pointer->procedure", 0, type
,
540 else if (scm_is_eq (type
, sym_asterisk
))
543 *ftype
= ffi_type_pointer
;
550 len
= scm_ilength (type
);
553 ftype
->alignment
= 0;
554 ftype
->type
= FFI_TYPE_STRUCT
;
555 ftype
->elements
= *type_ptrs
;
556 *type_ptrs
+= len
+ 1;
558 for (i
= 0; i
< len
; i
++)
560 ftype
->elements
[i
] = *types
;
562 fill_ffi_type (scm_car (type
), ftype
->elements
[i
],
564 type
= scm_cdr (type
);
566 ftype
->elements
[i
] = NULL
;
570 /* Return a "cif" (call interface) for the given RETURN_TYPE and
573 make_cif (SCM return_type
, SCM arg_types
, const char *caller
)
574 #define FUNC_NAME caller
577 long i
, nargs
, n_structs
, n_struct_elts
;
581 ffi_type
**type_ptrs
;
584 nargs
= scm_ilength (arg_types
);
585 SCM_ASSERT (nargs
>= 0, arg_types
, 3, FUNC_NAME
);
586 /* fixme: assert nargs < 1<<32 */
587 n_structs
= n_struct_elts
= 0;
589 /* For want of talloc, we're going to have to do this in two passes: first we
590 figure out how much memory is needed for all types, then we allocate the
591 cif and the types all in one block. */
592 if (!parse_ffi_type (return_type
, 1, &n_structs
, &n_struct_elts
))
593 scm_wrong_type_arg (FUNC_NAME
, 1, return_type
);
594 for (walk
= arg_types
; scm_is_pair (walk
); walk
= scm_cdr (walk
))
595 if (!parse_ffi_type (scm_car (walk
), 0, &n_structs
, &n_struct_elts
))
596 scm_wrong_type_arg (FUNC_NAME
, 3, scm_car (walk
));
598 /* the memory: with space for the cif itself */
599 cif_len
= sizeof (ffi_cif
);
601 /* then ffi_type pointers: one for each arg, one for each struct
602 element, and one for each struct (for null-termination) */
603 cif_len
= (ROUND_UP (cif_len
, alignof(void*))
604 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*));
606 /* then the ffi_type structs themselves, one per arg and struct element, and
607 one for the return val */
608 cif_len
= (ROUND_UP (cif_len
, alignof(ffi_type
))
609 + (nargs
+ n_struct_elts
+ 1)*sizeof(ffi_type
));
611 mem
= scm_gc_malloc_pointerless (cif_len
, "foreign");
612 /* ensure all the memory is initialized, even the holes */
613 memset (mem
, 0, cif_len
);
614 cif
= (ffi_cif
*) mem
;
616 /* reuse cif_len to walk through the mem */
617 cif_len
= ROUND_UP (sizeof (ffi_cif
), alignof(void*));
618 type_ptrs
= (ffi_type
**)(mem
+ cif_len
);
619 cif_len
= ROUND_UP (cif_len
620 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*),
622 types
= (ffi_type
*)(mem
+ cif_len
);
624 /* whew. now knit the pointers together. */
625 cif
->rtype
= types
++;
626 fill_ffi_type (return_type
, cif
->rtype
, &type_ptrs
, &types
);
627 cif
->arg_types
= type_ptrs
;
629 for (walk
= arg_types
, i
= 0; scm_is_pair (walk
); walk
= scm_cdr (walk
), i
++)
631 cif
->arg_types
[i
] = types
++;
632 fill_ffi_type (scm_car (walk
), cif
->arg_types
[i
], &type_ptrs
, &types
);
635 /* round out the cif, and we're done. */
636 cif
->abi
= FFI_DEFAULT_ABI
;
641 if (FFI_OK
!= ffi_prep_cif (cif
, FFI_DEFAULT_ABI
, cif
->nargs
, cif
->rtype
,
643 SCM_MISC_ERROR ("ffi_prep_cif failed", SCM_EOL
);
649 SCM_DEFINE (scm_pointer_to_procedure
, "pointer->procedure", 3, 0, 0,
650 (SCM return_type
, SCM func_ptr
, SCM arg_types
),
651 "Make a foreign function.\n\n"
652 "Given the foreign void pointer @var{func_ptr}, its argument and\n"
653 "return types @var{arg_types} and @var{return_type}, return a\n"
654 "procedure that will pass arguments to the foreign function\n"
655 "and return appropriate values.\n\n"
656 "@var{arg_types} should be a list of foreign types.\n"
657 "@code{return_type} should be a foreign type.")
658 #define FUNC_NAME s_scm_pointer_to_procedure
662 SCM_VALIDATE_POINTER (2, func_ptr
);
664 cif
= make_cif (return_type
, arg_types
, FUNC_NAME
);
666 return cif_to_procedure (scm_from_pointer (cif
, NULL
), func_ptr
);
672 /* Pre-generate trampolines for less than 10 arguments. */
674 #ifdef WORDS_BIGENDIAN
675 #define OBJCODE_HEADER 0, 0, 0, 8, 0, 0, 0, 40
676 #define META_HEADER 0, 0, 0, 32, 0, 0, 0, 0
678 #define OBJCODE_HEADER 8, 0, 0, 0, 40, 0, 0, 0
679 #define META_HEADER 32, 0, 0, 0, 0, 0, 0, 0
684 /* 0 */ scm_op_assert_nargs_ee, 0, nreq, /* assert number of args */ \
685 /* 3 */ scm_op_object_ref, 0, /* push the pair with the cif and the function pointer */ \
686 /* 5 */ scm_op_foreign_call, nreq, /* and call (will return value as well) */ \
687 /* 7 */ scm_op_nop, \
688 /* 8 */ META (3, 7, nreq)
690 #define META(start, end, nreq) \
692 /* 0 */ scm_op_make_eol, /* bindings */ \
693 /* 1 */ scm_op_make_eol, /* sources */ \
694 /* 2 */ scm_op_make_int8, start, scm_op_make_int8, end, /* arity: from ip N to ip N */ \
695 /* 6 */ scm_op_make_int8, nreq, /* the arity is N required args */ \
696 /* 8 */ scm_op_list, 0, 3, /* make a list of those 3 vals */ \
697 /* 11 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */ \
698 /* 14 */ scm_op_load_symbol, 0, 0, 4, 'n', 'a', 'm', 'e', /* `name' */ \
699 /* 22 */ scm_op_object_ref, 1, /* the name from the object table */ \
700 /* 24 */ scm_op_cons, /* make a pair for the properties */ \
701 /* 25 */ scm_op_list, 0, 4, /* pack bindings, sources, and arities into list */ \
702 /* 28 */ scm_op_return, /* and return */ \
703 /* 29 */ scm_op_nop, scm_op_nop, scm_op_nop \
708 scm_t_uint64 dummy
; /* ensure 8-byte alignment; perhaps there's a better way */
709 const scm_t_uint8 bytes
[10 * (sizeof (struct scm_objcode
) + 8
710 + sizeof (struct scm_objcode
) + 32)];
714 CODE (0), CODE (1), CODE (2), CODE (3), CODE (4),
715 CODE (5), CODE (6), CODE (7), CODE (8), CODE (9)
721 #undef OBJCODE_HEADER
725 (defun generate-objcode-cells (n)
726 "Generate objcode cells for up to N arguments"
731 (format " { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + %d) },\n"
732 (* (+ 4 4 8 4 4 32) i)))
733 (insert " { SCM_BOOL_F, SCM_PACK (0) },\n")
736 #define STATIC_OBJCODE_TAG \
737 SCM_PACK (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0))
741 scm_t_uint64 dummy
; /* alignment */
742 scm_t_cell cells
[10 * 2]; /* 10 double cells */
745 /* C-u 1 0 M-x generate-objcode-cells RET */
747 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 0) },
748 { SCM_BOOL_F
, SCM_PACK (0) },
749 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 56) },
750 { SCM_BOOL_F
, SCM_PACK (0) },
751 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 112) },
752 { SCM_BOOL_F
, SCM_PACK (0) },
753 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 168) },
754 { SCM_BOOL_F
, SCM_PACK (0) },
755 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 224) },
756 { SCM_BOOL_F
, SCM_PACK (0) },
757 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 280) },
758 { SCM_BOOL_F
, SCM_PACK (0) },
759 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 336) },
760 { SCM_BOOL_F
, SCM_PACK (0) },
761 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 392) },
762 { SCM_BOOL_F
, SCM_PACK (0) },
763 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 448) },
764 { SCM_BOOL_F
, SCM_PACK (0) },
765 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 504) },
766 { SCM_BOOL_F
, SCM_PACK (0) }
770 static const SCM objcode_trampolines
[10] = {
771 SCM_PACK (objcode_cells
.cells
+0),
772 SCM_PACK (objcode_cells
.cells
+2),
773 SCM_PACK (objcode_cells
.cells
+4),
774 SCM_PACK (objcode_cells
.cells
+6),
775 SCM_PACK (objcode_cells
.cells
+8),
776 SCM_PACK (objcode_cells
.cells
+10),
777 SCM_PACK (objcode_cells
.cells
+12),
778 SCM_PACK (objcode_cells
.cells
+14),
779 SCM_PACK (objcode_cells
.cells
+16),
780 SCM_PACK (objcode_cells
.cells
+18),
784 cif_to_procedure (SCM cif
, SCM func_ptr
)
788 SCM objcode
, table
, ret
;
790 c_cif
= (ffi_cif
*) SCM_POINTER_VALUE (cif
);
791 nargs
= c_cif
->nargs
;
794 objcode
= objcode_trampolines
[nargs
];
796 scm_misc_error ("make-foreign-function", "args >= 10 currently unimplemented",
799 table
= scm_c_make_vector (2, SCM_UNDEFINED
);
800 SCM_SIMPLE_VECTOR_SET (table
, 0, scm_cons (cif
, func_ptr
));
801 SCM_SIMPLE_VECTOR_SET (table
, 1, SCM_BOOL_F
); /* name */
802 ret
= scm_make_program (objcode
, table
, SCM_BOOL_F
);
807 /* Set *LOC to the foreign representation of X with TYPE. */
809 unpack (const ffi_type
*type
, void *loc
, SCM x
)
814 *(float *) loc
= scm_to_double (x
);
816 case FFI_TYPE_DOUBLE
:
817 *(double *) loc
= scm_to_double (x
);
820 *(scm_t_uint8
*) loc
= scm_to_uint8 (x
);
823 *(scm_t_int8
*) loc
= scm_to_int8 (x
);
825 case FFI_TYPE_UINT16
:
826 *(scm_t_uint16
*) loc
= scm_to_uint16 (x
);
828 case FFI_TYPE_SINT16
:
829 *(scm_t_int16
*) loc
= scm_to_int16 (x
);
831 case FFI_TYPE_UINT32
:
832 *(scm_t_uint32
*) loc
= scm_to_uint32 (x
);
834 case FFI_TYPE_SINT32
:
835 *(scm_t_int32
*) loc
= scm_to_int32 (x
);
837 case FFI_TYPE_UINT64
:
838 *(scm_t_uint64
*) loc
= scm_to_uint64 (x
);
840 case FFI_TYPE_SINT64
:
841 *(scm_t_int64
*) loc
= scm_to_int64 (x
);
843 case FFI_TYPE_STRUCT
:
844 memcpy (loc
, SCM_POINTER_VALUE (x
), type
->size
);
846 case FFI_TYPE_POINTER
:
847 *(void **) loc
= SCM_POINTER_VALUE (x
);
854 /* Return a Scheme representation of the foreign value at LOC of type TYPE. */
856 pack (const ffi_type
* type
, const void *loc
)
861 return SCM_UNSPECIFIED
;
863 return scm_from_double (*(float *) loc
);
864 case FFI_TYPE_DOUBLE
:
865 return scm_from_double (*(double *) loc
);
867 return scm_from_uint8 (*(scm_t_uint8
*) loc
);
869 return scm_from_int8 (*(scm_t_int8
*) loc
);
870 case FFI_TYPE_UINT16
:
871 return scm_from_uint16 (*(scm_t_uint16
*) loc
);
872 case FFI_TYPE_SINT16
:
873 return scm_from_int16 (*(scm_t_int16
*) loc
);
874 case FFI_TYPE_UINT32
:
875 return scm_from_uint32 (*(scm_t_uint32
*) loc
);
876 case FFI_TYPE_SINT32
:
877 return scm_from_int32 (*(scm_t_int32
*) loc
);
878 case FFI_TYPE_UINT64
:
879 return scm_from_uint64 (*(scm_t_uint64
*) loc
);
880 case FFI_TYPE_SINT64
:
881 return scm_from_int64 (*(scm_t_int64
*) loc
);
882 case FFI_TYPE_STRUCT
:
884 void *mem
= scm_gc_malloc_pointerless (type
->size
, "foreign");
885 memcpy (mem
, loc
, type
->size
);
886 return scm_from_pointer (mem
, NULL
);
888 case FFI_TYPE_POINTER
:
889 return scm_from_pointer (*(void **) loc
, NULL
);
897 scm_i_foreign_call (SCM foreign
, const SCM
*argv
)
899 /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
910 cif
= SCM_POINTER_VALUE (SCM_CAR (foreign
));
911 func
= SCM_POINTER_VALUE (SCM_CDR (foreign
));
913 /* Argument pointers. */
914 args
= alloca (sizeof (void *) * cif
->nargs
);
916 /* Compute the worst-case amount of memory needed to store all the argument
917 values. Note: as of libffi 3.0.9 `cif->bytes' is undocumented and is zero,
918 so it can't be used for that purpose. */
919 for (i
= 0, arg_size
= 0; i
< cif
->nargs
; i
++)
920 arg_size
+= cif
->arg_types
[i
]->size
+ cif
->arg_types
[i
]->alignment
- 1;
922 /* Space for argument values, followed by return value. */
923 data
= alloca (arg_size
+ cif
->rtype
->size
924 + max (sizeof (void *), cif
->rtype
->alignment
));
926 /* Unpack ARGV to native values, setting ARGV pointers. */
929 off
= (scm_t_uint8
*) args
[i
] - data
+ cif
->arg_types
[i
]->size
,
932 /* Suitably align the storage area for argument I. */
933 args
[i
] = (void *) ROUND_UP ((scm_t_uintptr
) data
+ off
,
934 cif
->arg_types
[i
]->alignment
);
935 assert ((scm_t_uintptr
) args
[i
] % cif
->arg_types
[i
]->alignment
== 0);
936 unpack (cif
->arg_types
[i
], args
[i
], argv
[i
]);
939 /* Prepare space for the return value. On some platforms, such as
940 `armv5tel-*-linux-gnueabi', the return value has to be at least
941 word-aligned, even if its type doesn't have any alignment requirement as is
942 the case with `char'. */
943 rvalue
= (void *) ROUND_UP ((scm_t_uintptr
) data
+ off
,
944 max (sizeof (void *), cif
->rtype
->alignment
));
947 ffi_call (cif
, func
, rvalue
, args
);
949 return pack (cif
->rtype
, rvalue
);
953 /* Function pointers aka. "callbacks" or "closures". */
957 /* Trampoline to invoke a libffi closure that wraps a Scheme
960 invoke_closure (ffi_cif
*cif
, void *ret
, void **args
, void *data
)
963 SCM proc
, *argv
, result
;
965 proc
= PTR2SCM (data
);
967 argv
= alloca (cif
->nargs
* sizeof (*argv
));
969 /* Pack ARGS to SCM values, setting ARGV pointers. */
970 for (i
= 0; i
< cif
->nargs
; i
++)
971 argv
[i
] = pack (cif
->arg_types
[i
], args
[i
]);
973 result
= scm_call_n (proc
, argv
, cif
->nargs
);
975 unpack (cif
->rtype
, ret
, result
);
978 SCM_DEFINE (scm_procedure_to_pointer
, "procedure->pointer", 3, 0, 0,
979 (SCM return_type
, SCM proc
, SCM arg_types
),
980 "Return a pointer to a C function of type @var{return-type}\n"
981 "taking arguments of types @var{arg-types} (a list) and\n"
982 "behaving as a proxy to procedure @var{proc}. Thus\n"
983 "@var{proc}'s arity, supported argument types, and return\n"
984 "type should match @var{return-type} and @var{arg-types}.\n")
985 #define FUNC_NAME s_scm_procedure_to_pointer
990 void *closure
, *executable
;
992 cif
= make_cif (return_type
, arg_types
, FUNC_NAME
);
994 closure
= ffi_closure_alloc (sizeof (ffi_closure
), &executable
);
995 err
= ffi_prep_closure_loc ((ffi_closure
*) closure
, cif
,
996 invoke_closure
, SCM2PTR (proc
),
1000 ffi_closure_free (closure
);
1001 SCM_MISC_ERROR ("`ffi_prep_closure_loc' failed", SCM_EOL
);
1004 if (closure
== executable
)
1005 pointer
= scm_from_pointer (executable
, ffi_closure_free
);
1008 /* CLOSURE needs to be freed eventually. However, since
1009 `GC_all_interior_pointers' is disabled, we can't just register
1010 a finalizer for CLOSURE. Instead, we create a pointer object
1011 for CLOSURE, with a finalizer, and register it as a weak
1012 reference of POINTER. */
1015 pointer
= scm_from_pointer (executable
, NULL
);
1016 friend = scm_from_pointer (closure
, ffi_closure_free
);
1018 register_weak_reference (pointer
, friend);
1025 #endif /* FFI_CLOSURES */
1030 scm_init_foreign (void)
1032 #ifndef SCM_MAGIC_SNARFER
1033 #include "libguile/foreign.x"
1035 scm_define (sym_void
, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID
));
1036 scm_define (sym_float
, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT
));
1037 scm_define (sym_double
, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE
));
1038 scm_define (sym_uint8
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8
));
1039 scm_define (sym_int8
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8
));
1040 scm_define (sym_uint16
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16
));
1041 scm_define (sym_int16
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16
));
1042 scm_define (sym_uint32
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
));
1043 scm_define (sym_int32
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
));
1044 scm_define (sym_uint64
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
));
1045 scm_define (sym_int64
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
));
1047 scm_define (sym_short
,
1048 #if SIZEOF_SHORT == 8
1049 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1050 #elif SIZEOF_SHORT == 4
1051 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1052 #elif SIZEOF_SHORT == 2
1053 scm_from_uint8 (SCM_FOREIGN_TYPE_INT16
)
1055 # error unsupported sizeof (short)
1059 scm_define (sym_unsigned_short
,
1060 #if SIZEOF_SHORT == 8
1061 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1062 #elif SIZEOF_SHORT == 4
1063 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1064 #elif SIZEOF_SHORT == 2
1065 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16
)
1067 # error unsupported sizeof (short)
1071 scm_define (sym_int
,
1073 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1074 #elif SIZEOF_INT == 4
1075 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1077 # error unsupported sizeof (int)
1081 scm_define (sym_unsigned_int
,
1082 #if SIZEOF_UNSIGNED_INT == 8
1083 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1084 #elif SIZEOF_UNSIGNED_INT == 4
1085 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1087 # error unsupported sizeof (unsigned int)
1091 scm_define (sym_long
,
1092 #if SIZEOF_LONG == 8
1093 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1094 #elif SIZEOF_LONG == 4
1095 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1097 # error unsupported sizeof (long)
1101 scm_define (sym_unsigned_long
,
1102 #if SIZEOF_UNSIGNED_LONG == 8
1103 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1104 #elif SIZEOF_UNSIGNED_LONG == 4
1105 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1107 # error unsupported sizeof (unsigned long)
1111 scm_define (sym_size_t
,
1112 #if SIZEOF_SIZE_T == 8
1113 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1114 #elif SIZEOF_SIZE_T == 4
1115 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1117 # error unsupported sizeof (size_t)
1121 null_pointer
= scm_cell (scm_tc7_pointer
, 0);
1122 scm_define (sym_null
, null_pointer
);
1126 scm_register_foreign (void)
1128 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
1130 (scm_t_extension_init_func
)scm_init_foreign
,
1132 pointer_weak_refs
= scm_make_weak_key_hash_table (SCM_UNDEFINED
);