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