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