Debian Codegraph @

src/doc/manual/extensions/ffi_sffi.txi

raw
1 @node SFFI Reference
2 @subsection SFFI Reference
3 @cindex C/C++ code inlining
4 @cindex Static foreign function interface
5
6 @subsubheading Reference
7
8 @lspdef ffi:clines
9 @defspec ffi:clines c/c++-code*
10
11 Insert C declarations and definitions
12
13 @table @var
14 @item c/c++-code
15 One or more strings with C definitions. Not evaluated.
16 @item returns
17 No value.
18 @end table
19
20 @paragraph Description
21 This special form inserts C code from strings passed in the
22 @var{arguments} directly in the file that results from compiling lisp
23 sources. Contrary to @coderef{ffi:c-inline}, this function may have no
24 executable statements, accepts no input value and returns no value.
25
26 The main use of @coderef{ffi:clines} is to declare or define C variables
27 and functions that are going to be used later in other FFI
28 statements. All statements from @var{arguments} are grouped at the
29 beginning of the produced header file.
30
31 @coderef{ffi:clines} is a special form that can only be used in lisp
32 compiled files as a toplevel form. Other uses will lead to an error
33 being signaled, either at the compilation time or when loading the
34 file.
35
36 @paragraph Examples
37 @exindex @coderef{ffi:clines} adding c toplevel declarations
38 In this example the @coderef{ffi:clines} statement is required to get
39 access to the C function @code{cos}:
40 @lisp
41 (ffi:clines "#include <math.h>")
42 (defun cos (x)
43 (ffi:c-inline (x) (:double) :double "cos(#0)" :one-liner t))
44 @end lisp
45 @end defspec
46
47 @lspdef ffi:c-inline
48 @defspec ffi:c-inline (lisp-values) (arg-c-types) return-type c/c++-code @
49 &key (side-effects t) (one-liner nil)
50
51 Inline C code in a lisp form
52
53 @table @var
54 @item lisp-values
55 One or more lisp expressions. Evaluated.
56 @item arg-c-types
57 One or more valid FFI types. Evaluated.
58 @item return-type
59 Valid FFI type or @code{(values ffi-type*)}.
60 @item c/c++-code
61 String containing valid C code plus some valid escape forms.
62 @item one-liner
63 Boolean indicating, if the expression is a valid R-value. Defaults to
64 @code{nil}.
65 @item side-effects
66 Boolean indicating, if the expression causes side effects. Defaults to
67 @code{t}.
68 @item returns
69 One or more lisp values.
70 @end table
71
72 @paragraph Description
73 This is a special form which can be only used in compiled code and whose
74 purpose is to execute some C code getting and returning values from and
75 to the lisp environment.
76
77 The first argument @var{lisp-values} is a list of lisp forms. These
78 forms are going to be evaluated and their lisp values will be
79 transformed to the corresponding C types denoted by the elements in the
80 list @var{arg-c-types}.
81
82 The input values are used to create a valid C expression using the
83 template in @var{C/C++-code}. This is a string of arbitrary size which
84 mixes C expressions with two kind of escape forms.
85
86 The first kind of escape form are made of a hash and a letter or a
87 number, as in: @code{#0}, @code{#1}, ..., until @code{#z}. These codes
88 are replaced by the corresponding input values. The second kind of
89 escape form has the format @code{@@(return [n])}, it can be used as
90 lvalue in a C expression and it is used to set the n-th output value of
91 the @coderef{ffi:c-inline} form.
92
93 When the parameter @var{one-liner} is true, then the C template must be
94 a simple C statement that outputs a value. In this case the use of
95 @code{@@(return)} is not allowed. When the parameter @var{one-liner} is
96 false, then the C template may be a more complicated block form, with
97 braces, conditionals, loops and spanning multiple lines. In this case
98 the output of the form can only be set using @code{@@(return)}.
99
100 Parameter @var{side-effects} set to false will indicate, that the
101 functions causes no side-effects. This information is used by the
102 compiler to optimize the resulting code. If @var{side-effects} is set to
103 false, but the function may cause the side effects, then results are
104 undefined.
105
106 Note that the conversion between lisp arguments and FFI types is
107 automatic. Note also that @coderef{ffi:c-inline} cannot be used in
108 interpreted or bytecompiled code! Such usage will signal an error.
109
110 @paragraph Examples
111 @exindex @coderef{ffi:c-inline} inlining c code
112 The following example implements the transcendental function SIN using
113 the C equivalent:
114
115 @lisp
116 (ffi:c-lines "#include <math.h>")
117 (defun mysin (x)
118 (ffi:c-inline (x) (:double) :double
119 "sin(#0)"
120 :one-liner t
121 :side-effects nil))
122 @end lisp
123
124 This function can also be implemented using the @code{@@(return)} form
125 as follows:
126
127 @lisp
128 @verbatim
129 (defun mysin (x)
130 (ffi:c-inline (x) (:double) :double
131 "@(return)=sin(#0);"
132 :side-effects nil))
133 @end verbatim
134 @end lisp
135
136 The following example is slightly more complicated as it involves loops
137 and two output values:
138
139 @exindex @coderef{ffi:c-inline} returning multiple values
140 @lisp
141 @verbatim
142 (defun sample (x)
143 (ffi:c-inline (x (+ x 2)) (:int :int) (values :int :int) "{
144 int n1 = #0, n2 = #1, out1 = 0, out2 = 1;
145 while (n1 <= n2) {
146 out1 += n1;
147 out2 *= n1;
148 n1++;
149 }
150 @(return 0)= out1;
151 @(return 1)= out2;
152 }"
153 :side-effects nil))
154 @end verbatim
155 @end lisp
156 @end defspec
157
158 @lspdef ffi:c-progn
159 @defspec ffi:c-progn args &body body
160
161 Interleave C statements with the Lisp code
162
163 @table @var
164 @item args
165 Lisp arguments. Evaluated.
166 @item returns
167 No value.
168 @end table
169
170 @paragraph Description
171 This form is used for it's side effects. It allows for interleaving C
172 statements with the Lisp code. The argument types doesn't have to be
173 declared – in such case the objects type in the C world will be
174 @code{cl_object}.
175
176 @paragraph Examples
177 @exindex @coderef{ffi:c-progn} interleaving c and lisp code
178 @lisp
179 @verbatim
180 (lambda (i)
181 (let* ((limit i)
182 (iterator 0)
183 (custom-var (cons 1 2)))
184 (declare (:int limit iterator))
185 (ffi:c-progn (limit iterator custom-var)
186 "cl_object cv = #2;"
187 "ecl_print(cv, ECL_T);"
188 "for (#1 = 0; #1 < #0; #1++) {"
189 (format t "~&Iterator: ~A, I: ~A~%" iterator i)
190 "}")))
191 @end verbatim
192 @end lisp
193 @end defspec
194
195 @c XXX: SFFI returns one-element list pointer, while DFFI returns just a
196 @c pointer. This is probably a bug.
197
198 @lspdef ffi:defcallback
199 @defspec ffi:defcallback name ret-type arg-desc &body body
200
201 @table @var
202 @item name
203 Name of the lisp function.
204 @item ret-type
205 Declaration of the return type which function returns.
206 @item arg-desc
207 List of pairs @code{(arg-name arg-type)}.
208 @item body
209 Function body.
210 @item returns
211 Pointer to the defined callback.
212 @end table
213
214 @paragraph Description
215 Defines Lisp function and generates a callback for the C world, which
216 may be passed to these functions. Note, that this special operator has
217 also a dynamic variant (with the same name and interface).
218 @end defspec
219
220 @lspdef ffi:defcbody
221 @defmac ffi:defcbody name arg-types result-type c-expression
222
223 Define C function under the lisp name
224
225 @table @var
226 @item name
227 Defined function name.
228 @item arg-types
229 Argument types of the defined Lisp function.
230 @item result-type
231 Result type of the C function (may be @code{(values ...)}.
232 @item returns
233 Defined function name.
234 @end table
235
236 @paragraph Description
237 The compiler defines a Lisp function named by @var{name} whose body
238 consists of the C code of the string @var{c-expression}. In the
239 @var{c-expression} one can reference the arguments of the function as
240 @code{#0}, @code{#1}, etc.
241
242 The interpreter ignores this form.
243 @end defmac
244
245 @lspdef ffi:defentry
246 @defmac ffi:defentry name arg-types c-name &key no-interrupts
247
248 @table @var
249 @item name
250 Lisp name for the function.
251
252 @item arg-types
253 Argument types of the C function.
254
255 @item c-name
256 If @var{c-name} is a list, then C function result type is declared as
257 @code{(car c-name)} and its name is @code{(string (cdr c-name))}.
258
259 If it's an atom, then the result type is @code{:object}, and function
260 name is @code{(string c-name)}.
261
262 @item returns
263 Lisp function @code{name}.
264 @end table
265
266 @paragraph Description
267 The compiler defines a Lisp function named by @var{name} whose body
268 consists of a calling sequence to the C language function named by
269 @var{c-name}.
270
271 The interpreter ignores this form.
272 @end defmac
273
274 @c XXX> note sure if this works
275 @c @paragraph @code{definline}
276 @c @lspindex ffi:definline
277 @c @defmac ffi:definline fun arg-types result-type code
278 @c @table @var
279 @c @item args
280 @c Lisp arguments. Evaluated.
281 @c @item returns
282 @c No value.
283 @c @end table
284 @c @end defmac
285
286 @c @paragraph Description
287 @c DEFINLINE behaves like a DEFCBODY (see), but also instructs the LISP
288 @c compiler to expand inline any call to function SYMBOL into code
289 @c corresponding to the C language expression C/C++-CODE, whenever it can
290 @c determine that the actual arguments are of the specified type.
291 @c <XXX
292
293 @lspdef ext:with-backend
294 @defspec ext:with-backend &key bytecodes c/c++
295
296 Use different code depending on the backend.
297
298 @paragraph Description
299 Depending on whether the bytecodes or C compiler is used, this form will
300 emit the code given in the corresponding keyword argument.
301
302 @paragraph Examples
303 @exindex @coderef{ext:with-backend} use different code for c and bytecodes compiler
304 @verbatim
305 CL-USER> (defmacro test ()
306 '(ext:with-backend :c/c++ "c/c++" :bytecodes "bytecodes"))
307 TEST
308 CL-USER> (test)
309 "bytecodes"
310 CL-USER> (funcall (compile nil (lambda () (test))))
311
312 ;;; OPTIMIZE levels: Safety=2, Space=0, Speed=3, Debug=3
313 "c/c++"
314
315 @end verbatim
316 @end defspec
317
318 @lspdef ffi:defla
319 @defmac ffi:defla name args &body body
320
321 Provide Lisp alternative for interpreted code.
322
323 @paragraph Description
324 Used to DEFine Lisp Alternative. For the interpreter,
325 @coderef{ffi:defla} is equivalent to @code{defun}, but the compiler
326 ignores this form.
327 @end defmac