| 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.overview"> |
| 8 | <title>Overview</title> |
| 9 | |
| 10 | <section> |
| 11 | <title>Reading this manual</title> |
| 12 | |
| 13 | <section> |
| 14 | <title>Common Lisp users</title> |
| 15 | |
| 16 | <para>&ECL; supports all Common-Lisp data types exactly as defined |
| 17 | in the &ANSI;. All functions and macros are expected to behave as |
| 18 | described in that document and in the HyperSpec &HyperSpec; which |
| 19 | is the online version of &ANSI;. In other words, the Standard is |
| 20 | the basic reference for Common Lisp and also for &ECL;, and this |
| 21 | first part of the book just complements it, describing |
| 22 | implementation-specific features such as |
| 23 | <itemizedlist> |
| 24 | <listitem><para>Platform dependent limits.</para></listitem> |
| 25 | <listitem><para>Behavior which is marked as <quote>implementation specific</quote> in the standard.</para></listitem> |
| 26 | <listitem><para>Some corner cases which are not described in &ANSI;.</para></listitem> |
| 27 | <listitem><para>The philosophy behind certain implementation choices, etc.</para></listitem> |
| 28 | </itemizedlist> |
| 29 | </para> |
| 30 | |
| 31 | <para>In order to aid in locating these differences, this first |
| 32 | part of the manual copies the structure of the &ANSI; standard, |
| 33 | having the same number of chapters, each one with a set of sections |
| 34 | documenting the implementation-specific details.</para> |
| 35 | |
| 36 | </section> |
| 37 | |
| 38 | <section> |
| 39 | <title>C/C++ programmers</title> |
| 40 | |
| 41 | <para>The second goal of this document is to provide a reference |
| 42 | for C programmers that want to create, manipulate and operate with |
| 43 | Common Lisp programs at a lower level, or simply embedding &ECL; as |
| 44 | a library.</para> |
| 45 | |
| 46 | <para>The C/C++ reference evolves in parallel with the Common Lisp |
| 47 | one, in the form of one section with the name "C Reference" for |
| 48 | each chapter of the &ANSI; standard. Much of what is presented in |
| 49 | those sections is redundant with the Common Lisp specification. In |
| 50 | particular, there is a one-to-one mapping between types and |
| 51 | functions which should be obvious given the rules explained in |
| 52 | <xref linkend="ansi.overview.c-dict"/>.</para> |
| 53 | |
| 54 | <para>We must remark that the reference in this part of the manual |
| 55 | is not enough to know how to embed &ECL; in a program. In practice |
| 56 | the user or developer will also have to learn how to <link |
| 57 | linkend="ext.asdf">build programs</link>, <link |
| 58 | linkend="ext.ffi">interface with foreign libraries</link>, <link |
| 59 | linkend="ext.memory">manage memory</link>, etc. These concepts are |
| 60 | explained in a different part of the book.</para> |
| 61 | </section> |
| 62 | </section> |
| 63 | |
| 64 | <section xml:id="ansi.overview.c-dict"> |
| 65 | <title>C Reference</title> |
| 66 | |
| 67 | <section xml:id="cl_object"> |
| 68 | <title>One type for everything: <type>cl_object</type></title> |
| 69 | |
| 70 | <para>&ECL; is designed around the basic principle that Common Lisp |
| 71 | already provides everything that a programmer could need, orienting |
| 72 | itself around the creation and manipulation of Common Lisp objects: |
| 73 | conses, arrays, strings, characters, ... When embedding &ECL; there |
| 74 | should be no need to use other C/C++ types, except when interfacing |
| 75 | data to and from those other languages.</para> |
| 76 | |
| 77 | <para>All Common Lisp objects are represented internally through |
| 78 | the same C type, <type>cl_object</type>, which is either a pointer |
| 79 | to a union type or an integer, depending on the situation. While |
| 80 | the inner guts of this type are exposed through various headers, |
| 81 | the user should never rely on these details but rather use the |
| 82 | macros and functions that are listed in this manual</para> |
| 83 | |
| 84 | <para>There are two types of Common Lisp objects: immediate and |
| 85 | memory allocated ones. Immediate types fit in the bits of the |
| 86 | <type>cl_object</type> word, and do not require the garbage |
| 87 | collector to be created. The list of such types may depend on the |
| 88 | platform, but it includes at least the <type>fixnum</type> and |
| 89 | <type>character</type> types.</para> |
| 90 | |
| 91 | <para>Memory allocated types on the other hand require the use of |
| 92 | the garbage collector to be created. &ECL; abstracts this from the |
| 93 | user providing enough constructors, either in the form of Common |
| 94 | Lisp functions (<function>cl_make_array()</function>, |
| 95 | <function>cl_complex()</function>,...), or in the form of C/C++ |
| 96 | constructors (<function>ecl_make_symbol()</function>, etc).</para> |
| 97 | |
| 98 | <para>Memory allocated types must always be kept alive so that the |
| 99 | garbage collector does not reclaim them. This involves referencing |
| 100 | the object from one of the places that the collector scans: |
| 101 | <itemizedlist> |
| 102 | <listitem><para>The fields of an object (array, structure, etc) whic is itself alive.</para></listitem> |
| 103 | <listitem><para>A special variable or a constant.</para></listitem> |
| 104 | <listitem><para>The C stack (i.e. automatic variables in a function).</para></listitem> |
| 105 | <listitem><para>Global variables or pointers that have been registered with the garbage collector.</para></listitem> |
| 106 | </itemizedlist> |
| 107 | Further details will be provided in the section on <link linkend="ext.memory">Memory Management</link>.</para> |
| 108 | </section> |
| 109 | |
| 110 | <section> |
| 111 | <title>Naming conventions</title> |
| 112 | |
| 113 | <para>As explained in the introduction, each of the chapters in the |
| 114 | Common Lisp standard can also be implemented using C functions and |
| 115 | types. The mapping between both languages is done using a small set |
| 116 | of rules described below.</para> |
| 117 | <itemizedlist> |
| 118 | <listitem><para>Functions in the Common Lisp ("CL") package are prefixed with the characters "cl_", functions in the System ("SI") package are prefix with "si_", etc, etc.</para></listitem> |
| 119 | <listitem><para>If a function takes only a fixed number of arguments, it is mapped to a C function with also a fixed number of arguments. For instance, <symbol>COS</symbol> maps to <code>cl_object cl_cos(cl_object)</code>, which takes a single Lisp object and returns a Lisp object of type <type>FLOAT</type>.</para></listitem> |
| 120 | <listitem><para>If the function takes a variable number of arguments, its signature consists on an integer with the number of arguments and zero or more of required arguments and then a C vararg. This is the case of <code>cl_object cl_list(cl_narg narg, ...)</code>, which can be invoked without arguments, as in <code>cl_list(0)</code>, with one, <code>cl_list(1, a)</code>, etc.</para></listitem> |
| 121 | <listitem><para>Functions return at least one value, which is either the first value output by the function, or <symbol>NIL</symbol>. The extra values may be retrieved immediately after the function call using the function <link linkend="ecl_nth_value"><function>ecl_nth_value</function></link>.</para></listitem> |
| 122 | </itemizedlist> |
| 123 | <para>In addition to the Common Lisp core functions (cl_*), there exist functions which are devoted only to C/C++ programming, with tasks such as coercion of objects to and from C types, optimized functions, inlined macroexpansions, etc. These functions and macros typically carry the prefix "ecl_" or "ECL_" and only return one value, if any.</para> |
| 124 | </section> |
| 125 | |
| 126 | <section xml:id="ansi.OCL"> |
| 127 | <title>Only in Common Lisp</title> |
| 128 | |
| 129 | <para>Some parts of the language are not available as C functions, |
| 130 | even though they can be used in Common Lisp programs. These parts |
| 131 | are either marked in the "ANSI Dictionary" sections using the tag |
| 132 | &OCL;, or they are simply not mentioned (macros and special |
| 133 | constructs). This typically happens with non-translatable |
| 134 | constructs such as</para> |
| 135 | <itemizedlist> |
| 136 | <listitem><para>Common Lisp macros such as <symbol>with-open-files</symbol>.</para></listitem> |
| 137 | <listitem><para>Common Lisp special forms, such as <symbol>cond</symbol></para></listitem> |
| 138 | <listitem><para>Common Lisp generic functions, which cannot be written in C because of their dynamical dispatch and automatic redefinition properties.</para></listitem> |
| 139 | </itemizedlist> |
| 140 | |
| 141 | <para>In most of those cases there exist straightforward alternatives using the constructs and functions in &ECL;. For example, <symbol>unwind-protect</symbol> can be implemented using a C macro which is provided by &ECL;</para> |
| 142 | <programlisting> |
| 143 | cl_env_ptr env = ecl_process_env(); |
| 144 | CL_UNWIND_PROTECT_BEGIN(env) { |
| 145 | /* protected code goes here */ |
| 146 | } CL_UNWIND_PROTECT_EXIT { |
| 147 | /* exit code goes here */ |
| 148 | } CL_UNWIND_PROTECT_END; |
| 149 | </programlisting> |
| 150 | |
| 151 | <para>Common Lisp generic functions can be directly accessed using |
| 152 | <symbol>funcall</symbol> or <symbol>apply</symbol> and the function |
| 153 | name, as shown in the code below</para> |
| 154 | <programlisting> |
| 155 | cl_object name = ecl_make_symbol("MY-GENERIC-FUNCTION","CL-USER"); |
| 156 | cl_object output = cl_funcall(2, name, argument); |
| 157 | </programlisting> |
| 158 | |
| 159 | <para>Identifying these alternatives requires some knowledge of |
| 160 | Common Lisp, which is why it is recommended to approach the |
| 161 | embeddable components in &ECL; only when there is some familiarity |
| 162 | with the language.</para> |
| 163 | </section> |
| 164 | </section> |
| 165 | </chapter> |
| 166 | </book> |