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");
46 /* that's for pointers, you know. */
47 SCM_SYMBOL (sym_asterisk
, "*");
50 static SCM
cif_to_procedure (SCM cif
, SCM func_ptr
);
53 static SCM foreign_weak_refs
= SCM_BOOL_F
;
56 register_weak_reference (SCM from
, SCM to
)
58 scm_hashq_set_x (foreign_weak_refs
, from
, to
);
62 foreign_finalizer_trampoline (GC_PTR ptr
, GC_PTR data
)
64 scm_t_foreign_finalizer finalizer
= data
;
65 finalizer (SCM_FOREIGN_POINTER (PTR2SCM (ptr
), void));
69 scm_take_foreign_pointer (scm_t_foreign_type type
, void *ptr
, size_t len
,
70 scm_t_foreign_finalizer finalizer
)
75 word0
= (scm_t_bits
)(scm_tc7_foreign
| (type
<<8)
76 | (finalizer
? (1<<16) : 0) | (len
<<17));
77 if (SCM_UNLIKELY ((word0
>> 17) != len
))
78 scm_out_of_range ("scm_take_foreign_pointer", scm_from_size_t (len
));
80 ret
= PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_bits
) * 2,
82 SCM_SET_CELL_WORD_0 (ret
, word0
);
83 SCM_SET_CELL_WORD_1 (ret
, (scm_t_bits
)ptr
);
87 /* Register a finalizer for the newly created instance. */
88 GC_finalization_proc prev_finalizer
;
89 GC_PTR prev_finalizer_data
;
90 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret
),
91 foreign_finalizer_trampoline
,
94 &prev_finalizer_data
);
100 SCM_DEFINE (scm_foreign_ref
, "foreign-ref", 1, 0, 0,
102 "Reference the foreign value wrapped by @var{foreign}.\n\n"
103 "The value will be referenced according to its type.")
104 #define FUNC_NAME s_scm_foreign_ref
106 scm_t_foreign_type ftype
;
109 SCM_VALIDATE_FOREIGN (1, foreign
);
110 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_uint8
);
111 ftype
= SCM_FOREIGN_TYPE (foreign
);
113 /* FIXME: is there a window in which we can see ptr but not foreign? */
114 /* FIXME: accessing unaligned pointers */
117 case SCM_FOREIGN_TYPE_VOID
:
118 return scm_from_ulong ((unsigned long)ptr
);
119 case SCM_FOREIGN_TYPE_FLOAT
:
120 return scm_from_double (*(float*)ptr
);
121 case SCM_FOREIGN_TYPE_DOUBLE
:
122 return scm_from_double (*(double*)ptr
);
123 case SCM_FOREIGN_TYPE_UINT8
:
124 return scm_from_uint8 (*(scm_t_uint8
*)ptr
);
125 case SCM_FOREIGN_TYPE_INT8
:
126 return scm_from_int8 (*(scm_t_int8
*)ptr
);
127 case SCM_FOREIGN_TYPE_UINT16
:
128 return scm_from_uint16 (*(scm_t_uint16
*)ptr
);
129 case SCM_FOREIGN_TYPE_INT16
:
130 return scm_from_int16 (*(scm_t_int16
*)ptr
);
131 case SCM_FOREIGN_TYPE_UINT32
:
132 return scm_from_uint32 (*(scm_t_uint32
*)ptr
);
133 case SCM_FOREIGN_TYPE_INT32
:
134 return scm_from_int32 (*(scm_t_int32
*)ptr
);
135 case SCM_FOREIGN_TYPE_UINT64
:
136 return scm_from_uint64 (*(scm_t_uint64
*)ptr
);
137 case SCM_FOREIGN_TYPE_INT64
:
138 return scm_from_int64 (*(scm_t_int64
*)ptr
);
140 scm_wrong_type_arg_msg (FUNC_NAME
, 1, foreign
, "foreign");
145 SCM_DEFINE (scm_foreign_set_x
, "foreign-set!", 2, 0, 0,
146 (SCM foreign
, SCM val
),
147 "Set the foreign value wrapped by @var{foreign}.\n\n"
148 "The value will be set according to its type.")
149 #define FUNC_NAME s_scm_foreign_set_x
151 scm_t_foreign_type ftype
;
154 SCM_VALIDATE_FOREIGN (1, foreign
);
155 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_uint8
);
156 ftype
= SCM_FOREIGN_TYPE (foreign
);
158 /* FIXME: is there a window in which we can see ptr but not foreign? */
159 /* FIXME: unaligned access */
162 case SCM_FOREIGN_TYPE_VOID
:
163 SCM_SET_CELL_WORD_1 (foreign
, scm_to_ulong (val
));
165 case SCM_FOREIGN_TYPE_FLOAT
:
166 *(float*)ptr
= scm_to_double (val
);
168 case SCM_FOREIGN_TYPE_DOUBLE
:
169 *(double*)ptr
= scm_to_double (val
);
171 case SCM_FOREIGN_TYPE_UINT8
:
172 *(scm_t_uint8
*)ptr
= scm_to_uint8 (val
);
174 case SCM_FOREIGN_TYPE_INT8
:
175 *(scm_t_int8
*)ptr
= scm_to_int8 (val
);
177 case SCM_FOREIGN_TYPE_UINT16
:
178 *(scm_t_uint16
*)ptr
= scm_to_uint16 (val
);
180 case SCM_FOREIGN_TYPE_INT16
:
181 *(scm_t_int16
*)ptr
= scm_to_int16 (val
);
183 case SCM_FOREIGN_TYPE_UINT32
:
184 *(scm_t_uint32
*)ptr
= scm_to_uint32 (val
);
186 case SCM_FOREIGN_TYPE_INT32
:
187 *(scm_t_int32
*)ptr
= scm_to_int32 (val
);
189 case SCM_FOREIGN_TYPE_UINT64
:
190 *(scm_t_uint64
*)ptr
= scm_to_uint64 (val
);
192 case SCM_FOREIGN_TYPE_INT64
:
193 *(scm_t_int64
*)ptr
= scm_to_int64 (val
);
196 scm_wrong_type_arg_msg (FUNC_NAME
, 1, val
, "foreign");
199 return SCM_UNSPECIFIED
;
203 SCM_DEFINE (scm_foreign_to_bytevector
, "foreign->bytevector", 1, 3, 0,
204 (SCM foreign
, SCM uvec_type
, SCM offset
, SCM len
),
205 "Return a bytevector aliasing the memory pointed to by\n"
207 "@var{foreign} must be a void pointer, a foreign whose type is\n"
208 "@var{void}. By default, the resulting bytevector will alias\n"
209 "all of the memory pointed to by @var{foreign}, from beginning\n"
210 "to end, treated as a @code{vu8} array.\n\n"
211 "The user may specify an alternate default interpretation for\n"
212 "the memory by passing the @var{uvec_type} argument, to indicate\n"
213 "that the memory is an array of elements of that type.\n"
214 "@var{uvec_type} should be something that\n"
215 "@code{uniform-vector-element-type} would return, like @code{f32}\n"
217 "Users may also specify that the bytevector should only alias a\n"
218 "subset of the memory, by specifying @var{offset} and @var{len}\n"
220 #define FUNC_NAME s_scm_foreign_to_bytevector
224 size_t boffset
, blen
;
225 scm_t_array_element_type btype
;
227 SCM_VALIDATE_FOREIGN_TYPED (1, foreign
, VOID
);
228 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_int8
);
230 if (SCM_UNBNDP (uvec_type
))
231 btype
= SCM_ARRAY_ELEMENT_TYPE_VU8
;
235 for (i
= 0; i
<= SCM_ARRAY_ELEMENT_TYPE_LAST
; i
++)
236 if (scm_is_eq (uvec_type
, scm_i_array_element_types
[i
]))
240 case SCM_ARRAY_ELEMENT_TYPE_VU8
:
241 case SCM_ARRAY_ELEMENT_TYPE_U8
:
242 case SCM_ARRAY_ELEMENT_TYPE_S8
:
243 case SCM_ARRAY_ELEMENT_TYPE_U16
:
244 case SCM_ARRAY_ELEMENT_TYPE_S16
:
245 case SCM_ARRAY_ELEMENT_TYPE_U32
:
246 case SCM_ARRAY_ELEMENT_TYPE_S32
:
247 case SCM_ARRAY_ELEMENT_TYPE_U64
:
248 case SCM_ARRAY_ELEMENT_TYPE_S64
:
249 case SCM_ARRAY_ELEMENT_TYPE_F32
:
250 case SCM_ARRAY_ELEMENT_TYPE_F64
:
251 case SCM_ARRAY_ELEMENT_TYPE_C32
:
252 case SCM_ARRAY_ELEMENT_TYPE_C64
:
256 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, uvec_type
,
257 "uniform vector type");
261 if (SCM_UNBNDP (offset
))
263 else if (SCM_FOREIGN_LEN (foreign
))
264 boffset
= scm_to_unsigned_integer (offset
, 0,
265 SCM_FOREIGN_LEN (foreign
) - 1);
267 boffset
= scm_to_size_t (offset
);
269 if (SCM_UNBNDP (len
))
271 if (SCM_FOREIGN_LEN (foreign
))
272 blen
= SCM_FOREIGN_LEN (foreign
) - boffset
;
274 scm_misc_error (FUNC_NAME
,
275 "length needed to convert foreign pointer to bytevector",
280 if (SCM_FOREIGN_LEN (foreign
))
281 blen
= scm_to_unsigned_integer (len
, 0,
282 SCM_FOREIGN_LEN (foreign
) - boffset
);
284 blen
= scm_to_size_t (len
);
287 ret
= scm_c_take_typed_bytevector (ptr
+ boffset
, blen
, btype
);
288 register_weak_reference (ret
, foreign
);
293 SCM_DEFINE (scm_bytevector_to_foreign
, "bytevector->foreign", 1, 2, 0,
294 (SCM bv
, SCM offset
, SCM len
),
295 "Return a foreign pointer aliasing the memory pointed to by\n"
297 "The resulting foreign will be a void pointer, a foreign whose\n"
298 "type is @code{void}. By default it will alias all of the\n"
299 "memory pointed to by @var{bv}, from beginning to end.\n\n"
300 "Users may explicily specify that the foreign should only alias a\n"
301 "subset of the memory, by specifying @var{offset} and @var{len}\n"
303 #define FUNC_NAME s_scm_bytevector_to_foreign
307 size_t boffset
, blen
;
309 SCM_VALIDATE_BYTEVECTOR (1, bv
);
310 ptr
= SCM_BYTEVECTOR_CONTENTS (bv
);
312 if (SCM_UNBNDP (offset
))
315 boffset
= scm_to_unsigned_integer (offset
, 0,
316 SCM_BYTEVECTOR_LENGTH (bv
) - 1);
318 if (SCM_UNBNDP (len
))
319 blen
= SCM_BYTEVECTOR_LENGTH (bv
) - boffset
;
321 blen
= scm_to_unsigned_integer (len
, 0,
322 SCM_BYTEVECTOR_LENGTH (bv
) - boffset
);
324 ret
= scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
, ptr
+ boffset
, blen
,
326 register_weak_reference (ret
, bv
);
331 SCM_DEFINE (scm_foreign_set_finalizer_x
, "foreign-set-finalizer!", 2, 0, 0,
332 (SCM foreign
, SCM finalizer
),
333 "Arrange for the C procedure wrapped by @var{finalizer} to be\n"
334 "called on the pointer wrapped by @var{foreign} when @var{foreign}\n"
335 "becomes unreachable. Note: the C procedure should not call into\n"
336 "Scheme. If you need a Scheme finalizer, use guardians.")
337 #define FUNC_NAME s_scm_foreign_set_finalizer_x
340 GC_finalization_proc prev_finalizer
;
341 GC_PTR prev_finalizer_data
;
343 SCM_VALIDATE_FOREIGN_TYPED (1, foreign
, VOID
);
344 SCM_VALIDATE_FOREIGN_TYPED (2, finalizer
, VOID
);
346 c_finalizer
= SCM_FOREIGN_POINTER (finalizer
, void);
348 SCM_SET_CELL_WORD_0 (foreign
, SCM_CELL_WORD_0 (foreign
) | (1<<16));
350 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (foreign
),
351 foreign_finalizer_trampoline
,
354 &prev_finalizer_data
);
356 return SCM_UNSPECIFIED
;
363 scm_i_foreign_print (SCM foreign
, SCM port
, scm_print_state
*pstate
)
365 scm_puts ("#<foreign ", port
);
366 switch (SCM_FOREIGN_TYPE (foreign
))
368 case SCM_FOREIGN_TYPE_FLOAT
:
369 scm_puts ("float ", port
);
371 case SCM_FOREIGN_TYPE_DOUBLE
:
372 scm_puts ("double ", port
);
374 case SCM_FOREIGN_TYPE_UINT8
:
375 scm_puts ("uint8 ", port
);
377 case SCM_FOREIGN_TYPE_INT8
:
378 scm_puts ("int8 ", port
);
380 case SCM_FOREIGN_TYPE_UINT16
:
381 scm_puts ("uint16 ", port
);
383 case SCM_FOREIGN_TYPE_INT16
:
384 scm_puts ("int16 ", port
);
386 case SCM_FOREIGN_TYPE_UINT32
:
387 scm_puts ("uint32 ", port
);
389 case SCM_FOREIGN_TYPE_INT32
:
390 scm_puts ("int32 ", port
);
392 case SCM_FOREIGN_TYPE_UINT64
:
393 scm_puts ("uint64 ", port
);
395 case SCM_FOREIGN_TYPE_INT64
:
396 scm_puts ("int64 ", port
);
398 case SCM_FOREIGN_TYPE_VOID
:
399 scm_puts ("pointer ", port
);
402 scm_wrong_type_arg_msg ("%print-foreign", 1, foreign
, "foreign");
404 scm_display (scm_foreign_ref (foreign
), port
);
405 scm_putc ('>', port
);
411 #define ROUND_UP(len,align) (align?(((len-1)|(align-1))+1):len)
413 SCM_DEFINE (scm_alignof
, "alignof", 1, 0, 0, (SCM type
), "")
414 #define FUNC_NAME s_scm_alignof
416 if (SCM_I_INUMP (type
))
418 switch (SCM_I_INUM (type
))
420 case SCM_FOREIGN_TYPE_FLOAT
:
421 return scm_from_size_t (alignof (float));
422 case SCM_FOREIGN_TYPE_DOUBLE
:
423 return scm_from_size_t (alignof (double));
424 case SCM_FOREIGN_TYPE_UINT8
:
425 return scm_from_size_t (alignof (scm_t_uint8
));
426 case SCM_FOREIGN_TYPE_INT8
:
427 return scm_from_size_t (alignof (scm_t_int8
));
428 case SCM_FOREIGN_TYPE_UINT16
:
429 return scm_from_size_t (alignof (scm_t_uint16
));
430 case SCM_FOREIGN_TYPE_INT16
:
431 return scm_from_size_t (alignof (scm_t_int16
));
432 case SCM_FOREIGN_TYPE_UINT32
:
433 return scm_from_size_t (alignof (scm_t_uint32
));
434 case SCM_FOREIGN_TYPE_INT32
:
435 return scm_from_size_t (alignof (scm_t_int32
));
436 case SCM_FOREIGN_TYPE_UINT64
:
437 return scm_from_size_t (alignof (scm_t_uint64
));
438 case SCM_FOREIGN_TYPE_INT64
:
439 return scm_from_size_t (alignof (scm_t_int64
));
441 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
444 else if (scm_is_eq (type
, sym_asterisk
))
446 return scm_from_size_t (alignof (void*));
447 else if (scm_is_pair (type
))
449 return scm_alignof (scm_car (type
));
451 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
455 SCM_DEFINE (scm_sizeof
, "sizeof", 1, 0, 0, (SCM type
), "")
456 #define FUNC_NAME s_scm_sizeof
458 if (SCM_I_INUMP (type
))
460 switch (SCM_I_INUM (type
))
462 case SCM_FOREIGN_TYPE_FLOAT
:
463 return scm_from_size_t (sizeof (float));
464 case SCM_FOREIGN_TYPE_DOUBLE
:
465 return scm_from_size_t (sizeof (double));
466 case SCM_FOREIGN_TYPE_UINT8
:
467 return scm_from_size_t (sizeof (scm_t_uint8
));
468 case SCM_FOREIGN_TYPE_INT8
:
469 return scm_from_size_t (sizeof (scm_t_int8
));
470 case SCM_FOREIGN_TYPE_UINT16
:
471 return scm_from_size_t (sizeof (scm_t_uint16
));
472 case SCM_FOREIGN_TYPE_INT16
:
473 return scm_from_size_t (sizeof (scm_t_int16
));
474 case SCM_FOREIGN_TYPE_UINT32
:
475 return scm_from_size_t (sizeof (scm_t_uint32
));
476 case SCM_FOREIGN_TYPE_INT32
:
477 return scm_from_size_t (sizeof (scm_t_int32
));
478 case SCM_FOREIGN_TYPE_UINT64
:
479 return scm_from_size_t (sizeof (scm_t_uint64
));
480 case SCM_FOREIGN_TYPE_INT64
:
481 return scm_from_size_t (sizeof (scm_t_int64
));
483 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
486 else if (scm_is_eq (type
, sym_asterisk
))
488 return scm_from_size_t (sizeof (void*));
489 else if (scm_is_pair (type
))
493 while (scm_is_pair (type
))
495 off
= ROUND_UP (off
, scm_to_size_t (scm_alignof (scm_car (type
))));
496 off
+= scm_to_size_t (scm_sizeof (scm_car (type
)));
497 type
= scm_cdr (type
);
499 return scm_from_size_t (off
);
502 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
507 /* return 1 on success, 0 on failure */
509 parse_ffi_type (SCM type
, int return_p
, long *n_structs
, long *n_struct_elts
)
511 if (SCM_I_INUMP (type
))
513 if ((SCM_I_INUM (type
) < 0 )
514 || (SCM_I_INUM (type
) > SCM_FOREIGN_TYPE_LAST
))
516 else if (SCM_I_INUM (type
) == SCM_FOREIGN_TYPE_VOID
&& !return_p
)
521 else if (scm_is_eq (type
, sym_asterisk
))
528 len
= scm_ilength (type
);
533 if (!parse_ffi_type (scm_car (type
), 0, n_structs
, n_struct_elts
))
536 type
= scm_cdr (type
);
544 fill_ffi_type (SCM type
, ffi_type
*ftype
, ffi_type
***type_ptrs
,
547 if (SCM_I_INUMP (type
))
549 switch (SCM_I_INUM (type
))
551 case SCM_FOREIGN_TYPE_FLOAT
:
552 *ftype
= ffi_type_float
;
554 case SCM_FOREIGN_TYPE_DOUBLE
:
555 *ftype
= ffi_type_double
;
557 case SCM_FOREIGN_TYPE_UINT8
:
558 *ftype
= ffi_type_uint8
;
560 case SCM_FOREIGN_TYPE_INT8
:
561 *ftype
= ffi_type_sint8
;
563 case SCM_FOREIGN_TYPE_UINT16
:
564 *ftype
= ffi_type_uint16
;
566 case SCM_FOREIGN_TYPE_INT16
:
567 *ftype
= ffi_type_sint16
;
569 case SCM_FOREIGN_TYPE_UINT32
:
570 *ftype
= ffi_type_uint32
;
572 case SCM_FOREIGN_TYPE_INT32
:
573 *ftype
= ffi_type_sint32
;
575 case SCM_FOREIGN_TYPE_UINT64
:
576 *ftype
= ffi_type_uint64
;
578 case SCM_FOREIGN_TYPE_INT64
:
579 *ftype
= ffi_type_sint64
;
581 case SCM_FOREIGN_TYPE_VOID
:
582 *ftype
= ffi_type_void
;
585 scm_wrong_type_arg_msg ("make-foreign-function", 0, type
,
589 else if (scm_is_eq (type
, sym_asterisk
))
592 *ftype
= ffi_type_pointer
;
599 len
= scm_ilength (type
);
602 ftype
->alignment
= 0;
603 ftype
->type
= FFI_TYPE_STRUCT
;
604 ftype
->elements
= *type_ptrs
;
605 *type_ptrs
+= len
+ 1;
607 for (i
= 0; i
< len
; i
++)
609 ftype
->elements
[i
] = *types
;
611 fill_ffi_type (scm_car (type
), ftype
->elements
[i
],
613 type
= scm_cdr (type
);
615 ftype
->elements
[i
] = NULL
;
619 SCM_DEFINE (scm_make_foreign_function
, "make-foreign-function", 3, 0, 0,
620 (SCM return_type
, SCM func_ptr
, SCM arg_types
),
622 #define FUNC_NAME s_scm_make_foreign_function
625 long i
, nargs
, n_structs
, n_struct_elts
;
629 ffi_type
**type_ptrs
;
632 SCM_VALIDATE_FOREIGN_TYPED (2, func_ptr
, VOID
);
633 nargs
= scm_ilength (arg_types
);
634 SCM_ASSERT (nargs
>= 0, arg_types
, 3, FUNC_NAME
);
635 /* fixme: assert nargs < 1<<32 */
636 n_structs
= n_struct_elts
= 0;
638 /* For want of talloc, we're going to have to do this in two passes: first we
639 figure out how much memory is needed for all types, then we allocate the
640 cif and the types all in one block. */
641 if (!parse_ffi_type (return_type
, 1, &n_structs
, &n_struct_elts
))
642 scm_wrong_type_arg (FUNC_NAME
, 1, return_type
);
643 for (walk
= arg_types
; scm_is_pair (walk
); walk
= scm_cdr (walk
))
644 if (!parse_ffi_type (scm_car (walk
), 0, &n_structs
, &n_struct_elts
))
645 scm_wrong_type_arg (FUNC_NAME
, 3, scm_car (walk
));
647 /* the memory: with space for the cif itself */
648 cif_len
= sizeof (ffi_cif
);
650 /* then ffi_type pointers: one for each arg, one for each struct
651 element, and one for each struct (for null-termination) */
652 cif_len
= (ROUND_UP (cif_len
, alignof(void*))
653 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*));
655 /* then the ffi_type structs themselves, one per arg and struct element, and
656 one for the return val */
657 cif_len
= (ROUND_UP (cif_len
, alignof(ffi_type
))
658 + (nargs
+ n_struct_elts
+ 1)*sizeof(ffi_type
));
660 mem
= scm_gc_malloc_pointerless (cif_len
, "foreign");
661 scm_cif
= scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
, mem
,
663 cif
= (ffi_cif
*) mem
;
665 /* reuse cif_len to walk through the mem */
666 cif_len
= ROUND_UP (sizeof (ffi_cif
), alignof(void*));
667 type_ptrs
= (ffi_type
**)(mem
+ cif_len
);
668 cif_len
= ROUND_UP (cif_len
669 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*),
671 types
= (ffi_type
*)(mem
+ cif_len
);
673 /* whew. now knit the pointers together. */
674 cif
->rtype
= types
++;
675 fill_ffi_type (return_type
, cif
->rtype
, &type_ptrs
, &types
);
676 cif
->arg_types
= type_ptrs
;
678 for (walk
= arg_types
, i
= 0; scm_is_pair (walk
); walk
= scm_cdr (walk
), i
++)
680 cif
->arg_types
[i
] = types
++;
681 fill_ffi_type (scm_car (walk
), cif
->arg_types
[i
], &type_ptrs
, &types
);
684 /* round out the cif, and we're done. */
685 cif
->abi
= FFI_DEFAULT_ABI
;
690 if (FFI_OK
!= ffi_prep_cif (cif
, FFI_DEFAULT_ABI
, cif
->nargs
, cif
->rtype
,
692 scm_misc_error (FUNC_NAME
, "ffi_prep_cif failed", SCM_EOL
);
694 return cif_to_procedure (scm_cif
, func_ptr
);
700 /* Pre-generate trampolines for less than 10 arguments. */
702 #ifdef WORDS_BIGENDIAN
703 #define OBJCODE_HEADER 0, 0, 0, 8, 0, 0, 0, 40
704 #define META_HEADER 0, 0, 0, 32, 0, 0, 0, 0
706 #define OBJCODE_HEADER 8, 0, 0, 0, 40, 0, 0, 0
707 #define META_HEADER 32, 0, 0, 0, 0, 0, 0, 0
712 /* 0 */ scm_op_assert_nargs_ee, 0, nreq, /* assert number of args */ \
713 /* 3 */ scm_op_object_ref, 0, /* push the pair with the cif and the function pointer */ \
714 /* 5 */ scm_op_foreign_call, nreq, /* and call (will return value as well) */ \
715 /* 7 */ scm_op_nop, \
716 /* 8 */ META (3, 7, nreq)
718 #define META(start, end, nreq) \
720 /* 0 */ scm_op_make_eol, /* bindings */ \
721 /* 1 */ scm_op_make_eol, /* sources */ \
722 /* 2 */ scm_op_make_int8, start, scm_op_make_int8, end, /* arity: from ip N to ip N */ \
723 /* 6 */ scm_op_make_int8, nreq, /* the arity is N required args */ \
724 /* 8 */ scm_op_list, 0, 3, /* make a list of those 3 vals */ \
725 /* 11 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */ \
726 /* 14 */ scm_op_load_symbol, 0, 0, 4, 'n', 'a', 'm', 'e', /* `name' */ \
727 /* 22 */ scm_op_object_ref, 1, /* the name from the object table */ \
728 /* 24 */ scm_op_cons, /* make a pair for the properties */ \
729 /* 25 */ scm_op_list, 0, 4, /* pack bindings, sources, and arities into list */ \
730 /* 28 */ scm_op_return, /* and return */ \
731 /* 29 */ scm_op_nop, scm_op_nop, scm_op_nop \
736 scm_t_uint64 dummy
; /* ensure 8-byte alignment; perhaps there's a better way */
737 const scm_t_uint8 bytes
[10 * (sizeof (struct scm_objcode
) + 8
738 + sizeof (struct scm_objcode
) + 32)];
742 CODE (0), CODE (1), CODE (2), CODE (3), CODE (4),
743 CODE (5), CODE (6), CODE (7), CODE (8), CODE (9)
749 #undef OBJCODE_HEADER
753 (defun generate-objcode-cells (n)
754 "Generate objcode cells for up to N arguments"
759 (format " { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + %d) },\n"
760 (* (+ 4 4 8 4 4 32) i)))
761 (insert " { SCM_BOOL_F, SCM_PACK (0) },\n")
764 #define STATIC_OBJCODE_TAG \
765 SCM_PACK (scm_tc7_objcode | (SCM_F_OBJCODE_IS_STATIC << 8))
769 scm_t_uint64 dummy
; /* alignment */
770 scm_t_cell cells
[10 * 2]; /* 10 double cells */
773 /* C-u 1 0 M-x generate-objcode-cells RET */
775 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 0) },
776 { SCM_BOOL_F
, SCM_PACK (0) },
777 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 56) },
778 { SCM_BOOL_F
, SCM_PACK (0) },
779 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 112) },
780 { SCM_BOOL_F
, SCM_PACK (0) },
781 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 168) },
782 { SCM_BOOL_F
, SCM_PACK (0) },
783 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 224) },
784 { SCM_BOOL_F
, SCM_PACK (0) },
785 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 280) },
786 { SCM_BOOL_F
, SCM_PACK (0) },
787 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 336) },
788 { SCM_BOOL_F
, SCM_PACK (0) },
789 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 392) },
790 { SCM_BOOL_F
, SCM_PACK (0) },
791 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 448) },
792 { SCM_BOOL_F
, SCM_PACK (0) },
793 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 504) },
794 { SCM_BOOL_F
, SCM_PACK (0) }
798 static const SCM objcode_trampolines
[10] = {
799 SCM_PACK (objcode_cells
.cells
+0),
800 SCM_PACK (objcode_cells
.cells
+2),
801 SCM_PACK (objcode_cells
.cells
+4),
802 SCM_PACK (objcode_cells
.cells
+6),
803 SCM_PACK (objcode_cells
.cells
+8),
804 SCM_PACK (objcode_cells
.cells
+10),
805 SCM_PACK (objcode_cells
.cells
+12),
806 SCM_PACK (objcode_cells
.cells
+14),
807 SCM_PACK (objcode_cells
.cells
+16),
808 SCM_PACK (objcode_cells
.cells
+18),
812 cif_to_procedure (SCM cif
, SCM func_ptr
)
814 unsigned nargs
= SCM_FOREIGN_POINTER (cif
, ffi_cif
)->nargs
;
815 SCM objcode
, table
, ret
;
818 objcode
= objcode_trampolines
[nargs
];
820 scm_misc_error ("make-foreign-function", "args >= 10 currently unimplemented",
823 table
= scm_c_make_vector (2, SCM_UNDEFINED
);
824 SCM_SIMPLE_VECTOR_SET (table
, 0, scm_cons (cif
, func_ptr
));
825 SCM_SIMPLE_VECTOR_SET (table
, 1, SCM_BOOL_F
); /* name */
826 ret
= scm_make_program (objcode
, table
, SCM_BOOL_F
);
832 unpack (ffi_type
*type
, void *loc
, SCM x
)
837 *(float*)loc
= scm_to_double (x
);
839 case FFI_TYPE_DOUBLE
:
840 *(double*)loc
= scm_to_double (x
);
843 *(scm_t_uint8
*)loc
= scm_to_uint8 (x
);
846 *(scm_t_int8
*)loc
= scm_to_int8 (x
);
848 case FFI_TYPE_UINT16
:
849 *(scm_t_uint16
*)loc
= scm_to_uint16 (x
);
851 case FFI_TYPE_SINT16
:
852 *(scm_t_int16
*)loc
= scm_to_int16 (x
);
854 case FFI_TYPE_UINT32
:
855 *(scm_t_uint32
*)loc
= scm_to_uint32 (x
);
857 case FFI_TYPE_SINT32
:
858 *(scm_t_int32
*)loc
= scm_to_int32 (x
);
860 case FFI_TYPE_UINT64
:
861 *(scm_t_uint64
*)loc
= scm_to_uint64 (x
);
863 case FFI_TYPE_SINT64
:
864 *(scm_t_int64
*)loc
= scm_to_int64 (x
);
866 case FFI_TYPE_STRUCT
:
867 if (!SCM_FOREIGN_TYPED_P (x
, VOID
))
868 scm_wrong_type_arg_msg ("foreign-call", 0, x
,
869 "foreign void pointer");
870 if (SCM_FOREIGN_LEN (x
) && SCM_FOREIGN_LEN (x
) != type
->size
)
871 scm_wrong_type_arg_msg ("foreign-call", 0, x
,
872 "foreign void pointer of correct length");
873 memcpy (loc
, SCM_FOREIGN_POINTER (x
, void), type
->size
);
875 case FFI_TYPE_POINTER
:
876 if (!SCM_FOREIGN_TYPED_P (x
, VOID
))
877 scm_wrong_type_arg_msg ("foreign-call", 0, x
,
878 "foreign void pointer");
879 *(void**)loc
= SCM_FOREIGN_POINTER (x
, void);
887 pack (ffi_type
*type
, void *loc
)
892 return SCM_UNSPECIFIED
;
894 return scm_from_double (*(float*)loc
);
895 case FFI_TYPE_DOUBLE
:
896 return scm_from_double (*(double*)loc
);
898 return scm_from_uint8 (*(scm_t_uint8
*)loc
);
900 return scm_from_int8 (*(scm_t_int8
*)loc
);
901 case FFI_TYPE_UINT16
:
902 return scm_from_uint16 (*(scm_t_uint16
*)loc
);
903 case FFI_TYPE_SINT16
:
904 return scm_from_int16 (*(scm_t_int16
*)loc
);
905 case FFI_TYPE_UINT32
:
906 return scm_from_uint32 (*(scm_t_uint32
*)loc
);
907 case FFI_TYPE_SINT32
:
908 return scm_from_int32 (*(scm_t_int32
*)loc
);
909 case FFI_TYPE_UINT64
:
910 return scm_from_uint64 (*(scm_t_uint64
*)loc
);
911 case FFI_TYPE_SINT64
:
912 return scm_from_int64 (*(scm_t_int64
*)loc
);
913 case FFI_TYPE_STRUCT
:
915 void *mem
= scm_gc_malloc_pointerless (type
->size
, "foreign");
916 memcpy (mem
, loc
, type
->size
);
917 return scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
,
918 mem
, type
->size
, NULL
);
920 case FFI_TYPE_POINTER
:
921 return scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
,
922 *(void**)loc
, 0, NULL
);
929 scm_i_foreign_call (SCM foreign
, SCM
*argv
)
931 /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
941 cif
= SCM_FOREIGN_POINTER (scm_car (foreign
), ffi_cif
);
942 func
= SCM_FOREIGN_POINTER (scm_cdr (foreign
), void);
945 args
= alloca (sizeof(void*) * cif
->nargs
);
946 /* arg values, then return type value */
947 data
= alloca (ROUND_UP (cif
->bytes
, cif
->rtype
->alignment
)
949 /* unpack argv to native values, setting argv pointers */
951 for (i
= 0; i
< cif
->nargs
; i
++)
953 off
= ROUND_UP (off
, cif
->arg_types
[i
]->alignment
);
954 args
[i
] = data
+ off
;
955 unpack (cif
->arg_types
[i
], args
[i
], argv
[i
]);
956 off
+= cif
->arg_types
[i
]->size
;
958 /* prep space for the return value */
959 off
= ROUND_UP (off
, cif
->rtype
->alignment
);
963 ffi_call (cif
, func
, rvalue
, args
);
965 return pack (cif
->rtype
, rvalue
);
971 scm_init_foreign (void)
973 #ifndef SCM_MAGIC_SNARFER
974 #include "libguile/foreign.x"
976 scm_define (sym_void
, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID
));
977 scm_define (sym_float
, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT
));
978 scm_define (sym_double
, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE
));
979 scm_define (sym_uint8
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8
));
980 scm_define (sym_int8
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8
));
981 scm_define (sym_uint16
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16
));
982 scm_define (sym_int16
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16
));
983 scm_define (sym_uint32
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
));
984 scm_define (sym_int32
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
));
985 scm_define (sym_uint64
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
));
986 scm_define (sym_int64
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
));
990 scm_register_foreign (void)
992 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
994 (scm_t_extension_init_func
)scm_init_foreign
,
996 foreign_weak_refs
= scm_make_weak_key_hash_table (SCM_UNDEFINED
);