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
27 #include "libguile/_scm.h"
28 #include "libguile/bytevectors.h"
29 #include "libguile/instructions.h"
30 #include "libguile/foreign.h"
34 SCM_SYMBOL (sym_void
, "void");
35 SCM_SYMBOL (sym_float
, "float");
36 SCM_SYMBOL (sym_double
, "double");
37 SCM_SYMBOL (sym_uint8
, "uint8");
38 SCM_SYMBOL (sym_int8
, "int8");
39 SCM_SYMBOL (sym_uint16
, "uint16");
40 SCM_SYMBOL (sym_int16
, "int16");
41 SCM_SYMBOL (sym_uint32
, "uint32");
42 SCM_SYMBOL (sym_int32
, "int32");
43 SCM_SYMBOL (sym_uint64
, "uint64");
44 SCM_SYMBOL (sym_int64
, "int64");
45 SCM_SYMBOL (sym_int
, "int");
46 SCM_SYMBOL (sym_long
, "long");
47 SCM_SYMBOL (sym_unsigned_int
, "unsigned-int");
48 SCM_SYMBOL (sym_unsigned_long
, "unsigned-long");
49 SCM_SYMBOL (sym_size_t
, "size_t");
51 /* that's for pointers, you know. */
52 SCM_SYMBOL (sym_asterisk
, "*");
54 SCM_SYMBOL (sym_null
, "%null-pointer");
56 /* The cell representing the null pointer. */
57 static const scm_t_bits null_pointer
[2] =
59 scm_tc7_foreign
| (SCM_FOREIGN_TYPE_VOID
<< 8UL),
63 static SCM
cif_to_procedure (SCM cif
, SCM func_ptr
);
66 static SCM foreign_weak_refs
= SCM_BOOL_F
;
69 register_weak_reference (SCM from
, SCM to
)
71 scm_hashq_set_x (foreign_weak_refs
, from
, to
);
75 foreign_finalizer_trampoline (GC_PTR ptr
, GC_PTR data
)
77 scm_t_foreign_finalizer finalizer
= data
;
78 finalizer (SCM_FOREIGN_POINTER (PTR2SCM (ptr
), void));
82 scm_take_foreign_pointer (scm_t_foreign_type type
, void *ptr
, size_t len
,
83 scm_t_foreign_finalizer finalizer
)
88 word0
= (scm_t_bits
)(scm_tc7_foreign
| (type
<<8)
89 | (finalizer
? (1<<16) : 0) | (len
<<17));
90 if (SCM_UNLIKELY ((word0
>> 17) != len
))
91 scm_out_of_range ("scm_take_foreign_pointer", scm_from_size_t (len
));
93 ret
= PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_bits
) * 2,
95 SCM_SET_CELL_WORD_0 (ret
, word0
);
96 SCM_SET_CELL_WORD_1 (ret
, (scm_t_bits
)ptr
);
100 /* Register a finalizer for the newly created instance. */
101 GC_finalization_proc prev_finalizer
;
102 GC_PTR prev_finalizer_data
;
103 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret
),
104 foreign_finalizer_trampoline
,
107 &prev_finalizer_data
);
113 SCM_DEFINE (scm_foreign_ref
, "foreign-ref", 1, 0, 0,
115 "Reference the foreign value wrapped by @var{foreign}.\n\n"
116 "The value will be referenced according to its type.")
117 #define FUNC_NAME s_scm_foreign_ref
119 scm_t_foreign_type ftype
;
122 SCM_VALIDATE_FOREIGN (1, foreign
);
123 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_uint8
);
124 ftype
= SCM_FOREIGN_TYPE (foreign
);
126 /* FIXME: is there a window in which we can see ptr but not foreign? */
127 /* FIXME: accessing unaligned pointers */
130 case SCM_FOREIGN_TYPE_VOID
:
131 return scm_from_ulong ((unsigned long)ptr
);
132 case SCM_FOREIGN_TYPE_FLOAT
:
133 return scm_from_double (*(float*)ptr
);
134 case SCM_FOREIGN_TYPE_DOUBLE
:
135 return scm_from_double (*(double*)ptr
);
136 case SCM_FOREIGN_TYPE_UINT8
:
137 return scm_from_uint8 (*(scm_t_uint8
*)ptr
);
138 case SCM_FOREIGN_TYPE_INT8
:
139 return scm_from_int8 (*(scm_t_int8
*)ptr
);
140 case SCM_FOREIGN_TYPE_UINT16
:
141 return scm_from_uint16 (*(scm_t_uint16
*)ptr
);
142 case SCM_FOREIGN_TYPE_INT16
:
143 return scm_from_int16 (*(scm_t_int16
*)ptr
);
144 case SCM_FOREIGN_TYPE_UINT32
:
145 return scm_from_uint32 (*(scm_t_uint32
*)ptr
);
146 case SCM_FOREIGN_TYPE_INT32
:
147 return scm_from_int32 (*(scm_t_int32
*)ptr
);
148 case SCM_FOREIGN_TYPE_UINT64
:
149 return scm_from_uint64 (*(scm_t_uint64
*)ptr
);
150 case SCM_FOREIGN_TYPE_INT64
:
151 return scm_from_int64 (*(scm_t_int64
*)ptr
);
153 scm_wrong_type_arg_msg (FUNC_NAME
, 1, foreign
, "foreign");
158 SCM_DEFINE (scm_foreign_set_x
, "foreign-set!", 2, 0, 0,
159 (SCM foreign
, SCM val
),
160 "Set the foreign value wrapped by @var{foreign}.\n\n"
161 "The value will be set according to its type.")
162 #define FUNC_NAME s_scm_foreign_set_x
164 scm_t_foreign_type ftype
;
167 SCM_VALIDATE_FOREIGN (1, foreign
);
168 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_uint8
);
169 ftype
= SCM_FOREIGN_TYPE (foreign
);
171 /* FIXME: is there a window in which we can see ptr but not foreign? */
172 /* FIXME: unaligned access */
175 case SCM_FOREIGN_TYPE_VOID
:
176 SCM_SET_CELL_WORD_1 (foreign
, scm_to_ulong (val
));
178 case SCM_FOREIGN_TYPE_FLOAT
:
179 *(float*)ptr
= scm_to_double (val
);
181 case SCM_FOREIGN_TYPE_DOUBLE
:
182 *(double*)ptr
= scm_to_double (val
);
184 case SCM_FOREIGN_TYPE_UINT8
:
185 *(scm_t_uint8
*)ptr
= scm_to_uint8 (val
);
187 case SCM_FOREIGN_TYPE_INT8
:
188 *(scm_t_int8
*)ptr
= scm_to_int8 (val
);
190 case SCM_FOREIGN_TYPE_UINT16
:
191 *(scm_t_uint16
*)ptr
= scm_to_uint16 (val
);
193 case SCM_FOREIGN_TYPE_INT16
:
194 *(scm_t_int16
*)ptr
= scm_to_int16 (val
);
196 case SCM_FOREIGN_TYPE_UINT32
:
197 *(scm_t_uint32
*)ptr
= scm_to_uint32 (val
);
199 case SCM_FOREIGN_TYPE_INT32
:
200 *(scm_t_int32
*)ptr
= scm_to_int32 (val
);
202 case SCM_FOREIGN_TYPE_UINT64
:
203 *(scm_t_uint64
*)ptr
= scm_to_uint64 (val
);
205 case SCM_FOREIGN_TYPE_INT64
:
206 *(scm_t_int64
*)ptr
= scm_to_int64 (val
);
209 scm_wrong_type_arg_msg (FUNC_NAME
, 1, val
, "foreign");
212 return SCM_UNSPECIFIED
;
216 SCM_DEFINE (scm_foreign_to_bytevector
, "foreign->bytevector", 1, 3, 0,
217 (SCM foreign
, SCM uvec_type
, SCM offset
, SCM len
),
218 "Return a bytevector aliasing the memory pointed to by\n"
220 "@var{foreign} must be a void pointer, a foreign whose type is\n"
221 "@var{void}. By default, the resulting bytevector will alias\n"
222 "all of the memory pointed to by @var{foreign}, from beginning\n"
223 "to end, treated as a @code{vu8} array.\n\n"
224 "The user may specify an alternate default interpretation for\n"
225 "the memory by passing the @var{uvec_type} argument, to indicate\n"
226 "that the memory is an array of elements of that type.\n"
227 "@var{uvec_type} should be something that\n"
228 "@code{uniform-vector-element-type} would return, like @code{f32}\n"
230 "Users may also specify that the bytevector should only alias a\n"
231 "subset of the memory, by specifying @var{offset} and @var{len}\n"
233 #define FUNC_NAME s_scm_foreign_to_bytevector
237 size_t boffset
, blen
;
238 scm_t_array_element_type btype
;
240 SCM_VALIDATE_FOREIGN_TYPED (1, foreign
, VOID
);
241 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_int8
);
243 if (SCM_UNLIKELY (ptr
== NULL
))
244 scm_misc_error (FUNC_NAME
, "null pointer dereference", SCM_EOL
);
246 if (SCM_UNBNDP (uvec_type
))
247 btype
= SCM_ARRAY_ELEMENT_TYPE_VU8
;
251 for (i
= 0; i
<= SCM_ARRAY_ELEMENT_TYPE_LAST
; i
++)
252 if (scm_is_eq (uvec_type
, scm_i_array_element_types
[i
]))
256 case SCM_ARRAY_ELEMENT_TYPE_VU8
:
257 case SCM_ARRAY_ELEMENT_TYPE_U8
:
258 case SCM_ARRAY_ELEMENT_TYPE_S8
:
259 case SCM_ARRAY_ELEMENT_TYPE_U16
:
260 case SCM_ARRAY_ELEMENT_TYPE_S16
:
261 case SCM_ARRAY_ELEMENT_TYPE_U32
:
262 case SCM_ARRAY_ELEMENT_TYPE_S32
:
263 case SCM_ARRAY_ELEMENT_TYPE_U64
:
264 case SCM_ARRAY_ELEMENT_TYPE_S64
:
265 case SCM_ARRAY_ELEMENT_TYPE_F32
:
266 case SCM_ARRAY_ELEMENT_TYPE_F64
:
267 case SCM_ARRAY_ELEMENT_TYPE_C32
:
268 case SCM_ARRAY_ELEMENT_TYPE_C64
:
272 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, uvec_type
,
273 "uniform vector type");
277 if (SCM_UNBNDP (offset
))
279 else if (SCM_FOREIGN_LEN (foreign
))
280 boffset
= scm_to_unsigned_integer (offset
, 0,
281 SCM_FOREIGN_LEN (foreign
) - 1);
283 boffset
= scm_to_size_t (offset
);
285 if (SCM_UNBNDP (len
))
287 if (SCM_FOREIGN_LEN (foreign
))
288 blen
= SCM_FOREIGN_LEN (foreign
) - boffset
;
290 scm_misc_error (FUNC_NAME
,
291 "length needed to convert foreign pointer to bytevector",
296 if (SCM_FOREIGN_LEN (foreign
))
297 blen
= scm_to_unsigned_integer (len
, 0,
298 SCM_FOREIGN_LEN (foreign
) - boffset
);
300 blen
= scm_to_size_t (len
);
303 ret
= scm_c_take_typed_bytevector (ptr
+ boffset
, blen
, btype
);
304 register_weak_reference (ret
, foreign
);
309 SCM_DEFINE (scm_bytevector_to_foreign
, "bytevector->foreign", 1, 2, 0,
310 (SCM bv
, SCM offset
, SCM len
),
311 "Return a foreign pointer aliasing the memory pointed to by\n"
313 "The resulting foreign will be a void pointer, a foreign whose\n"
314 "type is @code{void}. By default it will alias all of the\n"
315 "memory pointed to by @var{bv}, from beginning to end.\n\n"
316 "Users may explicily specify that the foreign should only alias a\n"
317 "subset of the memory, by specifying @var{offset} and @var{len}\n"
319 #define FUNC_NAME s_scm_bytevector_to_foreign
323 size_t boffset
, blen
;
325 SCM_VALIDATE_BYTEVECTOR (1, bv
);
326 ptr
= SCM_BYTEVECTOR_CONTENTS (bv
);
328 if (SCM_UNBNDP (offset
))
331 boffset
= scm_to_unsigned_integer (offset
, 0,
332 SCM_BYTEVECTOR_LENGTH (bv
) - 1);
334 if (SCM_UNBNDP (len
))
335 blen
= SCM_BYTEVECTOR_LENGTH (bv
) - boffset
;
337 blen
= scm_to_unsigned_integer (len
, 0,
338 SCM_BYTEVECTOR_LENGTH (bv
) - boffset
);
340 ret
= scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
, ptr
+ boffset
, blen
,
342 register_weak_reference (ret
, bv
);
347 SCM_DEFINE (scm_foreign_set_finalizer_x
, "foreign-set-finalizer!", 2, 0, 0,
348 (SCM foreign
, SCM finalizer
),
349 "Arrange for the C procedure wrapped by @var{finalizer} to be\n"
350 "called on the pointer wrapped by @var{foreign} when @var{foreign}\n"
351 "becomes unreachable. Note: the C procedure should not call into\n"
352 "Scheme. If you need a Scheme finalizer, use guardians.")
353 #define FUNC_NAME s_scm_foreign_set_finalizer_x
356 GC_finalization_proc prev_finalizer
;
357 GC_PTR prev_finalizer_data
;
359 SCM_VALIDATE_FOREIGN_TYPED (1, foreign
, VOID
);
360 SCM_VALIDATE_FOREIGN_TYPED (2, finalizer
, VOID
);
362 c_finalizer
= SCM_FOREIGN_POINTER (finalizer
, void);
364 SCM_SET_CELL_WORD_0 (foreign
, SCM_CELL_WORD_0 (foreign
) | (1<<16));
366 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (foreign
),
367 foreign_finalizer_trampoline
,
370 &prev_finalizer_data
);
372 return SCM_UNSPECIFIED
;
379 scm_i_foreign_print (SCM foreign
, SCM port
, scm_print_state
*pstate
)
381 scm_puts ("#<foreign ", port
);
382 switch (SCM_FOREIGN_TYPE (foreign
))
384 case SCM_FOREIGN_TYPE_FLOAT
:
385 scm_puts ("float ", port
);
387 case SCM_FOREIGN_TYPE_DOUBLE
:
388 scm_puts ("double ", port
);
390 case SCM_FOREIGN_TYPE_UINT8
:
391 scm_puts ("uint8 ", port
);
393 case SCM_FOREIGN_TYPE_INT8
:
394 scm_puts ("int8 ", port
);
396 case SCM_FOREIGN_TYPE_UINT16
:
397 scm_puts ("uint16 ", port
);
399 case SCM_FOREIGN_TYPE_INT16
:
400 scm_puts ("int16 ", port
);
402 case SCM_FOREIGN_TYPE_UINT32
:
403 scm_puts ("uint32 ", port
);
405 case SCM_FOREIGN_TYPE_INT32
:
406 scm_puts ("int32 ", port
);
408 case SCM_FOREIGN_TYPE_UINT64
:
409 scm_puts ("uint64 ", port
);
411 case SCM_FOREIGN_TYPE_INT64
:
412 scm_puts ("int64 ", port
);
414 case SCM_FOREIGN_TYPE_VOID
:
415 scm_puts ("pointer ", port
);
418 scm_wrong_type_arg_msg ("%print-foreign", 1, foreign
, "foreign");
420 scm_display (scm_foreign_ref (foreign
), port
);
421 scm_putc ('>', port
);
427 #define ROUND_UP(len,align) (align?(((len-1)|(align-1))+1):len)
429 SCM_DEFINE (scm_alignof
, "alignof", 1, 0, 0, (SCM type
), "")
430 #define FUNC_NAME s_scm_alignof
432 if (SCM_I_INUMP (type
))
434 switch (SCM_I_INUM (type
))
436 case SCM_FOREIGN_TYPE_FLOAT
:
437 return scm_from_size_t (alignof (float));
438 case SCM_FOREIGN_TYPE_DOUBLE
:
439 return scm_from_size_t (alignof (double));
440 case SCM_FOREIGN_TYPE_UINT8
:
441 return scm_from_size_t (alignof (scm_t_uint8
));
442 case SCM_FOREIGN_TYPE_INT8
:
443 return scm_from_size_t (alignof (scm_t_int8
));
444 case SCM_FOREIGN_TYPE_UINT16
:
445 return scm_from_size_t (alignof (scm_t_uint16
));
446 case SCM_FOREIGN_TYPE_INT16
:
447 return scm_from_size_t (alignof (scm_t_int16
));
448 case SCM_FOREIGN_TYPE_UINT32
:
449 return scm_from_size_t (alignof (scm_t_uint32
));
450 case SCM_FOREIGN_TYPE_INT32
:
451 return scm_from_size_t (alignof (scm_t_int32
));
452 case SCM_FOREIGN_TYPE_UINT64
:
453 return scm_from_size_t (alignof (scm_t_uint64
));
454 case SCM_FOREIGN_TYPE_INT64
:
455 return scm_from_size_t (alignof (scm_t_int64
));
457 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
460 else if (scm_is_eq (type
, sym_asterisk
))
462 return scm_from_size_t (alignof (void*));
463 else if (scm_is_pair (type
))
465 return scm_alignof (scm_car (type
));
467 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
471 SCM_DEFINE (scm_sizeof
, "sizeof", 1, 0, 0, (SCM type
), "")
472 #define FUNC_NAME s_scm_sizeof
474 if (SCM_I_INUMP (type
))
476 switch (SCM_I_INUM (type
))
478 case SCM_FOREIGN_TYPE_FLOAT
:
479 return scm_from_size_t (sizeof (float));
480 case SCM_FOREIGN_TYPE_DOUBLE
:
481 return scm_from_size_t (sizeof (double));
482 case SCM_FOREIGN_TYPE_UINT8
:
483 return scm_from_size_t (sizeof (scm_t_uint8
));
484 case SCM_FOREIGN_TYPE_INT8
:
485 return scm_from_size_t (sizeof (scm_t_int8
));
486 case SCM_FOREIGN_TYPE_UINT16
:
487 return scm_from_size_t (sizeof (scm_t_uint16
));
488 case SCM_FOREIGN_TYPE_INT16
:
489 return scm_from_size_t (sizeof (scm_t_int16
));
490 case SCM_FOREIGN_TYPE_UINT32
:
491 return scm_from_size_t (sizeof (scm_t_uint32
));
492 case SCM_FOREIGN_TYPE_INT32
:
493 return scm_from_size_t (sizeof (scm_t_int32
));
494 case SCM_FOREIGN_TYPE_UINT64
:
495 return scm_from_size_t (sizeof (scm_t_uint64
));
496 case SCM_FOREIGN_TYPE_INT64
:
497 return scm_from_size_t (sizeof (scm_t_int64
));
499 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
502 else if (scm_is_eq (type
, sym_asterisk
))
504 return scm_from_size_t (sizeof (void*));
505 else if (scm_is_pair (type
))
509 while (scm_is_pair (type
))
511 off
= ROUND_UP (off
, scm_to_size_t (scm_alignof (scm_car (type
))));
512 off
+= scm_to_size_t (scm_sizeof (scm_car (type
)));
513 type
= scm_cdr (type
);
515 return scm_from_size_t (off
);
518 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
523 /* return 1 on success, 0 on failure */
525 parse_ffi_type (SCM type
, int return_p
, long *n_structs
, long *n_struct_elts
)
527 if (SCM_I_INUMP (type
))
529 if ((SCM_I_INUM (type
) < 0 )
530 || (SCM_I_INUM (type
) > SCM_FOREIGN_TYPE_LAST
))
532 else if (SCM_I_INUM (type
) == SCM_FOREIGN_TYPE_VOID
&& !return_p
)
537 else if (scm_is_eq (type
, sym_asterisk
))
544 len
= scm_ilength (type
);
549 if (!parse_ffi_type (scm_car (type
), 0, n_structs
, n_struct_elts
))
552 type
= scm_cdr (type
);
560 fill_ffi_type (SCM type
, ffi_type
*ftype
, ffi_type
***type_ptrs
,
563 if (SCM_I_INUMP (type
))
565 switch (SCM_I_INUM (type
))
567 case SCM_FOREIGN_TYPE_FLOAT
:
568 *ftype
= ffi_type_float
;
570 case SCM_FOREIGN_TYPE_DOUBLE
:
571 *ftype
= ffi_type_double
;
573 case SCM_FOREIGN_TYPE_UINT8
:
574 *ftype
= ffi_type_uint8
;
576 case SCM_FOREIGN_TYPE_INT8
:
577 *ftype
= ffi_type_sint8
;
579 case SCM_FOREIGN_TYPE_UINT16
:
580 *ftype
= ffi_type_uint16
;
582 case SCM_FOREIGN_TYPE_INT16
:
583 *ftype
= ffi_type_sint16
;
585 case SCM_FOREIGN_TYPE_UINT32
:
586 *ftype
= ffi_type_uint32
;
588 case SCM_FOREIGN_TYPE_INT32
:
589 *ftype
= ffi_type_sint32
;
591 case SCM_FOREIGN_TYPE_UINT64
:
592 *ftype
= ffi_type_uint64
;
594 case SCM_FOREIGN_TYPE_INT64
:
595 *ftype
= ffi_type_sint64
;
597 case SCM_FOREIGN_TYPE_VOID
:
598 *ftype
= ffi_type_void
;
601 scm_wrong_type_arg_msg ("make-foreign-function", 0, type
,
605 else if (scm_is_eq (type
, sym_asterisk
))
608 *ftype
= ffi_type_pointer
;
615 len
= scm_ilength (type
);
618 ftype
->alignment
= 0;
619 ftype
->type
= FFI_TYPE_STRUCT
;
620 ftype
->elements
= *type_ptrs
;
621 *type_ptrs
+= len
+ 1;
623 for (i
= 0; i
< len
; i
++)
625 ftype
->elements
[i
] = *types
;
627 fill_ffi_type (scm_car (type
), ftype
->elements
[i
],
629 type
= scm_cdr (type
);
631 ftype
->elements
[i
] = NULL
;
635 SCM_DEFINE (scm_make_foreign_function
, "make-foreign-function", 3, 0, 0,
636 (SCM return_type
, SCM func_ptr
, SCM arg_types
),
638 #define FUNC_NAME s_scm_make_foreign_function
641 long i
, nargs
, n_structs
, n_struct_elts
;
645 ffi_type
**type_ptrs
;
648 SCM_VALIDATE_FOREIGN_TYPED (2, func_ptr
, VOID
);
649 nargs
= scm_ilength (arg_types
);
650 SCM_ASSERT (nargs
>= 0, arg_types
, 3, FUNC_NAME
);
651 /* fixme: assert nargs < 1<<32 */
652 n_structs
= n_struct_elts
= 0;
654 /* For want of talloc, we're going to have to do this in two passes: first we
655 figure out how much memory is needed for all types, then we allocate the
656 cif and the types all in one block. */
657 if (!parse_ffi_type (return_type
, 1, &n_structs
, &n_struct_elts
))
658 scm_wrong_type_arg (FUNC_NAME
, 1, return_type
);
659 for (walk
= arg_types
; scm_is_pair (walk
); walk
= scm_cdr (walk
))
660 if (!parse_ffi_type (scm_car (walk
), 0, &n_structs
, &n_struct_elts
))
661 scm_wrong_type_arg (FUNC_NAME
, 3, scm_car (walk
));
663 /* the memory: with space for the cif itself */
664 cif_len
= sizeof (ffi_cif
);
666 /* then ffi_type pointers: one for each arg, one for each struct
667 element, and one for each struct (for null-termination) */
668 cif_len
= (ROUND_UP (cif_len
, alignof(void*))
669 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*));
671 /* then the ffi_type structs themselves, one per arg and struct element, and
672 one for the return val */
673 cif_len
= (ROUND_UP (cif_len
, alignof(ffi_type
))
674 + (nargs
+ n_struct_elts
+ 1)*sizeof(ffi_type
));
676 mem
= scm_gc_malloc_pointerless (cif_len
, "foreign");
677 scm_cif
= scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
, mem
,
679 cif
= (ffi_cif
*) mem
;
681 /* reuse cif_len to walk through the mem */
682 cif_len
= ROUND_UP (sizeof (ffi_cif
), alignof(void*));
683 type_ptrs
= (ffi_type
**)(mem
+ cif_len
);
684 cif_len
= ROUND_UP (cif_len
685 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*),
687 types
= (ffi_type
*)(mem
+ cif_len
);
689 /* whew. now knit the pointers together. */
690 cif
->rtype
= types
++;
691 fill_ffi_type (return_type
, cif
->rtype
, &type_ptrs
, &types
);
692 cif
->arg_types
= type_ptrs
;
694 for (walk
= arg_types
, i
= 0; scm_is_pair (walk
); walk
= scm_cdr (walk
), i
++)
696 cif
->arg_types
[i
] = types
++;
697 fill_ffi_type (scm_car (walk
), cif
->arg_types
[i
], &type_ptrs
, &types
);
700 /* round out the cif, and we're done. */
701 cif
->abi
= FFI_DEFAULT_ABI
;
706 if (FFI_OK
!= ffi_prep_cif (cif
, FFI_DEFAULT_ABI
, cif
->nargs
, cif
->rtype
,
708 scm_misc_error (FUNC_NAME
, "ffi_prep_cif failed", SCM_EOL
);
710 return cif_to_procedure (scm_cif
, func_ptr
);
716 /* Pre-generate trampolines for less than 10 arguments. */
718 #ifdef WORDS_BIGENDIAN
719 #define OBJCODE_HEADER 0, 0, 0, 8, 0, 0, 0, 40
720 #define META_HEADER 0, 0, 0, 32, 0, 0, 0, 0
722 #define OBJCODE_HEADER 8, 0, 0, 0, 40, 0, 0, 0
723 #define META_HEADER 32, 0, 0, 0, 0, 0, 0, 0
728 /* 0 */ scm_op_assert_nargs_ee, 0, nreq, /* assert number of args */ \
729 /* 3 */ scm_op_object_ref, 0, /* push the pair with the cif and the function pointer */ \
730 /* 5 */ scm_op_foreign_call, nreq, /* and call (will return value as well) */ \
731 /* 7 */ scm_op_nop, \
732 /* 8 */ META (3, 7, nreq)
734 #define META(start, end, nreq) \
736 /* 0 */ scm_op_make_eol, /* bindings */ \
737 /* 1 */ scm_op_make_eol, /* sources */ \
738 /* 2 */ scm_op_make_int8, start, scm_op_make_int8, end, /* arity: from ip N to ip N */ \
739 /* 6 */ scm_op_make_int8, nreq, /* the arity is N required args */ \
740 /* 8 */ scm_op_list, 0, 3, /* make a list of those 3 vals */ \
741 /* 11 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */ \
742 /* 14 */ scm_op_load_symbol, 0, 0, 4, 'n', 'a', 'm', 'e', /* `name' */ \
743 /* 22 */ scm_op_object_ref, 1, /* the name from the object table */ \
744 /* 24 */ scm_op_cons, /* make a pair for the properties */ \
745 /* 25 */ scm_op_list, 0, 4, /* pack bindings, sources, and arities into list */ \
746 /* 28 */ scm_op_return, /* and return */ \
747 /* 29 */ scm_op_nop, scm_op_nop, scm_op_nop \
752 scm_t_uint64 dummy
; /* ensure 8-byte alignment; perhaps there's a better way */
753 const scm_t_uint8 bytes
[10 * (sizeof (struct scm_objcode
) + 8
754 + sizeof (struct scm_objcode
) + 32)];
758 CODE (0), CODE (1), CODE (2), CODE (3), CODE (4),
759 CODE (5), CODE (6), CODE (7), CODE (8), CODE (9)
765 #undef OBJCODE_HEADER
769 (defun generate-objcode-cells (n)
770 "Generate objcode cells for up to N arguments"
775 (format " { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + %d) },\n"
776 (* (+ 4 4 8 4 4 32) i)))
777 (insert " { SCM_BOOL_F, SCM_PACK (0) },\n")
780 #define STATIC_OBJCODE_TAG \
781 SCM_PACK (scm_tc7_objcode | (SCM_F_OBJCODE_IS_STATIC << 8))
785 scm_t_uint64 dummy
; /* alignment */
786 scm_t_cell cells
[10 * 2]; /* 10 double cells */
789 /* C-u 1 0 M-x generate-objcode-cells RET */
791 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 0) },
792 { SCM_BOOL_F
, SCM_PACK (0) },
793 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 56) },
794 { SCM_BOOL_F
, SCM_PACK (0) },
795 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 112) },
796 { SCM_BOOL_F
, SCM_PACK (0) },
797 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 168) },
798 { SCM_BOOL_F
, SCM_PACK (0) },
799 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 224) },
800 { SCM_BOOL_F
, SCM_PACK (0) },
801 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 280) },
802 { SCM_BOOL_F
, SCM_PACK (0) },
803 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 336) },
804 { SCM_BOOL_F
, SCM_PACK (0) },
805 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 392) },
806 { SCM_BOOL_F
, SCM_PACK (0) },
807 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 448) },
808 { SCM_BOOL_F
, SCM_PACK (0) },
809 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 504) },
810 { SCM_BOOL_F
, SCM_PACK (0) }
814 static const SCM objcode_trampolines
[10] = {
815 SCM_PACK (objcode_cells
.cells
+0),
816 SCM_PACK (objcode_cells
.cells
+2),
817 SCM_PACK (objcode_cells
.cells
+4),
818 SCM_PACK (objcode_cells
.cells
+6),
819 SCM_PACK (objcode_cells
.cells
+8),
820 SCM_PACK (objcode_cells
.cells
+10),
821 SCM_PACK (objcode_cells
.cells
+12),
822 SCM_PACK (objcode_cells
.cells
+14),
823 SCM_PACK (objcode_cells
.cells
+16),
824 SCM_PACK (objcode_cells
.cells
+18),
828 cif_to_procedure (SCM cif
, SCM func_ptr
)
830 unsigned nargs
= SCM_FOREIGN_POINTER (cif
, ffi_cif
)->nargs
;
831 SCM objcode
, table
, ret
;
834 objcode
= objcode_trampolines
[nargs
];
836 scm_misc_error ("make-foreign-function", "args >= 10 currently unimplemented",
839 table
= scm_c_make_vector (2, SCM_UNDEFINED
);
840 SCM_SIMPLE_VECTOR_SET (table
, 0, scm_cons (cif
, func_ptr
));
841 SCM_SIMPLE_VECTOR_SET (table
, 1, SCM_BOOL_F
); /* name */
842 ret
= scm_make_program (objcode
, table
, SCM_BOOL_F
);
848 unpack (ffi_type
*type
, void *loc
, SCM x
)
853 *(float*)loc
= scm_to_double (x
);
855 case FFI_TYPE_DOUBLE
:
856 *(double*)loc
= scm_to_double (x
);
859 *(scm_t_uint8
*)loc
= scm_to_uint8 (x
);
862 *(scm_t_int8
*)loc
= scm_to_int8 (x
);
864 case FFI_TYPE_UINT16
:
865 *(scm_t_uint16
*)loc
= scm_to_uint16 (x
);
867 case FFI_TYPE_SINT16
:
868 *(scm_t_int16
*)loc
= scm_to_int16 (x
);
870 case FFI_TYPE_UINT32
:
871 *(scm_t_uint32
*)loc
= scm_to_uint32 (x
);
873 case FFI_TYPE_SINT32
:
874 *(scm_t_int32
*)loc
= scm_to_int32 (x
);
876 case FFI_TYPE_UINT64
:
877 *(scm_t_uint64
*)loc
= scm_to_uint64 (x
);
879 case FFI_TYPE_SINT64
:
880 *(scm_t_int64
*)loc
= scm_to_int64 (x
);
882 case FFI_TYPE_STRUCT
:
883 if (!SCM_FOREIGN_TYPED_P (x
, VOID
))
884 scm_wrong_type_arg_msg ("foreign-call", 0, x
,
885 "foreign void pointer");
886 if (SCM_FOREIGN_LEN (x
) && SCM_FOREIGN_LEN (x
) != type
->size
)
887 scm_wrong_type_arg_msg ("foreign-call", 0, x
,
888 "foreign void pointer of correct length");
889 memcpy (loc
, SCM_FOREIGN_POINTER (x
, void), type
->size
);
891 case FFI_TYPE_POINTER
:
892 if (!SCM_FOREIGN_TYPED_P (x
, VOID
))
893 scm_wrong_type_arg_msg ("foreign-call", 0, x
,
894 "foreign void pointer");
895 *(void**)loc
= SCM_FOREIGN_POINTER (x
, void);
903 pack (ffi_type
*type
, void *loc
)
908 return SCM_UNSPECIFIED
;
910 return scm_from_double (*(float*)loc
);
911 case FFI_TYPE_DOUBLE
:
912 return scm_from_double (*(double*)loc
);
914 return scm_from_uint8 (*(scm_t_uint8
*)loc
);
916 return scm_from_int8 (*(scm_t_int8
*)loc
);
917 case FFI_TYPE_UINT16
:
918 return scm_from_uint16 (*(scm_t_uint16
*)loc
);
919 case FFI_TYPE_SINT16
:
920 return scm_from_int16 (*(scm_t_int16
*)loc
);
921 case FFI_TYPE_UINT32
:
922 return scm_from_uint32 (*(scm_t_uint32
*)loc
);
923 case FFI_TYPE_SINT32
:
924 return scm_from_int32 (*(scm_t_int32
*)loc
);
925 case FFI_TYPE_UINT64
:
926 return scm_from_uint64 (*(scm_t_uint64
*)loc
);
927 case FFI_TYPE_SINT64
:
928 return scm_from_int64 (*(scm_t_int64
*)loc
);
929 case FFI_TYPE_STRUCT
:
931 void *mem
= scm_gc_malloc_pointerless (type
->size
, "foreign");
932 memcpy (mem
, loc
, type
->size
);
933 return scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
,
934 mem
, type
->size
, NULL
);
936 case FFI_TYPE_POINTER
:
937 return scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
,
938 *(void**)loc
, 0, NULL
);
945 scm_i_foreign_call (SCM foreign
, SCM
*argv
)
947 /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
958 cif
= SCM_FOREIGN_POINTER (scm_car (foreign
), ffi_cif
);
959 func
= SCM_FOREIGN_POINTER (scm_cdr (foreign
), void);
961 /* Argument pointers. */
962 args
= alloca (sizeof(void*) * cif
->nargs
);
964 /* Compute the amount of memory needed to store all the argument values.
965 Note: as of libffi 3.0.9 `cif->bytes' is undocumented and is zero, so it
966 can't be used for that purpose. */
967 for (i
= 0, arg_size
= 0;
970 arg_size
+= ROUND_UP (cif
->arg_types
[i
]->size
,
971 cif
->arg_types
[i
]->alignment
);
973 /* Space for argument values, followed by return value. */
974 data
= alloca (arg_size
975 + ROUND_UP (cif
->rtype
->size
, cif
->rtype
->alignment
));
977 /* unpack argv to native values, setting argv pointers */
979 for (i
= 0; i
< cif
->nargs
; i
++)
981 off
= ROUND_UP (off
, cif
->arg_types
[i
]->alignment
);
982 args
[i
] = data
+ off
;
983 unpack (cif
->arg_types
[i
], args
[i
], argv
[i
]);
984 off
+= cif
->arg_types
[i
]->size
;
986 /* prep space for the return value */
987 off
= ROUND_UP (off
, cif
->rtype
->alignment
);
991 ffi_call (cif
, func
, rvalue
, args
);
993 return pack (cif
->rtype
, rvalue
);
999 scm_init_foreign (void)
1001 #ifndef SCM_MAGIC_SNARFER
1002 #include "libguile/foreign.x"
1004 scm_define (sym_void
, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID
));
1005 scm_define (sym_float
, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT
));
1006 scm_define (sym_double
, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE
));
1007 scm_define (sym_uint8
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8
));
1008 scm_define (sym_int8
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8
));
1009 scm_define (sym_uint16
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16
));
1010 scm_define (sym_int16
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16
));
1011 scm_define (sym_uint32
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
));
1012 scm_define (sym_int32
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
));
1013 scm_define (sym_uint64
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
));
1014 scm_define (sym_int64
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
));
1016 scm_define (sym_int
,
1018 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1019 #elif SIZEOF_INT == 4
1020 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1022 # error unsupported sizeof (int)
1026 scm_define (sym_unsigned_int
,
1027 #if SIZEOF_UNSIGNED_INT == 8
1028 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1029 #elif SIZEOF_UNSIGNED_INT == 4
1030 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1032 # error unsupported sizeof (unsigned int)
1036 scm_define (sym_long
,
1037 #if SIZEOF_LONG == 8
1038 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1039 #elif SIZEOF_LONG == 4
1040 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1042 # error unsupported sizeof (long)
1046 scm_define (sym_unsigned_long
,
1047 #if SIZEOF_UNSIGNED_LONG == 8
1048 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1049 #elif SIZEOF_UNSIGNED_LONG == 4
1050 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1052 # error unsupported sizeof (unsigned long)
1056 scm_define (sym_size_t
,
1057 #if SIZEOF_SIZE_T == 8
1058 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1059 #elif SIZEOF_SIZE_T == 4
1060 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1062 # error unsupported sizeof (size_t)
1066 scm_define (sym_null
, PTR2SCM (&null_pointer
));
1070 scm_register_foreign (void)
1072 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
1074 (scm_t_extension_init_func
)scm_init_foreign
,
1076 foreign_weak_refs
= scm_make_weak_key_hash_table (SCM_UNDEFINED
);