@@ -26,6 +26,7 @@ notably in the name of the @code{SB-ALIEN} package.
2626 * Foreign Data Structure Examples ::
2727 * Loading Shared Object Files ::
2828 * Foreign Function Calls ::
29+ * Calling Lisp From C ::
2930 * Step-By-Step Example of the Foreign Function Interface ::
3031@end menu
3132
@@ -718,7 +719,6 @@ the only documentation. Users of a Lisp built with the
718719* The alien-funcall Primitive ::
719720 * The define-alien-routine Macro ::
720721 * define-alien-routine Example ::
721- * Calling Lisp From C ::
722722@end menu
723723
724724@node The alien-funcall Primitive
@@ -883,40 +883,6 @@ This can be described by the following call to
883883The Lisp function @code {cfoo } will have two arguments (@var{str} and
884884@var{a}) and two return values (@var{a} and @var{i}).
885885
886- @node Calling Lisp From C
887- @comment node-name, next, previous, up
888- @subsection Calling Lisp From C
889-
890- Calling Lisp functions from C is sometimes possible, but is extremely
891- hackish and poorly supported as of SBCL 0.7.5. See @code {funcall0 }
892- @dots {} @code {funcall3 } in the runtime system. The arguments must be
893- valid SBCL object descriptors (so that e.g. fixnums must be
894- left-shifted by 2.) As of SBCL 0.7.5, the format of object descriptors
895- is documented only by the source code and, in parts, by the old CMUCL
896- @file {INTERNALS } documentation.
897-
898- Note that the garbage collector moves objects, and won't be
899- able to fix up any references in C variables. There are three
900- mechanisms for coping with this:
901-
902- @enumerate
903- @item
904- The @code {sb-ext:purify } moves all live Lisp
905- data into static or read-only areas such that it will never be moved
906- (or freed) again in the life of the Lisp session
907-
908- @item
909- @code {sb-sys:with-pinned-objects } is a macro which arranges for some
910- set of objects to be pinned in memory for the dynamic extent of its
911- body forms. On ports which use the generational garbage collector (most,
912- as of this writing) this affects exactly the specified objects. On
913- other ports it is implemented by turning off GC for the duration (so
914- could be said to have a whole-world granularity).
915-
916- @item
917- Disable GC, using the @code {without-gcing } macro.
918- @end enumerate
919-
920886@c <!-- FIXME: This is a "changebar" section from the CMU CL manual.
921887@c I (WHN 2002-07-14) am not very familiar with this content, so
922888@c I'm not immediately prepared to try to update it for SBCL, and
@@ -1058,6 +1024,72 @@ Disable GC, using the @code{without-gcing} macro.
10581024@c LaTeX
10591025@c -->
10601026
1027+ @node Calling Lisp From C
1028+ @comment node-name, next, previous, up
1029+ @section Calling Lisp From C
1030+
1031+ SBCL supports the calling of Lisp functions using the C calling
1032+ convention. This is useful for both defining callbacks and for creating
1033+ an interface for calling into Lisp as a shared library directly from C.
1034+
1035+ The @code {define-alien-callable } macro wraps Lisp code and creates a C
1036+ foreign function which can be called with the C calling convention.
1037+
1038+ @include macro-sb-alien-define-alien-callable.texinfo
1039+
1040+ The @code {alien-callable-function } function returns the foreign callable
1041+ value associated with any name defined by @code {define-alien-callable },
1042+ so that we can, for example, pass the callable value to C as a callback.
1043+
1044+ @include fun-sb-alien-alien-callable-function.texinfo
1045+
1046+ Note that the garbage collector moves objects, and won't be able to fix
1047+ up any references in C variables. There are three mechanisms for coping
1048+ with this:
1049+
1050+ @enumerate
1051+ @item
1052+ The @code {sb-ext:purify } moves all live Lisp data into static or
1053+ read-only areas such that it will never be moved (or freed) again in the
1054+ life of the Lisp session
1055+
1056+ @item
1057+ @code {sb-sys:with-pinned-objects } is a macro which arranges for some set
1058+ of objects to be pinned in memory for the dynamic extent of its body
1059+ forms. On ports which use the generational garbage collector (most, as
1060+ of this writing) this affects exactly the specified objects. On other
1061+ ports it is implemented by turning off GC for the duration (so could be
1062+ said to have a whole-world granularity).
1063+
1064+ @item
1065+ Disable GC, using the @code {without-gcing } macro.
1066+ @end enumerate
1067+
1068+ @menu
1069+ * Lisp as a Shared Library ::
1070+ @end menu
1071+
1072+ @node Lisp as a Shared Library
1073+ @comment node-name, next, previous, up
1074+ @subsection Lisp as a Shared Library
1075+ SBCL supports the use of Lisp as a shared library that can be used by C
1076+ programs using the @code {define-alien-callable } interface. See the
1077+ @code {:callable-exports } keyword to @code {save-lisp-and-die } for how to
1078+ save the Lisp image in a way that allows a C program to initialize the
1079+ Lisp runtime and the exported symbols. When SBCL is built as a library,
1080+ it exposes the symbol @code {initialize_lisp } which can be used in
1081+ conjunction with a core initializing global symbols to foreign callables
1082+ as function pointers and with object code allocating those symbols to
1083+ initialize the runtime properly. The arguments to @code {initialize_lisp }
1084+ are the same as the arguments to the main @code {sbcl } program.
1085+
1086+ While standalone C code can call exposed Lisp functions which spawn Lisp
1087+ threads after the runtime has been initialized, it is currently not
1088+ advised to call into Lisp this way from separate C threads running
1089+ concurrently.
1090+
1091+ Note: There is also currently no way to run exit hooks or otherwise undo
1092+ Lisp initialization gracefully from C.
10611093
10621094@node Step-By-Step Example of the Foreign Function Interface
10631095@comment node-name, next, previous, up
0 commit comments