summaryrefslogtreecommitdiffstats
path: root/ffi.h
Commit message (Collapse)AuthorAgeFilesLines
* ffi: support bitfields on types narrower than int.Kaz Kylheku2017-06-061-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * ffi.c (bit_s): New symbol variable. (ffi_generic_sbit_put, ffi_generic_sbit_get, ffi_generic_ubit_put, ffi_generic_ubit_get): New static functions. (bitfield_syntax_p): Include bit symbol in the check. (make_ffi_type_pointer): Zero size is no longer an early rejection test for bitfields; don't rely on it. (make_ffi_type_struct): Revise the member calculating loop to handle bitfields of various sizes. If a bitfield follows one of a different size, it starts a new cell even if the previous one has room, et cetera. The masking and shifting is set up to work on cells of int size; however, the new ffi_generic_s?bit_{put,get} functions use a temporary buffer and transfer just the right number of bytes to and from the actual buffer. (ffi_struct_compile): The check against incomplete type members only needs to test size zero; bitfields have nonzero size now, which is the true size of the underlying storage unit. They also have true alignment, which is used in make_ffi_type_struct rather than hard-coding to alignof (int). New syntax (bit width type) is now handled, where type can be any of the signed and unsigned integral types up to int32 and int. The endian types are not supported for now. (ffi_typedef, ffi_size, ffi_alignof): Zero size is no longer an early rejection test for bitfields; don't rely on it. (ffi_init): Initialize bit_s. * ffi.h (bit_s): Declared. * txr.1: Documented.
* ffi: big and little endian types.Kaz Kylheku2017-06-041-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | * ffi.c (be_uint16_s, be_int16_s, be_uint32_s, be_int32_s, be_uint64_s, be_int64_s, be_float_s, be_double_s, le_uint16_s, le_int16_s, le_uint32_s, le_int32_s, le_uint64_s, le_int64_s, le_float_s, le_double_s): New symbol variables. (ffi_be_i16_put, ffi_be_i16_get, ffi_be_u16_put, ffi_be_u16_get, ffi_le_i16_put, ffi_le_i16_get, ffi_le_u16_put, ffi_le_u16_get, ffi_be_i32_put, ffi_be_i32_get, ffi_be_u32_put, ffi_be_u32_get, ffi_le_i32_put, ffi_le_i32_get, ffi_le_u32_put, ffi_le_u32_get, ffi_be_i64_put, ffi_be_i64_get, ffi_be_u64_put, ffi_be_u64_get, ffi_le_i64_put, ffi_le_i64_get, ffi_le_u64_put, ffi_le_u64_get, ffi_be_float_put, ffi_be_float_get, ffi_le_float_put, ffi_le_float_get, ffi_be_double_put, ffi_be_double_get, ffi_le_double_put, ffi_le_double_get): New static functions. (ffi_init_types): Register new type symbols via typedef mechanism. (ffi_init): Initialize new symbol variables. * ffi.c (be_uint16_s, be_int16_s, be_uint32_s, be_int32_s, be_uint64_s, be_int64_s, be_float_s, be_double_s, le_uint16_s, le_int16_s, le_uint32_s, le_int32_s, le_uint64_s, le_int64_s, le_float_s, le_double_s): Declared.
* ffi: new type operator align for customizing alignment.Kaz Kylheku2017-06-041-0/+2
| | | | | | | | | | | | | | | | | | | | | | | * ffi.c (align_s): New symbol variable. (struct txr_ffi_type): New function pointer member, clone. (ffi_simple_clone): New static function. (make_ffi_type_builtin, make_ffi_type_pointer, make_ffi_type_enum): Wire in ffi_simple_clone as the clone function. (ffi_struct_clone): New static function. (make_ffi_type_struct): Wire in ffi_struct_clone as the clone function for struct types. (ffi_array_clone): New static function. (make_ffi_type_array): Wire in ffi_array_clone as the clone function for array types. (ffi_type_copy): New static function. (ffi_type_compile): Recognize new (align <num> <type>) syntax. This works by cloning <type>, and then punching in the specified alignment. The align syntax then denotes this modified type. (ffi_init): Initialize align_s. * ffi.h (align_s): Declared.
* ffi: functions and macros for basic type properties.Kaz Kylheku2017-06-021-0/+5
| | | | | | | | | | | | | | | | | | * ffi.c (ffi_alignof, ffi_offsetof, ffi_arraysize, ffi_elemsize, ffi_elemtype): New functions. (ffi_init): Registered intrinsics ffi-alignof, ffi-offsetof, ffi-arraysize, ffi-elemsize, ffi-elemtype. * ffi.h (ffi_alignof, ffi_offsetof, ffi_arraysize, ffi_elemsize, ffi_elemtype): Declared. * lisplib.c (ffi_set_entries): New autoload entries alignof, offsetof, arraysize, elemsize, elemtype. * share/txr/stdlib/ffi.tl (alignof, offsetof, arraysize, elemsize, elemtype): New macros. * txr.1: Documented new functions and macros.
* ffi: new feature: enums.Kaz Kylheku2017-05-311-0/+2
| | | | | | | | | | | | | | | | * ffi.c (enum_s): New symbol variable. (struct txr_ffi_type): New member, sym_num, num_sym. (ffi_enum_type_mark): New static function. (ffi_type_enum_ops): New static structure. (ffi_enum_put, ffi_enum_get, ffi_enum_rput, ffi_enum_rget): New static functions. (make_ffi_type_enum): New static function. (ffi_type_compile): Extend with enum syntax. (ffi_init): Initialize enum_s with new interned symbol. * ffi.c (enum_s): Declared. * txr.1: Documented.
* ffi: bitfield support.Kaz Kylheku2017-05-251-0/+2
| | | | | | | | | | | | | | | | | | | | | | | * ffi.c (sbit_s, ubit_s): New symbol variables. (struct txr_ffi_type): New members, shift and mask. (ffi_sbit_put, ffi_sbit_get, ffi_ubit_put, ffi_ubit_get, bitfield_syntax_p): New static functions. (make_ffi_type_pointer): Disallow pointers to bitfields. (make_ffi_type_struct): Process bitfield members and set up shifts and masks accordingly. Recently introduced bug fixed here at the same time: the alignment calculation for each member must be done top-of-loop. (ffi_struct_compile): Exclude bitfields from the check against members with zero type. Compile the bitfield syntax. (ffi_typedef): Do not allow typedefs of bitfield type. Not only doesn't this make sense, but bitfield types are destructively modified in make_ffi_type_struct: they are imbued with a mask and offset tied to their position in a particular struct. * ffi.h (sbit_s, ubit_s): Delared. * txr.1: Documented bitfields.
* ffi: val type.Kaz Kylheku2017-05-231-0/+2
| | | | | | | | | | | * ffi.c (val_s): New symbol variable. (ffi_val_put, ffi_val_get): New functions. (ffi_init_types): Register val type. (ffi_init): Initialize val_s. * ffi.h (val_s): Declared. * txr.1: Documented.
* ffi: overhaul ffi-call API and document it.Kaz Kylheku2017-05-201-1/+1
| | | | | | | | | | | | | | | * ffi.c (ffi_call_wrap): Take struct args * parameters rather than a list. Check that number of arguments matches required number from call desc. No need to build argument array any more; we just refer to the one in args. Also, the first two parameters are reversed for consistency with other functions. (ffi_init): Update registration of ffi-call to reflect type change. * ffi.h (ffi_call_wrap): Declaration updated. * txr.1: Documented ffi-call.
* ffi: new carray-buf-sync function.Kaz Kylheku2017-05-201-0/+1
| | | | | | | | | * ffi.c (carray_buf_sync): New function. (ffi_init): carray-buf-sync intrinsic registered. * ffi.h (carray_buf_sync): Declared. * txr.1: Documented.
* ffi: new function, carray-cptr.Kaz Kylheku2017-05-191-0/+1
| | | | | | | | | * ffi.c (carray_cptr): New function. (ffi_init): Registered intrinsic. * ffi.h (carray_cptr): Declared. * txr.1: Documented.
* ffi: new carray_buf function.Kaz Kylheku2017-05-171-0/+1
| | | | | | | | | Treat a buffer through a carray view. * ffi.c (carray_buf): New function. (ffi_init): Register carray-buf intrinsic. * ffi.h (carray_buf): Declared.
* ffi: carrays able to reference objects.Kaz Kylheku2017-05-171-1/+1
| | | | | | | | | | | | | | | | | | | | | The idea here is that carrays can have a pointer to some area that is owned by another object. So that the area doesn't disappear when the other object becomes garbage, a carray which does this maintains a pointer to that other object. * ffi.c (ffi_carray_get): Pass new parameter to make_carray. (struct carray): New member, ref. (carray_mark_op): Mark new ref member. (make_carray): New parameter: the object which is stored in the structure as ref. (carray_dup): Null out the ref member. When a carray is duplicated, it owns its own buffer, and henceforth mustn't prevent the original object from being reclaimed. (carray_own): Don't allow a carray to claim ownership of the pointer if it references another object; we have no protocol to inform that other object. * ffi.h (make_carray): Declaration updated.
* ffi: carray type to round out semantics.Kaz Kylheku2017-05-171-1/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The carray type deals with C array passing conventions as a pointer to the first element of an array of unknown size. It fills in the functionality gap not covered by array and varray. * ffi.c (carray_s): New symbol variable. (ffi_carray_get, ffi_carray_put): New static functions. (ffi_type_compile): Handle (carray <type>) syntax. (struct carray): New struct type. (carray_struct, carray_struct_checked, carray_print_op, carray_mark_op, carray_destroy_op): New static functions. (carray_borrowed_ops, carray_owned_ops): New static structs. (make_carray, carray_set_length, carray_dup, carray_own, carray_free, carray_type, length_carray, carray_get, carray_vec, carray_blank, vec_carray, list_carray, carray_ref, carray_refset): New functions. (ffi_init): Initialize carray_s. Register carray-set_length, carray_dup, carray_own, carray-free, carray_type, length_carray, carray-vec, array_blank, vec_carray, list_carray, carray_ref and carray-refset intrinsics. * ffi.h (carray_s): Declared. (make_carray, carray_set_length, carray_dup, carray_own, carray_free, carray_type, length_carray, carray_get, carray_vec, carray_blank, vec_carray, list_carray, carray_ref, carray_refset): Declared.
* ffi: support programmable abort return value.Kaz Kylheku2017-05-111-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | * ffi.c (stuct txr_ffi_closure): New member, abort_retval. (ffi_closure_mark_op): Mark the new member. (ffi_closure_dispatch_safe): Implement the abort_retval. If it is not nil, use put to place the value into the return buffer. There is a risk that this could also throw an exception, which is no longer protected: programer's problem. (ffi_make_closure): New abort_ret_in argument, which is defaulted and stored. (ffi_init): Update registration of ffi-make-closure to reflect new argument. * ffi.h (ffi_make_closure): Declaration updated. * share/txr/stdlib/ffi.tl (sys:deffi-cb-expander): Add abort-retval parameter; insert into ffi-make-closure call. (deffi-cb): Take optional abort-retval expression; pass it down to the expander function. (deffi-cb-unsafe): Pass nil as abort-retval down to expander. * txr.1: Documented.
* ffi: a measure of safety for callbacks.Kaz Kylheku2017-05-111-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We don't want, by default, for callbacks to capture delimited continuations across foreign code, or perpetrate non-local transfers across foreign code. Here, we take an approach similar for what was done in ftw_wrap. * ffi.c (s_exit_point): New global variable with internal linkage. (ffi_call_wrap): If s_exit_point isn't nil, then it means that the callback intercepted a nonlocal transfer and stored its exit point. We resume the transfer to that exit point instead of returning normally. (ffi_closure_dispatch_safe): New static function. (ffi_make_closure): Support a new argument which indicates whether to make a closure which uses ffi_closure_dispatch_safe, or ffi_closure_dispatch. (ffi_init): Update registration of ffi-make-closure intrinsic. * ffi.h (ffi_make_closure): Declaration updated. * share/txr/stdlib/ffi.tl (sys:deffi-cb-expander): New function. (deffi-cb): Macro internals replaced by call to new function. (deffi-cb-safe): New macro. * txr.1: Documentation of ffi-make-closure updated. New deffi-cb-unsafe macro documented.
* ffi: bugfix: all in calls must fall back on get.Kaz Kylheku2017-05-071-1/+1
| | | | | | | | | | | | | | | | * ffi.c (ffi_ptr_out_in, ffi_ptr_out_s_in): If the target type has no in handler, fall back on its get. Here, it is without regard for the copy flag, because a zero value of that flag just indicates that the ptr-out itself is passed by-value. The target object is never by value (ffi_in): Add copy flag parameter, so the full interface is exposed, like in ffi_out. Fall back on get, if there is no in and the copy flag is true. Just return the original object if the type has no in, and copy is false. (ffi_init): Registration of ffi-in adjusted to four parameters. * ffi.h (ffi_in): Declaration updated.
* ffi: ffi-size function.Kaz Kylheku2017-05-061-0/+1
| | | | | | | | | This will support a sizeof macro. * ffi.c (ffi_size): New function. (ffi_init): Register ffi-size intrinsic. * ffi.h (ffi_size): Declared.
* ffi: functions for type-system-driven buffer coding.Kaz Kylheku2017-05-041-0/+5
| | | | | | | | | | * ffi.c (ffi_put_into, ffi_put, ffi_in, ffi_get, ffi_out): New functions. (ffi_init): ffi-put-into, ffi-put, ffi-in, ffi-get, ffi-out: intrinsics registered. * ffi.h (ffi_put_into, ffi_put, ffi_in, ffi_get, ffi_out): Declared.
* ffi: new bstr type.Kaz Kylheku2017-05-041-1/+1
| | | | | | | | | | | | | | | | | | The bstr type is like str, but doesn't perform UTF-8 conversion. The C data is assumed to be null terminated byte strings representing code points U+0000 through U+00FF. * ffi.c (bstr_s, bstr_d_s): New symbol variables. (ffi_bstr_put, ffi_bstr_get, ffi_bstr_d_get): New static functions. (ffi_init_types): Register bstr and bstr-d types. (ffi_init): Initialize bstr_s and bstr_d_s. * ffi.h (bstr_s, bstr_d_s): Declared. * lib.c (chk_strdup_8bit, string_8bit): New function. * lib.h (chk_strdup_8bit, string_8bit): Declared.
* ffi: implement bchar type.Kaz Kylheku2017-05-041-1/+1
| | | | | | | | | | | | bchar is like uchar, except that in the decode direction, it produces character objects rather than integers. * ffi.c (bchar_s): New symbol variable. (ffi_bchar_get): New static function. (ffi_init_types): Register bchar type. (ffi_init): Initialize bchar_s. * ffi.h (bchar_s): Declared.
* ffi: new ptr-out-s type.Kaz Kylheku2017-05-031-1/+1
| | | | | | | | | | | | | | | | | | One more ptr type is useful. This type is for objects returned via pointers embedded in arrays or structures, whereby the callee establishes both the pointer and the data. This is similar to ptr-out-d; the difference is that the data has an indefinite lifetime ("s" denotes "static") and so the pointer is not freed after the call takes place and the data is extracted into Lisp objects. * ffi.c (ptr_out_s_s): New symbol variable. (ffi_ptr_out_s_in): New function. (ffi_type_compile): Handle new ptr_out_s_s. (ffi_init): Initialize ptr_out_s. * ffi.h (ptr_out_s_s): Declared.
* ffi: remove ffi-copy-type.Kaz Kylheku2017-05-021-1/+0
| | | | | | | | | | | | | | | | | We don't need type copying because the need for this was driven by the rtvec implementation, which assigned a unique rtidx to the nodes in a type tree, requiring like types to be separately instantiated. * ffi.c (struct txr_ffi_type): Remove member dup. (ffi_struct_dup, ffi_ptr_dup): Function removed. (make_ffi_type_builtin, make_ffi_type_struct, make_ffi_type_array): Don't initialize dup. (ffi_copy_type): Function removed. (ffi_type_compile): don't call ffi_copy_type. (ffi_init): ffi-copy-type intrinsic removed. * ffi.h (ffi_copy_type): Declaration removed.
* ffi: simple typedef mechanism.Kaz Kylheku2017-05-011-0/+1
| | | | | | | | | | | | * ffi.c (ffi_typedef_hash): New static variable. (ffi_type_compile): Handle undefined atom case by trying through typedef hash. (ffi_typedef): New function. (ffi_init): gc-protect ffi_type_compile variable and initialize it with a hash table. Register ffi-typedef intrinsic. * ffi.h (ffi_typedef): Declared
* ffi: support for duplicating type objects.Kaz Kylheku2017-05-011-0/+1
| | | | | | | | | | | | * ffi.c (struct txr_ffi_type): New member, dup. (ffi_struct_dup, ffi_ptr_dup): New static functions. (make_ffi_type_pointer, make_ffi_type_struct, make_ffi_type_array): Set up dup virtual function for these types. (ffi_copy_type): New function. (ffi_init): Register ffi-copy-type intrinsic. * ffi.h (ffi_copy_type): Declared.
* ffi: implementing FFI callback closures.Kaz Kylheku2017-04-301-1/+5
| | | | | | | | | | | | | | | | | | | * ffi.c (closure_s, ffi_closure_s): New symbol vars. (struct txr_ffi_closure): New type. (ffi_closure_struct, ffi_closure_struct_checked, ffi_closure_print_op, ffi_closure_destroy_op, ffi_closure_mark_op): New static functions. (ffi_closure_ops): New static struct. (ffi_closure_put): New static function. (ffi_type_compile): Handle closure_s to support closure type specifier. (ffi_closure_dispatch): New static function. (ffi_make_closure, ffi_closure_get_fptr): New function. (ffi_init): Initialize closure_ and ffi_closure_s. Register ffi-make-closure intrinsic. * ffi.c (closure_s, ffi_closure_s, ffi_make_closure, ffi_closure_get_fptr): Likewise.
* ffi: housecleaning: reorder type sym declarations.Kaz Kylheku2017-04-301-5/+5
| | | | | | | | | | | | * ffi.c: Move declarations of array_s and zarray_s after void_s, to keep built-in FFI types together. Eliminate some blank lines. (ffi_init): Move initialization of void_s to to match declaration order. * ffi.h: Update declarations of type symbol variables to match ffi.c. (struct_s): Now declared.
* ffi: add support for wchar_t type.Kaz Kylheku2017-04-291-1/+1
| | | | | | | | | | | | We have wstr strings already but no wchar type. * ffi.c (wchar_s): New symbol variable. (ffi_type_wchar): New macro. (ffi_wchar_put, ffi_wchar_get): New static functions. (ffi_type_compile): Handle wchar_s. (ffi_init): Initialize wchar_s. * ffi.h (wchar_s): Declared.
* ffi: zarray type for null terminated array support.Kaz Kylheku2017-04-291-1/+1
| | | | | | | | | | | | | | | | | | | | | A zarray is null terminated when we convert from Lisp to C representation. The last element of the Lisp vector is ignored and all zero bits are written to the destination buffer in the size of the element. * ffi.c (zarray_s): New symbol variable. (struct txr_ffi_type): New bitfield member, null_term. (ffi_array_in): If the array's null_term flag is set, then don't process the last element, since the corresponding put call wasn't done for that element in ffi_array_put. (ffi_array_put): If the array's null_term flag is set, then when processing the last element, just memset the output element to zero and don't call put. (ffi_type_compile): Recognize zarray_s and set up the flag accordingly. (ffi_init): Initialize zarray_s. * ffi.h (zarray_s): Declared.
* ffi: add ptr-in-d and ptr-out-d.Kaz Kylheku2017-04-291-1/+1
| | | | | | | | | | | | | | | | | | | | ptr-in-d means we pass a buffer to the foreign function, which it owns and must free. We don't touch it. ptr-out-d is meaningful for returned values. It means that the function returned the object in a malloced buffer which the caller owns. It will be freed. * ffi.c (ptr_in_d_s, ptr_out_d_s): New symbol variables. (ffi_ptr_in_d_put, ffi_ptr_out_d_get): New static functions. (make_ffi_type_pointer): Don't set rtsize unconditionally to 1. If we are passed a null pointer for the in function, set it to zero. ptr-in-d does this: it has no need to free the buffer since the called function owns it and so there is no in function for that type. (ffi_type_compile): Handle ptr_in_d_s and ptr_out_d_s. (ffi_init): Initialize ptr_in_d_s and ptr_out_d_s. * ffi.h (ptr_in_d_s, ptr_out_d_s): Declared.
* ffi: rename ptr-in-out to just ptr.Kaz Kylheku2017-04-291-1/+1
| | | | | | | | | | | | Why have this verbose naming for the most basic case. Rather, ptr-in and ptr-out are optimizations of ptr which just avoid copying the object in one direction. * ffi.c (ptr_in_out_s): Renamed to ptr_s. (ffi_ptr_in_out_put): Renamed to ffi_ptr_put. (ffi_type_compile, ffi_init): Follow rename. * ffi.h (ptr_in_out_s): Declaration updated
* ffi: malloc-free variants for str, dstr and buf.Kaz Kylheku2017-04-291-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The FFI types str-d, wstr-d and buf-d are like their unsuffixed counterparts, but indicate that ownership of a dynamically allocated object is being passed which the receiver is responsible for freeing. If we pass a str, wstr or buf into a foreign function, we are generally only guaranteeing the lifetime of the buffer over the function call. If we pass a str-d or buf-d, then the callee keeps the pointer indefinitely, and must free it. If we get str or buf return value from a function, it is assumed that the pointer is static or something borrowed with some limited lifetime. A copy of it is made. If we get a str-d, wstr-d or buf-d, it is assumed that the pointer has been malloc-ed for us. The object is either duplicated, and the original one immediately freed, or else the pointer is retained directly in the Lisp object and freed if that object becomes garbage. * ffi.c (str_d_s, wstr_d_s, buf_d_s): New symbol variables. (ffi_str_get): Coalesce assignment with declaration. (ffi_str_d_get, ffi_str_d_put): New static functions. (ffi_buf_d_get, ffi_buf_d_put): New static functions. (ffi_type_compile): Handle buf_d_s, str_d_s and wstr_d_s. (ffi_init): Initialize str_d_s, wstr_d_s and buf_d_s. * ffi.c (str_d_s, wstr_d_s, buf_d_s): Declared.
* ffi: fix problems caught by g++.Kaz Kylheku2017-04-271-1/+1
| | | | | | | | | | | | | | * ffi.c (float_s): Variable removed. This is a duplicate definition; we already have this symbol in lib.c. (ffi_type_s): Duplicate definition removed; it is repeated two lines below. (ffi_str_put): Remove pointless const qualifier on u8s variable. (ffi_call_wrap): Cast return value of alloca. Also, rc pointer needs to be cast to mem_t *. (ffi_init): Remove initialization of float_s. * ffi.h (float_s): Declaration removed.
* ffi: array support.Kaz Kylheku2017-04-261-0/+2
| | | | | | | | | | | * ffi.c (array_s): New symbol variable. (ffi_array_put, ffi_array_get, ffi_array_fill, make_ffi_type_array): New static functions. (ffi_type_compile): Support (array <dim> <type>) syntax. (ffi_init): Initialize array_s. * ffi.h (array_s): Declared.
* Start of FFI implementation based on libffi.Kaz Kylheku2017-04-241-0/+51
* Makefile (OBJS): Add ffi.o. * configure (have_libffi): New variable. (gen_config_make): Generate have_libffi make variable. New check for availability of libffi. * ffi.c, ffi.h: New files. * lib.c (init): Call ffi_init.