| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* ffi.d - user defined data types and foreign functions interface |
| 6 |
* |
| 7 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 8 |
* |
| 9 |
* See file 'LICENSE' for the copyright details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <string.h> |
| 14 |
#define ECL_INCLUDE_FFI_H |
| 15 |
#include <ecl/ecl.h> |
| 16 |
#include <ecl/internal.h> |
| 17 |
|
| 18 |
static const cl_object ecl_aet_to_ffi_table[ecl_aet_bc+1] = { |
| 19 |
@':void', /* ecl_aet_object */ |
| 20 |
@':float', /* ecl_aet_sf */ |
| 21 |
@':double', /* ecl_aet_df */ |
| 22 |
@':long-double', /* ecl_aet_lf */ |
| 23 |
#ifdef ECL_COMPLEX_FLOAT |
| 24 |
@':csfloat', /* ecl_aet_csf */ |
| 25 |
@':cdfloat', /* ecl_aet_cdf */ |
| 26 |
@':clfloat', /* ecl_aet_clf */ |
| 27 |
#endif |
| 28 |
@':void', /* ecl_aet_bit */ |
| 29 |
#if ECL_FIXNUM_BITS == 32 && defined(ecl_uint32_t) |
| 30 |
@':int32-t', /* ecl_aet_fix */ |
| 31 |
@':uint32-t', /* ecl_aet_index */ |
| 32 |
#else |
| 33 |
# if ECL_FIXNUM_BITS == 64 && defined(ecl_uint64_t) |
| 34 |
@':int64-t', /* ecl_aet_fix */ |
| 35 |
@':uint64-t', /* ecl_aet_index */ |
| 36 |
# else |
| 37 |
@':void', /* ecl_aet_fix */ |
| 38 |
@':void', /* ecl_aet_index */ |
| 39 |
# endif |
| 40 |
#endif |
| 41 |
@':uint8-t', /* ecl_aet_b8 */ |
| 42 |
@':int8-t', /* ecl_aet_i8 */ |
| 43 |
#ifdef ecl_uint16_t |
| 44 |
@':uint16-t', /* ecl_aet_b16 */ |
| 45 |
@':int16-t', /* ecl_aet_i16 */ |
| 46 |
#endif |
| 47 |
#ifdef ecl_uint32_t |
| 48 |
@':uint32-t', /* ecl_aet_b32 */ |
| 49 |
@':int32-t', /* ecl_aet_i32 */ |
| 50 |
#endif |
| 51 |
#ifdef ecl_uint64_t |
| 52 |
@':uint64-t', /* ecl_aet_b64 */ |
| 53 |
@':int64-t', /* ecl_aet_i64 */ |
| 54 |
#endif |
| 55 |
#ifdef ECL_UNICODE |
| 56 |
# ifdef ecl_int32_t |
| 57 |
@':int32-t', /* ecl_aet_ch */ |
| 58 |
# else |
| 59 |
@':void', /* ecl_aet_ch */ |
| 60 |
# endif |
| 61 |
#endif |
| 62 |
@':char' /* ecl_aet_bc */ |
| 63 |
}; |
| 64 |
|
| 65 |
#define AUX_PTR(type) \ |
| 66 |
((struct { char a[1]; union { type c[1]; char d[sizeof(type)]; } b; } *)0) |
| 67 |
#ifdef __GNUC__ |
| 68 |
typedef struct { |
| 69 |
cl_object name; |
| 70 |
cl_index size; |
| 71 |
cl_index alignment; |
| 72 |
} ecl_foreign_type_record; |
| 73 |
# define ALIGNMENT(tag) (ecl_foreign_type_table[tag].alignment) |
| 74 |
# define FFI_DESC(symbol,type) \ |
| 75 |
{symbol, sizeof(type), (AUX_PTR(type)->b.d - AUX_PTR(type)->a)} |
| 76 |
#else |
| 77 |
typedef struct { |
| 78 |
cl_object name; |
| 79 |
cl_index size; |
| 80 |
char *d, *a; |
| 81 |
} ecl_foreign_type_record; |
| 82 |
#define ALIGNMENT(tag) (ecl_foreign_type_table[tag].d - ecl_foreign_type_table[tag].a) |
| 83 |
#define AUX_PTR(type) \ |
| 84 |
((struct { char a[1]; union { type c[1]; char d[sizeof(type)]; } b; } *)0) |
| 85 |
#define FFI_DESC(symbol,type) \ |
| 86 |
{symbol, sizeof(type), AUX_PTR(type)->b.d, AUX_PTR(type)->a} |
| 87 |
#endif |
| 88 |
|
| 89 |
static const ecl_foreign_type_record |
| 90 |
ecl_foreign_type_table[] = { |
| 91 |
FFI_DESC(@':char', char), |
| 92 |
FFI_DESC(@':unsigned-char', unsigned char), |
| 93 |
FFI_DESC(@':byte', ecl_int8_t), |
| 94 |
FFI_DESC(@':unsigned-byte', ecl_uint8_t), |
| 95 |
FFI_DESC(@':short', short), |
| 96 |
FFI_DESC(@':unsigned-short', unsigned short), |
| 97 |
FFI_DESC(@':int', int), |
| 98 |
FFI_DESC(@':unsigned-int', unsigned int), |
| 99 |
FFI_DESC(@':long', long), |
| 100 |
FFI_DESC(@':unsigned-long', unsigned long), |
| 101 |
#ifdef ecl_uint8_t |
| 102 |
FFI_DESC(@':int8-t', ecl_int8_t), |
| 103 |
FFI_DESC(@':uint8-t', ecl_uint8_t), |
| 104 |
#endif |
| 105 |
#ifdef ecl_uint16_t |
| 106 |
FFI_DESC(@':int16-t', ecl_int16_t), |
| 107 |
FFI_DESC(@':uint16-t', ecl_uint16_t), |
| 108 |
#endif |
| 109 |
#ifdef ecl_uint32_t |
| 110 |
FFI_DESC(@':int32-t', ecl_int32_t), |
| 111 |
FFI_DESC(@':uint32-t', ecl_uint32_t), |
| 112 |
#endif |
| 113 |
#ifdef ecl_uint64_t |
| 114 |
FFI_DESC(@':int64-t', ecl_int64_t), |
| 115 |
FFI_DESC(@':uint64-t', ecl_uint64_t), |
| 116 |
#endif |
| 117 |
#ifdef ecl_long_long_t |
| 118 |
FFI_DESC(@':long-long', long long), |
| 119 |
FFI_DESC(@':unsigned-long-long', unsigned long long), |
| 120 |
#endif |
| 121 |
FFI_DESC(@':pointer-void', void *), |
| 122 |
FFI_DESC(@':cstring', char *), |
| 123 |
FFI_DESC(@':object', cl_object), |
| 124 |
FFI_DESC(@':float', float), |
| 125 |
FFI_DESC(@':double', double), |
| 126 |
FFI_DESC(@':long-double', long double), |
| 127 |
#ifdef ECL_COMPLEX_FLOAT |
| 128 |
FFI_DESC(@':csfloat', _Complex float), |
| 129 |
FFI_DESC(@':cdfloat', _Complex double), |
| 130 |
FFI_DESC(@':clfloat', _Complex long double), |
| 131 |
#endif |
| 132 |
{@':void', 0, 0} |
| 133 |
}; |
| 134 |
|
| 135 |
#ifdef HAVE_LIBFFI |
| 136 |
|
| 137 |
/* FIXME libffi does not define long long. */ |
| 138 |
# ifndef ffi_type_slonglong |
| 139 |
# define ffi_type_slonglong ffi_type_sint64 |
| 140 |
# define ffi_type_ulonglong ffi_type_uint64 |
| 141 |
# endif |
| 142 |
|
| 143 |
static struct { |
| 144 |
const cl_object symbol; |
| 145 |
ffi_abi abi; |
| 146 |
} ecl_foreign_cc_table[] = { |
| 147 |
{@':default', FFI_DEFAULT_ABI}, |
| 148 |
#ifdef FFI_MS_CDECL |
| 149 |
{@':cdecl', FFI_MS_CDECL} |
| 150 |
#elif defined(FFI_SYSV) |
| 151 |
{@':cdecl', FFI_SYSV} |
| 152 |
#endif |
| 153 |
#ifdef FFI_EABI |
| 154 |
{@':eabi', FFI_EABI} |
| 155 |
#endif |
| 156 |
#ifdef FFI_GNUW64 |
| 157 |
{@':gnuw64', FFI_GNUW64} |
| 158 |
#endif |
| 159 |
#ifdef FFI_STDCALL |
| 160 |
{@':stdcall', FFI_STDCALL} |
| 161 |
#endif |
| 162 |
#ifdef FFI_SYSV |
| 163 |
{@':sysv', FFI_SYSV} |
| 164 |
#endif |
| 165 |
#ifdef FFI_UNIX |
| 166 |
{@':unix', FFI_UNIX} |
| 167 |
#endif |
| 168 |
#ifdef FFI_UNIX64 |
| 169 |
{@':unix64', FFI_UNIX64} |
| 170 |
#endif |
| 171 |
#ifdef FFI_VFP |
| 172 |
{@':vfp', FFI_VFP} |
| 173 |
#endif |
| 174 |
#ifdef FFI_WIN64 |
| 175 |
{@':win64', FFI_WIN64} |
| 176 |
#endif |
| 177 |
{NULL, 0} |
| 178 |
}; |
| 179 |
|
| 180 |
static ffi_type *ecl_type_to_libffi_types[] = { |
| 181 |
&ffi_type_schar, /*@':char',*/ |
| 182 |
&ffi_type_uchar, /*@':unsigned-char',*/ |
| 183 |
&ffi_type_sint8, /*@':byte',*/ |
| 184 |
&ffi_type_uint8, /*@':unsigned-byte',*/ |
| 185 |
&ffi_type_sshort, /*@':short',*/ |
| 186 |
&ffi_type_ushort, /*@':unsigned-short',*/ |
| 187 |
&ffi_type_sint, /*@':int',*/ |
| 188 |
&ffi_type_uint, /*@':unsigned-int',*/ |
| 189 |
&ffi_type_slong, /*@':long',*/ |
| 190 |
&ffi_type_ulong, /*@':unsigned-long',*/ |
| 191 |
#ifdef ecl_uint8_t |
| 192 |
&ffi_type_sint8, /*@':int8-t',*/ |
| 193 |
&ffi_type_uint8, /*@':uint8-t',*/ |
| 194 |
#endif |
| 195 |
#ifdef ecl_uint16_t |
| 196 |
&ffi_type_sint16, /*@':int16-t',*/ |
| 197 |
&ffi_type_uint16, /*@':uint16-t',*/ |
| 198 |
#endif |
| 199 |
#ifdef ecl_uint32_t |
| 200 |
&ffi_type_sint32, /*@':int32-t',*/ |
| 201 |
&ffi_type_uint32, /*@':uint32-t',*/ |
| 202 |
#endif |
| 203 |
#ifdef ecl_uint64_t |
| 204 |
&ffi_type_sint64, /*@':int64-t',*/ |
| 205 |
&ffi_type_uint64, /*@':uint64-t',*/ |
| 206 |
#endif |
| 207 |
#ifdef ecl_long_long_t |
| 208 |
&ffi_type_slonglong, /*@':long-long',*/ |
| 209 |
&ffi_type_ulonglong, /*@':unsigned-long-long',*/ |
| 210 |
#endif |
| 211 |
&ffi_type_pointer, /*@':pointer-void',*/ |
| 212 |
&ffi_type_pointer, /*@':cstring',*/ |
| 213 |
&ffi_type_pointer, /*@':object',*/ |
| 214 |
&ffi_type_float, /*@':float',*/ |
| 215 |
&ffi_type_double, /*@':double',*/ |
| 216 |
&ffi_type_longdouble, /*@':long-double',*/ |
| 217 |
#ifdef ECL_COMPLEX_FLOAT |
| 218 |
# ifdef FFI_TARGET_HAS_COMPLEX_TYPE |
| 219 |
&ffi_type_complex_float, /*@':csfloat',*/ |
| 220 |
&ffi_type_complex_double, /*@':cdfloat',*/ |
| 221 |
&ffi_type_complex_longdouble, /*@':clfloat',*/ |
| 222 |
# else |
| 223 |
NULL, /*@':csfloat',*/ |
| 224 |
NULL, /*@':cdfloat',*/ |
| 225 |
NULL, /*@':clfloat',*/ |
| 226 |
# endif |
| 227 |
#endif |
| 228 |
&ffi_type_void /*@':void'*/ |
| 229 |
}; |
| 230 |
|
| 231 |
static ffi_type * |
| 232 |
ecl_type_to_libffi_type(cl_object type) { |
| 233 |
enum ecl_ffi_tag tag = ecl_foreign_type_code(type); |
| 234 |
ffi_type *result = ecl_type_to_libffi_types[tag]; |
| 235 |
if (result == NULL) { |
| 236 |
FEerror("Dynamic FFI cannot encode argument of type ~s.", 1, type); |
| 237 |
} |
| 238 |
return result; |
| 239 |
} |
| 240 |
#endif /* HAVE_LIBFFI */ |
| 241 |
|
| 242 |
cl_object |
| 243 |
ecl_make_foreign_data(cl_object tag, cl_index size, void *data) |
| 244 |
{ |
| 245 |
cl_object output = ecl_alloc_object(t_foreign); |
| 246 |
output->foreign.tag = tag == ECL_NIL ? @':void' : tag; |
| 247 |
output->foreign.size = size; |
| 248 |
output->foreign.data = (char*)data; |
| 249 |
return output; |
| 250 |
} |
| 251 |
|
| 252 |
cl_object |
| 253 |
ecl_allocate_foreign_data(cl_object tag, cl_index size) |
| 254 |
{ |
| 255 |
cl_object output = ecl_alloc_object(t_foreign); |
| 256 |
output->foreign.tag = tag; |
| 257 |
output->foreign.size = size; |
| 258 |
output->foreign.data = (char*)ecl_alloc_atomic(size); |
| 259 |
return output; |
| 260 |
} |
| 261 |
|
| 262 |
void * |
| 263 |
ecl_foreign_data_pointer_safe(cl_object f) |
| 264 |
{ |
| 265 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) { |
| 266 |
FEwrong_type_only_arg(@[si::foreign-data-pointer], f, |
| 267 |
@[si::foreign-data]); |
| 268 |
} |
| 269 |
return f->foreign.data; |
| 270 |
} |
| 271 |
|
| 272 |
char * |
| 273 |
ecl_base_string_pointer_safe(cl_object f) |
| 274 |
{ |
| 275 |
unsigned char *s; |
| 276 |
if (ecl_unlikely(!ECL_BASE_STRING_P(f))) { |
| 277 |
FEwrong_type_argument(@[base-string], f); |
| 278 |
} |
| 279 |
s = f->base_string.self; |
| 280 |
if (ecl_unlikely(ECL_ARRAY_HAS_FILL_POINTER_P(f) && |
| 281 |
s[f->base_string.fillp] != 0)) { |
| 282 |
FEerror("Cannot coerce a string with fill pointer to (char *)", 0); |
| 283 |
} |
| 284 |
return (char *)s; |
| 285 |
} |
| 286 |
|
| 287 |
cl_object |
| 288 |
ecl_null_terminated_base_string(cl_object f) |
| 289 |
{ |
| 290 |
if (ecl_unlikely(!ECL_STRINGP(f))) { |
| 291 |
FEwrong_type_argument(@[string], f); |
| 292 |
} |
| 293 |
if (ecl_t_of(f) == t_base_string) { |
| 294 |
if (ECL_ARRAY_HAS_FILL_POINTER_P(f) && |
| 295 |
f->base_string.self[f->base_string.fillp] != 0) { |
| 296 |
return cl_copy_seq(f); |
| 297 |
} else { |
| 298 |
return f; |
| 299 |
} |
| 300 |
} else { |
| 301 |
return si_copy_to_simple_base_string(f); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
cl_object |
| 306 |
si_allocate_foreign_data(cl_object tag, cl_object size) |
| 307 |
{ |
| 308 |
cl_object output = ecl_alloc_object(t_foreign); |
| 309 |
cl_index bytes = ecl_to_size(size); |
| 310 |
output->foreign.tag = tag; |
| 311 |
output->foreign.size = bytes; |
| 312 |
/* FIXME! Should be atomic uncollectable or malloc, but we do not export |
| 313 |
* that garbage collector interface and malloc may be overwritten |
| 314 |
* by the GC library */ |
| 315 |
output->foreign.data = bytes? ecl_alloc_uncollectable(bytes) : NULL; |
| 316 |
@(return output); |
| 317 |
} |
| 318 |
|
| 319 |
cl_object |
| 320 |
si_free_foreign_data(cl_object f) |
| 321 |
{ |
| 322 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) { |
| 323 |
FEwrong_type_only_arg(@[si::free-foreign-data], f, |
| 324 |
@[si::foreign-data]); |
| 325 |
} |
| 326 |
if (f->foreign.size) { |
| 327 |
/* See si_allocate_foreign_data() */ |
| 328 |
ecl_free_uncollectable(f->foreign.data); |
| 329 |
} |
| 330 |
f->foreign.size = 0; |
| 331 |
f->foreign.data = NULL; |
| 332 |
@(return); |
| 333 |
} |
| 334 |
|
| 335 |
cl_object |
| 336 |
si_make_foreign_data_from_array(cl_object array) |
| 337 |
{ |
| 338 |
cl_object tag; |
| 339 |
if (!ECL_ARRAYP (array)) |
| 340 |
FEwrong_type_only_arg(@[si::make-foreign-data-from-array], |
| 341 |
array, @[array]); |
| 342 |
tag = ecl_aet_to_ffi_table[array->array.elttype]; |
| 343 |
if (ecl_unlikely(Null(tag))) { |
| 344 |
FEerror("Cannot make foreign object from array " |
| 345 |
"with element type ~S.", 1, |
| 346 |
ecl_elttype_to_symbol(array->array.elttype)); |
| 347 |
} |
| 348 |
@(return ecl_make_foreign_data(tag, 0, array->array.self.bc)); |
| 349 |
} |
| 350 |
|
| 351 |
cl_object |
| 352 |
si_foreign_data_p(cl_object f) |
| 353 |
{ |
| 354 |
@(return (ECL_FOREIGN_DATA_P(f)? ECL_T : ECL_NIL)); |
| 355 |
} |
| 356 |
|
| 357 |
cl_object |
| 358 |
si_foreign_data_address(cl_object f) |
| 359 |
{ |
| 360 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) { |
| 361 |
FEwrong_type_only_arg(@[si::foreign-data-address], f, |
| 362 |
@[si::foreign-data]); |
| 363 |
} |
| 364 |
@(return ecl_make_unsigned_integer((cl_index)f->foreign.data)); |
| 365 |
} |
| 366 |
|
| 367 |
cl_object |
| 368 |
si_foreign_data_tag(cl_object f) |
| 369 |
{ |
| 370 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) { |
| 371 |
FEwrong_type_only_arg(@[si::foreign-data-tag], f, |
| 372 |
@[si::foreign-data]); |
| 373 |
} |
| 374 |
@(return f->foreign.tag); |
| 375 |
} |
| 376 |
|
| 377 |
cl_object |
| 378 |
si_foreign_data_equal(cl_object f1, cl_object f2) |
| 379 |
{ |
| 380 |
if (ecl_unlikely(!ECL_FOREIGN_DATA_P(f1))) { |
| 381 |
FEwrong_type_only_arg(@[si::foreign-data-address], f1, |
| 382 |
@[si::foreign-data]); |
| 383 |
} |
| 384 |
if (ecl_unlikely(!ECL_FOREIGN_DATA_P(f2))) { |
| 385 |
FEwrong_type_only_arg(@[si::foreign-data-address], f2, |
| 386 |
@[si::foreign-data]); |
| 387 |
} |
| 388 |
@(return ((f1->foreign.data == f2->foreign.data)? ECL_T : ECL_NIL)); |
| 389 |
} |
| 390 |
|
| 391 |
cl_object |
| 392 |
si_foreign_data_pointer(cl_object f, cl_object andx, cl_object asize, |
| 393 |
cl_object tag) |
| 394 |
{ |
| 395 |
cl_index ndx = ecl_to_size(andx); |
| 396 |
cl_index size = ecl_to_size(asize); |
| 397 |
cl_object output; |
| 398 |
|
| 399 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) { |
| 400 |
FEwrong_type_only_arg(@[si::foreign-data-pointer], f, |
| 401 |
@[si::foreign-data]); |
| 402 |
} |
| 403 |
if (ecl_unlikely(ndx >= f->foreign.size || (f->foreign.size - ndx) < size)) { |
| 404 |
FEerror("Out of bounds reference into foreign data type ~A.", 1, f); |
| 405 |
} |
| 406 |
output = ecl_alloc_object(t_foreign); |
| 407 |
output->foreign.tag = tag; |
| 408 |
output->foreign.size = size; |
| 409 |
output->foreign.data = f->foreign.data + ndx; |
| 410 |
@(return output); |
| 411 |
} |
| 412 |
|
| 413 |
cl_object |
| 414 |
si_foreign_data_ref(cl_object f, cl_object andx, cl_object asize, cl_object tag) |
| 415 |
{ |
| 416 |
cl_index ndx = ecl_to_size(andx); |
| 417 |
cl_index size = ecl_to_size(asize); |
| 418 |
cl_object output; |
| 419 |
|
| 420 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) { |
| 421 |
FEwrong_type_nth_arg(@[si::foreign-data-ref], 1, f, |
| 422 |
@[si::foreign-data]); |
| 423 |
} |
| 424 |
if (ecl_unlikely(ndx >= f->foreign.size || (f->foreign.size - ndx) < size)) { |
| 425 |
FEerror("Out of bounds reference into foreign data type ~A.", 1, f); |
| 426 |
} |
| 427 |
output = ecl_allocate_foreign_data(tag, size); |
| 428 |
memcpy(output->foreign.data, f->foreign.data + ndx, size); |
| 429 |
@(return output); |
| 430 |
} |
| 431 |
|
| 432 |
cl_object |
| 433 |
si_foreign_data_set(cl_object f, cl_object andx, cl_object value) |
| 434 |
{ |
| 435 |
cl_index ndx = ecl_to_size(andx); |
| 436 |
cl_index size, limit; |
| 437 |
|
| 438 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) { |
| 439 |
FEwrong_type_nth_arg(@[si::foreign-data-set], 1, f, |
| 440 |
@[si::foreign-data]); |
| 441 |
} |
| 442 |
if (ecl_unlikely(ecl_t_of(value) != t_foreign)) { |
| 443 |
FEwrong_type_nth_arg(@[si::foreign-data-set], 3, value, |
| 444 |
@[si::foreign-data]); |
| 445 |
} |
| 446 |
size = value->foreign.size; |
| 447 |
limit = f->foreign.size; |
| 448 |
if (ecl_unlikely(ndx >= limit || (limit - ndx) < size)) { |
| 449 |
FEerror("Out of bounds reference into foreign data type ~A.", 1, f); |
| 450 |
} |
| 451 |
memcpy(f->foreign.data + ndx, value->foreign.data, size); |
| 452 |
@(return value); |
| 453 |
} |
| 454 |
|
| 455 |
static int |
| 456 |
foreign_type_code(cl_object type) |
| 457 |
{ |
| 458 |
int i; |
| 459 |
for (i = 0; i <= ECL_FFI_VOID; i++) { |
| 460 |
if (type == ecl_foreign_type_table[i].name) |
| 461 |
return i; |
| 462 |
} |
| 463 |
return -1; |
| 464 |
} |
| 465 |
|
| 466 |
enum ecl_ffi_tag |
| 467 |
ecl_foreign_type_code(cl_object type) |
| 468 |
{ |
| 469 |
int i = foreign_type_code(type); |
| 470 |
if (ecl_unlikely(i < 0)) { |
| 471 |
FEerror("~A does not denote an elementary foreign type.", 1, type); |
| 472 |
} |
| 473 |
return (enum ecl_ffi_tag)i; |
| 474 |
} |
| 475 |
|
| 476 |
#ifdef HAVE_LIBFFI |
| 477 |
ffi_abi |
| 478 |
ecl_foreign_cc_code(cl_object cc) |
| 479 |
{ |
| 480 |
int i; |
| 481 |
for (i = 0; ecl_foreign_cc_table[i].symbol != NULL; i++) { |
| 482 |
if (cc == ecl_foreign_cc_table[i].symbol) |
| 483 |
return ecl_foreign_cc_table[i].abi; |
| 484 |
} |
| 485 |
FEerror("~A does no denote a valid calling convention.", 1, cc); |
| 486 |
return FFI_DEFAULT_ABI; |
| 487 |
} |
| 488 |
#endif |
| 489 |
|
| 490 |
static void wrong_ffi_tag(enum ecl_ffi_tag tag) ecl_attr_noreturn; |
| 491 |
|
| 492 |
static void |
| 493 |
wrong_ffi_tag(enum ecl_ffi_tag tag) |
| 494 |
{ |
| 495 |
FEerror("Invalid ecl_ffi_tag code ~D", 1, ecl_make_integer(tag)); |
| 496 |
} |
| 497 |
|
| 498 |
cl_object |
| 499 |
ecl_foreign_data_ref_elt(void *p, enum ecl_ffi_tag tag) |
| 500 |
{ |
| 501 |
switch (tag) { |
| 502 |
case ECL_FFI_CHAR: |
| 503 |
return ECL_CODE_CHAR(*(char *)p); |
| 504 |
case ECL_FFI_UNSIGNED_CHAR: |
| 505 |
return ECL_CODE_CHAR(*(unsigned char *)p); |
| 506 |
case ECL_FFI_BYTE: |
| 507 |
return ecl_make_fixnum(*(int8_t *)p); |
| 508 |
case ECL_FFI_UNSIGNED_BYTE: |
| 509 |
return ecl_make_fixnum(*(uint8_t *)p); |
| 510 |
case ECL_FFI_SHORT: |
| 511 |
return ecl_make_fixnum(*(short *)p); |
| 512 |
case ECL_FFI_UNSIGNED_SHORT: |
| 513 |
return ecl_make_fixnum(*(unsigned short *)p); |
| 514 |
case ECL_FFI_INT: |
| 515 |
return ecl_make_integer(*(int *)p); |
| 516 |
case ECL_FFI_UNSIGNED_INT: |
| 517 |
return ecl_make_unsigned_integer(*(unsigned int *)p); |
| 518 |
case ECL_FFI_LONG: |
| 519 |
return ecl_make_integer(*(long *)p); |
| 520 |
#ifdef ecl_uint8_t |
| 521 |
case ECL_FFI_INT8_T: |
| 522 |
return ecl_make_fixnum(*(ecl_int8_t *)p); |
| 523 |
case ECL_FFI_UINT8_T: |
| 524 |
return ecl_make_fixnum(*(ecl_uint8_t *)p); |
| 525 |
#endif |
| 526 |
#ifdef ecl_uint16_t |
| 527 |
case ECL_FFI_INT16_T: |
| 528 |
return ecl_make_int16_t(*(ecl_int16_t *)p); |
| 529 |
case ECL_FFI_UINT16_T: |
| 530 |
return ecl_make_uint16_t(*(ecl_uint16_t *)p); |
| 531 |
#endif |
| 532 |
#ifdef ecl_uint32_t |
| 533 |
case ECL_FFI_INT32_T: |
| 534 |
return ecl_make_int32_t(*(ecl_int32_t *)p); |
| 535 |
case ECL_FFI_UINT32_T: |
| 536 |
return ecl_make_uint32_t(*(ecl_uint32_t *)p); |
| 537 |
#endif |
| 538 |
#ifdef ecl_uint64_t |
| 539 |
case ECL_FFI_INT64_T: |
| 540 |
return ecl_make_int64_t(*(ecl_int64_t *)p); |
| 541 |
case ECL_FFI_UINT64_T: |
| 542 |
return ecl_make_uint64_t(*(ecl_uint64_t *)p); |
| 543 |
#endif |
| 544 |
#ifdef ecl_long_long_t |
| 545 |
case ECL_FFI_LONG_LONG: |
| 546 |
return ecl_make_long_long(*(ecl_long_long_t *)p); |
| 547 |
case ECL_FFI_UNSIGNED_LONG_LONG: |
| 548 |
return ecl_make_ulong_long(*(ecl_ulong_long_t *)p); |
| 549 |
#endif |
| 550 |
case ECL_FFI_UNSIGNED_LONG: |
| 551 |
return ecl_make_unsigned_integer(*(unsigned long *)p); |
| 552 |
case ECL_FFI_POINTER_VOID: |
| 553 |
return ecl_make_foreign_data(@':pointer-void', 0, *(void **)p); |
| 554 |
case ECL_FFI_CSTRING: |
| 555 |
return *(char **)p ? |
| 556 |
ecl_make_constant_base_string(*(char **)p, -1) : ECL_NIL; |
| 557 |
case ECL_FFI_OBJECT: |
| 558 |
return *(cl_object *)p; |
| 559 |
case ECL_FFI_FLOAT: |
| 560 |
return ecl_make_single_float(*(float *)p); |
| 561 |
case ECL_FFI_DOUBLE: |
| 562 |
return ecl_make_double_float(*(double *)p); |
| 563 |
case ECL_FFI_LONG_DOUBLE: |
| 564 |
return ecl_make_long_float(*(long double *)p); |
| 565 |
#ifdef ECL_COMPLEX_FLOAT |
| 566 |
case ECL_FFI_CSFLOAT: |
| 567 |
return ecl_make_csfloat(*(_Complex float *)p); |
| 568 |
case ECL_FFI_CDFLOAT: |
| 569 |
return ecl_make_cdfloat(*(_Complex double *)p); |
| 570 |
case ECL_FFI_CLFLOAT: |
| 571 |
return ecl_make_clfloat(*(_Complex long double *)p); |
| 572 |
#endif |
| 573 |
case ECL_FFI_VOID: |
| 574 |
return ECL_NIL; |
| 575 |
default: |
| 576 |
wrong_ffi_tag(tag); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
void |
| 581 |
ecl_foreign_data_set_elt(void *p, enum ecl_ffi_tag tag, cl_object value) |
| 582 |
{ |
| 583 |
switch (tag) { |
| 584 |
case ECL_FFI_CHAR: |
| 585 |
*(char *)p = (char)ecl_base_char_code(value); |
| 586 |
break; |
| 587 |
case ECL_FFI_UNSIGNED_CHAR: |
| 588 |
*(unsigned char*)p = (unsigned char)ecl_base_char_code(value); |
| 589 |
break; |
| 590 |
case ECL_FFI_BYTE: |
| 591 |
*(int8_t *)p = ecl_to_int8_t(value); |
| 592 |
break; |
| 593 |
case ECL_FFI_UNSIGNED_BYTE: |
| 594 |
*(uint8_t *)p = ecl_to_uint8_t(value); |
| 595 |
break; |
| 596 |
case ECL_FFI_SHORT: |
| 597 |
*(short *)p = ecl_to_short(value); |
| 598 |
break; |
| 599 |
case ECL_FFI_UNSIGNED_SHORT: |
| 600 |
*(unsigned short *)p = ecl_to_ushort(value); |
| 601 |
break; |
| 602 |
case ECL_FFI_INT: |
| 603 |
*(int *)p = ecl_to_int(value); |
| 604 |
break; |
| 605 |
case ECL_FFI_UNSIGNED_INT: |
| 606 |
*(unsigned int *)p = ecl_to_uint(value); |
| 607 |
break; |
| 608 |
case ECL_FFI_LONG: |
| 609 |
*(long *)p = ecl_to_long(value); |
| 610 |
break; |
| 611 |
case ECL_FFI_UNSIGNED_LONG: |
| 612 |
*(unsigned long *)p = ecl_to_ulong(value); |
| 613 |
break; |
| 614 |
case ECL_FFI_INT8_T: |
| 615 |
*(ecl_int8_t *)p = ecl_to_int8_t(value); |
| 616 |
break; |
| 617 |
case ECL_FFI_UINT8_T: |
| 618 |
*(ecl_uint8_t *)p = ecl_to_uint8_t(value); |
| 619 |
break; |
| 620 |
#ifdef ecl_uint16_t |
| 621 |
case ECL_FFI_INT16_T: |
| 622 |
*(ecl_int16_t *)p = ecl_to_int16_t(value); |
| 623 |
break; |
| 624 |
case ECL_FFI_UINT16_T: |
| 625 |
*(ecl_uint16_t *)p = ecl_to_uint16_t(value); |
| 626 |
break; |
| 627 |
#endif |
| 628 |
#ifdef ecl_uint32_t |
| 629 |
case ECL_FFI_INT32_T: |
| 630 |
*(ecl_int32_t *)p = ecl_to_int32_t(value); |
| 631 |
break; |
| 632 |
case ECL_FFI_UINT32_T: |
| 633 |
*(ecl_uint32_t *)p = ecl_to_uint32_t(value); |
| 634 |
break; |
| 635 |
#endif |
| 636 |
#ifdef ecl_uint64_t |
| 637 |
case ECL_FFI_INT64_T: |
| 638 |
*(ecl_int64_t *)p = ecl_to_int64_t(value); |
| 639 |
break; |
| 640 |
case ECL_FFI_UINT64_T: |
| 641 |
*(ecl_uint64_t *)p = ecl_to_uint64_t(value); |
| 642 |
break; |
| 643 |
#endif |
| 644 |
#ifdef ecl_long_long_t |
| 645 |
case ECL_FFI_LONG_LONG: |
| 646 |
*(ecl_long_long_t *)p = ecl_to_long_long(value); |
| 647 |
break; |
| 648 |
case ECL_FFI_UNSIGNED_LONG_LONG: |
| 649 |
*(ecl_ulong_long_t *)p = ecl_to_ulong_long(value); |
| 650 |
break; |
| 651 |
#endif |
| 652 |
case ECL_FFI_POINTER_VOID: |
| 653 |
*(void **)p = ecl_foreign_data_pointer_safe(value); |
| 654 |
break; |
| 655 |
case ECL_FFI_CSTRING: |
| 656 |
*(char **)p = value == ECL_NIL ? NULL : (char*)value->base_string.self; |
| 657 |
break; |
| 658 |
case ECL_FFI_OBJECT: |
| 659 |
*(cl_object *)p = value; |
| 660 |
break; |
| 661 |
case ECL_FFI_FLOAT: |
| 662 |
*(float *)p = ecl_to_float(value); |
| 663 |
break; |
| 664 |
case ECL_FFI_DOUBLE: |
| 665 |
*(double *)p = ecl_to_double(value); |
| 666 |
break; |
| 667 |
case ECL_FFI_LONG_DOUBLE: |
| 668 |
*(long double *)p = ecl_to_long_double(value); |
| 669 |
break; |
| 670 |
#ifdef ECL_COMPLEX_FLOAT |
| 671 |
case ECL_FFI_CSFLOAT: |
| 672 |
*(_Complex float *)p = ecl_to_csfloat(value); |
| 673 |
break; |
| 674 |
case ECL_FFI_CDFLOAT: |
| 675 |
*(_Complex double *)p = ecl_to_cdfloat(value); |
| 676 |
break; |
| 677 |
case ECL_FFI_CLFLOAT: |
| 678 |
*(_Complex long double *)p = ecl_to_clfloat(value); |
| 679 |
break; |
| 680 |
#endif |
| 681 |
case ECL_FFI_VOID: |
| 682 |
break; |
| 683 |
default: |
| 684 |
wrong_ffi_tag(tag); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
cl_object |
| 689 |
si_foreign_data_ref_elt(cl_object f, cl_object andx, cl_object type) |
| 690 |
{ |
| 691 |
cl_index ndx = ecl_to_size(andx); |
| 692 |
cl_index limit = f->foreign.size; |
| 693 |
enum ecl_ffi_tag tag = ecl_foreign_type_code(type); |
| 694 |
if (ecl_unlikely(ndx >= limit || |
| 695 |
(ndx + ecl_foreign_type_table[tag].size > limit))) { |
| 696 |
FEerror("Out of bounds reference into foreign data type ~A.", 1, f); |
| 697 |
} |
| 698 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) { |
| 699 |
FEwrong_type_nth_arg(@[si::foreign-data-ref-elt], 1, f, |
| 700 |
@[si::foreign-data]); |
| 701 |
} |
| 702 |
@(return ecl_foreign_data_ref_elt((void*)(f->foreign.data + ndx), tag)); |
| 703 |
} |
| 704 |
|
| 705 |
cl_object |
| 706 |
si_foreign_data_set_elt(cl_object f, cl_object andx, cl_object type, cl_object value) |
| 707 |
{ |
| 708 |
cl_index ndx = ecl_to_size(andx); |
| 709 |
cl_index limit = f->foreign.size; |
| 710 |
enum ecl_ffi_tag tag = ecl_foreign_type_code(type); |
| 711 |
if (ecl_unlikely(ndx >= limit || |
| 712 |
ndx + ecl_foreign_type_table[tag].size > limit)) { |
| 713 |
FEerror("Out of bounds reference into foreign data type ~A.", 1, f); |
| 714 |
} |
| 715 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) { |
| 716 |
FEwrong_type_nth_arg(@[si::foreign-data-set-elt], 1, f, |
| 717 |
@[si::foreign-data]); |
| 718 |
} |
| 719 |
ecl_foreign_data_set_elt((void*)(f->foreign.data + ndx), tag, value); |
| 720 |
@(return value); |
| 721 |
} |
| 722 |
|
| 723 |
cl_object |
| 724 |
si_size_of_foreign_elt_type(cl_object type) |
| 725 |
{ |
| 726 |
enum ecl_ffi_tag tag = ecl_foreign_type_code(type); |
| 727 |
@(return ecl_make_fixnum(ecl_foreign_type_table[tag].size)); |
| 728 |
} |
| 729 |
|
| 730 |
cl_object |
| 731 |
si_alignment_of_foreign_elt_type(cl_object type) |
| 732 |
{ |
| 733 |
enum ecl_ffi_tag tag = ecl_foreign_type_code(type); |
| 734 |
@(return ecl_make_fixnum(ALIGNMENT(tag))); |
| 735 |
} |
| 736 |
|
| 737 |
cl_object |
| 738 |
si_foreign_elt_type_p(cl_object type) |
| 739 |
{ |
| 740 |
@(return ((foreign_type_code(type) < 0)? ECL_NIL : ECL_T)); |
| 741 |
} |
| 742 |
|
| 743 |
cl_object |
| 744 |
si_null_pointer_p(cl_object f) |
| 745 |
{ |
| 746 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) |
| 747 |
FEwrong_type_only_arg(@[si::null-pointer-p], f, |
| 748 |
@[si::foreign-data]); |
| 749 |
@(return ((f->foreign.data == NULL)? ECL_T : ECL_NIL)); |
| 750 |
} |
| 751 |
|
| 752 |
cl_object |
| 753 |
si_foreign_data_recast(cl_object f, cl_object size, cl_object tag) |
| 754 |
{ |
| 755 |
if (ecl_unlikely(ecl_t_of(f) != t_foreign)) |
| 756 |
FEwrong_type_nth_arg(@[si::foreign-data-recast], 1, f, |
| 757 |
@[si::foreign-data]); |
| 758 |
f->foreign.size = ecl_to_size(size); |
| 759 |
f->foreign.tag = tag; |
| 760 |
@(return f); |
| 761 |
} |
| 762 |
|
| 763 |
cl_object |
| 764 |
si_load_foreign_module(cl_object filename) |
| 765 |
{ |
| 766 |
#if !defined(ENABLE_DLOPEN) |
| 767 |
FEerror("SI:LOAD-FOREIGN-MODULE does not work when ECL is statically linked", 0); |
| 768 |
#else |
| 769 |
cl_object output; |
| 770 |
|
| 771 |
# ifdef ECL_THREADS |
| 772 |
mp_get_lock_wait(ecl_symbol_value(@'mp::+load-compile-lock+')); |
| 773 |
ECL_UNWIND_PROTECT_BEGIN(ecl_process_env()) { |
| 774 |
# endif |
| 775 |
output = ecl_library_open(filename, 0); |
| 776 |
if (output->cblock.handle == NULL) { |
| 777 |
cl_object aux = ecl_library_error(output); |
| 778 |
ecl_library_close(output); |
| 779 |
output = aux; |
| 780 |
} |
| 781 |
# ifdef ECL_THREADS |
| 782 |
(void)0; /* MSVC complains about missing ';' before '}' */ |
| 783 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT { |
| 784 |
mp_giveup_lock(ecl_symbol_value(@'mp::+load-compile-lock+')); |
| 785 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_END; |
| 786 |
# endif |
| 787 |
if (ecl_unlikely(ecl_t_of(output) != t_codeblock)) { |
| 788 |
FEerror("LOAD-FOREIGN-MODULE: Could not load " |
| 789 |
"foreign module ~S (Error: ~S)", 2, filename, output); |
| 790 |
} |
| 791 |
output->cblock.locked |= 1; |
| 792 |
@(return output); |
| 793 |
#endif |
| 794 |
} |
| 795 |
|
| 796 |
cl_object |
| 797 |
si_unload_foreign_module(cl_object module) |
| 798 |
{ |
| 799 |
#if !defined(ENABLE_DLOPEN) |
| 800 |
FEerror("SI:UNLOAD-FOREIGN-MODULE does not work when ECL is statically linked", 0); |
| 801 |
#else |
| 802 |
cl_object output = ECL_NIL; |
| 803 |
|
| 804 |
if (ecl_unlikely(ecl_t_of(module) != t_codeblock)) { |
| 805 |
FEerror("UNLOAD-FOREIGN-MODULE: Argument is not a foreign module: ~S ", |
| 806 |
1, module); |
| 807 |
} |
| 808 |
# ifdef ECL_THREADS |
| 809 |
mp_get_lock_wait(ecl_symbol_value(@'mp::+load-compile-lock+')); |
| 810 |
ECL_UNWIND_PROTECT_BEGIN(ecl_process_env()) { |
| 811 |
# endif |
| 812 |
if (ecl_likely(ecl_library_close(module))) output = ECL_T; |
| 813 |
# ifdef ECL_THREADS |
| 814 |
(void)0; /* MSVC complains about missing ';' before '}' */ |
| 815 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT { |
| 816 |
mp_giveup_lock(ecl_symbol_value(@'mp::+load-compile-lock+')); |
| 817 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_END; |
| 818 |
# endif |
| 819 |
@(return output); |
| 820 |
#endif |
| 821 |
} |
| 822 |
|
| 823 |
cl_object |
| 824 |
si_find_foreign_symbol(cl_object var, cl_object module, cl_object type, cl_object size) |
| 825 |
{ |
| 826 |
#if !defined(ENABLE_DLOPEN) |
| 827 |
FEerror("SI:FIND-FOREIGN-SYMBOL does not work when ECL is statically linked", 0); |
| 828 |
#else |
| 829 |
cl_object block; |
| 830 |
cl_object output = ECL_NIL; |
| 831 |
void *sym; |
| 832 |
|
| 833 |
block = (module == @':default' ? module : si_load_foreign_module(module)); |
| 834 |
var = ecl_null_terminated_base_string(var); |
| 835 |
sym = ecl_library_symbol(block, (char*)var->base_string.self, 1); |
| 836 |
if (sym == NULL) { |
| 837 |
if (block != @':default') |
| 838 |
output = ecl_library_error(block); |
| 839 |
goto OUTPUT; |
| 840 |
} |
| 841 |
output = ecl_make_foreign_data(type, ecl_to_fixnum(size), sym); |
| 842 |
OUTPUT: |
| 843 |
if (ecl_unlikely(ecl_t_of(output) != t_foreign)) |
| 844 |
FEerror("FIND-FOREIGN-SYMBOL: Could not load " |
| 845 |
"foreign symbol ~S from module ~S (Error: ~S)", |
| 846 |
3, var, module, output); |
| 847 |
@(return output); |
| 848 |
#endif |
| 849 |
} |
| 850 |
|
| 851 |
#ifdef HAVE_LIBFFI |
| 852 |
static void |
| 853 |
resize_call_stack(cl_env_ptr env, cl_index new_size) |
| 854 |
{ |
| 855 |
cl_index i; |
| 856 |
ffi_type **types = |
| 857 |
ecl_alloc_atomic((new_size + 1) * sizeof(ffi_type*)); |
| 858 |
union ecl_ffi_values *values = |
| 859 |
ecl_alloc_atomic((new_size + 1) * sizeof(union ecl_ffi_values)); |
| 860 |
union ecl_ffi_values **values_ptrs = |
| 861 |
ecl_alloc_atomic(new_size * sizeof(union ecl_ffi_values *)); |
| 862 |
memcpy(types, env->ffi_types, env->ffi_args_limit * sizeof(ffi_type*)); |
| 863 |
memcpy(values, env->ffi_values, env->ffi_args_limit * |
| 864 |
sizeof(union ecl_ffi_values)); |
| 865 |
for (i = 0; i < new_size; i++) { |
| 866 |
values_ptrs[i] = (values + i + 1); |
| 867 |
} |
| 868 |
env->ffi_args_limit = new_size; |
| 869 |
ecl_dealloc(env->ffi_types); |
| 870 |
env->ffi_types = types; |
| 871 |
ecl_dealloc(env->ffi_values); |
| 872 |
env->ffi_values = values; |
| 873 |
ecl_dealloc(env->ffi_values_ptrs); |
| 874 |
env->ffi_values_ptrs = values_ptrs; |
| 875 |
} |
| 876 |
|
| 877 |
static int |
| 878 |
prepare_cif(cl_env_ptr the_env, ffi_cif *cif, cl_object return_type, |
| 879 |
cl_object arg_types, cl_object args, |
| 880 |
cl_object cc_type, ffi_type ***output_copy) |
| 881 |
{ |
| 882 |
int n, ok; |
| 883 |
ffi_type **types; |
| 884 |
enum ecl_ffi_tag type; |
| 885 |
cl_object arg_type; |
| 886 |
if (!the_env->ffi_args_limit) |
| 887 |
resize_call_stack(the_env, 32); |
| 888 |
the_env->ffi_types[0] = ecl_type_to_libffi_type(return_type); |
| 889 |
for (n=0; !Null(arg_types); ) { |
| 890 |
if (!LISTP(arg_types)) { |
| 891 |
FEerror("In CALL-CFUN, types lists is not a proper list", 0); |
| 892 |
} |
| 893 |
if (n >= the_env->ffi_args_limit) { |
| 894 |
resize_call_stack(the_env, n + 32); |
| 895 |
} |
| 896 |
arg_type = ECL_CONS_CAR(arg_types); |
| 897 |
arg_types = ECL_CONS_CDR(arg_types); |
| 898 |
type = ecl_foreign_type_code(arg_type); |
| 899 |
the_env->ffi_types[++n] = ecl_type_to_libffi_type(arg_type); |
| 900 |
if (CONSP(args)) { |
| 901 |
cl_object object = ECL_CONS_CAR(args); |
| 902 |
if (type == ECL_FFI_CSTRING) { |
| 903 |
object = ecl_null_terminated_base_string(object); |
| 904 |
/* Push the newly allocated object onto the stack so that it |
| 905 |
* is reachable by the garbage collector */ |
| 906 |
if (ECL_CONS_CAR(args) != object) { |
| 907 |
ECL_STACK_PUSH(the_env, object); |
| 908 |
} |
| 909 |
} |
| 910 |
args = ECL_CONS_CDR(args); |
| 911 |
ecl_foreign_data_set_elt(the_env->ffi_values + n, type, object); |
| 912 |
} |
| 913 |
} |
| 914 |
if (output_copy) { |
| 915 |
cl_index bytes = (n + 1) * sizeof(ffi_type*); |
| 916 |
*output_copy = types = (ffi_type**)ecl_alloc_atomic(bytes); |
| 917 |
memcpy(types, the_env->ffi_types, bytes); |
| 918 |
} else { |
| 919 |
types = the_env->ffi_types; |
| 920 |
} |
| 921 |
ok = ffi_prep_cif(cif, ecl_foreign_cc_code(cc_type), n, types[0], types + 1); |
| 922 |
if (ok != FFI_OK) { |
| 923 |
if (ok == FFI_BAD_ABI) { |
| 924 |
FEerror("In CALL-CFUN, not a valid ABI: ~A", 1, |
| 925 |
cc_type); |
| 926 |
} |
| 927 |
if (ok == FFI_BAD_TYPEDEF) { |
| 928 |
FEerror("In CALL-CFUN, wrong or malformed argument types", 0); |
| 929 |
} |
| 930 |
} |
| 931 |
return n; |
| 932 |
} |
| 933 |
|
| 934 |
@(defun si::call-cfun (fun return_type arg_types args &optional (cc_type @':default')) |
| 935 |
void *cfun = ecl_foreign_data_pointer_safe(fun); |
| 936 |
cl_object object; |
| 937 |
volatile cl_index sp; |
| 938 |
ffi_cif cif; |
| 939 |
@ { |
| 940 |
sp = ECL_STACK_INDEX(the_env); |
| 941 |
prepare_cif(the_env, &cif, return_type, arg_types, args, cc_type, NULL); |
| 942 |
ffi_call(&cif, cfun, the_env->ffi_values, (void **)the_env->ffi_values_ptrs); |
| 943 |
object = ecl_foreign_data_ref_elt(the_env->ffi_values, |
| 944 |
ecl_foreign_type_code(return_type)); |
| 945 |
ECL_STACK_UNWIND(the_env, sp); |
| 946 |
if (object != ECL_NIL) { |
| 947 |
@(return object); |
| 948 |
} else { |
| 949 |
@(return); |
| 950 |
} |
| 951 |
} @) |
| 952 |
|
| 953 |
static void |
| 954 |
callback_executor(ffi_cif *cif, void *result, void **args, void *userdata) |
| 955 |
{ |
| 956 |
cl_object data = (cl_object)userdata; |
| 957 |
cl_object fun = ECL_CONS_CAR(data); |
| 958 |
cl_object ret_type = (data = ECL_CONS_CDR(data), ECL_CONS_CAR(data)); |
| 959 |
cl_object arg_types = (data = ECL_CONS_CDR(data), ECL_CONS_CAR(data)); |
| 960 |
cl_env_ptr the_env = ecl_process_env(); |
| 961 |
struct ecl_stack_frame frame_aux; |
| 962 |
const cl_object frame = ecl_stack_frame_open(the_env, (cl_object)&frame_aux, 0); |
| 963 |
cl_object x; |
| 964 |
while (arg_types != ECL_NIL) { |
| 965 |
cl_object type = ECL_CONS_CAR(arg_types); |
| 966 |
enum ecl_ffi_tag tag = ecl_foreign_type_code(type); |
| 967 |
x = ecl_foreign_data_ref_elt(*args, tag); |
| 968 |
ecl_stack_frame_push(frame, x); |
| 969 |
arg_types = ECL_CONS_CDR(arg_types); |
| 970 |
args++; |
| 971 |
} |
| 972 |
x = ecl_apply_from_stack_frame(frame, fun); |
| 973 |
ecl_stack_frame_close(frame); |
| 974 |
ecl_foreign_data_set_elt(result, ecl_foreign_type_code(ret_type), x); |
| 975 |
} |
| 976 |
|
| 977 |
cl_object |
| 978 |
si_free_ffi_closure(cl_object closure) |
| 979 |
{ |
| 980 |
ffi_closure_free(ecl_foreign_data_pointer_safe(closure)); |
| 981 |
@(return); |
| 982 |
} |
| 983 |
|
| 984 |
@(defun si::make-dynamic-callback (fun sym return_type arg_types |
| 985 |
&optional (cc_type @':default')) |
| 986 |
@ { |
| 987 |
ffi_cif *cif = ecl_alloc(sizeof(ffi_cif)); |
| 988 |
ffi_type **types; |
| 989 |
int n = prepare_cif(the_env, cif, return_type, arg_types, ECL_NIL, cc_type, |
| 990 |
&types); |
| 991 |
|
| 992 |
/* libffi allocates executable memory for us. ffi_closure_alloc() |
| 993 |
* returns a pointer to memory and a pointer to the beginning of |
| 994 |
* the actual executable region (executable_closure) which is |
| 995 |
* where the code resides. */ |
| 996 |
void *executable_region; |
| 997 |
ffi_closure *closure = ffi_closure_alloc(sizeof(ffi_closure), &executable_region); |
| 998 |
|
| 999 |
cl_object closure_object = ecl_make_foreign_data(@':pointer-void', |
| 1000 |
sizeof(ffi_closure), |
| 1001 |
closure); |
| 1002 |
si_set_finalizer(closure_object, @'si::free-ffi-closure'); |
| 1003 |
|
| 1004 |
cl_object data = cl_list(5, |
| 1005 |
fun, return_type, arg_types, cc_type, |
| 1006 |
ecl_make_foreign_data(@':pointer-void', |
| 1007 |
sizeof(*cif), cif), |
| 1008 |
ecl_make_foreign_data(@':pointer-void', |
| 1009 |
(n + 1) * sizeof(ffi_type*), |
| 1010 |
types)); |
| 1011 |
int status = ffi_prep_closure_loc(closure, cif, callback_executor, |
| 1012 |
data, executable_region); |
| 1013 |
|
| 1014 |
if (status != FFI_OK) { |
| 1015 |
FEerror("Unable to build callback. libffi returns ~D", 1, |
| 1016 |
ecl_make_fixnum(status)); |
| 1017 |
} |
| 1018 |
si_put_sysprop(sym, @':callback', closure_object); |
| 1019 |
@(return closure_object); |
| 1020 |
} @) |
| 1021 |
#endif /* HAVE_LIBFFI */ |