Commit | Line | Data |
---|---|---|
f9654187 | 1 | /* Copyright (C) 2010, 2011 Free Software Foundation, Inc. |
5b46a8c2 | 2 | * |
e2c2a699 AW |
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 | ||
d8b04f04 AW |
23 | #include <ffi.h> |
24 | ||
cdd47ec7 | 25 | #include <alloca.h> |
d8b04f04 | 26 | #include <alignof.h> |
e2c2a699 | 27 | #include <string.h> |
86425e26 LC |
28 | #include <assert.h> |
29 | ||
ea7d717b | 30 | #include "libguile/_scm.h" |
20aafae2 | 31 | #include "libguile/bytevectors.h" |
d8b04f04 | 32 | #include "libguile/instructions.h" |
ea7d717b | 33 | #include "libguile/foreign.h" |
e2c2a699 AW |
34 | |
35 | \f | |
36 | ||
ab4779ff AW |
37 | SCM_SYMBOL (sym_void, "void"); |
38 | SCM_SYMBOL (sym_float, "float"); | |
39 | SCM_SYMBOL (sym_double, "double"); | |
40 | SCM_SYMBOL (sym_uint8, "uint8"); | |
41 | SCM_SYMBOL (sym_int8, "int8"); | |
42 | SCM_SYMBOL (sym_uint16, "uint16"); | |
43 | SCM_SYMBOL (sym_int16, "int16"); | |
44 | SCM_SYMBOL (sym_uint32, "uint32"); | |
45 | SCM_SYMBOL (sym_int32, "int32"); | |
46 | SCM_SYMBOL (sym_uint64, "uint64"); | |
47 | SCM_SYMBOL (sym_int64, "int64"); | |
42f7c01e | 48 | SCM_SYMBOL (sym_short, "short"); |
dd1464bf LC |
49 | SCM_SYMBOL (sym_int, "int"); |
50 | SCM_SYMBOL (sym_long, "long"); | |
42f7c01e | 51 | SCM_SYMBOL (sym_unsigned_short, "unsigned-short"); |
dd1464bf LC |
52 | SCM_SYMBOL (sym_unsigned_int, "unsigned-int"); |
53 | SCM_SYMBOL (sym_unsigned_long, "unsigned-long"); | |
54 | SCM_SYMBOL (sym_size_t, "size_t"); | |
ab4779ff | 55 | |
3435f3c0 AW |
56 | /* that's for pointers, you know. */ |
57 | SCM_SYMBOL (sym_asterisk, "*"); | |
58 | ||
54eb59cf | 59 | SCM_SYMBOL (sym_null, "%null-pointer"); |
01ad5a7b | 60 | SCM_SYMBOL (sym_null_pointer_error, "null-pointer-error"); |
54eb59cf LC |
61 | |
62 | /* The cell representing the null pointer. */ | |
3e5ea35c | 63 | static SCM null_pointer; |
3435f3c0 | 64 | |
d4149a51 LC |
65 | #if SIZEOF_VOID_P == 4 |
66 | # define scm_to_uintptr scm_to_uint32 | |
67 | # define scm_from_uintptr scm_from_uint32 | |
68 | #elif SIZEOF_VOID_P == 8 | |
69 | # define scm_to_uintptr scm_to_uint64 | |
70 | # define scm_from_uintptr scm_from_uint64 | |
71 | #else | |
72 | # error unsupported pointer size | |
73 | #endif | |
74 | ||
75 | ||
01ad5a7b LC |
76 | /* Raise a null pointer dereference error. */ |
77 | static void | |
78 | null_pointer_error (const char *func_name) | |
79 | { | |
80 | scm_error (sym_null_pointer_error, func_name, | |
81 | "null pointer dereference", SCM_EOL, SCM_EOL); | |
82 | } | |
83 | ||
84 | \f | |
d8b04f04 AW |
85 | static SCM cif_to_procedure (SCM cif, SCM func_ptr); |
86 | ||
87 | ||
5b46a8c2 | 88 | static SCM pointer_weak_refs = SCM_BOOL_F; |
20aafae2 AW |
89 | |
90 | static void | |
91 | register_weak_reference (SCM from, SCM to) | |
92 | { | |
5b46a8c2 | 93 | scm_hashq_set_x (pointer_weak_refs, from, to); |
20aafae2 | 94 | } |
d4149a51 | 95 | |
e2c2a699 | 96 | static void |
5b46a8c2 | 97 | pointer_finalizer_trampoline (GC_PTR ptr, GC_PTR data) |
e2c2a699 | 98 | { |
5b46a8c2 LC |
99 | scm_t_pointer_finalizer finalizer = data; |
100 | finalizer (SCM_POINTER_VALUE (PTR2SCM (ptr))); | |
e2c2a699 AW |
101 | } |
102 | ||
d4149a51 LC |
103 | SCM_DEFINE (scm_make_pointer, "make-pointer", 1, 1, 0, |
104 | (SCM address, SCM finalizer), | |
105 | "Return a foreign pointer object pointing to @var{address}. " | |
106 | "If @var{finalizer} is passed, it should be a pointer to a " | |
107 | "one-argument C function that will be called when the pointer " | |
108 | "object becomes unreachable.") | |
109 | #define FUNC_NAME s_scm_make_pointer | |
110 | { | |
111 | void *c_finalizer; | |
112 | scm_t_uintptr c_address; | |
d4149a51 LC |
113 | |
114 | c_address = scm_to_uintptr (address); | |
115 | if (SCM_UNBNDP (finalizer)) | |
116 | c_finalizer = NULL; | |
117 | else | |
118 | { | |
5b46a8c2 LC |
119 | SCM_VALIDATE_POINTER (2, finalizer); |
120 | c_finalizer = SCM_POINTER_VALUE (finalizer); | |
d4149a51 LC |
121 | } |
122 | ||
854aa906 | 123 | return scm_from_pointer ((void *) c_address, c_finalizer); |
d4149a51 LC |
124 | } |
125 | #undef FUNC_NAME | |
126 | ||
e2c2a699 | 127 | SCM |
5b46a8c2 | 128 | scm_from_pointer (void *ptr, scm_t_pointer_finalizer finalizer) |
e2c2a699 | 129 | { |
9fdee5b4 | 130 | SCM ret; |
d4149a51 | 131 | |
854aa906 LC |
132 | if (ptr == NULL && finalizer == NULL) |
133 | ret = null_pointer; | |
134 | else | |
e2c2a699 | 135 | { |
690a0112 | 136 | ret = scm_cell (scm_tc7_pointer, (scm_t_bits) ptr); |
854aa906 LC |
137 | |
138 | if (finalizer) | |
139 | { | |
140 | /* Register a finalizer for the newly created instance. */ | |
141 | GC_finalization_proc prev_finalizer; | |
142 | GC_PTR prev_finalizer_data; | |
143 | GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret), | |
144 | pointer_finalizer_trampoline, | |
145 | finalizer, | |
146 | &prev_finalizer, | |
147 | &prev_finalizer_data); | |
148 | } | |
e2c2a699 AW |
149 | } |
150 | ||
9fdee5b4 | 151 | return ret; |
e2c2a699 AW |
152 | } |
153 | ||
5b46a8c2 LC |
154 | SCM_DEFINE (scm_pointer_address, "pointer-address", 1, 0, 0, |
155 | (SCM pointer), | |
156 | "Return the numerical value of @var{pointer}.") | |
157 | #define FUNC_NAME s_scm_pointer_address | |
e2c2a699 | 158 | { |
5b46a8c2 | 159 | SCM_VALIDATE_POINTER (1, pointer); |
01ad5a7b | 160 | |
5b46a8c2 | 161 | return scm_from_uintptr ((scm_t_uintptr) SCM_POINTER_VALUE (pointer)); |
e2c2a699 AW |
162 | } |
163 | #undef FUNC_NAME | |
164 | ||
5b46a8c2 LC |
165 | SCM_DEFINE (scm_pointer_to_bytevector, "pointer->bytevector", 2, 2, 0, |
166 | (SCM pointer, SCM len, SCM offset, SCM uvec_type), | |
183a2a22 LC |
167 | "Return a bytevector aliasing the @var{len} bytes pointed\n" |
168 | "to by @var{pointer}.\n\n" | |
20aafae2 AW |
169 | "The user may specify an alternate default interpretation for\n" |
170 | "the memory by passing the @var{uvec_type} argument, to indicate\n" | |
171 | "that the memory is an array of elements of that type.\n" | |
172 | "@var{uvec_type} should be something that\n" | |
173 | "@code{uniform-vector-element-type} would return, like @code{f32}\n" | |
174 | "or @code{s16}.\n\n" | |
183a2a22 LC |
175 | "When @var{offset} is passed, it specifies the offset in bytes\n" |
176 | "relative to @var{pointer} of the memory region aliased by the\n" | |
177 | "returned bytevector.") | |
5b46a8c2 | 178 | #define FUNC_NAME s_scm_pointer_to_bytevector |
20aafae2 AW |
179 | { |
180 | SCM ret; | |
181 | scm_t_int8 *ptr; | |
182 | size_t boffset, blen; | |
183 | scm_t_array_element_type btype; | |
184 | ||
5b46a8c2 LC |
185 | SCM_VALIDATE_POINTER (1, pointer); |
186 | ptr = SCM_POINTER_VALUE (pointer); | |
54eb59cf LC |
187 | |
188 | if (SCM_UNLIKELY (ptr == NULL)) | |
01ad5a7b | 189 | null_pointer_error (FUNC_NAME); |
54eb59cf | 190 | |
20aafae2 AW |
191 | if (SCM_UNBNDP (uvec_type)) |
192 | btype = SCM_ARRAY_ELEMENT_TYPE_VU8; | |
193 | else | |
194 | { | |
195 | int i; | |
196 | for (i = 0; i <= SCM_ARRAY_ELEMENT_TYPE_LAST; i++) | |
197 | if (scm_is_eq (uvec_type, scm_i_array_element_types[i])) | |
198 | break; | |
199 | switch (i) | |
200 | { | |
201 | case SCM_ARRAY_ELEMENT_TYPE_VU8: | |
202 | case SCM_ARRAY_ELEMENT_TYPE_U8: | |
203 | case SCM_ARRAY_ELEMENT_TYPE_S8: | |
204 | case SCM_ARRAY_ELEMENT_TYPE_U16: | |
205 | case SCM_ARRAY_ELEMENT_TYPE_S16: | |
206 | case SCM_ARRAY_ELEMENT_TYPE_U32: | |
207 | case SCM_ARRAY_ELEMENT_TYPE_S32: | |
208 | case SCM_ARRAY_ELEMENT_TYPE_U64: | |
209 | case SCM_ARRAY_ELEMENT_TYPE_S64: | |
210 | case SCM_ARRAY_ELEMENT_TYPE_F32: | |
211 | case SCM_ARRAY_ELEMENT_TYPE_F64: | |
212 | case SCM_ARRAY_ELEMENT_TYPE_C32: | |
213 | case SCM_ARRAY_ELEMENT_TYPE_C64: | |
214 | btype = i; | |
215 | break; | |
216 | default: | |
217 | scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, uvec_type, | |
218 | "uniform vector type"); | |
219 | } | |
220 | } | |
d4149a51 | 221 | |
20aafae2 AW |
222 | if (SCM_UNBNDP (offset)) |
223 | boffset = 0; | |
20aafae2 AW |
224 | else |
225 | boffset = scm_to_size_t (offset); | |
226 | ||
d4149a51 | 227 | blen = scm_to_size_t (len); |
20aafae2 AW |
228 | |
229 | ret = scm_c_take_typed_bytevector (ptr + boffset, blen, btype); | |
5b46a8c2 | 230 | register_weak_reference (ret, pointer); |
20aafae2 AW |
231 | return ret; |
232 | } | |
233 | #undef FUNC_NAME | |
234 | ||
22697acb LC |
235 | SCM_DEFINE (scm_bytevector_to_pointer, "bytevector->pointer", 1, 1, 0, |
236 | (SCM bv, SCM offset), | |
5b46a8c2 | 237 | "Return a pointer pointer aliasing the memory pointed to by\n" |
22697acb LC |
238 | "@var{bv} or @var{offset} bytes after @var{bv} when @var{offset}\n" |
239 | "is passed.") | |
5b46a8c2 | 240 | #define FUNC_NAME s_scm_bytevector_to_pointer |
20aafae2 AW |
241 | { |
242 | SCM ret; | |
243 | scm_t_int8 *ptr; | |
22697acb | 244 | size_t boffset; |
20aafae2 AW |
245 | |
246 | SCM_VALIDATE_BYTEVECTOR (1, bv); | |
247 | ptr = SCM_BYTEVECTOR_CONTENTS (bv); | |
22697acb | 248 | |
20aafae2 AW |
249 | if (SCM_UNBNDP (offset)) |
250 | boffset = 0; | |
251 | else | |
252 | boffset = scm_to_unsigned_integer (offset, 0, | |
253 | SCM_BYTEVECTOR_LENGTH (bv) - 1); | |
254 | ||
5b46a8c2 | 255 | ret = scm_from_pointer (ptr + boffset, NULL); |
20aafae2 AW |
256 | register_weak_reference (ret, bv); |
257 | return ret; | |
258 | } | |
259 | #undef FUNC_NAME | |
260 | ||
5b46a8c2 LC |
261 | SCM_DEFINE (scm_set_pointer_finalizer_x, "set-pointer-finalizer!", 2, 0, 0, |
262 | (SCM pointer, SCM finalizer), | |
3435f3c0 | 263 | "Arrange for the C procedure wrapped by @var{finalizer} to be\n" |
5b46a8c2 | 264 | "called on the pointer wrapped by @var{pointer} when @var{pointer}\n" |
3435f3c0 AW |
265 | "becomes unreachable. Note: the C procedure should not call into\n" |
266 | "Scheme. If you need a Scheme finalizer, use guardians.") | |
5b46a8c2 | 267 | #define FUNC_NAME s_scm_set_pointer_finalizer_x |
3435f3c0 AW |
268 | { |
269 | void *c_finalizer; | |
270 | GC_finalization_proc prev_finalizer; | |
271 | GC_PTR prev_finalizer_data; | |
272 | ||
5b46a8c2 LC |
273 | SCM_VALIDATE_POINTER (1, pointer); |
274 | SCM_VALIDATE_POINTER (2, finalizer); | |
d4149a51 | 275 | |
5b46a8c2 | 276 | c_finalizer = SCM_POINTER_VALUE (finalizer); |
3435f3c0 | 277 | |
5b46a8c2 | 278 | SCM_SET_CELL_WORD_0 (pointer, SCM_CELL_WORD_0 (pointer) | (1 << 16UL)); |
3435f3c0 | 279 | |
5b46a8c2 LC |
280 | GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (pointer), |
281 | pointer_finalizer_trampoline, | |
3435f3c0 AW |
282 | c_finalizer, |
283 | &prev_finalizer, | |
284 | &prev_finalizer_data); | |
285 | ||
286 | return SCM_UNSPECIFIED; | |
287 | } | |
288 | #undef FUNC_NAME | |
289 | ||
e2c2a699 | 290 | void |
5b46a8c2 | 291 | scm_i_pointer_print (SCM pointer, SCM port, scm_print_state *pstate) |
e2c2a699 | 292 | { |
e5f7f675 AW |
293 | scm_puts ("#<pointer 0x", port); |
294 | scm_uintprint (scm_to_uintptr (scm_pointer_address (pointer)), 16, port); | |
e2c2a699 AW |
295 | scm_putc ('>', port); |
296 | } | |
297 | ||
298 | \f | |
fa2a89a6 LC |
299 | /* Non-primitive helpers functions. These procedures could be |
300 | implemented in terms of the primitives above but would be inefficient | |
301 | (heap allocation overhead, Scheme/C round trips, etc.) */ | |
302 | ||
303 | SCM_DEFINE (scm_dereference_pointer, "dereference-pointer", 1, 0, 0, | |
304 | (SCM pointer), | |
305 | "Assuming @var{pointer} points to a memory region that\n" | |
306 | "holds a pointer, return this pointer.") | |
307 | #define FUNC_NAME s_scm_dereference_pointer | |
308 | { | |
309 | SCM_VALIDATE_POINTER (1, pointer); | |
310 | ||
311 | return scm_from_pointer (* (void **) SCM_POINTER_VALUE (pointer), NULL); | |
312 | } | |
313 | #undef FUNC_NAME | |
314 | ||
315 | SCM_DEFINE (scm_string_to_pointer, "string->pointer", 1, 0, 0, | |
316 | (SCM string), | |
317 | "Return a foreign pointer to a nul-terminated copy of\n" | |
318 | "@var{string} in the current locale encoding. The C\n" | |
319 | "string is freed when the returned foreign pointer\n" | |
320 | "becomes unreachable.\n\n" | |
321 | "This is the Scheme equivalent of @code{scm_to_locale_string}.") | |
322 | #define FUNC_NAME s_scm_string_to_pointer | |
323 | { | |
324 | SCM_VALIDATE_STRING (1, string); | |
325 | ||
326 | /* XXX: Finalizers slow down libgc; they could be avoided if | |
327 | `scm_to_string' & co. were able to use libgc-allocated memory. */ | |
328 | ||
329 | return scm_from_pointer (scm_to_locale_string (string), free); | |
330 | } | |
331 | #undef FUNC_NAME | |
332 | ||
333 | SCM_DEFINE (scm_pointer_to_string, "pointer->string", 1, 0, 0, | |
334 | (SCM pointer), | |
335 | "Return the string representing the C nul-terminated string\n" | |
336 | "pointed to by @var{pointer}. The C string is assumed to be\n" | |
337 | "in the current locale encoding.\n\n" | |
338 | "This is the Scheme equivalent of @code{scm_from_locale_string}.") | |
339 | #define FUNC_NAME s_scm_pointer_to_string | |
340 | { | |
341 | SCM_VALIDATE_POINTER (1, pointer); | |
342 | ||
343 | return scm_from_locale_string (SCM_POINTER_VALUE (pointer)); | |
344 | } | |
345 | #undef FUNC_NAME | |
346 | ||
347 | \f | |
e2c2a699 | 348 | |
b9264dc5 AW |
349 | SCM_DEFINE (scm_alignof, "alignof", 1, 0, 0, (SCM type), |
350 | "Return the alignment of @var{type}, in bytes.\n\n" | |
351 | "@var{type} should be a valid C type, like @code{int}.\n" | |
352 | "Alternately @var{type} may be the symbol @code{*}, in which\n" | |
353 | "case the alignment of a pointer is returned. @var{type} may\n" | |
354 | "also be a list of types, in which case the alignment of a\n" | |
355 | "@code{struct} with ABI-conventional packing is returned.") | |
9a396cbd AW |
356 | #define FUNC_NAME s_scm_alignof |
357 | { | |
358 | if (SCM_I_INUMP (type)) | |
359 | { | |
360 | switch (SCM_I_INUM (type)) | |
361 | { | |
362 | case SCM_FOREIGN_TYPE_FLOAT: | |
363 | return scm_from_size_t (alignof (float)); | |
364 | case SCM_FOREIGN_TYPE_DOUBLE: | |
365 | return scm_from_size_t (alignof (double)); | |
366 | case SCM_FOREIGN_TYPE_UINT8: | |
367 | return scm_from_size_t (alignof (scm_t_uint8)); | |
368 | case SCM_FOREIGN_TYPE_INT8: | |
369 | return scm_from_size_t (alignof (scm_t_int8)); | |
370 | case SCM_FOREIGN_TYPE_UINT16: | |
371 | return scm_from_size_t (alignof (scm_t_uint16)); | |
372 | case SCM_FOREIGN_TYPE_INT16: | |
373 | return scm_from_size_t (alignof (scm_t_int16)); | |
374 | case SCM_FOREIGN_TYPE_UINT32: | |
375 | return scm_from_size_t (alignof (scm_t_uint32)); | |
376 | case SCM_FOREIGN_TYPE_INT32: | |
377 | return scm_from_size_t (alignof (scm_t_int32)); | |
378 | case SCM_FOREIGN_TYPE_UINT64: | |
379 | return scm_from_size_t (alignof (scm_t_uint64)); | |
380 | case SCM_FOREIGN_TYPE_INT64: | |
381 | return scm_from_size_t (alignof (scm_t_int64)); | |
382 | default: | |
383 | scm_wrong_type_arg (FUNC_NAME, 1, type); | |
384 | } | |
385 | } | |
3435f3c0 AW |
386 | else if (scm_is_eq (type, sym_asterisk)) |
387 | /* a pointer */ | |
388 | return scm_from_size_t (alignof (void*)); | |
9a396cbd AW |
389 | else if (scm_is_pair (type)) |
390 | /* a struct, yo */ | |
391 | return scm_alignof (scm_car (type)); | |
392 | else | |
393 | scm_wrong_type_arg (FUNC_NAME, 1, type); | |
394 | } | |
395 | #undef FUNC_NAME | |
396 | ||
b9264dc5 AW |
397 | SCM_DEFINE (scm_sizeof, "sizeof", 1, 0, 0, (SCM type), |
398 | "Return the size of @var{type}, in bytes.\n\n" | |
399 | "@var{type} should be a valid C type, like @code{int}.\n" | |
400 | "Alternately @var{type} may be the symbol @code{*}, in which\n" | |
401 | "case the size of a pointer is returned. @var{type} may also\n" | |
402 | "be a list of types, in which case the size of a @code{struct}\n" | |
403 | "with ABI-conventional packing is returned.") | |
9a396cbd AW |
404 | #define FUNC_NAME s_scm_sizeof |
405 | { | |
406 | if (SCM_I_INUMP (type)) | |
407 | { | |
408 | switch (SCM_I_INUM (type)) | |
409 | { | |
410 | case SCM_FOREIGN_TYPE_FLOAT: | |
411 | return scm_from_size_t (sizeof (float)); | |
412 | case SCM_FOREIGN_TYPE_DOUBLE: | |
413 | return scm_from_size_t (sizeof (double)); | |
414 | case SCM_FOREIGN_TYPE_UINT8: | |
415 | return scm_from_size_t (sizeof (scm_t_uint8)); | |
416 | case SCM_FOREIGN_TYPE_INT8: | |
417 | return scm_from_size_t (sizeof (scm_t_int8)); | |
418 | case SCM_FOREIGN_TYPE_UINT16: | |
419 | return scm_from_size_t (sizeof (scm_t_uint16)); | |
420 | case SCM_FOREIGN_TYPE_INT16: | |
421 | return scm_from_size_t (sizeof (scm_t_int16)); | |
422 | case SCM_FOREIGN_TYPE_UINT32: | |
423 | return scm_from_size_t (sizeof (scm_t_uint32)); | |
424 | case SCM_FOREIGN_TYPE_INT32: | |
425 | return scm_from_size_t (sizeof (scm_t_int32)); | |
426 | case SCM_FOREIGN_TYPE_UINT64: | |
427 | return scm_from_size_t (sizeof (scm_t_uint64)); | |
428 | case SCM_FOREIGN_TYPE_INT64: | |
429 | return scm_from_size_t (sizeof (scm_t_int64)); | |
430 | default: | |
431 | scm_wrong_type_arg (FUNC_NAME, 1, type); | |
432 | } | |
433 | } | |
3435f3c0 AW |
434 | else if (scm_is_eq (type, sym_asterisk)) |
435 | /* a pointer */ | |
436 | return scm_from_size_t (sizeof (void*)); | |
9a396cbd AW |
437 | else if (scm_is_pair (type)) |
438 | { | |
439 | /* a struct */ | |
440 | size_t off = 0; | |
441 | while (scm_is_pair (type)) | |
442 | { | |
443 | off = ROUND_UP (off, scm_to_size_t (scm_alignof (scm_car (type)))); | |
444 | off += scm_to_size_t (scm_sizeof (scm_car (type))); | |
445 | type = scm_cdr (type); | |
446 | } | |
447 | return scm_from_size_t (off); | |
448 | } | |
449 | else | |
450 | scm_wrong_type_arg (FUNC_NAME, 1, type); | |
451 | } | |
452 | #undef FUNC_NAME | |
453 | ||
454 | ||
d8b04f04 AW |
455 | /* return 1 on success, 0 on failure */ |
456 | static int | |
457 | parse_ffi_type (SCM type, int return_p, long *n_structs, long *n_struct_elts) | |
458 | { | |
459 | if (SCM_I_INUMP (type)) | |
460 | { | |
461 | if ((SCM_I_INUM (type) < 0 ) | |
462 | || (SCM_I_INUM (type) > SCM_FOREIGN_TYPE_LAST)) | |
463 | return 0; | |
464 | else if (SCM_I_INUM (type) == SCM_FOREIGN_TYPE_VOID && !return_p) | |
465 | return 0; | |
466 | else | |
467 | return 1; | |
468 | } | |
3435f3c0 AW |
469 | else if (scm_is_eq (type, sym_asterisk)) |
470 | /* a pointer */ | |
471 | return 1; | |
d8b04f04 AW |
472 | else |
473 | { | |
474 | long len; | |
475 | ||
476 | len = scm_ilength (type); | |
477 | if (len < 1) | |
478 | return 0; | |
479 | while (len--) | |
480 | { | |
481 | if (!parse_ffi_type (scm_car (type), 0, n_structs, n_struct_elts)) | |
482 | return 0; | |
483 | (*n_struct_elts)++; | |
484 | type = scm_cdr (type); | |
485 | } | |
486 | (*n_structs)++; | |
487 | return 1; | |
488 | } | |
489 | } | |
490 | ||
491 | static void | |
492 | fill_ffi_type (SCM type, ffi_type *ftype, ffi_type ***type_ptrs, | |
493 | ffi_type **types) | |
494 | { | |
495 | if (SCM_I_INUMP (type)) | |
496 | { | |
497 | switch (SCM_I_INUM (type)) | |
498 | { | |
499 | case SCM_FOREIGN_TYPE_FLOAT: | |
500 | *ftype = ffi_type_float; | |
501 | return; | |
502 | case SCM_FOREIGN_TYPE_DOUBLE: | |
503 | *ftype = ffi_type_double; | |
504 | return; | |
505 | case SCM_FOREIGN_TYPE_UINT8: | |
506 | *ftype = ffi_type_uint8; | |
507 | return; | |
508 | case SCM_FOREIGN_TYPE_INT8: | |
509 | *ftype = ffi_type_sint8; | |
510 | return; | |
511 | case SCM_FOREIGN_TYPE_UINT16: | |
512 | *ftype = ffi_type_uint16; | |
513 | return; | |
514 | case SCM_FOREIGN_TYPE_INT16: | |
515 | *ftype = ffi_type_sint16; | |
516 | return; | |
517 | case SCM_FOREIGN_TYPE_UINT32: | |
518 | *ftype = ffi_type_uint32; | |
519 | return; | |
520 | case SCM_FOREIGN_TYPE_INT32: | |
521 | *ftype = ffi_type_sint32; | |
522 | return; | |
523 | case SCM_FOREIGN_TYPE_UINT64: | |
524 | *ftype = ffi_type_uint64; | |
525 | return; | |
526 | case SCM_FOREIGN_TYPE_INT64: | |
527 | *ftype = ffi_type_sint64; | |
528 | return; | |
529 | case SCM_FOREIGN_TYPE_VOID: | |
530 | *ftype = ffi_type_void; | |
531 | return; | |
532 | default: | |
2ee07358 | 533 | scm_wrong_type_arg_msg ("pointer->procedure", 0, type, |
75383ddb | 534 | "foreign type"); |
d8b04f04 AW |
535 | } |
536 | } | |
3435f3c0 AW |
537 | else if (scm_is_eq (type, sym_asterisk)) |
538 | /* a pointer */ | |
539 | { | |
540 | *ftype = ffi_type_pointer; | |
541 | return; | |
542 | } | |
d8b04f04 AW |
543 | else |
544 | { | |
545 | long i, len; | |
546 | ||
547 | len = scm_ilength (type); | |
548 | ||
549 | ftype->size = 0; | |
550 | ftype->alignment = 0; | |
551 | ftype->type = FFI_TYPE_STRUCT; | |
552 | ftype->elements = *type_ptrs; | |
553 | *type_ptrs += len + 1; | |
554 | ||
555 | for (i = 0; i < len; i++) | |
556 | { | |
9a396cbd AW |
557 | ftype->elements[i] = *types; |
558 | *types += 1; | |
d8b04f04 AW |
559 | fill_ffi_type (scm_car (type), ftype->elements[i], |
560 | type_ptrs, types); | |
561 | type = scm_cdr (type); | |
562 | } | |
563 | ftype->elements[i] = NULL; | |
564 | } | |
565 | } | |
33186356 LC |
566 | |
567 | /* Return a "cif" (call interface) for the given RETURN_TYPE and | |
568 | ARG_TYPES. */ | |
569 | static ffi_cif * | |
570 | make_cif (SCM return_type, SCM arg_types, const char *caller) | |
571 | #define FUNC_NAME caller | |
d8b04f04 | 572 | { |
33186356 | 573 | SCM walk; |
d8b04f04 AW |
574 | long i, nargs, n_structs, n_struct_elts; |
575 | size_t cif_len; | |
576 | char *mem; | |
577 | ffi_cif *cif; | |
578 | ffi_type **type_ptrs; | |
579 | ffi_type *types; | |
5b46a8c2 | 580 | |
d8b04f04 AW |
581 | nargs = scm_ilength (arg_types); |
582 | SCM_ASSERT (nargs >= 0, arg_types, 3, FUNC_NAME); | |
583 | /* fixme: assert nargs < 1<<32 */ | |
584 | n_structs = n_struct_elts = 0; | |
585 | ||
586 | /* For want of talloc, we're going to have to do this in two passes: first we | |
587 | figure out how much memory is needed for all types, then we allocate the | |
588 | cif and the types all in one block. */ | |
589 | if (!parse_ffi_type (return_type, 1, &n_structs, &n_struct_elts)) | |
590 | scm_wrong_type_arg (FUNC_NAME, 1, return_type); | |
591 | for (walk = arg_types; scm_is_pair (walk); walk = scm_cdr (walk)) | |
592 | if (!parse_ffi_type (scm_car (walk), 0, &n_structs, &n_struct_elts)) | |
593 | scm_wrong_type_arg (FUNC_NAME, 3, scm_car (walk)); | |
33186356 | 594 | |
d8b04f04 AW |
595 | /* the memory: with space for the cif itself */ |
596 | cif_len = sizeof (ffi_cif); | |
597 | ||
598 | /* then ffi_type pointers: one for each arg, one for each struct | |
599 | element, and one for each struct (for null-termination) */ | |
600 | cif_len = (ROUND_UP (cif_len, alignof(void*)) | |
33186356 LC |
601 | + (nargs + n_structs + n_struct_elts)*sizeof(void*)); |
602 | ||
d8b04f04 AW |
603 | /* then the ffi_type structs themselves, one per arg and struct element, and |
604 | one for the return val */ | |
605 | cif_len = (ROUND_UP (cif_len, alignof(ffi_type)) | |
33186356 | 606 | + (nargs + n_struct_elts + 1)*sizeof(ffi_type)); |
087aa6aa LC |
607 | |
608 | mem = scm_gc_malloc_pointerless (cif_len, "foreign"); | |
3ef6650d AW |
609 | /* ensure all the memory is initialized, even the holes */ |
610 | memset (mem, 0, cif_len); | |
087aa6aa LC |
611 | cif = (ffi_cif *) mem; |
612 | ||
d8b04f04 AW |
613 | /* reuse cif_len to walk through the mem */ |
614 | cif_len = ROUND_UP (sizeof (ffi_cif), alignof(void*)); | |
615 | type_ptrs = (ffi_type**)(mem + cif_len); | |
616 | cif_len = ROUND_UP (cif_len | |
33186356 LC |
617 | + (nargs + n_structs + n_struct_elts)*sizeof(void*), |
618 | alignof(ffi_type)); | |
d8b04f04 | 619 | types = (ffi_type*)(mem + cif_len); |
33186356 | 620 | |
d8b04f04 AW |
621 | /* whew. now knit the pointers together. */ |
622 | cif->rtype = types++; | |
623 | fill_ffi_type (return_type, cif->rtype, &type_ptrs, &types); | |
624 | cif->arg_types = type_ptrs; | |
625 | type_ptrs += nargs; | |
626 | for (walk = arg_types, i = 0; scm_is_pair (walk); walk = scm_cdr (walk), i++) | |
627 | { | |
628 | cif->arg_types[i] = types++; | |
629 | fill_ffi_type (scm_car (walk), cif->arg_types[i], &type_ptrs, &types); | |
630 | } | |
631 | ||
632 | /* round out the cif, and we're done. */ | |
633 | cif->abi = FFI_DEFAULT_ABI; | |
634 | cif->nargs = nargs; | |
635 | cif->bytes = 0; | |
636 | cif->flags = 0; | |
33186356 | 637 | |
d8b04f04 | 638 | if (FFI_OK != ffi_prep_cif (cif, FFI_DEFAULT_ABI, cif->nargs, cif->rtype, |
33186356 LC |
639 | cif->arg_types)) |
640 | SCM_MISC_ERROR ("ffi_prep_cif failed", SCM_EOL); | |
641 | ||
642 | return cif; | |
643 | } | |
644 | #undef FUNC_NAME | |
645 | ||
2ee07358 | 646 | SCM_DEFINE (scm_pointer_to_procedure, "pointer->procedure", 3, 0, 0, |
33186356 LC |
647 | (SCM return_type, SCM func_ptr, SCM arg_types), |
648 | "Make a foreign function.\n\n" | |
649 | "Given the foreign void pointer @var{func_ptr}, its argument and\n" | |
650 | "return types @var{arg_types} and @var{return_type}, return a\n" | |
651 | "procedure that will pass arguments to the foreign function\n" | |
652 | "and return appropriate values.\n\n" | |
653 | "@var{arg_types} should be a list of foreign types.\n" | |
654 | "@code{return_type} should be a foreign type.") | |
2ee07358 | 655 | #define FUNC_NAME s_scm_pointer_to_procedure |
33186356 LC |
656 | { |
657 | ffi_cif *cif; | |
658 | ||
659 | SCM_VALIDATE_POINTER (2, func_ptr); | |
d8b04f04 | 660 | |
33186356 LC |
661 | cif = make_cif (return_type, arg_types, FUNC_NAME); |
662 | ||
663 | return cif_to_procedure (scm_from_pointer (cif, NULL), func_ptr); | |
d8b04f04 AW |
664 | } |
665 | #undef FUNC_NAME | |
666 | ||
667 | \f | |
668 | ||
669 | /* Pre-generate trampolines for less than 10 arguments. */ | |
670 | ||
671 | #ifdef WORDS_BIGENDIAN | |
672 | #define OBJCODE_HEADER 0, 0, 0, 8, 0, 0, 0, 40 | |
673 | #define META_HEADER 0, 0, 0, 32, 0, 0, 0, 0 | |
674 | #else | |
675 | #define OBJCODE_HEADER 8, 0, 0, 0, 40, 0, 0, 0 | |
676 | #define META_HEADER 32, 0, 0, 0, 0, 0, 0, 0 | |
677 | #endif | |
678 | ||
679 | #define CODE(nreq) \ | |
680 | OBJCODE_HEADER, \ | |
681 | /* 0 */ scm_op_assert_nargs_ee, 0, nreq, /* assert number of args */ \ | |
682 | /* 3 */ scm_op_object_ref, 0, /* push the pair with the cif and the function pointer */ \ | |
683 | /* 5 */ scm_op_foreign_call, nreq, /* and call (will return value as well) */ \ | |
684 | /* 7 */ scm_op_nop, \ | |
685 | /* 8 */ META (3, 7, nreq) | |
686 | ||
687 | #define META(start, end, nreq) \ | |
688 | META_HEADER, \ | |
689 | /* 0 */ scm_op_make_eol, /* bindings */ \ | |
690 | /* 1 */ scm_op_make_eol, /* sources */ \ | |
691 | /* 2 */ scm_op_make_int8, start, scm_op_make_int8, end, /* arity: from ip N to ip N */ \ | |
692 | /* 6 */ scm_op_make_int8, nreq, /* the arity is N required args */ \ | |
693 | /* 8 */ scm_op_list, 0, 3, /* make a list of those 3 vals */ \ | |
694 | /* 11 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */ \ | |
695 | /* 14 */ scm_op_load_symbol, 0, 0, 4, 'n', 'a', 'm', 'e', /* `name' */ \ | |
696 | /* 22 */ scm_op_object_ref, 1, /* the name from the object table */ \ | |
697 | /* 24 */ scm_op_cons, /* make a pair for the properties */ \ | |
698 | /* 25 */ scm_op_list, 0, 4, /* pack bindings, sources, and arities into list */ \ | |
699 | /* 28 */ scm_op_return, /* and return */ \ | |
700 | /* 29 */ scm_op_nop, scm_op_nop, scm_op_nop \ | |
701 | /* 32 */ | |
702 | ||
703 | static const struct | |
704 | { | |
705 | scm_t_uint64 dummy; /* ensure 8-byte alignment; perhaps there's a better way */ | |
706 | const scm_t_uint8 bytes[10 * (sizeof (struct scm_objcode) + 8 | |
707 | + sizeof (struct scm_objcode) + 32)]; | |
708 | } raw_bytecode = { | |
709 | 0, | |
710 | { | |
711 | CODE (0), CODE (1), CODE (2), CODE (3), CODE (4), | |
712 | CODE (5), CODE (6), CODE (7), CODE (8), CODE (9) | |
713 | } | |
714 | }; | |
715 | ||
716 | #undef CODE | |
717 | #undef META | |
718 | #undef OBJCODE_HEADER | |
719 | #undef META_HEADER | |
720 | ||
721 | /* | |
722 | (defun generate-objcode-cells (n) | |
723 | "Generate objcode cells for up to N arguments" | |
724 | (interactive "p") | |
725 | (let ((i 0)) | |
726 | (while (< i n) | |
727 | (insert | |
728 | (format " { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + %d) },\n" | |
729 | (* (+ 4 4 8 4 4 32) i))) | |
730 | (insert " { SCM_BOOL_F, SCM_PACK (0) },\n") | |
731 | (setq i (1+ i))))) | |
732 | */ | |
733 | #define STATIC_OBJCODE_TAG \ | |
f9654187 | 734 | SCM_PACK (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0)) |
d8b04f04 AW |
735 | |
736 | static const struct | |
737 | { | |
738 | scm_t_uint64 dummy; /* alignment */ | |
739 | scm_t_cell cells[10 * 2]; /* 10 double cells */ | |
740 | } objcode_cells = { | |
741 | 0, | |
742 | /* C-u 1 0 M-x generate-objcode-cells RET */ | |
743 | { | |
744 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 0) }, | |
745 | { SCM_BOOL_F, SCM_PACK (0) }, | |
746 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 56) }, | |
747 | { SCM_BOOL_F, SCM_PACK (0) }, | |
748 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 112) }, | |
749 | { SCM_BOOL_F, SCM_PACK (0) }, | |
750 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 168) }, | |
751 | { SCM_BOOL_F, SCM_PACK (0) }, | |
752 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 224) }, | |
753 | { SCM_BOOL_F, SCM_PACK (0) }, | |
754 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 280) }, | |
755 | { SCM_BOOL_F, SCM_PACK (0) }, | |
756 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 336) }, | |
757 | { SCM_BOOL_F, SCM_PACK (0) }, | |
758 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 392) }, | |
759 | { SCM_BOOL_F, SCM_PACK (0) }, | |
760 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 448) }, | |
761 | { SCM_BOOL_F, SCM_PACK (0) }, | |
762 | { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 504) }, | |
763 | { SCM_BOOL_F, SCM_PACK (0) } | |
764 | } | |
765 | }; | |
766 | ||
767 | static const SCM objcode_trampolines[10] = { | |
768 | SCM_PACK (objcode_cells.cells+0), | |
769 | SCM_PACK (objcode_cells.cells+2), | |
770 | SCM_PACK (objcode_cells.cells+4), | |
771 | SCM_PACK (objcode_cells.cells+6), | |
772 | SCM_PACK (objcode_cells.cells+8), | |
773 | SCM_PACK (objcode_cells.cells+10), | |
774 | SCM_PACK (objcode_cells.cells+12), | |
775 | SCM_PACK (objcode_cells.cells+14), | |
776 | SCM_PACK (objcode_cells.cells+16), | |
777 | SCM_PACK (objcode_cells.cells+18), | |
778 | }; | |
779 | ||
780 | static SCM | |
781 | cif_to_procedure (SCM cif, SCM func_ptr) | |
782 | { | |
d4149a51 LC |
783 | ffi_cif *c_cif; |
784 | unsigned int nargs; | |
d8b04f04 | 785 | SCM objcode, table, ret; |
d4149a51 | 786 | |
5b46a8c2 | 787 | c_cif = (ffi_cif *) SCM_POINTER_VALUE (cif); |
d4149a51 LC |
788 | nargs = c_cif->nargs; |
789 | ||
d8b04f04 AW |
790 | if (nargs < 10) |
791 | objcode = objcode_trampolines[nargs]; | |
792 | else | |
75383ddb AW |
793 | scm_misc_error ("make-foreign-function", "args >= 10 currently unimplemented", |
794 | SCM_EOL); | |
d8b04f04 AW |
795 | |
796 | table = scm_c_make_vector (2, SCM_UNDEFINED); | |
797 | SCM_SIMPLE_VECTOR_SET (table, 0, scm_cons (cif, func_ptr)); | |
798 | SCM_SIMPLE_VECTOR_SET (table, 1, SCM_BOOL_F); /* name */ | |
799 | ret = scm_make_program (objcode, table, SCM_BOOL_F); | |
800 | ||
801 | return ret; | |
802 | } | |
803 | ||
165a8643 | 804 | /* Set *LOC to the foreign representation of X with TYPE. */ |
4d9130a5 | 805 | static void |
165a8643 | 806 | unpack (const ffi_type *type, void *loc, SCM x) |
4d9130a5 AW |
807 | { |
808 | switch (type->type) | |
809 | { | |
810 | case FFI_TYPE_FLOAT: | |
165a8643 | 811 | *(float *) loc = scm_to_double (x); |
4d9130a5 AW |
812 | break; |
813 | case FFI_TYPE_DOUBLE: | |
165a8643 | 814 | *(double *) loc = scm_to_double (x); |
4d9130a5 AW |
815 | break; |
816 | case FFI_TYPE_UINT8: | |
165a8643 | 817 | *(scm_t_uint8 *) loc = scm_to_uint8 (x); |
4d9130a5 AW |
818 | break; |
819 | case FFI_TYPE_SINT8: | |
165a8643 | 820 | *(scm_t_int8 *) loc = scm_to_int8 (x); |
4d9130a5 AW |
821 | break; |
822 | case FFI_TYPE_UINT16: | |
165a8643 | 823 | *(scm_t_uint16 *) loc = scm_to_uint16 (x); |
4d9130a5 AW |
824 | break; |
825 | case FFI_TYPE_SINT16: | |
165a8643 | 826 | *(scm_t_int16 *) loc = scm_to_int16 (x); |
4d9130a5 AW |
827 | break; |
828 | case FFI_TYPE_UINT32: | |
165a8643 | 829 | *(scm_t_uint32 *) loc = scm_to_uint32 (x); |
4d9130a5 AW |
830 | break; |
831 | case FFI_TYPE_SINT32: | |
165a8643 | 832 | *(scm_t_int32 *) loc = scm_to_int32 (x); |
4d9130a5 AW |
833 | break; |
834 | case FFI_TYPE_UINT64: | |
165a8643 | 835 | *(scm_t_uint64 *) loc = scm_to_uint64 (x); |
4d9130a5 AW |
836 | break; |
837 | case FFI_TYPE_SINT64: | |
165a8643 | 838 | *(scm_t_int64 *) loc = scm_to_int64 (x); |
4d9130a5 AW |
839 | break; |
840 | case FFI_TYPE_STRUCT: | |
5b46a8c2 | 841 | memcpy (loc, SCM_POINTER_VALUE (x), type->size); |
4d9130a5 AW |
842 | break; |
843 | case FFI_TYPE_POINTER: | |
5b46a8c2 | 844 | *(void **) loc = SCM_POINTER_VALUE (x); |
4d9130a5 AW |
845 | break; |
846 | default: | |
847 | abort (); | |
848 | } | |
849 | } | |
850 | ||
165a8643 | 851 | /* Return a Scheme representation of the foreign value at LOC of type TYPE. */ |
4d9130a5 | 852 | static SCM |
165a8643 | 853 | pack (const ffi_type * type, const void *loc) |
4d9130a5 AW |
854 | { |
855 | switch (type->type) | |
856 | { | |
857 | case FFI_TYPE_VOID: | |
858 | return SCM_UNSPECIFIED; | |
859 | case FFI_TYPE_FLOAT: | |
165a8643 | 860 | return scm_from_double (*(float *) loc); |
4d9130a5 | 861 | case FFI_TYPE_DOUBLE: |
165a8643 | 862 | return scm_from_double (*(double *) loc); |
4d9130a5 | 863 | case FFI_TYPE_UINT8: |
165a8643 | 864 | return scm_from_uint8 (*(scm_t_uint8 *) loc); |
4d9130a5 | 865 | case FFI_TYPE_SINT8: |
165a8643 | 866 | return scm_from_int8 (*(scm_t_int8 *) loc); |
4d9130a5 | 867 | case FFI_TYPE_UINT16: |
165a8643 | 868 | return scm_from_uint16 (*(scm_t_uint16 *) loc); |
4d9130a5 | 869 | case FFI_TYPE_SINT16: |
165a8643 | 870 | return scm_from_int16 (*(scm_t_int16 *) loc); |
4d9130a5 | 871 | case FFI_TYPE_UINT32: |
165a8643 | 872 | return scm_from_uint32 (*(scm_t_uint32 *) loc); |
4d9130a5 | 873 | case FFI_TYPE_SINT32: |
165a8643 | 874 | return scm_from_int32 (*(scm_t_int32 *) loc); |
4d9130a5 | 875 | case FFI_TYPE_UINT64: |
165a8643 | 876 | return scm_from_uint64 (*(scm_t_uint64 *) loc); |
4d9130a5 | 877 | case FFI_TYPE_SINT64: |
165a8643 | 878 | return scm_from_int64 (*(scm_t_int64 *) loc); |
4d9130a5 AW |
879 | case FFI_TYPE_STRUCT: |
880 | { | |
165a8643 LC |
881 | void *mem = scm_gc_malloc_pointerless (type->size, "foreign"); |
882 | memcpy (mem, loc, type->size); | |
5b46a8c2 | 883 | return scm_from_pointer (mem, NULL); |
4d9130a5 AW |
884 | } |
885 | case FFI_TYPE_POINTER: | |
5b46a8c2 | 886 | return scm_from_pointer (*(void **) loc, NULL); |
4d9130a5 AW |
887 | default: |
888 | abort (); | |
889 | } | |
890 | } | |
891 | ||
165a8643 | 892 | |
4d9130a5 | 893 | SCM |
165a8643 | 894 | scm_i_foreign_call (SCM foreign, const SCM *argv) |
4d9130a5 AW |
895 | { |
896 | /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the | |
897 | objtable. */ | |
898 | ffi_cif *cif; | |
b577bc90 | 899 | void (*func) (void); |
4d9130a5 AW |
900 | scm_t_uint8 *data; |
901 | void *rvalue; | |
902 | void **args; | |
903 | unsigned i; | |
a2c69049 | 904 | size_t arg_size; |
4d9130a5 AW |
905 | scm_t_ptrdiff off; |
906 | ||
5b46a8c2 LC |
907 | cif = SCM_POINTER_VALUE (SCM_CAR (foreign)); |
908 | func = SCM_POINTER_VALUE (SCM_CDR (foreign)); | |
a2c69049 LC |
909 | |
910 | /* Argument pointers. */ | |
b577bc90 | 911 | args = alloca (sizeof (void *) * cif->nargs); |
a2c69049 | 912 | |
86425e26 LC |
913 | /* Compute the worst-case amount of memory needed to store all the argument |
914 | values. Note: as of libffi 3.0.9 `cif->bytes' is undocumented and is zero, | |
915 | so it can't be used for that purpose. */ | |
916 | for (i = 0, arg_size = 0; i < cif->nargs; i++) | |
917 | arg_size += cif->arg_types[i]->size + cif->arg_types[i]->alignment - 1; | |
a2c69049 LC |
918 | |
919 | /* Space for argument values, followed by return value. */ | |
86425e26 LC |
920 | data = alloca (arg_size + cif->rtype->size |
921 | + max (sizeof (void *), cif->rtype->alignment)); | |
a2c69049 | 922 | |
165a8643 LC |
923 | /* Unpack ARGV to native values, setting ARGV pointers. */ |
924 | for (i = 0, off = 0; | |
925 | i < cif->nargs; | |
86425e26 LC |
926 | off = (scm_t_uint8 *) args[i] - data + cif->arg_types[i]->size, |
927 | i++) | |
4d9130a5 | 928 | { |
86425e26 LC |
929 | /* Suitably align the storage area for argument I. */ |
930 | args[i] = (void *) ROUND_UP ((scm_t_uintptr) data + off, | |
931 | cif->arg_types[i]->alignment); | |
932 | assert ((scm_t_uintptr) args[i] % cif->arg_types[i]->alignment == 0); | |
4d9130a5 | 933 | unpack (cif->arg_types[i], args[i], argv[i]); |
4d9130a5 | 934 | } |
165a8643 | 935 | |
86425e26 LC |
936 | /* Prepare space for the return value. On some platforms, such as |
937 | `armv5tel-*-linux-gnueabi', the return value has to be at least | |
938 | word-aligned, even if its type doesn't have any alignment requirement as is | |
939 | the case with `char'. */ | |
940 | rvalue = (void *) ROUND_UP ((scm_t_uintptr) data + off, | |
941 | max (sizeof (void *), cif->rtype->alignment)); | |
4d9130a5 AW |
942 | |
943 | /* off we go! */ | |
944 | ffi_call (cif, func, rvalue, args); | |
945 | ||
946 | return pack (cif->rtype, rvalue); | |
947 | } | |
948 | ||
d8b04f04 | 949 | \f |
33186356 LC |
950 | /* Function pointers aka. "callbacks" or "closures". */ |
951 | ||
952 | #ifdef FFI_CLOSURES | |
953 | ||
954 | /* Trampoline to invoke a libffi closure that wraps a Scheme | |
955 | procedure. */ | |
956 | static void | |
957 | invoke_closure (ffi_cif *cif, void *ret, void **args, void *data) | |
958 | { | |
959 | size_t i; | |
960 | SCM proc, *argv, result; | |
961 | ||
962 | proc = PTR2SCM (data); | |
963 | ||
964 | argv = alloca (cif->nargs * sizeof (*argv)); | |
965 | ||
966 | /* Pack ARGS to SCM values, setting ARGV pointers. */ | |
967 | for (i = 0; i < cif->nargs; i++) | |
968 | argv[i] = pack (cif->arg_types[i], args[i]); | |
969 | ||
970 | result = scm_call_n (proc, argv, cif->nargs); | |
971 | ||
972 | unpack (cif->rtype, ret, result); | |
973 | } | |
974 | ||
975 | SCM_DEFINE (scm_procedure_to_pointer, "procedure->pointer", 3, 0, 0, | |
976 | (SCM return_type, SCM proc, SCM arg_types), | |
977 | "Return a pointer to a C function of type @var{return-type}\n" | |
978 | "taking arguments of types @var{arg-types} (a list) and\n" | |
979 | "behaving as a proxy to procedure @var{proc}. Thus\n" | |
980 | "@var{proc}'s arity, supported argument types, and return\n" | |
981 | "type should match @var{return-type} and @var{arg-types}.\n") | |
982 | #define FUNC_NAME s_scm_procedure_to_pointer | |
983 | { | |
984 | SCM pointer; | |
985 | ffi_cif *cif; | |
986 | ffi_status err; | |
987 | void *closure, *executable; | |
988 | ||
989 | cif = make_cif (return_type, arg_types, FUNC_NAME); | |
990 | ||
991 | closure = ffi_closure_alloc (sizeof (ffi_closure), &executable); | |
992 | err = ffi_prep_closure_loc ((ffi_closure *) closure, cif, | |
993 | invoke_closure, SCM2PTR (proc), | |
994 | executable); | |
995 | if (err != FFI_OK) | |
996 | { | |
997 | ffi_closure_free (closure); | |
998 | SCM_MISC_ERROR ("`ffi_prep_closure_loc' failed", SCM_EOL); | |
999 | } | |
1000 | ||
1001 | if (closure == executable) | |
1002 | pointer = scm_from_pointer (executable, ffi_closure_free); | |
1003 | else | |
1004 | { | |
1005 | /* CLOSURE needs to be freed eventually. However, since | |
1006 | `GC_all_interior_pointers' is disabled, we can't just register | |
1007 | a finalizer for CLOSURE. Instead, we create a pointer object | |
1008 | for CLOSURE, with a finalizer, and register it as a weak | |
1009 | reference of POINTER. */ | |
1010 | SCM friend; | |
1011 | ||
1012 | pointer = scm_from_pointer (executable, NULL); | |
1013 | friend = scm_from_pointer (closure, ffi_closure_free); | |
1014 | ||
1015 | register_weak_reference (pointer, friend); | |
1016 | } | |
1017 | ||
1018 | return pointer; | |
1019 | } | |
1020 | #undef FUNC_NAME | |
1021 | ||
1022 | #endif /* FFI_CLOSURES */ | |
1023 | ||
1024 | \f | |
d8b04f04 | 1025 | |
ab4779ff | 1026 | static void |
e2c2a699 AW |
1027 | scm_init_foreign (void) |
1028 | { | |
1029 | #ifndef SCM_MAGIC_SNARFER | |
1030 | #include "libguile/foreign.x" | |
1031 | #endif | |
ab4779ff AW |
1032 | scm_define (sym_void, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID)); |
1033 | scm_define (sym_float, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT)); | |
1034 | scm_define (sym_double, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE)); | |
1035 | scm_define (sym_uint8, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8)); | |
1036 | scm_define (sym_int8, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8)); | |
1037 | scm_define (sym_uint16, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16)); | |
1038 | scm_define (sym_int16, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16)); | |
1039 | scm_define (sym_uint32, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)); | |
1040 | scm_define (sym_int32, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)); | |
1041 | scm_define (sym_uint64, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)); | |
1042 | scm_define (sym_int64, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)); | |
dd1464bf | 1043 | |
42f7c01e LC |
1044 | scm_define (sym_short, |
1045 | #if SIZEOF_SHORT == 8 | |
1046 | scm_from_uint8 (SCM_FOREIGN_TYPE_INT64) | |
1047 | #elif SIZEOF_SHORT == 4 | |
1048 | scm_from_uint8 (SCM_FOREIGN_TYPE_INT32) | |
1049 | #elif SIZEOF_SHORT == 2 | |
1050 | scm_from_uint8 (SCM_FOREIGN_TYPE_INT16) | |
1051 | #else | |
1052 | # error unsupported sizeof (short) | |
1053 | #endif | |
1054 | ); | |
1055 | ||
1056 | scm_define (sym_unsigned_short, | |
1057 | #if SIZEOF_SHORT == 8 | |
1058 | scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64) | |
1059 | #elif SIZEOF_SHORT == 4 | |
1060 | scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32) | |
1061 | #elif SIZEOF_SHORT == 2 | |
1062 | scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16) | |
1063 | #else | |
1064 | # error unsupported sizeof (short) | |
1065 | #endif | |
1066 | ); | |
1067 | ||
dd1464bf LC |
1068 | scm_define (sym_int, |
1069 | #if SIZEOF_INT == 8 | |
1070 | scm_from_uint8 (SCM_FOREIGN_TYPE_INT64) | |
1071 | #elif SIZEOF_INT == 4 | |
1072 | scm_from_uint8 (SCM_FOREIGN_TYPE_INT32) | |
1073 | #else | |
1074 | # error unsupported sizeof (int) | |
1075 | #endif | |
1076 | ); | |
1077 | ||
1078 | scm_define (sym_unsigned_int, | |
1079 | #if SIZEOF_UNSIGNED_INT == 8 | |
1080 | scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64) | |
1081 | #elif SIZEOF_UNSIGNED_INT == 4 | |
1082 | scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32) | |
1083 | #else | |
1084 | # error unsupported sizeof (unsigned int) | |
1085 | #endif | |
1086 | ); | |
1087 | ||
1088 | scm_define (sym_long, | |
1089 | #if SIZEOF_LONG == 8 | |
1090 | scm_from_uint8 (SCM_FOREIGN_TYPE_INT64) | |
1091 | #elif SIZEOF_LONG == 4 | |
1092 | scm_from_uint8 (SCM_FOREIGN_TYPE_INT32) | |
1093 | #else | |
1094 | # error unsupported sizeof (long) | |
1095 | #endif | |
1096 | ); | |
1097 | ||
1098 | scm_define (sym_unsigned_long, | |
1099 | #if SIZEOF_UNSIGNED_LONG == 8 | |
1100 | scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64) | |
1101 | #elif SIZEOF_UNSIGNED_LONG == 4 | |
1102 | scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32) | |
1103 | #else | |
1104 | # error unsupported sizeof (unsigned long) | |
1105 | #endif | |
1106 | ); | |
1107 | ||
1108 | scm_define (sym_size_t, | |
1109 | #if SIZEOF_SIZE_T == 8 | |
1110 | scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64) | |
1111 | #elif SIZEOF_SIZE_T == 4 | |
1112 | scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32) | |
1113 | #else | |
1114 | # error unsupported sizeof (size_t) | |
1115 | #endif | |
1116 | ); | |
54eb59cf | 1117 | |
5b46a8c2 | 1118 | null_pointer = scm_cell (scm_tc7_pointer, 0); |
3e5ea35c | 1119 | scm_define (sym_null, null_pointer); |
ab4779ff AW |
1120 | } |
1121 | ||
1122 | void | |
1123 | scm_register_foreign (void) | |
1124 | { | |
44602b08 AW |
1125 | scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, |
1126 | "scm_init_foreign", | |
ab4779ff AW |
1127 | (scm_t_extension_init_func)scm_init_foreign, |
1128 | NULL); | |
5b46a8c2 | 1129 | pointer_weak_refs = scm_make_weak_key_hash_table (SCM_UNDEFINED); |
e2c2a699 AW |
1130 | } |
1131 | ||
1132 | /* | |
1133 | Local Variables: | |
1134 | c-file-style: "gnu" | |
1135 | End: | |
1136 | */ |