Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/glacials/splits-io. a speedrunning data store and analysis engine splits.io
Something went wrong. Try again.
18 kB · 622 lines
at lambda-parse
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623\input texinfo @c -*-texinfo-*-@c %**start of header@setfilename libffi.info@settitle libffi@setchapternewpage off@c %**end of header
@c Merge the standard indexes into a single one.@syncodeindex fn cp@syncodeindex vr cp@syncodeindex ky cp@syncodeindex pg cp@syncodeindex tp cp
@include version.texi
@copying
This manual is for Libffi, a portable foreign-function interfacelibrary.
Copyright @copyright{} 2008, 2010, 2011 Red Hat, Inc.
@quotationPermission is granted to copy, distribute and/or modify this documentunder the terms of the GNU General Public License as published by theFree Software Foundation; either version 2, or (at your option) anylater version. A copy of the license is included in thesection entitled ``GNU General Public License''.
@end quotation@end copying
@dircategory Development@direntry* libffi: (libffi). Portable foreign-function interface library.@end direntry
@titlepage@title Libffi@page@vskip 0pt plus 1filll@insertcopying@end titlepage
@ifnottex@node Top@top libffi
@insertcopying
@menu* Introduction:: What is libffi?* Using libffi:: How to use libffi.* Missing Features:: Things libffi can't do.* Index:: Index.@end menu
@end ifnottex
@node Introduction@chapter What is libffi?
Compilers for high level languages generate code that follow certainconventions. These conventions are necessary, in part, for separatecompilation to work. One such convention is the @dfn{callingconvention}. The calling convention is a set of assumptions made bythe compiler about where function arguments will be found on entry toa function. A calling convention also specifies where the returnvalue for a function is found. The calling convention is alsosometimes called the @dfn{ABI} or @dfn{Application Binary Interface}.@cindex calling convention@cindex ABI@cindex Application Binary Interface
Some programs may not know at the time of compilation what argumentsare to be passed to a function. For instance, an interpreter may betold at run-time about the number and types of arguments used to calla given function. @samp{Libffi} can be used in such programs toprovide a bridge from the interpreter program to compiled code.
The @samp{libffi} library provides a portable, high level programminginterface to various calling conventions. This allows a programmer tocall any function specified by a call interface description at runtime.
@acronym{FFI} stands for Foreign Function Interface. A foreignfunction interface is the popular name for the interface that allowscode written in one language to call code written in another language.The @samp{libffi} library really only provides the lowest, machinedependent layer of a fully featured foreign function interface. Alayer must exist above @samp{libffi} that handles type conversions forvalues passed between the two languages.@cindex FFI@cindex Foreign Function Interface
@node Using libffi@chapter Using libffi
@menu* The Basics:: The basic libffi API.* Simple Example:: A simple example.* Types:: libffi type descriptions.* Multiple ABIs:: Different passing styles on one platform.* The Closure API:: Writing a generic function.* Closure Example:: A closure example.@end menu
@node The Basics@section The Basics
@samp{Libffi} assumes that you have a pointer to the function you wishto call and that you know the number and types of arguments to passit, as well as the return type of the function.
The first thing you must do is create an @code{ffi_cif} object thatmatches the signature of the function you wish to call. This is aseparate step because it is common to make multiple calls using asingle @code{ffi_cif}. The @dfn{cif} in @code{ffi_cif} stands forCall InterFace. To prepare a call interface object, use the function@code{ffi_prep_cif}.@cindex cif
@findex ffi_prep_cif@defun ffi_status ffi_prep_cif (ffi_cif *@var{cif}, ffi_abi @var{abi}, unsigned int @var{nargs}, ffi_type *@var{rtype}, ffi_type **@var{argtypes})This initializes @var{cif} according to the given parameters.
@var{abi} is the ABI to use; normally @code{FFI_DEFAULT_ABI} is whatyou want. @ref{Multiple ABIs} for more information.
@var{nargs} is the number of arguments that this function accepts.
@var{rtype} is a pointer to an @code{ffi_type} structure thatdescribes the return type of the function. @xref{Types}.
@var{argtypes} is a vector of @code{ffi_type} pointers.@var{argtypes} must have @var{nargs} elements. If @var{nargs} is 0,this argument is ignored.
@code{ffi_prep_cif} returns a @code{libffi} status code, of type@code{ffi_status}. This will be either @code{FFI_OK} if everythingworked properly; @code{FFI_BAD_TYPEDEF} if one of the @code{ffi_type}objects is incorrect; or @code{FFI_BAD_ABI} if the @var{abi} parameteris invalid.@end defun
If the function being called is variadic (varargs) then@code{ffi_prep_cif_var} must be used instead of @code{ffi_prep_cif}.
@findex ffi_prep_cif_var@defun ffi_status ffi_prep_cif_var (ffi_cif *@var{cif}, ffi_abi var{abi}, unsigned int @var{nfixedargs}, unsigned int var{ntotalargs}, ffi_type *@var{rtype}, ffi_type **@var{argtypes})This initializes @var{cif} according to the given parameters fora call to a variadic function. In general it's operation is thesame as for @code{ffi_prep_cif} except that:
@var{nfixedargs} is the number of fixed arguments, prior to anyvariadic arguments. It must be greater than zero.
@var{ntotalargs} the total number of arguments, including variadicand fixed arguments.
Note that, different cif's must be prepped for calls to the samefunction when different numbers of arguments are passed.
Also note that a call to @code{ffi_prep_cif_var} with@var{nfixedargs}=@var{nototalargs} is NOT equivalent to a call to@code{ffi_prep_cif}.
@end defun
To call a function using an initialized @code{ffi_cif}, use the@code{ffi_call} function:
@findex ffi_call@defun void ffi_call (ffi_cif *@var{cif}, void *@var{fn}, void *@var{rvalue}, void **@var{avalues})This calls the function @var{fn} according to the description given in@var{cif}. @var{cif} must have already been prepared using@code{ffi_prep_cif}.
@var{rvalue} is a pointer to a chunk of memory that will hold theresult of the function call. This must be large enough to hold theresult and must be suitably aligned; it is the caller's responsibilityto ensure this. If @var{cif} declares that the function returns@code{void} (using @code{ffi_type_void}), then @var{rvalue} isignored. If @var{rvalue} is @samp{NULL}, then the return value isdiscarded.
@var{avalues} is a vector of @code{void *} pointers that point to thememory locations holding the argument values for a call. If @var{cif}declares that the function has no arguments (i.e., @var{nargs} was 0),then @var{avalues} is ignored. Note that argument values may bemodified by the callee (for instance, structs passed by value); theburden of copying pass-by-value arguments is placed on the caller.@end defun
@node Simple Example@section Simple Example
Here is a trivial example that calls @code{puts} a few times.
@example#include <stdio.h>#include <ffi.h>
int main()@{ ffi_cif cif; ffi_type *args[1]; void *values[1]; char *s; int rc; /* Initialize the argument info vectors */ args[0] = &ffi_type_pointer; values[0] = &s; /* Initialize the cif */ if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_uint, args) == FFI_OK) @{ s = "Hello World!"; ffi_call(&cif, puts, &rc, values); /* rc now holds the result of the call to puts */ /* values holds a pointer to the function's arg, so to call puts() again all we need to do is change the value of s */ s = "This is cool!"; ffi_call(&cif, puts, &rc, values); @} return 0;@}@end example
@node Types@section Types
@menu* Primitive Types:: Built-in types.* Structures:: Structure types.* Type Example:: Structure type example.@end menu
@node Primitive Types@subsection Primitive Types
@code{Libffi} provides a number of built-in type descriptors that canbe used to describe argument and return types:
@table @code@item ffi_type_void@tindex ffi_type_voidThe type @code{void}. This cannot be used for argument types, onlyfor return values.
@item ffi_type_uint8@tindex ffi_type_uint8An unsigned, 8-bit integer type.
@item ffi_type_sint8@tindex ffi_type_sint8A signed, 8-bit integer type.
@item ffi_type_uint16@tindex ffi_type_uint16An unsigned, 16-bit integer type.
@item ffi_type_sint16@tindex ffi_type_sint16A signed, 16-bit integer type.
@item ffi_type_uint32@tindex ffi_type_uint32An unsigned, 32-bit integer type.
@item ffi_type_sint32@tindex ffi_type_sint32A signed, 32-bit integer type.
@item ffi_type_uint64@tindex ffi_type_uint64An unsigned, 64-bit integer type.
@item ffi_type_sint64@tindex ffi_type_sint64A signed, 64-bit integer type.
@item ffi_type_float@tindex ffi_type_floatThe C @code{float} type.
@item ffi_type_double@tindex ffi_type_doubleThe C @code{double} type.
@item ffi_type_uchar@tindex ffi_type_ucharThe C @code{unsigned char} type.
@item ffi_type_schar@tindex ffi_type_scharThe C @code{signed char} type. (Note that there is not an exactequivalent to the C @code{char} type in @code{libffi}; ordinarily youshould either use @code{ffi_type_schar} or @code{ffi_type_uchar}depending on whether @code{char} is signed.)
@item ffi_type_ushort@tindex ffi_type_ushortThe C @code{unsigned short} type.
@item ffi_type_sshort@tindex ffi_type_sshortThe C @code{short} type.
@item ffi_type_uint@tindex ffi_type_uintThe C @code{unsigned int} type.
@item ffi_type_sint@tindex ffi_type_sintThe C @code{int} type.
@item ffi_type_ulong@tindex ffi_type_ulongThe C @code{unsigned long} type.
@item ffi_type_slong@tindex ffi_type_slongThe C @code{long} type.
@item ffi_type_longdouble@tindex ffi_type_longdoubleOn platforms that have a C @code{long double} type, this is defined.On other platforms, it is not.
@item ffi_type_pointer@tindex ffi_type_pointerA generic @code{void *} pointer. You should use this for allpointers, regardless of their real type.@end table
Each of these is of type @code{ffi_type}, so you must take the addresswhen passing to @code{ffi_prep_cif}.
@node Structures@subsection Structures
Although @samp{libffi} has no special support for unions orbit-fields, it is perfectly happy passing structures back and forth.You must first describe the structure to @samp{libffi} by creating anew @code{ffi_type} object for it.
@tindex ffi_type@deftp ffi_typeThe @code{ffi_type} has the following members:@table @code@item size_t sizeThis is set by @code{libffi}; you should initialize it to zero.
@item unsigned short alignmentThis is set by @code{libffi}; you should initialize it to zero.
@item unsigned short typeFor a structure, this should be set to @code{FFI_TYPE_STRUCT}.
@item ffi_type **elementsThis is a @samp{NULL}-terminated array of pointers to @code{ffi_type}objects. There is one element per field of the struct.@end table@end deftp
@node Type Example@subsection Type Example
The following example initializes a @code{ffi_type} objectrepresenting the @code{tm} struct from Linux's @file{time.h}.
Here is how the struct is defined:
@examplestruct tm @{ int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; /* Those are for future use. */ long int __tm_gmtoff__; __const char *__tm_zone__;@};@end example
Here is the corresponding code to describe this struct to@code{libffi}:
@example @{ ffi_type tm_type; ffi_type *tm_type_elements[12]; int i;
tm_type.size = tm_type.alignment = 0; tm_type.elements = &tm_type_elements; for (i = 0; i < 9; i++) tm_type_elements[i] = &ffi_type_sint;
tm_type_elements[9] = &ffi_type_slong; tm_type_elements[10] = &ffi_type_pointer; tm_type_elements[11] = NULL;
/* tm_type can now be used to represent tm argument types and return types for ffi_prep_cif() */ @}@end example
@node Multiple ABIs@section Multiple ABIs
A given platform may provide multiple different ABIs at once. Forinstance, the x86 platform has both @samp{stdcall} and @samp{fastcall}functions.
@code{libffi} provides some support for this. However, this isnecessarily platform-specific.
@c FIXME: document the platforms
@node The Closure API@section The Closure API
@code{libffi} also provides a way to write a generic function -- afunction that can accept and decode any combination of arguments.This can be useful when writing an interpreter, or to provide wrappersfor arbitrary functions.
This facility is called the @dfn{closure API}. Closures are notsupported on all platforms; you can check the @code{FFI_CLOSURES}define to determine whether they are supported on the currentplatform.@cindex closures@cindex closure API@findex FFI_CLOSURES
Because closures work by assembling a tiny function at runtime, theyrequire special allocation on platforms that have a non-executableheap. Memory management for closures is handled by a pair offunctions:
@findex ffi_closure_alloc@defun void *ffi_closure_alloc (size_t @var{size}, void **@var{code})Allocate a chunk of memory holding @var{size} bytes. This returns apointer to the writable address, and sets *@var{code} to thecorresponding executable address.
@var{size} should be sufficient to hold a @code{ffi_closure} object.@end defun
@findex ffi_closure_free@defun void ffi_closure_free (void *@var{writable})Free memory allocated using @code{ffi_closure_alloc}. The argument isthe writable address that was returned.@end defun
Once you have allocated the memory for a closure, you must construct a@code{ffi_cif} describing the function call. Finally you can preparethe closure function:
@findex ffi_prep_closure_loc@defun ffi_status ffi_prep_closure_loc (ffi_closure *@var{closure}, ffi_cif *@var{cif}, void (*@var{fun}) (ffi_cif *@var{cif}, void *@var{ret}, void **@var{args}, void *@var{user_data}), void *@var{user_data}, void *@var{codeloc})Prepare a closure function.
@var{closure} is the address of a @code{ffi_closure} object; this isthe writable address returned by @code{ffi_closure_alloc}.
@var{cif} is the @code{ffi_cif} describing the function parameters.
@var{user_data} is an arbitrary datum that is passed, uninterpreted,to your closure function.
@var{codeloc} is the executable address returned by@code{ffi_closure_alloc}.
@var{fun} is the function which will be called when the closure isinvoked. It is called with the arguments:@table @var@item cifThe @code{ffi_cif} passed to @code{ffi_prep_closure_loc}.
@item retA pointer to the memory used for the function's return value.@var{fun} must fill this, unless the function is declared as returning@code{void}.@c FIXME: is this NULL for void-returning functions?
@item argsA vector of pointers to memory holding the arguments to the function.
@item user_dataThe same @var{user_data} that was passed to@code{ffi_prep_closure_loc}.@end table
@code{ffi_prep_closure_loc} will return @code{FFI_OK} if everythingwent ok, and something else on error.@c FIXME: what?
After calling @code{ffi_prep_closure_loc}, you can cast @var{codeloc}to the appropriate pointer-to-function type.@end defun
You may see old code referring to @code{ffi_prep_closure}. Thisfunction is deprecated, as it cannot handle the need for separatewritable and executable addresses.
@node Closure Example@section Closure Example
A trivial example that creates a new @code{puts} by binding @code{fputs} with @code{stdin}.
@example#include <stdio.h>#include <ffi.h>
/* Acts like puts with the file given at time of enclosure. */void puts_binding(ffi_cif *cif, unsigned int *ret, void* args[], FILE *stream)@{ *ret = fputs(*(char **)args[0], stream);@}
int main()@{ ffi_cif cif; ffi_type *args[1]; ffi_closure *closure;
int (*bound_puts)(char *); int rc; /* Allocate closure and bound_puts */ closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts);
if (closure) @{ /* Initialize the argument info vectors */ args[0] = &ffi_type_pointer;
/* Initialize the cif */ if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_uint, args) == FFI_OK) @{ /* Initialize the closure, setting stream to stdout */ if (ffi_prep_closure_loc(closure, &cif, puts_binding, stdout, bound_puts) == FFI_OK) @{ rc = bound_puts("Hello World!"); /* rc now holds the result of the call to fputs */ @} @} @}
/* Deallocate both closure, and bound_puts */ ffi_closure_free(closure);
return 0;@}
@end example
@node Missing Features@chapter Missing Features
@code{libffi} is missing a few features. We welcome patches to addsupport for these.
@itemize @bullet@itemVariadic closures.
@itemThere is no support for bit fields in structures.
@itemThe closure API is
@c FIXME: ...
@itemThe ``raw'' API is undocumented.@c argument promotion?@c unions?@c anything else?@end itemize
Note that variadic support is very new and tested on a relativelysmall number of platforms.
@node Index@unnumbered Index
@printindex cp
@bye