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