rename libguile to libguile-@EFFECTIVE_VERSION@, currently libguile-2.0
[bpt/guile.git] / libguile / foreign.c
1 /* Copyright (C) 2010 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 * 02110-1301 USA
17 */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <ffi.h>
24
25 #include <alignof.h>
26 #include <string.h>
27 #include "libguile/_scm.h"
28 #include "libguile/bytevectors.h"
29 #include "libguile/instructions.h"
30 #include "libguile/foreign.h"
31
32 \f
33
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
46 /* that's for pointers, you know. */
47 SCM_SYMBOL (sym_asterisk, "*");
48
49
50 static SCM cif_to_procedure (SCM cif, SCM func_ptr);
51
52
53 static SCM foreign_weak_refs = SCM_BOOL_F;
54
55 static void
56 register_weak_reference (SCM from, SCM to)
57 {
58 scm_hashq_set_x (foreign_weak_refs, from, to);
59 }
60
61 static void
62 foreign_finalizer_trampoline (GC_PTR ptr, GC_PTR data)
63 {
64 scm_t_foreign_finalizer finalizer = data;
65 finalizer (SCM_FOREIGN_POINTER (PTR2SCM (ptr), void));
66 }
67
68 SCM
69 scm_take_foreign_pointer (scm_t_foreign_type type, void *ptr, size_t len,
70 scm_t_foreign_finalizer finalizer)
71 {
72 SCM ret;
73 scm_t_bits word0;
74
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));
79
80 ret = PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_bits) * 2,
81 "foreign"));
82 SCM_SET_CELL_WORD_0 (ret, word0);
83 SCM_SET_CELL_WORD_1 (ret, (scm_t_bits)ptr);
84
85 if (finalizer)
86 {
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,
92 finalizer,
93 &prev_finalizer,
94 &prev_finalizer_data);
95 }
96
97 return ret;
98 }
99
100 SCM_DEFINE (scm_foreign_ref, "foreign-ref", 1, 0, 0,
101 (SCM foreign),
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
105 {
106 scm_t_foreign_type ftype;
107 scm_t_uint8 *ptr;
108
109 SCM_VALIDATE_FOREIGN (1, foreign);
110 ptr = SCM_FOREIGN_POINTER (foreign, scm_t_uint8);
111 ftype = SCM_FOREIGN_TYPE (foreign);
112
113 /* FIXME: is there a window in which we can see ptr but not foreign? */
114 /* FIXME: accessing unaligned pointers */
115 switch (ftype)
116 {
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);
139 default:
140 scm_wrong_type_arg_msg (FUNC_NAME, 1, foreign, "foreign");
141 }
142 }
143 #undef FUNC_NAME
144
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
150 {
151 scm_t_foreign_type ftype;
152 scm_t_uint8 *ptr;
153
154 SCM_VALIDATE_FOREIGN (1, foreign);
155 ptr = SCM_FOREIGN_POINTER (foreign, scm_t_uint8);
156 ftype = SCM_FOREIGN_TYPE (foreign);
157
158 /* FIXME: is there a window in which we can see ptr but not foreign? */
159 /* FIXME: unaligned access */
160 switch (ftype)
161 {
162 case SCM_FOREIGN_TYPE_VOID:
163 SCM_SET_CELL_WORD_1 (foreign, scm_to_ulong (val));
164 break;
165 case SCM_FOREIGN_TYPE_FLOAT:
166 *(float*)ptr = scm_to_double (val);
167 break;
168 case SCM_FOREIGN_TYPE_DOUBLE:
169 *(double*)ptr = scm_to_double (val);
170 break;
171 case SCM_FOREIGN_TYPE_UINT8:
172 *(scm_t_uint8*)ptr = scm_to_uint8 (val);
173 break;
174 case SCM_FOREIGN_TYPE_INT8:
175 *(scm_t_int8*)ptr = scm_to_int8 (val);
176 break;
177 case SCM_FOREIGN_TYPE_UINT16:
178 *(scm_t_uint16*)ptr = scm_to_uint16 (val);
179 break;
180 case SCM_FOREIGN_TYPE_INT16:
181 *(scm_t_int16*)ptr = scm_to_int16 (val);
182 break;
183 case SCM_FOREIGN_TYPE_UINT32:
184 *(scm_t_uint32*)ptr = scm_to_uint32 (val);
185 break;
186 case SCM_FOREIGN_TYPE_INT32:
187 *(scm_t_int32*)ptr = scm_to_int32 (val);
188 break;
189 case SCM_FOREIGN_TYPE_UINT64:
190 *(scm_t_uint64*)ptr = scm_to_uint64 (val);
191 break;
192 case SCM_FOREIGN_TYPE_INT64:
193 *(scm_t_int64*)ptr = scm_to_int64 (val);
194 break;
195 default:
196 scm_wrong_type_arg_msg (FUNC_NAME, 1, val, "foreign");
197 }
198
199 return SCM_UNSPECIFIED;
200 }
201 #undef FUNC_NAME
202
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"
206 "@var{foreign}.\n\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"
216 "or @code{s16}.\n\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"
219 "arguments.")
220 #define FUNC_NAME s_scm_foreign_to_bytevector
221 {
222 SCM ret;
223 scm_t_int8 *ptr;
224 size_t boffset, blen;
225 scm_t_array_element_type btype;
226
227 SCM_VALIDATE_FOREIGN_TYPED (1, foreign, VOID);
228 ptr = SCM_FOREIGN_POINTER (foreign, scm_t_int8);
229
230 if (SCM_UNBNDP (uvec_type))
231 btype = SCM_ARRAY_ELEMENT_TYPE_VU8;
232 else
233 {
234 int i;
235 for (i = 0; i <= SCM_ARRAY_ELEMENT_TYPE_LAST; i++)
236 if (scm_is_eq (uvec_type, scm_i_array_element_types[i]))
237 break;
238 switch (i)
239 {
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:
253 btype = i;
254 break;
255 default:
256 scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, uvec_type,
257 "uniform vector type");
258 }
259 }
260
261 if (SCM_UNBNDP (offset))
262 boffset = 0;
263 else if (SCM_FOREIGN_LEN (foreign))
264 boffset = scm_to_unsigned_integer (offset, 0,
265 SCM_FOREIGN_LEN (foreign) - 1);
266 else
267 boffset = scm_to_size_t (offset);
268
269 if (SCM_UNBNDP (len))
270 {
271 if (SCM_FOREIGN_LEN (foreign))
272 blen = SCM_FOREIGN_LEN (foreign) - boffset;
273 else
274 scm_misc_error (FUNC_NAME,
275 "length needed to convert foreign pointer to bytevector",
276 SCM_EOL);
277 }
278 else
279 {
280 if (SCM_FOREIGN_LEN (foreign))
281 blen = scm_to_unsigned_integer (len, 0,
282 SCM_FOREIGN_LEN (foreign) - boffset);
283 else
284 blen = scm_to_size_t (len);
285 }
286
287 ret = scm_c_take_typed_bytevector (ptr + boffset, blen, btype);
288 register_weak_reference (ret, foreign);
289 return ret;
290 }
291 #undef FUNC_NAME
292
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"
296 "@var{bv}.\n\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"
302 "arguments.")
303 #define FUNC_NAME s_scm_bytevector_to_foreign
304 {
305 SCM ret;
306 scm_t_int8 *ptr;
307 size_t boffset, blen;
308
309 SCM_VALIDATE_BYTEVECTOR (1, bv);
310 ptr = SCM_BYTEVECTOR_CONTENTS (bv);
311
312 if (SCM_UNBNDP (offset))
313 boffset = 0;
314 else
315 boffset = scm_to_unsigned_integer (offset, 0,
316 SCM_BYTEVECTOR_LENGTH (bv) - 1);
317
318 if (SCM_UNBNDP (len))
319 blen = SCM_BYTEVECTOR_LENGTH (bv) - boffset;
320 else
321 blen = scm_to_unsigned_integer (len, 0,
322 SCM_BYTEVECTOR_LENGTH (bv) - boffset);
323
324 ret = scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID, ptr + boffset, blen,
325 NULL);
326 register_weak_reference (ret, bv);
327 return ret;
328 }
329 #undef FUNC_NAME
330
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
338 {
339 void *c_finalizer;
340 GC_finalization_proc prev_finalizer;
341 GC_PTR prev_finalizer_data;
342
343 SCM_VALIDATE_FOREIGN_TYPED (1, foreign, VOID);
344 SCM_VALIDATE_FOREIGN_TYPED (2, finalizer, VOID);
345
346 c_finalizer = SCM_FOREIGN_POINTER (finalizer, void);
347
348 SCM_SET_CELL_WORD_0 (foreign, SCM_CELL_WORD_0 (foreign) | (1<<16));
349
350 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (foreign),
351 foreign_finalizer_trampoline,
352 c_finalizer,
353 &prev_finalizer,
354 &prev_finalizer_data);
355
356 return SCM_UNSPECIFIED;
357 }
358 #undef FUNC_NAME
359
360 \f
361
362 void
363 scm_i_foreign_print (SCM foreign, SCM port, scm_print_state *pstate)
364 {
365 scm_puts ("#<foreign ", port);
366 switch (SCM_FOREIGN_TYPE (foreign))
367 {
368 case SCM_FOREIGN_TYPE_FLOAT:
369 scm_puts ("float ", port);
370 break;
371 case SCM_FOREIGN_TYPE_DOUBLE:
372 scm_puts ("double ", port);
373 break;
374 case SCM_FOREIGN_TYPE_UINT8:
375 scm_puts ("uint8 ", port);
376 break;
377 case SCM_FOREIGN_TYPE_INT8:
378 scm_puts ("int8 ", port);
379 break;
380 case SCM_FOREIGN_TYPE_UINT16:
381 scm_puts ("uint16 ", port);
382 break;
383 case SCM_FOREIGN_TYPE_INT16:
384 scm_puts ("int16 ", port);
385 break;
386 case SCM_FOREIGN_TYPE_UINT32:
387 scm_puts ("uint32 ", port);
388 break;
389 case SCM_FOREIGN_TYPE_INT32:
390 scm_puts ("int32 ", port);
391 break;
392 case SCM_FOREIGN_TYPE_UINT64:
393 scm_puts ("uint64 ", port);
394 break;
395 case SCM_FOREIGN_TYPE_INT64:
396 scm_puts ("int64 ", port);
397 break;
398 case SCM_FOREIGN_TYPE_VOID:
399 scm_puts ("pointer ", port);
400 break;
401 default:
402 scm_wrong_type_arg_msg ("%print-foreign", 1, foreign, "foreign");
403 }
404 scm_display (scm_foreign_ref (foreign), port);
405 scm_putc ('>', port);
406 }
407
408 \f
409
410
411 #define ROUND_UP(len,align) (align?(((len-1)|(align-1))+1):len)
412
413 SCM_DEFINE (scm_alignof, "alignof", 1, 0, 0, (SCM type), "")
414 #define FUNC_NAME s_scm_alignof
415 {
416 if (SCM_I_INUMP (type))
417 {
418 switch (SCM_I_INUM (type))
419 {
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));
440 default:
441 scm_wrong_type_arg (FUNC_NAME, 1, type);
442 }
443 }
444 else if (scm_is_eq (type, sym_asterisk))
445 /* a pointer */
446 return scm_from_size_t (alignof (void*));
447 else if (scm_is_pair (type))
448 /* a struct, yo */
449 return scm_alignof (scm_car (type));
450 else
451 scm_wrong_type_arg (FUNC_NAME, 1, type);
452 }
453 #undef FUNC_NAME
454
455 SCM_DEFINE (scm_sizeof, "sizeof", 1, 0, 0, (SCM type), "")
456 #define FUNC_NAME s_scm_sizeof
457 {
458 if (SCM_I_INUMP (type))
459 {
460 switch (SCM_I_INUM (type))
461 {
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));
482 default:
483 scm_wrong_type_arg (FUNC_NAME, 1, type);
484 }
485 }
486 else if (scm_is_eq (type, sym_asterisk))
487 /* a pointer */
488 return scm_from_size_t (sizeof (void*));
489 else if (scm_is_pair (type))
490 {
491 /* a struct */
492 size_t off = 0;
493 while (scm_is_pair (type))
494 {
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);
498 }
499 return scm_from_size_t (off);
500 }
501 else
502 scm_wrong_type_arg (FUNC_NAME, 1, type);
503 }
504 #undef FUNC_NAME
505
506
507 /* return 1 on success, 0 on failure */
508 static int
509 parse_ffi_type (SCM type, int return_p, long *n_structs, long *n_struct_elts)
510 {
511 if (SCM_I_INUMP (type))
512 {
513 if ((SCM_I_INUM (type) < 0 )
514 || (SCM_I_INUM (type) > SCM_FOREIGN_TYPE_LAST))
515 return 0;
516 else if (SCM_I_INUM (type) == SCM_FOREIGN_TYPE_VOID && !return_p)
517 return 0;
518 else
519 return 1;
520 }
521 else if (scm_is_eq (type, sym_asterisk))
522 /* a pointer */
523 return 1;
524 else
525 {
526 long len;
527
528 len = scm_ilength (type);
529 if (len < 1)
530 return 0;
531 while (len--)
532 {
533 if (!parse_ffi_type (scm_car (type), 0, n_structs, n_struct_elts))
534 return 0;
535 (*n_struct_elts)++;
536 type = scm_cdr (type);
537 }
538 (*n_structs)++;
539 return 1;
540 }
541 }
542
543 static void
544 fill_ffi_type (SCM type, ffi_type *ftype, ffi_type ***type_ptrs,
545 ffi_type **types)
546 {
547 if (SCM_I_INUMP (type))
548 {
549 switch (SCM_I_INUM (type))
550 {
551 case SCM_FOREIGN_TYPE_FLOAT:
552 *ftype = ffi_type_float;
553 return;
554 case SCM_FOREIGN_TYPE_DOUBLE:
555 *ftype = ffi_type_double;
556 return;
557 case SCM_FOREIGN_TYPE_UINT8:
558 *ftype = ffi_type_uint8;
559 return;
560 case SCM_FOREIGN_TYPE_INT8:
561 *ftype = ffi_type_sint8;
562 return;
563 case SCM_FOREIGN_TYPE_UINT16:
564 *ftype = ffi_type_uint16;
565 return;
566 case SCM_FOREIGN_TYPE_INT16:
567 *ftype = ffi_type_sint16;
568 return;
569 case SCM_FOREIGN_TYPE_UINT32:
570 *ftype = ffi_type_uint32;
571 return;
572 case SCM_FOREIGN_TYPE_INT32:
573 *ftype = ffi_type_sint32;
574 return;
575 case SCM_FOREIGN_TYPE_UINT64:
576 *ftype = ffi_type_uint64;
577 return;
578 case SCM_FOREIGN_TYPE_INT64:
579 *ftype = ffi_type_sint64;
580 return;
581 case SCM_FOREIGN_TYPE_VOID:
582 *ftype = ffi_type_void;
583 return;
584 default:
585 scm_wrong_type_arg_msg ("make-foreign-function", 0, type,
586 "foreign type");
587 }
588 }
589 else if (scm_is_eq (type, sym_asterisk))
590 /* a pointer */
591 {
592 *ftype = ffi_type_pointer;
593 return;
594 }
595 else
596 {
597 long i, len;
598
599 len = scm_ilength (type);
600
601 ftype->size = 0;
602 ftype->alignment = 0;
603 ftype->type = FFI_TYPE_STRUCT;
604 ftype->elements = *type_ptrs;
605 *type_ptrs += len + 1;
606
607 for (i = 0; i < len; i++)
608 {
609 ftype->elements[i] = *types;
610 *types += 1;
611 fill_ffi_type (scm_car (type), ftype->elements[i],
612 type_ptrs, types);
613 type = scm_cdr (type);
614 }
615 ftype->elements[i] = NULL;
616 }
617 }
618
619 SCM_DEFINE (scm_make_foreign_function, "make-foreign-function", 3, 0, 0,
620 (SCM return_type, SCM func_ptr, SCM arg_types),
621 "foo")
622 #define FUNC_NAME s_scm_make_foreign_function
623 {
624 SCM walk, scm_cif;
625 long i, nargs, n_structs, n_struct_elts;
626 size_t cif_len;
627 char *mem;
628 ffi_cif *cif;
629 ffi_type **type_ptrs;
630 ffi_type *types;
631
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;
637
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));
646
647 /* the memory: with space for the cif itself */
648 cif_len = sizeof (ffi_cif);
649
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*));
654
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));
659
660 mem = scm_malloc (cif_len);
661 scm_cif = scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID, mem, cif_len, free);
662 cif = (ffi_cif*)mem;
663 /* reuse cif_len to walk through the mem */
664 cif_len = ROUND_UP (sizeof (ffi_cif), alignof(void*));
665 type_ptrs = (ffi_type**)(mem + cif_len);
666 cif_len = ROUND_UP (cif_len
667 + (nargs + n_structs + n_struct_elts)*sizeof(void*),
668 alignof(ffi_type));
669 types = (ffi_type*)(mem + cif_len);
670
671 /* whew. now knit the pointers together. */
672 cif->rtype = types++;
673 fill_ffi_type (return_type, cif->rtype, &type_ptrs, &types);
674 cif->arg_types = type_ptrs;
675 type_ptrs += nargs;
676 for (walk = arg_types, i = 0; scm_is_pair (walk); walk = scm_cdr (walk), i++)
677 {
678 cif->arg_types[i] = types++;
679 fill_ffi_type (scm_car (walk), cif->arg_types[i], &type_ptrs, &types);
680 }
681
682 /* round out the cif, and we're done. */
683 cif->abi = FFI_DEFAULT_ABI;
684 cif->nargs = nargs;
685 cif->bytes = 0;
686 cif->flags = 0;
687
688 if (FFI_OK != ffi_prep_cif (cif, FFI_DEFAULT_ABI, cif->nargs, cif->rtype,
689 cif->arg_types))
690 scm_misc_error (FUNC_NAME, "ffi_prep_cif failed", SCM_EOL);
691
692 return cif_to_procedure (scm_cif, func_ptr);
693 }
694 #undef FUNC_NAME
695
696 \f
697
698 /* Pre-generate trampolines for less than 10 arguments. */
699
700 #ifdef WORDS_BIGENDIAN
701 #define OBJCODE_HEADER 0, 0, 0, 8, 0, 0, 0, 40
702 #define META_HEADER 0, 0, 0, 32, 0, 0, 0, 0
703 #else
704 #define OBJCODE_HEADER 8, 0, 0, 0, 40, 0, 0, 0
705 #define META_HEADER 32, 0, 0, 0, 0, 0, 0, 0
706 #endif
707
708 #define CODE(nreq) \
709 OBJCODE_HEADER, \
710 /* 0 */ scm_op_assert_nargs_ee, 0, nreq, /* assert number of args */ \
711 /* 3 */ scm_op_object_ref, 0, /* push the pair with the cif and the function pointer */ \
712 /* 5 */ scm_op_foreign_call, nreq, /* and call (will return value as well) */ \
713 /* 7 */ scm_op_nop, \
714 /* 8 */ META (3, 7, nreq)
715
716 #define META(start, end, nreq) \
717 META_HEADER, \
718 /* 0 */ scm_op_make_eol, /* bindings */ \
719 /* 1 */ scm_op_make_eol, /* sources */ \
720 /* 2 */ scm_op_make_int8, start, scm_op_make_int8, end, /* arity: from ip N to ip N */ \
721 /* 6 */ scm_op_make_int8, nreq, /* the arity is N required args */ \
722 /* 8 */ scm_op_list, 0, 3, /* make a list of those 3 vals */ \
723 /* 11 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */ \
724 /* 14 */ scm_op_load_symbol, 0, 0, 4, 'n', 'a', 'm', 'e', /* `name' */ \
725 /* 22 */ scm_op_object_ref, 1, /* the name from the object table */ \
726 /* 24 */ scm_op_cons, /* make a pair for the properties */ \
727 /* 25 */ scm_op_list, 0, 4, /* pack bindings, sources, and arities into list */ \
728 /* 28 */ scm_op_return, /* and return */ \
729 /* 29 */ scm_op_nop, scm_op_nop, scm_op_nop \
730 /* 32 */
731
732 static const struct
733 {
734 scm_t_uint64 dummy; /* ensure 8-byte alignment; perhaps there's a better way */
735 const scm_t_uint8 bytes[10 * (sizeof (struct scm_objcode) + 8
736 + sizeof (struct scm_objcode) + 32)];
737 } raw_bytecode = {
738 0,
739 {
740 CODE (0), CODE (1), CODE (2), CODE (3), CODE (4),
741 CODE (5), CODE (6), CODE (7), CODE (8), CODE (9)
742 }
743 };
744
745 #undef CODE
746 #undef META
747 #undef OBJCODE_HEADER
748 #undef META_HEADER
749
750 /*
751 (defun generate-objcode-cells (n)
752 "Generate objcode cells for up to N arguments"
753 (interactive "p")
754 (let ((i 0))
755 (while (< i n)
756 (insert
757 (format " { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + %d) },\n"
758 (* (+ 4 4 8 4 4 32) i)))
759 (insert " { SCM_BOOL_F, SCM_PACK (0) },\n")
760 (setq i (1+ i)))))
761 */
762 #define STATIC_OBJCODE_TAG \
763 SCM_PACK (scm_tc7_objcode | (SCM_F_OBJCODE_IS_STATIC << 8))
764
765 static const struct
766 {
767 scm_t_uint64 dummy; /* alignment */
768 scm_t_cell cells[10 * 2]; /* 10 double cells */
769 } objcode_cells = {
770 0,
771 /* C-u 1 0 M-x generate-objcode-cells RET */
772 {
773 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 0) },
774 { SCM_BOOL_F, SCM_PACK (0) },
775 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 56) },
776 { SCM_BOOL_F, SCM_PACK (0) },
777 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 112) },
778 { SCM_BOOL_F, SCM_PACK (0) },
779 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 168) },
780 { SCM_BOOL_F, SCM_PACK (0) },
781 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 224) },
782 { SCM_BOOL_F, SCM_PACK (0) },
783 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 280) },
784 { SCM_BOOL_F, SCM_PACK (0) },
785 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 336) },
786 { SCM_BOOL_F, SCM_PACK (0) },
787 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 392) },
788 { SCM_BOOL_F, SCM_PACK (0) },
789 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 448) },
790 { SCM_BOOL_F, SCM_PACK (0) },
791 { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 504) },
792 { SCM_BOOL_F, SCM_PACK (0) }
793 }
794 };
795
796 static const SCM objcode_trampolines[10] = {
797 SCM_PACK (objcode_cells.cells+0),
798 SCM_PACK (objcode_cells.cells+2),
799 SCM_PACK (objcode_cells.cells+4),
800 SCM_PACK (objcode_cells.cells+6),
801 SCM_PACK (objcode_cells.cells+8),
802 SCM_PACK (objcode_cells.cells+10),
803 SCM_PACK (objcode_cells.cells+12),
804 SCM_PACK (objcode_cells.cells+14),
805 SCM_PACK (objcode_cells.cells+16),
806 SCM_PACK (objcode_cells.cells+18),
807 };
808
809 static SCM
810 cif_to_procedure (SCM cif, SCM func_ptr)
811 {
812 unsigned nargs = SCM_FOREIGN_POINTER (cif, ffi_cif)->nargs;
813 SCM objcode, table, ret;
814
815 if (nargs < 10)
816 objcode = objcode_trampolines[nargs];
817 else
818 scm_misc_error ("make-foreign-function", "args >= 10 currently unimplemented",
819 SCM_EOL);
820
821 table = scm_c_make_vector (2, SCM_UNDEFINED);
822 SCM_SIMPLE_VECTOR_SET (table, 0, scm_cons (cif, func_ptr));
823 SCM_SIMPLE_VECTOR_SET (table, 1, SCM_BOOL_F); /* name */
824 ret = scm_make_program (objcode, table, SCM_BOOL_F);
825
826 return ret;
827 }
828
829 static void
830 unpack (ffi_type *type, void *loc, SCM x)
831 {
832 switch (type->type)
833 {
834 case FFI_TYPE_FLOAT:
835 *(float*)loc = scm_to_double (x);
836 break;
837 case FFI_TYPE_DOUBLE:
838 *(double*)loc = scm_to_double (x);
839 break;
840 case FFI_TYPE_UINT8:
841 *(scm_t_uint8*)loc = scm_to_uint8 (x);
842 break;
843 case FFI_TYPE_SINT8:
844 *(scm_t_int8*)loc = scm_to_int8 (x);
845 break;
846 case FFI_TYPE_UINT16:
847 *(scm_t_uint16*)loc = scm_to_uint16 (x);
848 break;
849 case FFI_TYPE_SINT16:
850 *(scm_t_int16*)loc = scm_to_int16 (x);
851 break;
852 case FFI_TYPE_UINT32:
853 *(scm_t_uint32*)loc = scm_to_uint32 (x);
854 break;
855 case FFI_TYPE_SINT32:
856 *(scm_t_int32*)loc = scm_to_int32 (x);
857 break;
858 case FFI_TYPE_UINT64:
859 *(scm_t_uint64*)loc = scm_to_uint64 (x);
860 break;
861 case FFI_TYPE_SINT64:
862 *(scm_t_int64*)loc = scm_to_int64 (x);
863 break;
864 case FFI_TYPE_STRUCT:
865 if (!SCM_FOREIGN_TYPED_P (x, VOID))
866 scm_wrong_type_arg_msg ("foreign-call", 0, x,
867 "foreign void pointer");
868 if (SCM_FOREIGN_LEN (x) && SCM_FOREIGN_LEN (x) != type->size)
869 scm_wrong_type_arg_msg ("foreign-call", 0, x,
870 "foreign void pointer of correct length");
871 memcpy (loc, SCM_FOREIGN_POINTER (x, void), type->size);
872 break;
873 case FFI_TYPE_POINTER:
874 if (!SCM_FOREIGN_TYPED_P (x, VOID))
875 scm_wrong_type_arg_msg ("foreign-call", 0, x,
876 "foreign void pointer");
877 *(void**)loc = SCM_FOREIGN_POINTER (x, void);
878 break;
879 default:
880 abort ();
881 }
882 }
883
884 static SCM
885 pack (ffi_type *type, void *loc)
886 {
887 switch (type->type)
888 {
889 case FFI_TYPE_VOID:
890 return SCM_UNSPECIFIED;
891 case FFI_TYPE_FLOAT:
892 return scm_from_double (*(float*)loc);
893 case FFI_TYPE_DOUBLE:
894 return scm_from_double (*(double*)loc);
895 case FFI_TYPE_UINT8:
896 return scm_from_uint8 (*(scm_t_uint8*)loc);
897 case FFI_TYPE_SINT8:
898 return scm_from_int8 (*(scm_t_int8*)loc);
899 case FFI_TYPE_UINT16:
900 return scm_from_uint16 (*(scm_t_uint16*)loc);
901 case FFI_TYPE_SINT16:
902 return scm_from_int16 (*(scm_t_int16*)loc);
903 case FFI_TYPE_UINT32:
904 return scm_from_uint32 (*(scm_t_uint32*)loc);
905 case FFI_TYPE_SINT32:
906 return scm_from_int32 (*(scm_t_int32*)loc);
907 case FFI_TYPE_UINT64:
908 return scm_from_uint64 (*(scm_t_uint64*)loc);
909 case FFI_TYPE_SINT64:
910 return scm_from_int64 (*(scm_t_int64*)loc);
911 case FFI_TYPE_STRUCT:
912 {
913 void *mem = scm_malloc (type->size);
914 memcpy (mem, loc, type->size);
915 return scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID,
916 mem, type->size, free);
917 }
918 case FFI_TYPE_POINTER:
919 return scm_take_foreign_pointer (SCM_FOREIGN_TYPE_VOID,
920 *(void**)loc, 0, NULL);
921 default:
922 abort ();
923 }
924 }
925
926 SCM
927 scm_i_foreign_call (SCM foreign, SCM *argv)
928 {
929 /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
930 objtable. */
931 ffi_cif *cif;
932 void (*func)();
933 scm_t_uint8 *data;
934 void *rvalue;
935 void **args;
936 unsigned i;
937 scm_t_ptrdiff off;
938
939 cif = SCM_FOREIGN_POINTER (scm_car (foreign), ffi_cif);
940 func = SCM_FOREIGN_POINTER (scm_cdr (foreign), void);
941
942 /* arg pointers */
943 args = alloca (sizeof(void*) * cif->nargs);
944 /* arg values, then return type value */
945 data = alloca (ROUND_UP (cif->bytes, cif->rtype->alignment)
946 + cif->rtype->size);
947 /* unpack argv to native values, setting argv pointers */
948 off = 0;
949 for (i = 0; i < cif->nargs; i++)
950 {
951 off = ROUND_UP (off, cif->arg_types[i]->alignment);
952 args[i] = data + off;
953 unpack (cif->arg_types[i], args[i], argv[i]);
954 off += cif->arg_types[i]->size;
955 }
956 /* prep space for the return value */
957 off = ROUND_UP (off, cif->rtype->alignment);
958 rvalue = data + off;
959
960 /* off we go! */
961 ffi_call (cif, func, rvalue, args);
962
963 return pack (cif->rtype, rvalue);
964 }
965
966 \f
967
968 static void
969 scm_init_foreign (void)
970 {
971 #ifndef SCM_MAGIC_SNARFER
972 #include "libguile/foreign.x"
973 #endif
974 scm_define (sym_void, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID));
975 scm_define (sym_float, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT));
976 scm_define (sym_double, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE));
977 scm_define (sym_uint8, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8));
978 scm_define (sym_int8, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8));
979 scm_define (sym_uint16, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16));
980 scm_define (sym_int16, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16));
981 scm_define (sym_uint32, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32));
982 scm_define (sym_int32, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32));
983 scm_define (sym_uint64, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64));
984 scm_define (sym_int64, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64));
985 }
986
987 void
988 scm_register_foreign (void)
989 {
990 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
991 "scm_init_foreign",
992 (scm_t_extension_init_func)scm_init_foreign,
993 NULL);
994 foreign_weak_refs = scm_make_weak_key_hash_table (SCM_UNDEFINED);
995 }
996
997 /*
998 Local Variables:
999 c-file-style: "gnu"
1000 End:
1001 */