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
, "*");
55 static SCM
cif_to_procedure (SCM cif
, SCM func_ptr
);
58 static SCM foreign_weak_refs
= SCM_BOOL_F
;
61 register_weak_reference (SCM from
, SCM to
)
63 scm_hashq_set_x (foreign_weak_refs
, from
, to
);
67 foreign_finalizer_trampoline (GC_PTR ptr
, GC_PTR data
)
69 scm_t_foreign_finalizer finalizer
= data
;
70 finalizer (SCM_FOREIGN_POINTER (PTR2SCM (ptr
), void));
74 scm_take_foreign_pointer (scm_t_foreign_type type
, void *ptr
, size_t len
,
75 scm_t_foreign_finalizer finalizer
)
80 word0
= (scm_t_bits
)(scm_tc7_foreign
| (type
<<8)
81 | (finalizer
? (1<<16) : 0) | (len
<<17));
82 if (SCM_UNLIKELY ((word0
>> 17) != len
))
83 scm_out_of_range ("scm_take_foreign_pointer", scm_from_size_t (len
));
85 ret
= PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_bits
) * 2,
87 SCM_SET_CELL_WORD_0 (ret
, word0
);
88 SCM_SET_CELL_WORD_1 (ret
, (scm_t_bits
)ptr
);
92 /* Register a finalizer for the newly created instance. */
93 GC_finalization_proc prev_finalizer
;
94 GC_PTR prev_finalizer_data
;
95 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret
),
96 foreign_finalizer_trampoline
,
99 &prev_finalizer_data
);
105 SCM_DEFINE (scm_foreign_ref
, "foreign-ref", 1, 0, 0,
107 "Reference the foreign value wrapped by @var{foreign}.\n\n"
108 "The value will be referenced according to its type.")
109 #define FUNC_NAME s_scm_foreign_ref
111 scm_t_foreign_type ftype
;
114 SCM_VALIDATE_FOREIGN (1, foreign
);
115 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_uint8
);
116 ftype
= SCM_FOREIGN_TYPE (foreign
);
118 /* FIXME: is there a window in which we can see ptr but not foreign? */
119 /* FIXME: accessing unaligned pointers */
122 case SCM_FOREIGN_TYPE_VOID
:
123 return scm_from_ulong ((unsigned long)ptr
);
124 case SCM_FOREIGN_TYPE_FLOAT
:
125 return scm_from_double (*(float*)ptr
);
126 case SCM_FOREIGN_TYPE_DOUBLE
:
127 return scm_from_double (*(double*)ptr
);
128 case SCM_FOREIGN_TYPE_UINT8
:
129 return scm_from_uint8 (*(scm_t_uint8
*)ptr
);
130 case SCM_FOREIGN_TYPE_INT8
:
131 return scm_from_int8 (*(scm_t_int8
*)ptr
);
132 case SCM_FOREIGN_TYPE_UINT16
:
133 return scm_from_uint16 (*(scm_t_uint16
*)ptr
);
134 case SCM_FOREIGN_TYPE_INT16
:
135 return scm_from_int16 (*(scm_t_int16
*)ptr
);
136 case SCM_FOREIGN_TYPE_UINT32
:
137 return scm_from_uint32 (*(scm_t_uint32
*)ptr
);
138 case SCM_FOREIGN_TYPE_INT32
:
139 return scm_from_int32 (*(scm_t_int32
*)ptr
);
140 case SCM_FOREIGN_TYPE_UINT64
:
141 return scm_from_uint64 (*(scm_t_uint64
*)ptr
);
142 case SCM_FOREIGN_TYPE_INT64
:
143 return scm_from_int64 (*(scm_t_int64
*)ptr
);
145 scm_wrong_type_arg_msg (FUNC_NAME
, 1, foreign
, "foreign");
150 SCM_DEFINE (scm_foreign_set_x
, "foreign-set!", 2, 0, 0,
151 (SCM foreign
, SCM val
),
152 "Set the foreign value wrapped by @var{foreign}.\n\n"
153 "The value will be set according to its type.")
154 #define FUNC_NAME s_scm_foreign_set_x
156 scm_t_foreign_type ftype
;
159 SCM_VALIDATE_FOREIGN (1, foreign
);
160 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_uint8
);
161 ftype
= SCM_FOREIGN_TYPE (foreign
);
163 /* FIXME: is there a window in which we can see ptr but not foreign? */
164 /* FIXME: unaligned access */
167 case SCM_FOREIGN_TYPE_VOID
:
168 SCM_SET_CELL_WORD_1 (foreign
, scm_to_ulong (val
));
170 case SCM_FOREIGN_TYPE_FLOAT
:
171 *(float*)ptr
= scm_to_double (val
);
173 case SCM_FOREIGN_TYPE_DOUBLE
:
174 *(double*)ptr
= scm_to_double (val
);
176 case SCM_FOREIGN_TYPE_UINT8
:
177 *(scm_t_uint8
*)ptr
= scm_to_uint8 (val
);
179 case SCM_FOREIGN_TYPE_INT8
:
180 *(scm_t_int8
*)ptr
= scm_to_int8 (val
);
182 case SCM_FOREIGN_TYPE_UINT16
:
183 *(scm_t_uint16
*)ptr
= scm_to_uint16 (val
);
185 case SCM_FOREIGN_TYPE_INT16
:
186 *(scm_t_int16
*)ptr
= scm_to_int16 (val
);
188 case SCM_FOREIGN_TYPE_UINT32
:
189 *(scm_t_uint32
*)ptr
= scm_to_uint32 (val
);
191 case SCM_FOREIGN_TYPE_INT32
:
192 *(scm_t_int32
*)ptr
= scm_to_int32 (val
);
194 case SCM_FOREIGN_TYPE_UINT64
:
195 *(scm_t_uint64
*)ptr
= scm_to_uint64 (val
);
197 case SCM_FOREIGN_TYPE_INT64
:
198 *(scm_t_int64
*)ptr
= scm_to_int64 (val
);
201 scm_wrong_type_arg_msg (FUNC_NAME
, 1, val
, "foreign");
204 return SCM_UNSPECIFIED
;
208 SCM_DEFINE (scm_foreign_to_bytevector
, "foreign->bytevector", 1, 3, 0,
209 (SCM foreign
, SCM uvec_type
, SCM offset
, SCM len
),
210 "Return a bytevector aliasing the memory pointed to by\n"
212 "@var{foreign} must be a void pointer, a foreign whose type is\n"
213 "@var{void}. By default, the resulting bytevector will alias\n"
214 "all of the memory pointed to by @var{foreign}, from beginning\n"
215 "to end, treated as a @code{vu8} array.\n\n"
216 "The user may specify an alternate default interpretation for\n"
217 "the memory by passing the @var{uvec_type} argument, to indicate\n"
218 "that the memory is an array of elements of that type.\n"
219 "@var{uvec_type} should be something that\n"
220 "@code{uniform-vector-element-type} would return, like @code{f32}\n"
222 "Users may also specify that the bytevector should only alias a\n"
223 "subset of the memory, by specifying @var{offset} and @var{len}\n"
225 #define FUNC_NAME s_scm_foreign_to_bytevector
229 size_t boffset
, blen
;
230 scm_t_array_element_type btype
;
232 SCM_VALIDATE_FOREIGN_TYPED (1, foreign
, VOID
);
233 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_int8
);
235 if (SCM_UNBNDP (uvec_type
))
236 btype
= SCM_ARRAY_ELEMENT_TYPE_VU8
;
240 for (i
= 0; i
<= SCM_ARRAY_ELEMENT_TYPE_LAST
; i
++)
241 if (scm_is_eq (uvec_type
, scm_i_array_element_types
[i
]))
245 case SCM_ARRAY_ELEMENT_TYPE_VU8
:
246 case SCM_ARRAY_ELEMENT_TYPE_U8
:
247 case SCM_ARRAY_ELEMENT_TYPE_S8
:
248 case SCM_ARRAY_ELEMENT_TYPE_U16
:
249 case SCM_ARRAY_ELEMENT_TYPE_S16
:
250 case SCM_ARRAY_ELEMENT_TYPE_U32
:
251 case SCM_ARRAY_ELEMENT_TYPE_S32
:
252 case SCM_ARRAY_ELEMENT_TYPE_U64
:
253 case SCM_ARRAY_ELEMENT_TYPE_S64
:
254 case SCM_ARRAY_ELEMENT_TYPE_F32
:
255 case SCM_ARRAY_ELEMENT_TYPE_F64
:
256 case SCM_ARRAY_ELEMENT_TYPE_C32
:
257 case SCM_ARRAY_ELEMENT_TYPE_C64
:
261 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, uvec_type
,
262 "uniform vector type");
266 if (SCM_UNBNDP (offset
))
268 else if (SCM_FOREIGN_LEN (foreign
))
269 boffset
= scm_to_unsigned_integer (offset
, 0,
270 SCM_FOREIGN_LEN (foreign
) - 1);
272 boffset
= scm_to_size_t (offset
);
274 if (SCM_UNBNDP (len
))
276 if (SCM_FOREIGN_LEN (foreign
))
277 blen
= SCM_FOREIGN_LEN (foreign
) - boffset
;
279 scm_misc_error (FUNC_NAME
,
280 "length needed to convert foreign pointer to bytevector",
285 if (SCM_FOREIGN_LEN (foreign
))
286 blen
= scm_to_unsigned_integer (len
, 0,
287 SCM_FOREIGN_LEN (foreign
) - boffset
);
289 blen
= scm_to_size_t (len
);
292 ret
= scm_c_take_typed_bytevector (ptr
+ boffset
, blen
, btype
);
293 register_weak_reference (ret
, foreign
);
298 SCM_DEFINE (scm_bytevector_to_foreign
, "bytevector->foreign", 1, 2, 0,
299 (SCM bv
, SCM offset
, SCM len
),
300 "Return a foreign pointer aliasing the memory pointed to by\n"
302 "The resulting foreign will be a void pointer, a foreign whose\n"
303 "type is @code{void}. By default it will alias all of the\n"
304 "memory pointed to by @var{bv}, from beginning to end.\n\n"
305 "Users may explicily specify that the foreign should only alias a\n"
306 "subset of the memory, by specifying @var{offset} and @var{len}\n"
308 #define FUNC_NAME s_scm_bytevector_to_foreign
312 size_t boffset
, blen
;
314 SCM_VALIDATE_BYTEVECTOR (1, bv
);
315 ptr
= SCM_BYTEVECTOR_CONTENTS (bv
);
317 if (SCM_UNBNDP (offset
))
320 boffset
= scm_to_unsigned_integer (offset
, 0,
321 SCM_BYTEVECTOR_LENGTH (bv
) - 1);
323 if (SCM_UNBNDP (len
))
324 blen
= SCM_BYTEVECTOR_LENGTH (bv
) - boffset
;
326 blen
= scm_to_unsigned_integer (len
, 0,
327 SCM_BYTEVECTOR_LENGTH (bv
) - boffset
);
329 ret
= scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
, ptr
+ boffset
, blen
,
331 register_weak_reference (ret
, bv
);
336 SCM_DEFINE (scm_foreign_set_finalizer_x
, "foreign-set-finalizer!", 2, 0, 0,
337 (SCM foreign
, SCM finalizer
),
338 "Arrange for the C procedure wrapped by @var{finalizer} to be\n"
339 "called on the pointer wrapped by @var{foreign} when @var{foreign}\n"
340 "becomes unreachable. Note: the C procedure should not call into\n"
341 "Scheme. If you need a Scheme finalizer, use guardians.")
342 #define FUNC_NAME s_scm_foreign_set_finalizer_x
345 GC_finalization_proc prev_finalizer
;
346 GC_PTR prev_finalizer_data
;
348 SCM_VALIDATE_FOREIGN_TYPED (1, foreign
, VOID
);
349 SCM_VALIDATE_FOREIGN_TYPED (2, finalizer
, VOID
);
351 c_finalizer
= SCM_FOREIGN_POINTER (finalizer
, void);
353 SCM_SET_CELL_WORD_0 (foreign
, SCM_CELL_WORD_0 (foreign
) | (1<<16));
355 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (foreign
),
356 foreign_finalizer_trampoline
,
359 &prev_finalizer_data
);
361 return SCM_UNSPECIFIED
;
368 scm_i_foreign_print (SCM foreign
, SCM port
, scm_print_state
*pstate
)
370 scm_puts ("#<foreign ", port
);
371 switch (SCM_FOREIGN_TYPE (foreign
))
373 case SCM_FOREIGN_TYPE_FLOAT
:
374 scm_puts ("float ", port
);
376 case SCM_FOREIGN_TYPE_DOUBLE
:
377 scm_puts ("double ", port
);
379 case SCM_FOREIGN_TYPE_UINT8
:
380 scm_puts ("uint8 ", port
);
382 case SCM_FOREIGN_TYPE_INT8
:
383 scm_puts ("int8 ", port
);
385 case SCM_FOREIGN_TYPE_UINT16
:
386 scm_puts ("uint16 ", port
);
388 case SCM_FOREIGN_TYPE_INT16
:
389 scm_puts ("int16 ", port
);
391 case SCM_FOREIGN_TYPE_UINT32
:
392 scm_puts ("uint32 ", port
);
394 case SCM_FOREIGN_TYPE_INT32
:
395 scm_puts ("int32 ", port
);
397 case SCM_FOREIGN_TYPE_UINT64
:
398 scm_puts ("uint64 ", port
);
400 case SCM_FOREIGN_TYPE_INT64
:
401 scm_puts ("int64 ", port
);
403 case SCM_FOREIGN_TYPE_VOID
:
404 scm_puts ("pointer ", port
);
407 scm_wrong_type_arg_msg ("%print-foreign", 1, foreign
, "foreign");
409 scm_display (scm_foreign_ref (foreign
), port
);
410 scm_putc ('>', port
);
416 #define ROUND_UP(len,align) (align?(((len-1)|(align-1))+1):len)
418 SCM_DEFINE (scm_alignof
, "alignof", 1, 0, 0, (SCM type
), "")
419 #define FUNC_NAME s_scm_alignof
421 if (SCM_I_INUMP (type
))
423 switch (SCM_I_INUM (type
))
425 case SCM_FOREIGN_TYPE_FLOAT
:
426 return scm_from_size_t (alignof (float));
427 case SCM_FOREIGN_TYPE_DOUBLE
:
428 return scm_from_size_t (alignof (double));
429 case SCM_FOREIGN_TYPE_UINT8
:
430 return scm_from_size_t (alignof (scm_t_uint8
));
431 case SCM_FOREIGN_TYPE_INT8
:
432 return scm_from_size_t (alignof (scm_t_int8
));
433 case SCM_FOREIGN_TYPE_UINT16
:
434 return scm_from_size_t (alignof (scm_t_uint16
));
435 case SCM_FOREIGN_TYPE_INT16
:
436 return scm_from_size_t (alignof (scm_t_int16
));
437 case SCM_FOREIGN_TYPE_UINT32
:
438 return scm_from_size_t (alignof (scm_t_uint32
));
439 case SCM_FOREIGN_TYPE_INT32
:
440 return scm_from_size_t (alignof (scm_t_int32
));
441 case SCM_FOREIGN_TYPE_UINT64
:
442 return scm_from_size_t (alignof (scm_t_uint64
));
443 case SCM_FOREIGN_TYPE_INT64
:
444 return scm_from_size_t (alignof (scm_t_int64
));
446 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
449 else if (scm_is_eq (type
, sym_asterisk
))
451 return scm_from_size_t (alignof (void*));
452 else if (scm_is_pair (type
))
454 return scm_alignof (scm_car (type
));
456 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
460 SCM_DEFINE (scm_sizeof
, "sizeof", 1, 0, 0, (SCM type
), "")
461 #define FUNC_NAME s_scm_sizeof
463 if (SCM_I_INUMP (type
))
465 switch (SCM_I_INUM (type
))
467 case SCM_FOREIGN_TYPE_FLOAT
:
468 return scm_from_size_t (sizeof (float));
469 case SCM_FOREIGN_TYPE_DOUBLE
:
470 return scm_from_size_t (sizeof (double));
471 case SCM_FOREIGN_TYPE_UINT8
:
472 return scm_from_size_t (sizeof (scm_t_uint8
));
473 case SCM_FOREIGN_TYPE_INT8
:
474 return scm_from_size_t (sizeof (scm_t_int8
));
475 case SCM_FOREIGN_TYPE_UINT16
:
476 return scm_from_size_t (sizeof (scm_t_uint16
));
477 case SCM_FOREIGN_TYPE_INT16
:
478 return scm_from_size_t (sizeof (scm_t_int16
));
479 case SCM_FOREIGN_TYPE_UINT32
:
480 return scm_from_size_t (sizeof (scm_t_uint32
));
481 case SCM_FOREIGN_TYPE_INT32
:
482 return scm_from_size_t (sizeof (scm_t_int32
));
483 case SCM_FOREIGN_TYPE_UINT64
:
484 return scm_from_size_t (sizeof (scm_t_uint64
));
485 case SCM_FOREIGN_TYPE_INT64
:
486 return scm_from_size_t (sizeof (scm_t_int64
));
488 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
491 else if (scm_is_eq (type
, sym_asterisk
))
493 return scm_from_size_t (sizeof (void*));
494 else if (scm_is_pair (type
))
498 while (scm_is_pair (type
))
500 off
= ROUND_UP (off
, scm_to_size_t (scm_alignof (scm_car (type
))));
501 off
+= scm_to_size_t (scm_sizeof (scm_car (type
)));
502 type
= scm_cdr (type
);
504 return scm_from_size_t (off
);
507 scm_wrong_type_arg (FUNC_NAME
, 1, type
);
512 /* return 1 on success, 0 on failure */
514 parse_ffi_type (SCM type
, int return_p
, long *n_structs
, long *n_struct_elts
)
516 if (SCM_I_INUMP (type
))
518 if ((SCM_I_INUM (type
) < 0 )
519 || (SCM_I_INUM (type
) > SCM_FOREIGN_TYPE_LAST
))
521 else if (SCM_I_INUM (type
) == SCM_FOREIGN_TYPE_VOID
&& !return_p
)
526 else if (scm_is_eq (type
, sym_asterisk
))
533 len
= scm_ilength (type
);
538 if (!parse_ffi_type (scm_car (type
), 0, n_structs
, n_struct_elts
))
541 type
= scm_cdr (type
);
549 fill_ffi_type (SCM type
, ffi_type
*ftype
, ffi_type
***type_ptrs
,
552 if (SCM_I_INUMP (type
))
554 switch (SCM_I_INUM (type
))
556 case SCM_FOREIGN_TYPE_FLOAT
:
557 *ftype
= ffi_type_float
;
559 case SCM_FOREIGN_TYPE_DOUBLE
:
560 *ftype
= ffi_type_double
;
562 case SCM_FOREIGN_TYPE_UINT8
:
563 *ftype
= ffi_type_uint8
;
565 case SCM_FOREIGN_TYPE_INT8
:
566 *ftype
= ffi_type_sint8
;
568 case SCM_FOREIGN_TYPE_UINT16
:
569 *ftype
= ffi_type_uint16
;
571 case SCM_FOREIGN_TYPE_INT16
:
572 *ftype
= ffi_type_sint16
;
574 case SCM_FOREIGN_TYPE_UINT32
:
575 *ftype
= ffi_type_uint32
;
577 case SCM_FOREIGN_TYPE_INT32
:
578 *ftype
= ffi_type_sint32
;
580 case SCM_FOREIGN_TYPE_UINT64
:
581 *ftype
= ffi_type_uint64
;
583 case SCM_FOREIGN_TYPE_INT64
:
584 *ftype
= ffi_type_sint64
;
586 case SCM_FOREIGN_TYPE_VOID
:
587 *ftype
= ffi_type_void
;
590 scm_wrong_type_arg_msg ("make-foreign-function", 0, type
,
594 else if (scm_is_eq (type
, sym_asterisk
))
597 *ftype
= ffi_type_pointer
;
604 len
= scm_ilength (type
);
607 ftype
->alignment
= 0;
608 ftype
->type
= FFI_TYPE_STRUCT
;
609 ftype
->elements
= *type_ptrs
;
610 *type_ptrs
+= len
+ 1;
612 for (i
= 0; i
< len
; i
++)
614 ftype
->elements
[i
] = *types
;
616 fill_ffi_type (scm_car (type
), ftype
->elements
[i
],
618 type
= scm_cdr (type
);
620 ftype
->elements
[i
] = NULL
;
624 SCM_DEFINE (scm_make_foreign_function
, "make-foreign-function", 3, 0, 0,
625 (SCM return_type
, SCM func_ptr
, SCM arg_types
),
627 #define FUNC_NAME s_scm_make_foreign_function
630 long i
, nargs
, n_structs
, n_struct_elts
;
634 ffi_type
**type_ptrs
;
637 SCM_VALIDATE_FOREIGN_TYPED (2, func_ptr
, VOID
);
638 nargs
= scm_ilength (arg_types
);
639 SCM_ASSERT (nargs
>= 0, arg_types
, 3, FUNC_NAME
);
640 /* fixme: assert nargs < 1<<32 */
641 n_structs
= n_struct_elts
= 0;
643 /* For want of talloc, we're going to have to do this in two passes: first we
644 figure out how much memory is needed for all types, then we allocate the
645 cif and the types all in one block. */
646 if (!parse_ffi_type (return_type
, 1, &n_structs
, &n_struct_elts
))
647 scm_wrong_type_arg (FUNC_NAME
, 1, return_type
);
648 for (walk
= arg_types
; scm_is_pair (walk
); walk
= scm_cdr (walk
))
649 if (!parse_ffi_type (scm_car (walk
), 0, &n_structs
, &n_struct_elts
))
650 scm_wrong_type_arg (FUNC_NAME
, 3, scm_car (walk
));
652 /* the memory: with space for the cif itself */
653 cif_len
= sizeof (ffi_cif
);
655 /* then ffi_type pointers: one for each arg, one for each struct
656 element, and one for each struct (for null-termination) */
657 cif_len
= (ROUND_UP (cif_len
, alignof(void*))
658 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*));
660 /* then the ffi_type structs themselves, one per arg and struct element, and
661 one for the return val */
662 cif_len
= (ROUND_UP (cif_len
, alignof(ffi_type
))
663 + (nargs
+ n_struct_elts
+ 1)*sizeof(ffi_type
));
665 mem
= scm_gc_malloc_pointerless (cif_len
, "foreign");
666 scm_cif
= scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
, mem
,
668 cif
= (ffi_cif
*) mem
;
670 /* reuse cif_len to walk through the mem */
671 cif_len
= ROUND_UP (sizeof (ffi_cif
), alignof(void*));
672 type_ptrs
= (ffi_type
**)(mem
+ cif_len
);
673 cif_len
= ROUND_UP (cif_len
674 + (nargs
+ n_structs
+ n_struct_elts
)*sizeof(void*),
676 types
= (ffi_type
*)(mem
+ cif_len
);
678 /* whew. now knit the pointers together. */
679 cif
->rtype
= types
++;
680 fill_ffi_type (return_type
, cif
->rtype
, &type_ptrs
, &types
);
681 cif
->arg_types
= type_ptrs
;
683 for (walk
= arg_types
, i
= 0; scm_is_pair (walk
); walk
= scm_cdr (walk
), i
++)
685 cif
->arg_types
[i
] = types
++;
686 fill_ffi_type (scm_car (walk
), cif
->arg_types
[i
], &type_ptrs
, &types
);
689 /* round out the cif, and we're done. */
690 cif
->abi
= FFI_DEFAULT_ABI
;
695 if (FFI_OK
!= ffi_prep_cif (cif
, FFI_DEFAULT_ABI
, cif
->nargs
, cif
->rtype
,
697 scm_misc_error (FUNC_NAME
, "ffi_prep_cif failed", SCM_EOL
);
699 return cif_to_procedure (scm_cif
, func_ptr
);
705 /* Pre-generate trampolines for less than 10 arguments. */
707 #ifdef WORDS_BIGENDIAN
708 #define OBJCODE_HEADER 0, 0, 0, 8, 0, 0, 0, 40
709 #define META_HEADER 0, 0, 0, 32, 0, 0, 0, 0
711 #define OBJCODE_HEADER 8, 0, 0, 0, 40, 0, 0, 0
712 #define META_HEADER 32, 0, 0, 0, 0, 0, 0, 0
717 /* 0 */ scm_op_assert_nargs_ee, 0, nreq, /* assert number of args */ \
718 /* 3 */ scm_op_object_ref, 0, /* push the pair with the cif and the function pointer */ \
719 /* 5 */ scm_op_foreign_call, nreq, /* and call (will return value as well) */ \
720 /* 7 */ scm_op_nop, \
721 /* 8 */ META (3, 7, nreq)
723 #define META(start, end, nreq) \
725 /* 0 */ scm_op_make_eol, /* bindings */ \
726 /* 1 */ scm_op_make_eol, /* sources */ \
727 /* 2 */ scm_op_make_int8, start, scm_op_make_int8, end, /* arity: from ip N to ip N */ \
728 /* 6 */ scm_op_make_int8, nreq, /* the arity is N required args */ \
729 /* 8 */ scm_op_list, 0, 3, /* make a list of those 3 vals */ \
730 /* 11 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */ \
731 /* 14 */ scm_op_load_symbol, 0, 0, 4, 'n', 'a', 'm', 'e', /* `name' */ \
732 /* 22 */ scm_op_object_ref, 1, /* the name from the object table */ \
733 /* 24 */ scm_op_cons, /* make a pair for the properties */ \
734 /* 25 */ scm_op_list, 0, 4, /* pack bindings, sources, and arities into list */ \
735 /* 28 */ scm_op_return, /* and return */ \
736 /* 29 */ scm_op_nop, scm_op_nop, scm_op_nop \
741 scm_t_uint64 dummy
; /* ensure 8-byte alignment; perhaps there's a better way */
742 const scm_t_uint8 bytes
[10 * (sizeof (struct scm_objcode
) + 8
743 + sizeof (struct scm_objcode
) + 32)];
747 CODE (0), CODE (1), CODE (2), CODE (3), CODE (4),
748 CODE (5), CODE (6), CODE (7), CODE (8), CODE (9)
754 #undef OBJCODE_HEADER
758 (defun generate-objcode-cells (n)
759 "Generate objcode cells for up to N arguments"
764 (format " { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + %d) },\n"
765 (* (+ 4 4 8 4 4 32) i)))
766 (insert " { SCM_BOOL_F, SCM_PACK (0) },\n")
769 #define STATIC_OBJCODE_TAG \
770 SCM_PACK (scm_tc7_objcode | (SCM_F_OBJCODE_IS_STATIC << 8))
774 scm_t_uint64 dummy
; /* alignment */
775 scm_t_cell cells
[10 * 2]; /* 10 double cells */
778 /* C-u 1 0 M-x generate-objcode-cells RET */
780 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 0) },
781 { SCM_BOOL_F
, SCM_PACK (0) },
782 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 56) },
783 { SCM_BOOL_F
, SCM_PACK (0) },
784 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 112) },
785 { SCM_BOOL_F
, SCM_PACK (0) },
786 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 168) },
787 { SCM_BOOL_F
, SCM_PACK (0) },
788 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 224) },
789 { SCM_BOOL_F
, SCM_PACK (0) },
790 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 280) },
791 { SCM_BOOL_F
, SCM_PACK (0) },
792 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 336) },
793 { SCM_BOOL_F
, SCM_PACK (0) },
794 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 392) },
795 { SCM_BOOL_F
, SCM_PACK (0) },
796 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 448) },
797 { SCM_BOOL_F
, SCM_PACK (0) },
798 { STATIC_OBJCODE_TAG
, SCM_PACK (raw_bytecode
.bytes
+ 504) },
799 { SCM_BOOL_F
, SCM_PACK (0) }
803 static const SCM objcode_trampolines
[10] = {
804 SCM_PACK (objcode_cells
.cells
+0),
805 SCM_PACK (objcode_cells
.cells
+2),
806 SCM_PACK (objcode_cells
.cells
+4),
807 SCM_PACK (objcode_cells
.cells
+6),
808 SCM_PACK (objcode_cells
.cells
+8),
809 SCM_PACK (objcode_cells
.cells
+10),
810 SCM_PACK (objcode_cells
.cells
+12),
811 SCM_PACK (objcode_cells
.cells
+14),
812 SCM_PACK (objcode_cells
.cells
+16),
813 SCM_PACK (objcode_cells
.cells
+18),
817 cif_to_procedure (SCM cif
, SCM func_ptr
)
819 unsigned nargs
= SCM_FOREIGN_POINTER (cif
, ffi_cif
)->nargs
;
820 SCM objcode
, table
, ret
;
823 objcode
= objcode_trampolines
[nargs
];
825 scm_misc_error ("make-foreign-function", "args >= 10 currently unimplemented",
828 table
= scm_c_make_vector (2, SCM_UNDEFINED
);
829 SCM_SIMPLE_VECTOR_SET (table
, 0, scm_cons (cif
, func_ptr
));
830 SCM_SIMPLE_VECTOR_SET (table
, 1, SCM_BOOL_F
); /* name */
831 ret
= scm_make_program (objcode
, table
, SCM_BOOL_F
);
837 unpack (ffi_type
*type
, void *loc
, SCM x
)
842 *(float*)loc
= scm_to_double (x
);
844 case FFI_TYPE_DOUBLE
:
845 *(double*)loc
= scm_to_double (x
);
848 *(scm_t_uint8
*)loc
= scm_to_uint8 (x
);
851 *(scm_t_int8
*)loc
= scm_to_int8 (x
);
853 case FFI_TYPE_UINT16
:
854 *(scm_t_uint16
*)loc
= scm_to_uint16 (x
);
856 case FFI_TYPE_SINT16
:
857 *(scm_t_int16
*)loc
= scm_to_int16 (x
);
859 case FFI_TYPE_UINT32
:
860 *(scm_t_uint32
*)loc
= scm_to_uint32 (x
);
862 case FFI_TYPE_SINT32
:
863 *(scm_t_int32
*)loc
= scm_to_int32 (x
);
865 case FFI_TYPE_UINT64
:
866 *(scm_t_uint64
*)loc
= scm_to_uint64 (x
);
868 case FFI_TYPE_SINT64
:
869 *(scm_t_int64
*)loc
= scm_to_int64 (x
);
871 case FFI_TYPE_STRUCT
:
872 if (!SCM_FOREIGN_TYPED_P (x
, VOID
))
873 scm_wrong_type_arg_msg ("foreign-call", 0, x
,
874 "foreign void pointer");
875 if (SCM_FOREIGN_LEN (x
) && SCM_FOREIGN_LEN (x
) != type
->size
)
876 scm_wrong_type_arg_msg ("foreign-call", 0, x
,
877 "foreign void pointer of correct length");
878 memcpy (loc
, SCM_FOREIGN_POINTER (x
, void), type
->size
);
880 case FFI_TYPE_POINTER
:
881 if (!SCM_FOREIGN_TYPED_P (x
, VOID
))
882 scm_wrong_type_arg_msg ("foreign-call", 0, x
,
883 "foreign void pointer");
884 *(void**)loc
= SCM_FOREIGN_POINTER (x
, void);
892 pack (ffi_type
*type
, void *loc
)
897 return SCM_UNSPECIFIED
;
899 return scm_from_double (*(float*)loc
);
900 case FFI_TYPE_DOUBLE
:
901 return scm_from_double (*(double*)loc
);
903 return scm_from_uint8 (*(scm_t_uint8
*)loc
);
905 return scm_from_int8 (*(scm_t_int8
*)loc
);
906 case FFI_TYPE_UINT16
:
907 return scm_from_uint16 (*(scm_t_uint16
*)loc
);
908 case FFI_TYPE_SINT16
:
909 return scm_from_int16 (*(scm_t_int16
*)loc
);
910 case FFI_TYPE_UINT32
:
911 return scm_from_uint32 (*(scm_t_uint32
*)loc
);
912 case FFI_TYPE_SINT32
:
913 return scm_from_int32 (*(scm_t_int32
*)loc
);
914 case FFI_TYPE_UINT64
:
915 return scm_from_uint64 (*(scm_t_uint64
*)loc
);
916 case FFI_TYPE_SINT64
:
917 return scm_from_int64 (*(scm_t_int64
*)loc
);
918 case FFI_TYPE_STRUCT
:
920 void *mem
= scm_gc_malloc_pointerless (type
->size
, "foreign");
921 memcpy (mem
, loc
, type
->size
);
922 return scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
,
923 mem
, type
->size
, NULL
);
925 case FFI_TYPE_POINTER
:
926 return scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID
,
927 *(void**)loc
, 0, NULL
);
934 scm_i_foreign_call (SCM foreign
, SCM
*argv
)
936 /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
946 cif
= SCM_FOREIGN_POINTER (scm_car (foreign
), ffi_cif
);
947 func
= SCM_FOREIGN_POINTER (scm_cdr (foreign
), void);
950 args
= alloca (sizeof(void*) * cif
->nargs
);
951 /* arg values, then return type value */
952 data
= alloca (ROUND_UP (cif
->bytes
, cif
->rtype
->alignment
)
954 /* unpack argv to native values, setting argv pointers */
956 for (i
= 0; i
< cif
->nargs
; i
++)
958 off
= ROUND_UP (off
, cif
->arg_types
[i
]->alignment
);
959 args
[i
] = data
+ off
;
960 unpack (cif
->arg_types
[i
], args
[i
], argv
[i
]);
961 off
+= cif
->arg_types
[i
]->size
;
963 /* prep space for the return value */
964 off
= ROUND_UP (off
, cif
->rtype
->alignment
);
968 ffi_call (cif
, func
, rvalue
, args
);
970 return pack (cif
->rtype
, rvalue
);
976 scm_init_foreign (void)
978 #ifndef SCM_MAGIC_SNARFER
979 #include "libguile/foreign.x"
981 scm_define (sym_void
, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID
));
982 scm_define (sym_float
, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT
));
983 scm_define (sym_double
, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE
));
984 scm_define (sym_uint8
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8
));
985 scm_define (sym_int8
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8
));
986 scm_define (sym_uint16
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16
));
987 scm_define (sym_int16
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16
));
988 scm_define (sym_uint32
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
));
989 scm_define (sym_int32
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
));
990 scm_define (sym_uint64
, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
));
991 scm_define (sym_int64
, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
));
995 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
996 #elif SIZEOF_INT == 4
997 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
999 # error unsupported sizeof (int)
1003 scm_define (sym_unsigned_int
,
1004 #if SIZEOF_UNSIGNED_INT == 8
1005 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1006 #elif SIZEOF_UNSIGNED_INT == 4
1007 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1009 # error unsupported sizeof (unsigned int)
1013 scm_define (sym_long
,
1014 #if SIZEOF_LONG == 8
1015 scm_from_uint8 (SCM_FOREIGN_TYPE_INT64
)
1016 #elif SIZEOF_LONG == 4
1017 scm_from_uint8 (SCM_FOREIGN_TYPE_INT32
)
1019 # error unsupported sizeof (long)
1023 scm_define (sym_unsigned_long
,
1024 #if SIZEOF_UNSIGNED_LONG == 8
1025 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1026 #elif SIZEOF_UNSIGNED_LONG == 4
1027 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1029 # error unsupported sizeof (unsigned long)
1033 scm_define (sym_size_t
,
1034 #if SIZEOF_SIZE_T == 8
1035 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64
)
1036 #elif SIZEOF_SIZE_T == 4
1037 scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32
)
1039 # error unsupported sizeof (size_t)
1045 scm_register_foreign (void)
1047 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
1049 (scm_t_extension_init_func
)scm_init_foreign
,
1051 foreign_weak_refs
= scm_make_weak_key_hash_table (SCM_UNDEFINED
);