| 1 | <?xml version="1.0" encoding="utf-8"?> |
| 2 | <!DOCTYPE book [ |
| 3 | <!ENTITY % eclent SYSTEM "ecl.ent"> |
| 4 | %eclent; |
| 5 | ]> |
| 6 | <book xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en"> |
| 7 | <chapter xml:id="ansi.data-and-control"> |
| 8 | <title>Data and control flow</title> |
| 9 | |
| 10 | <section xml:id="ansi.let-behavior"> |
| 11 | <title><function>LET</function>, <function>FLET</function>, |
| 12 | <function>LABELS</function> and function <emphasis>lambda |
| 13 | list</emphasis> &ANSI; addendum</title> |
| 14 | |
| 15 | <para>&ANSI; doesn't specify what should happen if any of the |
| 16 | <function>LET</function>, <function>FLET</function> and |
| 17 | <function>LABELS</function> blocks contain many |
| 18 | <emphasis>bindings</emphasis> (or <emphasis>functions</emphasis>) |
| 19 | sharing the same name. Because the behavior varies between the |
| 20 | implementations and the programmer can't rely on the spec, &ECL; |
| 21 | signals an <emphasis>error</emphasis> if such situation |
| 22 | occurs. This is also very unlikely, that programmer does that |
| 23 | intentionally, since there is no valid use-case for it. |
| 24 | </para> |
| 25 | |
| 26 | <para>Moreover, while &ANSI; defines <emphasis>lambda |
| 27 | list</emphasis> parameters in the terms of |
| 28 | <function>LET*</function>, when used in function context programmer |
| 29 | can't provide an initialization forms for required parameters. If |
| 30 | required parameters share the same name <emphasis>error</emphasis> |
| 31 | is signalled. |
| 32 | </para> |
| 33 | |
| 34 | <para>Described behavior is present in &ECL; since version |
| 35 | <emphasis>16.0.0</emphasis>. Previously the |
| 36 | <function>LET</function> operator were using first binding, while |
| 37 | <emphasis>lambda lists</emphasis> used last occurrence. Both |
| 38 | <function>FLET</function> and <function>LABELS</function> were |
| 39 | singalling an error if C compiler was used and used last binding as |
| 40 | visible one when <emphasis>bytecmp</emphasis> was used.</para> |
| 41 | </section> |
| 42 | |
| 43 | <section xml:id="ansi.minimal-compilation"> |
| 44 | <title>Minimal compilation</title> |
| 45 | |
| 46 | <para>Former versions of &ECL;, as well as many other lisps, used linked |
| 47 | lists to represent code. Executing code thus meant traversing these lists |
| 48 | and performing code transformations, such as macro expansion, every time |
| 49 | that a statement was to be executed. The result was a slow and memory |
| 50 | hungry interpreter.</para> |
| 51 | |
| 52 | <para>Beginning with version 0.3, &ECL; was shipped with a bytecodes |
| 53 | compiler and interpreter which circumvent the limitations of linked |
| 54 | lists. When you enter code at the lisp prompt, or when you load a source |
| 55 | file, &ECL; begins a process known as minimal compilation. Barely this |
| 56 | process consists on parsing each form, macroexpanding it and translating it |
| 57 | into an intermediate language made of |
| 58 | <emphasis>bytecodes</emphasis>.</para> |
| 59 | |
| 60 | <para>The bytecodes compiler is implemented in |
| 61 | <filename>src/c/compiler.d</filename>. The main entry point is the lisp |
| 62 | function <function>si::make-lambda</function>, which takes a name for the |
| 63 | function and the body of the lambda lists, and produces a lisp object that |
| 64 | can be invoked. For instance, |
| 65 | <screen>> (defvar fun (si::make-lambda 'f '((x) (1+ x)))) |
| 66 | *FUN* |
| 67 | > (funcall fun 2) |
| 68 | 3</screen></para> |
| 69 | |
| 70 | <para>&ECL; can only execute bytecodes. When a list is passed to |
| 71 | <literal>EVAL</literal> it must be first compiled to bytecodes and, if the |
| 72 | process succeeds, the resulting bytecodes are passed to the |
| 73 | interpreter. Similarly, every time a function object is created, such as in |
| 74 | <function>DEFUN</function> or <function>DEFMACRO</function>, the compiler |
| 75 | processes the lambda form to produce a suitable bytecodes object.</para> |
| 76 | |
| 77 | <para>The fact that &ECL; performs this eager compilation means that |
| 78 | changes on a macro are not immediately seen in code which was already |
| 79 | compiled. This has subtle implications. Take the following code:</para> |
| 80 | <screen>> (defmacro f (a b) `(+ ,a ,b)) |
| 81 | F |
| 82 | > (defun g (x y) (f x y)) |
| 83 | G |
| 84 | > (g 1 2) |
| 85 | 3 |
| 86 | > (defmacro f (a b) `(- ,a ,b)) |
| 87 | F |
| 88 | > (g 1 2) |
| 89 | 3</screen> |
| 90 | |
| 91 | <para>The last statement always outputs <literal>3</literal> while in former |
| 92 | implementations based on simple list traversal it would produce |
| 93 | <literal>-1</literal>.</para> |
| 94 | </section> |
| 95 | |
| 96 | <section xml:id="ansi.functions"> |
| 97 | <title>Function types</title> |
| 98 | |
| 99 | <para>Functions in &ECL; can be of two types: they are either compiled to |
| 100 | bytecodes or they have been compiled to machine code using a lisp to C |
| 101 | translator and a C compiler. To the first category belong function loaded |
| 102 | from lisp source files or entered at the toplevel. To the second category |
| 103 | belong all functions in the &ECL; core environment and functions in files |
| 104 | processed by <function>compile</function> or |
| 105 | <function>compile-file</function>.</para> |
| 106 | |
| 107 | <para>The output of <code>(symbol-function |
| 108 | <replaceable>fun</replaceable>)</code> is one of the following: |
| 109 | <itemizedlist> |
| 110 | <listitem><para>a function object denoting the definition of the function <literal>fun</literal>,</para></listitem> |
| 111 | <listitem><para>a list of the form <literal>(macro . function-object)</literal> when <literal>fun</literal> denotes a macro,</para></listitem> |
| 112 | <listitem><para>or simply <literal>'special</literal>, when <literal>fun</literal> denotes a special form, such as <symbol>block</symbol>, <symbol>if</symbol>, etc.</para></listitem> |
| 113 | </itemizedlist></para> |
| 114 | |
| 115 | <para>&ECL; usually drops the source code of a function unless the global |
| 116 | variable <varname>si:*keep-definitions*</varname> was true when the |
| 117 | function was translated into bytecodes. Therefore, if you wish to use |
| 118 | <function>compile</function> and <function>disassemble</function> on |
| 119 | defined functions, you should issue <code>(setq si:*keep-definitions* |
| 120 | t)</code> at the beginning of your session.</para> |
| 121 | |
| 122 | <para>In <xref linkend="table.function.constants"/> we list all |
| 123 | &CommonLisp; values related to the limits of functions.</para> |
| 124 | |
| 125 | <table xml:id="table.function.constants"> |
| 126 | <title>Function related constants</title> |
| 127 | <tgroup cols="2"> |
| 128 | <tbody> |
| 129 | <row> |
| 130 | <entry><constant>call-arguments-limit</constant></entry> |
| 131 | <entry>65536</entry> |
| 132 | </row> |
| 133 | <row> |
| 134 | <entry><constant>lambda-parameters-limit</constant></entry> |
| 135 | <entry>call-arguments-limit</entry> |
| 136 | </row> |
| 137 | <row> |
| 138 | <entry><constant>multiple-values-limit</constant></entry> |
| 139 | <entry>64</entry> |
| 140 | </row> |
| 141 | <row> |
| 142 | <entry><constant>lambda-list-keywords</constant></entry> |
| 143 | <entry><literal>(&optional; &rest; &key; &allow-other-keys; &aux; |
| 144 | &whole; &environment; &body;)</literal></entry> |
| 145 | </row> |
| 146 | </tbody> |
| 147 | </tgroup> |
| 148 | </table> |
| 149 | </section> |
| 150 | |
| 151 | <section xml:id="ansi.calling-conventions"> |
| 152 | <title>C Calling conventions</title> |
| 153 | |
| 154 | <para>&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; |
| 155 | <itemizedlist> |
| 156 | <listitem><para>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)</code>.</para></listitem> |
| 157 | <listitem><para>Functions with a variable number of arguments, such as those acception <symbol>&optional;</symbol>, <symbol>&rest;</symbol> or <symbol>&key;</symbol> arguments, must take as first argument the number of remaining ones, as in <code>cl_object cl_list(cl_narg narg, ...)</code>. Here <replaceable>narg</replaceable> is the number of supplied arguments.</para></listitem> |
| 158 | </itemizedlist> |
| 159 | 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.</para> |
| 160 | |
| 161 | <para>As an example let us assume that the user wants to invoke two functions which are part of the &ANSI; standard and thus are exported with a C name. The first example is <function>cl_cos</function>, which takes just one argument and has a signature <code>cl_object cl_cos(cl_object)</code>.</para> |
| 162 | <programlisting> |
| 163 | #include <math.h> |
| 164 | ... |
| 165 | cl_object angle = ecl_make_double_float(M_PI); |
| 166 | cl_object c = cl_cos(angle); |
| 167 | printf("\nThe cosine of PI is %g\n", ecl_double_float(c)); |
| 168 | </programlisting> |
| 169 | |
| 170 | <para>The second example also involves some Mathematics, but now we are going to use the C function corresponding to <symbol>+</symbol>. As described in <link linkend="ansi.numbers.c-dict.ref">the C dictionary</link>, the C name for the plus operator is <function>cl_P</function> and has a signature <code>cl_object cl_P(cl_narg narg,...)</code>. Our example now reads as follows</para> |
| 171 | <programlisting> |
| 172 | cl_object one = ecl_make_fixnum(1); |
| 173 | cl_object two = cl_P(2, one, one); |
| 174 | cl_object three = cl_P(3, one, one, one); |
| 175 | printf("\n1 + 1 is %d\n", ecl_fixnum(two)); |
| 176 | printf("\n1 + 1 + 1 is %d\n", ecl_fixnum(three)); |
| 177 | </programlisting> |
| 178 | |
| 179 | <para>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 <function>cl_funcall</function> or <function>cl_apply</function>. The previous examples may thus be rewritten as follows</para> |
| 180 | <programlisting> |
| 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 | </programlisting> |
| 189 | |
| 190 | <para>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, <constant>ECL_C_CALL_ARGUMENTS_LIMIT</constant>, which is as of this writing 63. The use of this stack is transparently handled by the Common Lisp functions, such as <symbol>apply</symbol>, <symbol>funcall</symbol> and their C equivalents, and also by a set of macros, <link linkend="ref.ecl_va_arg"><function>cl_va_arg</function></link>, which can be used for coding functions that take an arbitrary name of arguments.</para> |
| 191 | </section> |
| 192 | |
| 193 | <xi:include href="ref_c_data_flow.xml" xpointer="ansi.data-and-control.c-dict" xmlns:xi="http://www.w3.org/2001/XInclude"/> |
| 194 | |
| 195 | </chapter> |
| 196 | </book> |