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