Debian Codegraph @

src/doc/manual/standards/data_and_control_flow.txi

raw
1 @node Data and control flow
2 @section Data and control flow
3
4 @menu
5 * Shadowed bindings::
6 * Minimal compilation::
7 * Function types::
8 * C Calling conventions::
9 * C Reference::
10 @end menu
11
12 @node Shadowed bindings
13 @subsection Shadowed bindings
14 @cindex Shadowed bindings in let, flet, labels and lambda-list
15 ANSI doesn't specify what should happen if any of the @code{let},
16 @code{flet} and @code{labels} special operators contain many bindings
17 sharing the same name. Because the behavior varies between the
18 implementations and the programmer can't rely on the spec ECL signals an
19 error if such situation occur.
20
21 Moreover, while ANSI defines lambda list parameters in the terms of
22 @code{let*}, when used in function context programmer can't provide an
23 initialization forms for required parameters. If required parameters
24 share the same name an error is signaled.
25
26 Described behavior is present in ECL since version 16.0.0. Previously
27 the @code{let} operator were using first binding. Both @code{flet} and
28 @code{labels} were signaling an error if C compiler was used and used
29 the last binding as a visible one when the byte compiler was used.
30
31 @node Minimal compilation
32 @subsection Minimal compilation
33 @cindex Bytecodes eager compilation
34 @lspdef si::make-lambda
35 Former versions of ECL, as well as many other lisps, used linked lists
36 to represent code. Executing code thus meant traversing these lists and
37 performing code transformations, such as macro expansion, every time
38 that a statement was to be executed. The result was a slow and memory
39 hungry interpreter.
40
41 Beginning with version 0.3, ECL was shipped with a bytecodes compiler
42 and interpreter which circumvent the limitations of linked lists. When
43 you enter code at the lisp prompt, or when you load a source file, ECL
44 begins a process known as minimal compilation. Barely this process
45 consists on parsing each form, macroexpanding it and translating it into
46 an intermediate language made of bytecodes.
47
48 The bytecodes compiler is implemented in src/c/compiler.d. The main
49 entry point is the lisp function si::make-lambda, which takes a name for
50 the function and the body of the lambda lists, and produces a lisp
51 object that can be invoked. For instance,
52
53 @exindex @code{si::make-lambda} usage (bytecodes compilation)
54 @lisp
55 > (defvar fun (si::make-lambda 'f '((x) (1+ x))))
56 *FUN*
57 > (funcall fun 2)
58 3
59 @end lisp
60
61 ECL can only execute bytecodes. When a list is passed to @code{eval} it
62 must be first compiled to bytecodes and, if the process succeeds, the
63 resulting bytecodes are passed to the interpreter. Similarly, every time
64 a function object is created, such as in @code{defun} or
65 @code{defmacro}, the compiler processes the lambda form to produce a
66 suitable bytecodes object.
67
68 @cindex Eager compilation implications
69
70 The fact that ECL performs this eager compilation means that changes on
71 a macro are not immediately seen in code which was already
72 compiled. This has subtle implications. Take the following code:
73
74 @exindex Eager compilation impact on macros
75 @lisp
76 > (defmacro f (a b) `(+ ,a ,b))
77 F
78 > (defun g (x y) (f x y))
79 G
80 > (g 1 2)
81 3
82 > (defmacro f (a b) `(- ,a ,b))
83 F
84 > (g 1 2)
85 3
86 @end lisp
87
88 The last statement always outputs 3 while in former implementations
89 based on simple list traversal it would produce -1.
90
91 @node Function types
92 @subsection Function types
93
94 Functions in ECL can be of two types: they are either compiled to
95 bytecodes or they have been compiled to machine code using a lisp to C
96 translator and a C compiler. To the first category belong function
97 loaded from lisp source files or entered at the toplevel. To the second
98 category belong all functions in the ECL core environment and functions
99 in files processed by @code{compile} or @code{compile-file}.
100
101 The output of @code{(symbol-function fun)} is one of the following:
102 @itemize
103 @item a function object denoting the definition of the function @code{fun},
104 @item a list of the form @code{(macro . function-object)} when @code{fun} denotes a macro,
105 @item or simply @code{'special}, when @code{fun} denotes a special form, such as @code{block}, @code{if}, etc.
106 @end itemize
107
108 @cindex @code{disassemble} and @code{compile} on defined functions
109 @exindex Keeping lambda definitions with @code{si:*keep-definitions*}
110
111 ECL usually keeps the source code of a function unless the global
112 variable @code{si:*keep-definitions*} was false when the function was
113 translated into bytecodes. Therefore, if you don't need to use compile
114 and disassemble on defined functions, you should issue @code{(setq
115 si:*keep-definitions* nil)} at the beginning of your session.
116
117 @lspdef si:*keep-definitions*
118 @defvr Variable {si:*keep-definitions*}
119 If set to @code{t} ECL will preserve the compiled function source code
120 for disassembly and recompilation.
121 @end defvr
122
123 @cindex Common Lisp functions limits
124 @lspdef call-arguments-limit
125 @lspdef lambda-parameters-limit
126 @lspdef multiple-values-limit
127 @lspdef lambda-list-keywords
128
129 In @ref{tab:fun-const} we list all Common Lisp values related to the limits of functions.
130 @float Table,tab:fun-const
131 @caption{Function related constants}
132 @multitable @columnfractions 0.3 0.7
133 @item call-arguments-limit
134 @tab @code{65536}
135
136 @item lambda-parameters-limit
137 @tab @code{call-arguments-limit}
138
139 @item multiple-values-limit
140 @tab @code{64}
141
142 @item lambda-list-keywords
143 @tab @code{(&optional &rest &key &allow-other-keys &aux &whole &environment &body)}
144 @end multitable
145 @end float
146
147 @node C Calling conventions
148 @subsection C Calling conventions
149 ECL is implemented using either a C or a C++ compiler. This is not a limiting factor, but imposes some constraints on how these languages are used to implement functions, multiple values, closures, etc. In particular, while C functions can be called with a variable number of arguments, there is no facility to check how many values were actually passed. This forces us to have two types of functions in ECL
150
151 @itemize
152 @item Functions that take a fixed number of arguments have a simple C signature, with all arguments being properly declared, as in @code{cl_object cl_not(cl_object arg1)}.
153 @item Functions with a variable number of arguments, such as those accepting @code{&optional}, @code{&rest} or @code{&key} arguments, must take as first argument the number of remaining ones, as in @code{cl_object cl_list(cl_narg narg, ...)}. Here @var{narg} is the number of supplied arguments.
154 @end itemize
155
156 The previous conventions set some burden on the C programmer that calls ECL, for she must know the type of function that is being called and supply the right number of arguments. This burden disappears for Common Lisp programmers, though.
157
158 As an example let us assume that the user wants to invoke two functions which are part of the ANSI @bibcite{ANSI} standard and thus are exported with a C name. The first example is @code{cl_cos}, which takes just one argument and has a signature @code{cl_object cl_cos(cl_object)}.
159
160 @example
161 #include <math.h>
162 ...
163 cl_object angle = ecl_make_double_float(M_PI);
164 cl_object c = cl_cos(angle);
165 printf("\nThe cosine of PI is %g\n", ecl_double_float(c));
166 @end example
167
168 The second example also involves some Mathematics, but now we are going to use the C function corresponding to @code{+}. As described in @ref{Numbers - ANSI dictionary}, the C name for the plus operator is @code{cl_P} and has a signature @code{cl_object cl_P(cl_narg narg,...)}. Our example now reads as follows
169
170 @example
171 cl_object one = ecl_make_fixnum(1);
172 cl_object two = cl_P(2, one, one);
173 cl_object three = cl_P(3, one, one, one);
174 printf("\n1 + 1 is %d\n", ecl_fixnum(two));
175 printf("\n1 + 1 + 1 is %d\n", ecl_fixnum(three));
176 @end example
177
178 Note that most Common Lisp functions will not have a C name. In this case one must use the symbol that names them to actually call the functions, using @code{cl_funcall} or @code{cl_apply}. The previous examples may thus be rewritten as follows
179
180 @example
181 /* Symbol + in package CL */
182 cl_object plus = ecl_make_symbol("+","CL");
183 cl_object one = ecl_make_fixnum(1);
184 cl_object two = cl_funcall(3, plus, one, one);
185 cl_object three = cl_funcall(4, plus, one, one, one);
186 printf("\n1 + 1 is %d\n", ecl_fixnum(two));
187 printf("\n1 + 1 + 1 is %d\n", ecl_fixnum(three));
188 @end example
189
190 Another restriction of C and C++ is that functions can only take a limited number of arguments. In order to cope with this problem, ECL uses an internal stack to pass any argument above a hardcoded limit, @code{ECL_C_CALL_ARGUMENTS_LIMIT}, which is as of this writing 63. The use of this stack is transparently handled by the Common Lisp functions, such as @code{apply}, @code{funcall} and their C equivalents, and also by a set of macros, @code{cl_va_arg}, which can be used for coding functions that take an arbitrary name of arguments.
191
192
193 @node C Reference
194 @subsection C Reference
195 @cppdef ecl_bds_bind
196 @cppdef ecl_bds_push
197 @deftypefun void ecl_bds_bind (cl_env_ptr cl_env, cl_object var, cl_object value);
198 @deftypefunx void ecl_bds_push (cl_env_ptr cl_env, cl_object var);
199 Bind a special variable
200
201 @paragraph Description
202 Establishes a variable binding for the symbol @var{var} in the Common Lisp environment @var{env}, assigning it @var{value}.
203
204 This macro or function is the equivalent of @clhs{s_let_l.htm,let*} and @clhs{s_let_l.htm,let}.
205
206 @coderef{ecl_bds_push} does a similar thing, but reuses the old value of the same variable. It is thus the equivalent of @code{(let ((var var)) ...)}
207
208 Every variable binding must undone when no longer needed. It is best practice to match each call to @coderef{ecl_bds_bind} by another call to @coderef{ecl_bds_unwind1} in the same function.
209 @end deftypefun
210
211 @cppdef ecl_bds_unwind1
212 @cppdef ecl_bds_unwind_n
213 @deftypefun void ecl_bds_unwind1 (cl_env_ptr cl_env);
214 @deftypefunx void ecl_bds_unwind_n (cl_env_ptr cl_env, int n);
215 Undo one variable binding
216
217 @paragraph Description
218 @coderef{ecl_bds_unwind1} undoes the outermost variable binding, restoring the original value of the symbol in the process.
219
220 @coderef{ecl_bds_unwind_n} does the same, but for the @var{n} last variables.
221
222 Every variable binding must undone when no longer needed. It is best practice to match each call to @coderef{ecl_bds_bind} by another call to @coderef{ecl_bds_unwind1} in the same function.
223 @end deftypefun
224
225
226 @cppdef ecl_setq
227 @deftypefun cl_object ecl_setq (cl_env_ptr cl_env, cl_object var, cl_object value);
228 C equivalent of setq
229
230 @paragraph Description
231 Assigns @var{value} to the special variable denoted by the symbol @var{var}, in the Common Lisp environment @var{cl_env}.
232
233 This function implements a variable assignment, not a variable binding. It is thus the equivalent of @clhs{s_setq.htm,setq}.
234 @end deftypefun
235
236
237 @cppdef ecl_symbol_value
238 @deftypefun cl_object ecl_symbol_value (cl_object var);
239 @paragraph Description
240 Retrieves the value of the special variable or constant denoted by the symbol @var{var}, in the Common Lisp environment @var{cl_env}.
241
242 This function implements the equivalent of @clhs{f_symb_5.htm,symbol-value} and works both on special variables and constants.
243
244 If the symbol is not bound, an error is signaled.
245 @end deftypefun
246
247
248 @cppdef ecl_va_list
249 @cppdef ecl_va_start
250 @cppdef ecl_va_arg
251 @cppdef ecl_va_end
252 @deffn Macro {typedef struct @{ ... @} ecl_va_list[1];}
253 @end deffn
254
255 @deffn Macro ecl_va_start (ecl_va_list arglist, last_argument, narg, n_ordinary);
256 @end deffn
257
258 @deftypefn Macro cl_object ecl_va_arg (ecl_va_list arglist);
259 @end deftypefn
260
261 @deftypefn Macro cl_object ecl_va_end (ecl_va_list arglist);
262 Accepting a variable number of arguments
263
264 @paragraph Description
265 The macros above are used to code a function that accepts an arbitrary number of arguments. We will describe them in a practical example
266
267 @example
268 @verbatim
269 cl_object my_plus(cl_narg narg, cl_object required1, ...)
270 {
271 cl_env_ptr env = ecl_process_env();
272 cl_object other_value;
273 ecl_va_list varargs;
274 ecl_va_start(varargs, required1, narg, 1);
275 while (narg > 1) {
276 cl_object other_value = ecl_va_arg(varargs);
277 required1 = ecl_plus(required1, other_value);
278 }
279 ecl_va_end(varargs);
280 ecl_return1(env, required1);
281 }
282 @end verbatim
283 @end example
284
285 The first thing to do is to declare the variable that will hold the arguments. This is @var{varargs} in our example and it has the type @coderef{ecl_va_list}.
286
287 This arguments list is initialized with the @coderef{ecl_va_start} macro, based on the supplied number of arguments, @var{narg}, the number of required arguments which are passed as ordinary C arguments (1 in this case), the last such ordinary arguments, @var{required}, and the buffer for the argument list, @var{varargs}.
288
289 Once @var{varargs} has been initialized, we can retrieve these values one by one using @coderef{ecl_va_arg}. Note that the returned value always has the type @code{cl_object}, for it is always a Common Lisp object.
290
291 The last statement before returning the output of the function is @coderef{ecl_va_end}. This macro performs any required cleanup and should never be omitted.
292
293 @end deftypefn
294
295
296 @cppdef ecl_nvalues
297 @cppdef ecl_nth_value
298 @deftypefun cl_object ecl_nvalues (cl_env_ptr env);
299 @deftypefunx cl_object ecl_nth_value (cl_env_ptr env, int n);
300 Accessing output values
301
302 @paragraph Description
303 Common Lisp functions may return zero, one or more values. In ECL, the first two cases do not require any special manipulation, as the C function returns either @code{nil} or the first (zeroth) value directly. However, if one wishes to access additional values from a function, one needs to use these two macros or functions
304
305 @itemize
306 @item @code{ecl_nvalues(env)} returns the number of values that the function actually outputs. The single argument is the lisp environment. This value is larger or equal to 0 and smaller than @code{ECL_MULTIPLE_VALUES_LIMIT}.
307 @item Once we know the number of return values, they can be directly accessed using the function @code{ecl_nth_value(env,n)}, where @var{n} is a number larger than or equal to 1, and smaller than @code{ECL_MULTIPLE_VALUES_LIMIT}, which must correspond to a valid output value. No checking is done.
308 @end itemize
309
310 Note that in both cases these macros and functions have to be used right after the Lisp function was called. This is so because other Lisp functions might destroy the content of the return stack.
311
312 @paragraph Example
313 A C/C++ excerpt:
314
315 @example
316 cl_env_ptr env = ecl_process_env();
317 cl_object a = ecl_make_fixnum(13);
318 cl_object b = ecl_make_fixnum(6);
319 cl_object modulus = cl_floor(2, a, b);
320 cl_object remainder = ecl_nth_value(env, 1);
321 @end example
322
323 The somewhat equivalent Common Lisp code:
324
325 @lisp
326 (multiple-value-bind (modulus equivalent)
327 (floor 13 6))
328 @end lisp
329
330 @end deftypefun
331
332
333 @cppdef ecl_return0
334 @cppdef ecl_return1
335 @cppdef ecl_return2
336 @cppdef ecl_return3
337 @defmac ecl_return0 (cl_env_ptr cl_env);
338 @defmacx ecl_return1 (cl_env_ptr cl_env, cl_object value1);
339 @defmacx ecl_return2 (cl_env_ptr cl_env, cl_object value1, cl_object value2);
340 @defmacx ecl_return3 (cl_env_ptr cl_env, cl_object value1, cl_object value2, cl_object value3);
341 Returning multiple values
342
343 @paragraph Description
344 Returns @var{N} values from a C/C++ function in a way that a Common Lisp function can recognize and use them. The 0-th value is returned directly, while values 1 to N are stored in the Common Lisp environment @var{cl_env}. This macro has to be used from a function which returns an object of type @code{cl_object}.
345
346 @end defmac
347
348
349 @cppdef ECL_BLOCK_BEGIN
350 @deffn Macro ECL_BLOCK_BEGIN
351 @example
352 @verbatim
353 ECL_BLOCK_BEGIN(env,code) {
354
355 } ECL_BLOCK_END;
356 @end verbatim
357 @end example
358
359 @paragraph Description
360 @coderef{ECL_BLOCK_BEGIN} establishes a block named @var{code} that becomes visible for the Common Lisp code. This block can be used then as a target for @code{cl_return}.
361
362 @var{env} must be the value of the current Common Lisp environment, obtained with @coderef{ecl_process_env}.
363
364 The C/C++ program has to ensure that the code in @code{ECL_BLOCK_END} gets executed, avoiding a direct exit of the block via @code{goto} or a C/C++ return.
365 @end deffn
366
367
368 @cppdef ECL_CATCH_BEGIN
369 @deffn Macro ECL_CATCH_BEGIN
370 @example
371 @verbatim
372 ECL_CATCH_BEGIN(env,tag) {
373
374 } ECL_CATCH_END;
375 @end verbatim
376 @end example
377
378 @paragraph Description
379 @coderef{ECL_CATCH_BEGIN} establishes a destination for @code{throw} with the code given by @var{tag}.
380
381 @var{env} must be the value of the current Common Lisp environment, obtained with @coderef{ecl_process_env}.
382
383 The C/C++ program has to ensure that the code in @code{ECL_CATCH_END} gets executed, avoiding a direct exit of the catch block via goto or a C/C++ return.
384 @end deffn
385
386
387 @cppdef ECL_UNWIND_PROTECT_BEGIN
388 @deffn Macro ECL_UNWIND_PROTECT_BEGIN
389 C macro for unwind-protect
390
391 @paragraph Synopsis
392 @example
393 @verbatim
394 ECL_UNWIND_PROTECT_BEGIN(env) {
395
396 } ECL_UNWIND_PROTECT_EXIT {
397
398 } ECL_UNWIND_PROTECT_END;
399 @end verbatim
400 @end example
401
402 @paragraph Description
403 @coderef{ECL_UNWIND_PROTECT_BEGIN} establishes two blocks of C code that work like the equivalent ones in Common Lisp: a protected block, contained between the "BEGIN" and the "EXIT" statement, and the exit block, appearing immediately afterwards. The form guarantees that the exit block is always executed, even if the protected block attempts to exit via some nonlocal jump construct (@code{throw}, @code{return}, etc).
404
405 @var{env} must be the value of the current Common Lisp environment, obtained with @coderef{ecl_process_env}.
406
407 The utility of this construct is limited, for it only protects against nonlocal exits caused by Common Lisp constructs: it does not interfere with C @code{goto}, @code{return} or with C++ exceptions.
408 @end deffn
409
410
411 @subsubsection ANSI Dictionary
412 Common Lisp and C equivalence
413
414 @multitable @columnfractions .3 .7
415 @headitem Lisp symbol @tab C function or constant
416 @item @clhs{f_apply.htm,apply} @tab cl_object cl_apply(cl_narg narg, cl_object function, ...)
417 @item @clhs{v_call_a.htm,call-arguments-limit} @tab ECL_CALL_ARGUMENTS_LIMIT
418 @item @clhs{f_cmpd_f.htm,compiled-function-p} @tab cl_object cl_compiled_function_p(cl_object object)
419 @item @clhs{f_comple.htm,complement} @tab cl_object cl_complement(cl_object function)
420 @item @clhs{f_cons_1.htm,constantly} @tab cl_object cl_constantly(cl_object value)
421 @item @clhs{f_everyc.htm,every} @tab cl_object cl_every(cl_narg narg, cl_object predicate, ...)
422 @item @clhs{f_eq.htm,eq} @tab cl_object cl_eq(cl_object x, cl_object y)
423 @item @clhs{f_eql.htm,eql} @tab cl_object cl_eql(cl_object x, cl_object y)
424 @item @clhs{f_equal.htm,equal} @tab cl_object cl_equal(cl_object x, cl_object y)
425 @item @clhs{f_equalp.htm,equalp} @tab cl_object cl_equalp(cl_object x, cl_object y)
426 @item @clhs{f_fbound.htm,fboundp} @tab cl_object cl_fboundp(cl_object function_name)
427 @item @clhs{f_fdefin.htm,fdefinition} @tab cl_object cl_fdefinition(cl_object function_name)
428 @item @clhs{f_fdefin.htm,(setf fdefinition)} @tab cl_object si_fset(cl_narg narg, cl_object function_name, cl_object definition, ...)
429 @item @clhs{f_fmakun.htm,fmakunbound} @tab cl_object cl_fmakunbound(cl_object function_name)
430 @item @clhs{f_funcal.htm,funcall} @tab cl_object cl_funcall(cl_narg narg, cl_object function, ...)
431 @item @clhs{f_fn_lam.htm,function-lambda-expression} @tab cl_object cl_function_lambda_expression(cl_object function)
432 @item @clhs{f_fnp.htm,functionp} @tab cl_object cl_functionp(cl_object object)
433 @item @clhs{f_get_se.htm,get-setf-expansion} @tab cl_object cl_get_setf_expansion(cl_narg narg, cl_object place, ...)
434 @item @clhs{f_identi.htm,identity} @tab cl_object cl_identity(cl_object x)
435 @item @clhs{s_let_l.htm,let}, @clhs{s_let_l.htm,let*} @tab cl_object ecl_bds_bind(cl_env_ptr env, cl_object symbol, cl_object value)
436 @item @clhs{v_lamb_1.htm,lambda-parameters-limit} @tab ECL_LAMBDA_PARAMETERS_LIMIT
437 @item @clhs{v_multip.htm,multiple-values-limit} @tab ECL_MULTIPLE_VALUES_LIMIT
438 @item @clhs{f_not.htm,not} @tab cl_object cl_not(cl_object object)
439 @item @clhs{f_everyc.htm,notevery} @tab cl_object cl_notevery(cl_narg narg, cl_object predicate, ...)
440 @item @clhs{f_everyc.htm,notany} @tab cl_object cl_notany(cl_narg narg, cl_object predicate, ...)
441 @item @clhs{f_set.htm,set} @tab cl_object cl_set(cl_object symbol, cl_object value)
442 @item @clhs{s_setq.htm,setq} @tab cl_object ecl_setq(cl_env_ptr env, cl_object symbol, cl_object value)
443 @item @clhs{f_symb_5.htm,symbol-value} @tab cl_object ecl_symbol_value(cl_env_ptr env, cl_object symbol)
444 @item @clhs{f_everyc.htm,some} @tab cl_object cl_some(cl_narg narg, cl_object predicate, ...)
445 @item @clhs{f_vals_l.htm,values-list} @tab cl_object cl_values_list(cl_object list)
446 @end multitable