| 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"><part xml:id="part.internals"><title>Internals</title> |
| 7 |
<partintro> |
| 8 |
<para>&ECL; is an implementation of the Common-Lisp language that is based on a kernel |
| 9 |
written in C plus a set of libraries written in Common-Lisp. The kernel includes a |
| 10 |
bytecodes compiler, an interpreter, and enough functions to create and |
| 11 |
manipulate all lisp objects. The lisp libraries provide higher level constructs |
| 12 |
such as macro definitions, LOOPs, an implementation of CLOS, and a translator |
| 13 |
from Lisp to C.</para> |
| 14 |
<para>As a result of this design, which dates back to the Kyoto CL and was later |
| 15 |
improved in Giuseppe Attardi's ECoLisp, &ECL; can be used as</para> |
| 16 |
<itemizedlist mark="bullet"> |
| 17 |
<listitem> |
| 18 |
<para>As a standalone implementation of the Common-Lisp language</para> |
| 19 |
</listitem> |
| 20 |
<listitem> |
| 21 |
<para>As an embedded interpreter subject to the control of a larger C program.</para> |
| 22 |
</listitem> |
| 23 |
<listitem> |
| 24 |
<para>As a Common-Lisp environment with C/C++ extensions.</para> |
| 25 |
</listitem> |
| 26 |
</itemizedlist> |
| 27 |
<para role="continues">This manual describes the facility of &ECL; to interface the C language and |
| 28 |
&ECL;. With this facility, the user can arrange his or her C-language |
| 29 |
programs so that they can be invoked from &ECL;. In addition, the user can |
| 30 |
write Lisp function definitions in the C language to increase runtime |
| 31 |
efficiency.</para> |
| 32 |
</partintro> |
| 33 |
|
| 34 |
<chapter xml:id="Building-programs"> |
| 35 |
<title>Building programs</title> |
| 36 |
<para>In this section we describe how you can use &ECL; to build programs and |
| 37 |
loadable extensions that you can later on distribute to other people.</para> |
| 38 |
|
| 39 |
<section xml:id="Internals-What-can-ECL-do-"> |
| 40 |
<title>What can &ECL; do?</title> |
| 41 |
<para>Some day for some reasons you will be in the need to distribute code that |
| 42 |
has been developed using &ECL;. In the following sections we will describe |
| 43 |
the means that &ECL; offers you to do so. Basically, these are the |
| 44 |
alternatives</para> |
| 45 |
<variablelist> |
| 46 |
<varlistentry> |
| 47 |
<term>Source code</term> |
| 48 |
<listitem> |
| 49 |
<para>You distribute your programs in source code form. This is the easiest and most |
| 50 |
portable way, but not the fastest one.</para> |
| 51 |
</listitem> |
| 52 |
</varlistentry> |
| 53 |
<varlistentry> |
| 54 |
<term>Standalone programs</term> |
| 55 |
<listitem> |
| 56 |
<para>You translate all your lisp code to C using the &ECL; compiler. The final |
| 57 |
object files can be linked against other C/C++ libraries to obtain a standalone |
| 58 |
executable.</para> |
| 59 |
</listitem> |
| 60 |
</varlistentry> |
| 61 |
<varlistentry> |
| 62 |
<term>You can build statically and dynamically linked libraries.</term> |
| 63 |
<listitem> |
| 64 |
<para>You translate all your lisp code to C and combine the resulting object files |
| 65 |
into a single library with <filename>.a</filename> extension. You can distribute this library |
| 66 |
to other people and the final users can utilize these libraries to build |
| 67 |
standalone programs.</para> |
| 68 |
</listitem> |
| 69 |
</varlistentry> |
| 70 |
<varlistentry> |
| 71 |
<term>You can build dynamically loadable files.</term> |
| 72 |
<listitem> |
| 73 |
<para>This is the most flexible way. You translate all lisp code to C and link it |
| 74 |
against possibly other C/C++ libraries to obtain a dynamically loadable library |
| 75 |
(file type <filename>.so</filename> under unix). This library can be loaded a startup time to |
| 76 |
add new functionality to the &ECL; environment.</para> |
| 77 |
</listitem> |
| 78 |
</varlistentry> |
| 79 |
</variablelist> |
| 80 |
<para>In several of these options, we have mentioned the possibility to include C/C++ |
| 81 |
code. Even if this is possible, you cannot use ordinary C/C++ compilers and |
| 82 |
makefiles to build &ECL; extensions, let it be programs or |
| 83 |
libraries. Briefly, you have to organize your code as follows</para> |
| 84 |
<orderedlist numeration="arabic"> |
| 85 |
<listitem> |
| 86 |
<para>Organize the C code as a library, let it be static or dynamic.</para> |
| 87 |
</listitem> |
| 88 |
<listitem> |
| 89 |
<para>Build a function, say <literal>mymain()</literal>, in which the initialization phase |
| 90 |
for your library is performed.</para> |
| 91 |
</listitem> |
| 92 |
<listitem> |
| 93 |
<para>Group the code that interfaces to Lisp in separate C files, all of which |
| 94 |
should include <literal>#include <ecl/ecl.h></literal> at the beginning.</para> |
| 95 |
</listitem> |
| 96 |
<listitem> |
| 97 |
<para>Compile your lisp source files.</para> |
| 98 |
</listitem> |
| 99 |
<listitem> |
| 100 |
<para>Let &ECL; build the final executable or library.</para> |
| 101 |
</listitem> |
| 102 |
</orderedlist> |
| 103 |
<para role="continues">In the final step there are ways to instruct &ECL; to call your |
| 104 |
initialization function (<literal>mymain()</literal> in the example above). These means |
| 105 |
are explained in the following sections.</para> |
| 106 |
<!-- --> |
| 107 |
</section> |
| 108 |
|
| 109 |
<section xml:id="Internals-Compiling-files"> |
| 110 |
<title>Compiling files</title> |
| 111 |
<para>&ECL; supports two types of compilation. One is bytecodes compilation. This |
| 112 |
process is performed on-the-fly, as you load source files with lisp code. This |
| 113 |
leads to a series of bytes for each instruction, the so called |
| 114 |
"bytecodes". These bytecodes are interpreted in a virtual machine, which is |
| 115 |
written in C and which is reasonably fast.</para> |
| 116 |
<para>The other type of compilation is the so-called "native" compilation. This |
| 117 |
process consists on translating the lisp source file to C language. The |
| 118 |
intermediate file is later compiled using a C compiler. The result is an object |
| 119 |
file which may have different purposes.</para> |
| 120 |
<variablelist> |
| 121 |
<varlistentry> |
| 122 |
<term>Dynamically loadable files or FASL (FASt Loadable) files</term> |
| 123 |
<listitem> |
| 124 |
<para>These are produced in a &ECL; built with support for dynamically loadable |
| 125 |
libraries (Feature <replaceable>:DLOPEN</replaceable> is in <replaceable>*features*</replaceable>), when no extra |
| 126 |
arguments are passed to <literal>compile-file</literal>. These object files typically have |
| 127 |
the <filename>.fas</filename> extension, and can be loaded with <literal>load</literal>. They cannot be used |
| 128 |
to build libraries nor standalone executable programs.</para> |
| 129 |
</listitem> |
| 130 |
</varlistentry> |
| 131 |
<varlistentry> |
| 132 |
<term>linkable object files</term> |
| 133 |
<listitem> |
| 134 |
<para>These are produced when invoking <literal>compile-file</literal> with the keyword argument |
| 135 |
<replaceable>:system-p</replaceable> set to true. The object file typically has the <filename>.o</filename> |
| 136 |
extension. It cannot be loaded with <literal>load</literal>, but it can be used to build |
| 137 |
libraries, standalone executable programs, or larger FASL files.</para> |
| 138 |
</listitem> |
| 139 |
</varlistentry> |
| 140 |
</variablelist> |
| 141 |
<!-- --> |
| 142 |
</section> |
| 143 |
|
| 144 |
<section xml:id="Internals-Building-standalone-executables"> |
| 145 |
<title>Building standalone executables</title> |
| 146 |
<para>To build an executable you need a working &ECL; image with the |
| 147 |
compiler. The function to build customized images is |
| 148 |
<replaceable>c::build-program</replaceable>. The description of this function is as |
| 149 |
follows. Care should be taken that <replaceable>image-name</replaceable> differs from any |
| 150 |
filename in <replaceable>lisp-files</replaceable>.</para> |
| 151 |
<blockquote> |
| 152 |
<screen><indexterm role="fn"><primary>c:build-program</primary></indexterm> Function: <function>c:build-program</function> <varname>{</varname><varname>image-name</varname> <varname>&key</varname> <varname>lisp-files</varname> <varname>ld-flags</varname> <varname>prologue-code</varname> <varname>epilogue-code</varname><varname>}</varname></screen> |
| 153 |
<para>This function builds a lisp image up from the core lisp library, plus all |
| 154 |
components listed in <replaceable>lisp-files</replaceable>. Each component is either:</para> |
| 155 |
<itemizedlist mark="bullet"> |
| 156 |
<listitem> |
| 157 |
<para>A symbol: Names a statically linked library built from lisp code.</para> |
| 158 |
</listitem> |
| 159 |
<listitem> |
| 160 |
<para>A string: Denotes an object file built from lisp code.</para> |
| 161 |
</listitem> |
| 162 |
</itemizedlist> |
| 163 |
<para role="continues"><replaceable>ld-flags</replaceable> is a list of strings with additional parameters to be passed |
| 164 |
to the linker. You can include here your favorite C/C++ libraries.</para> |
| 165 |
<para><replaceable>prologue-code</replaceable> and <replaceable>epilogue-code</replaceable> are used to customize the |
| 166 |
initialization process of the lisp image. In order to build the executable, |
| 167 |
<replaceable>c:build-program</replaceable> first writes down a piece of C code which initializes the |
| 168 |
lisp environment. You can customize the initialization process by suppling code |
| 169 |
to be executed before (<replaceable>prologue-code</replaceable>) or after (<replaceable>epilogue-code</replaceable>) |
| 170 |
setting up the lisp environment. Typically <replaceable>prologue-code</replaceable> defaults to an |
| 171 |
empty string, while <replaceable>epilogue-code</replaceable> invokes the classical lisp |
| 172 |
<replaceable>top-level</replaceable>. Additionally, as a convenience, <replaceable>epilogue-code</replaceable> can |
| 173 |
be either a string with C code or also a list with a lisp form, which |
| 174 |
will be interpreted at run time.</para> |
| 175 |
</blockquote> |
| 176 |
<!-- --> |
| 177 |
</section> |
| 178 |
|
| 179 |
<section xml:id="Internals-Building-libraries"> |
| 180 |
<title>Building libraries</title> |
| 181 |
<para>To build a library you proceed more or less the same way as with standalone |
| 182 |
executables. There are two different functions depending on whether you need |
| 183 |
to build static or shared libraries.</para> |
| 184 |
<blockquote> |
| 185 |
<screen><indexterm role="fn"><primary>c:build-static-library</primary></indexterm> Function: <function>c:build-static-library</function> <varname>{</varname><varname>library-name</varname> <varname>&key</varname> <varname>lisp-files</varname> <varname>prologue-code</varname> <varname>epilogue-code</varname> <varname>init-name</varname><varname>}</varname></screen> |
| 186 |
<screen><indexterm role="fn"><primary>c:build-shared-library</primary></indexterm> Function: <function>c:build-shared-library</function> <varname>{</varname><varname>library-name</varname> <varname>&key</varname> <varname>lisp-files</varname> <varname>prologue-code</varname> <varname>epilogue-code</varname> <varname>ld-flags</varname> <varname>init-name</varname><varname>}</varname></screen> |
| 187 |
<para>This function builds a library file up from the object files listed in |
| 188 |
<replaceable>lisp-files</replaceable>. Each of the arguments to <replaceable>lisp-file</replaceable> must name a single |
| 189 |
object file produced with <literal>compile-file</literal>.</para> |
| 190 |
<para><replaceable>library-name</replaceable> is the physical pathname corresponding to the library. The |
| 191 |
value of <replaceable>library-name</replaceable> must follow some system-specific conventions. To |
| 192 |
make your program portable, <replaceable>library-name</replaceable> should be built using the |
| 193 |
output of <literal>compile-file-pathname</literal>.</para> |
| 194 |
<para><replaceable>prologue-code</replaceable> and <replaceable>epilogue-code</replaceable> are strings with C code to be |
| 195 |
executed before and after initializing the library, respectively. For |
| 196 |
dynamically linked libraries you can also provide a list of strings in |
| 197 |
<replaceable>ld-flags</replaceable>. These strings are additional parameters for the linker and |
| 198 |
their purpose is to link C/C++ extensions into the library.</para> |
| 199 |
<para><replaceable>init-name</replaceable> gives the initialization function of the library a |
| 200 |
user-specified name. Thus a the generated library may be used and/or |
| 201 |
linked to a C application. |
| 202 |
The recommended way to invoke <replaceable>init-name</replaceable> is following: |
| 203 |
<programlisting> |
| 204 |
cl_object the_block = read_VV(OBJNULL, init_FOO /* function name specified by init-name */); |
| 205 |
</programlisting> |
| 206 |
Be sure to call <literal>cl_boot</literal> before the invocation of <replaceable>init-name</replaceable>. |
| 207 |
In order to avoid that the returned object is garbage collected, |
| 208 |
you should keep the result of <literal>read_VV</literal> in a local variable. |
| 209 |
</para> |
| 210 |
</blockquote> |
| 211 |
<!-- --> |
| 212 |
</section> |
| 213 |
|
| 214 |
<section xml:id="Internals-File-names"> |
| 215 |
<title>File names</title> |
| 216 |
<blockquote> |
| 217 |
<screen><indexterm role="fn"><primary>compile-file-pathname</primary></indexterm> Function: <function>compile-file-pathname</function> <varname>{</varname><varname>filename-base</varname> <varname>&key</varname> <varname>output-file</varname> <varname>type</varname><varname>}</varname></screen> |
| 218 |
<para>When compiling lisp files, creating libraries, etc, a number of files are |
| 219 |
produced which are of interest for the user or programmer. However, the name |
| 220 |
of these files will change from system to system. The purpose of the function |
| 221 |
<literal>compile-file-pathname</literal> is to query the compiler about the name of the |
| 222 |
different files that it can produce. Possible values of the <replaceable>type</replaceable> |
| 223 |
argument include:</para> |
| 224 |
<variablelist> |
| 225 |
<varlistentry> |
| 226 |
<term>:fas (default)</term> |
| 227 |
<listitem> |
| 228 |
<para>Standard compiled files that can be loaded with <literal>load</literal>.</para> |
| 229 |
</listitem> |
| 230 |
</varlistentry> |
| 231 |
<varlistentry> |
| 232 |
<term>:c, :data, :h</term> |
| 233 |
<listitem> |
| 234 |
<para>Intermediate files produced by the Lisp-to-C translator.</para> |
| 235 |
</listitem> |
| 236 |
</varlistentry> |
| 237 |
<varlistentry> |
| 238 |
<term>:o</term> |
| 239 |
<listitem> |
| 240 |
<para>Linkable object files.</para> |
| 241 |
</listitem> |
| 242 |
</varlistentry> |
| 243 |
<varlistentry> |
| 244 |
<term>:lib, :static-library</term> |
| 245 |
<listitem> |
| 246 |
<para>A normal library produced with <literal>c:build-static-library</literal>.</para> |
| 247 |
</listitem> |
| 248 |
</varlistentry> |
| 249 |
<varlistentry> |
| 250 |
<term>:dll, :shared-library</term> |
| 251 |
<listitem> |
| 252 |
<para>A dynamically linked library produced with <literal>c:build-shared-library</literal>.</para> |
| 253 |
</listitem> |
| 254 |
</varlistentry> |
| 255 |
<varlistentry> |
| 256 |
<term>:program</term> |
| 257 |
<listitem> |
| 258 |
<para>An executable produced with <literal>c:build-program</literal>.</para> |
| 259 |
</listitem> |
| 260 |
</varlistentry> |
| 261 |
</variablelist> |
| 262 |
<para>The output of this function is system specific. For example, under FreeBSD<screen> |
| 263 |
> (compile-file-pathname "/this/path/mylib" :type :lib) |
| 264 |
#P"/this/path/libmylib.a" |
| 265 |
> (compile-file-pathname "/this/path/mylib" :type :dll) |
| 266 |
#P"/this/path/libmylib.so" |
| 267 |
> (compile-file-pathname "/this/path/mycode") |
| 268 |
#P"/this/path/mycode.fas" |
| 269 |
</screen></para> |
| 270 |
</blockquote> |
| 271 |
<!-- --> |
| 272 |
</section> |
| 273 |
|
| 274 |
<section xml:id="Internals-Compiler-examples"> |
| 275 |
<title>Compiler examples</title> |
| 276 |
<section> |
| 277 |
<title>The <filename>hello.lisp</filename> file</title> |
| 278 |
<para>In the following examples we will use the same lisp program. You have to |
| 279 |
create a file called <filename>hello.lisp</filename> which contains the following lines</para> |
| 280 |
<programlisting> |
| 281 |
(princ "Hello world!") |
| 282 |
(terpri) |
| 283 |
(quit) |
| 284 |
</programlisting> |
| 285 |
<para role="continues">If you start &ECL; and load this file in the Common-Lisp environment you |
| 286 |
will see the <literal>"Hello world!"</literal> message and the interpreter will be closed.<screen> |
| 287 |
ECL (Embeddable Common-Lisp) 0.9d |
| 288 |
Copyright (C) 1984 Taiichi Yuasa and Masami Hagiya |
| 289 |
Copyright (C) 1993 Giuseppe Attardi |
| 290 |
Copyright (C) 2000 Juan J. Garcia-Ripoll |
| 291 |
ECL is free software, and you are welcome to redistribute it |
| 292 |
under certain conditions; see file 'Copyright' for details. |
| 293 |
Type :h for Help. Top level. |
| 294 |
> <lineannotation>(load "hello.lisp")</lineannotation> |
| 295 |
;;; Loading "hello.lisp" |
| 296 |
Hello World! |
| 297 |
</screen></para> |
| 298 |
</section> |
| 299 |
|
| 300 |
<section> |
| 301 |
<title>Example of loadable object file</title> |
| 302 |
<para>You can only perform the example in this section if your &ECL; image supports |
| 303 |
dynamically loading of object files. This is true if you find the keyword |
| 304 |
<replaceable>:dlopen</replaceable> in the <replaceable>*features*</replaceable> variable. This is true, for instance, |
| 305 |
in a typical FreeBSD or Linux box,<screen> |
| 306 |
Type :h for Help. Top level. |
| 307 |
> <lineannotation>*features*</lineannotation> |
| 308 |
(:IEEE-FLOATING-POINT :IBM-PC :I386 :BSD :UNIX :DLOPEN :ANSI-CL :CLOS |
| 309 |
:BOEHM-GC :ECL :COMMON) |
| 310 |
</screen></para> |
| 311 |
<para>In this example we build a loadable extension which prints the <literal>"Hello |
| 312 |
world!"</literal> message. First you need to create a the <filename>hello.lisp</filename> file. Next |
| 313 |
you have to enter the &ECL; environment and type <literal>(compile-file |
| 314 |
"hello.lisp")</literal>. This produces a loadable object file.</para> |
| 315 |
<para><screen> |
| 316 |
Type :h for Help. Top level. |
| 317 |
> <lineannotation>(compile-file "hello.lisp")</lineannotation> |
| 318 |
;;; Loading #P"/usr/lib/ecl/cmp.fas" |
| 319 |
;;; Loading #P"/usr/lib/ecl/sysfun.lsp" |
| 320 |
;;; Compiling hello.lisp. |
| 321 |
;;; End of Pass 1. |
| 322 |
;;; Calling the C compiler... |
| 323 |
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "hello.c" -o "hello.o" |
| 324 |
;;; Invoking external command: gcc -o "hello.fas" -L"/usr/lib/ecl/" "hello.o" -Wl,–rpath,/usr/lib/ecl/ -shared -lecl -lgmp -lgc -ldl -lm |
| 325 |
;;; OPTIMIZE levels: Safety=2, Space=0, Speed=3 |
| 326 |
;;; Finished compiling hello.lisp. |
| 327 |
#P"hello.fas" |
| 328 |
Top level. |
| 329 |
> <lineannotation>(load "hello")</lineannotation> |
| 330 |
;;; Loading #P"hello.fas" |
| 331 |
Hello World! |
| 332 |
</screen></para> |
| 333 |
</section> |
| 334 |
|
| 335 |
<section> |
| 336 |
<title>Example of standalone program</title> |
| 337 |
<para>In this example we build a standalone program which prints the <literal>"Hello |
| 338 |
world!"</literal> message and does nothing else. First you must create the |
| 339 |
<filename>hello.lisp</filename> file shown above. Next you have to enter the &ECL; |
| 340 |
environment and type <literal>(compile-file "hello.lisp" :system-p t)</literal>. This |
| 341 |
produces an object file that can be linked against the &ECL; core image.</para> |
| 342 |
<para><screen> |
| 343 |
Type :h for Help. Top level. |
| 344 |
> <lineannotation>(compile-file "hello.lisp" :system-p t)</lineannotation> |
| 345 |
;;; Loading #P"/usr/lib/ecl/cmp.fas" |
| 346 |
;;; Loading #P"/usr/lib/ecl/sysfun.lsp" |
| 347 |
;;; Compiling hello.lisp. |
| 348 |
;;; End of Pass 1. |
| 349 |
;;; Calling the C compiler... |
| 350 |
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "hello.c" -o "hello.o" |
| 351 |
;;; OPTIMIZE levels: Safety=2, Space=0, Speed=3 |
| 352 |
;;; Finished compiling hello.lisp. |
| 353 |
#P"hello.o" |
| 354 |
</screen></para> |
| 355 |
<para role="continues">The final step is to build the executable using the <literal>c:build-program</literal> |
| 356 |
instruction.<screen> |
| 357 |
> <lineannotation>(c:build-program "myecl" :lisp-files '("hello.o"))</lineannotation> |
| 358 |
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "myecl.c" -o "myecl.o" |
| 359 |
;;; Invoking external command: gcc -o "myecl" -L"/usr/lib/ecl/" "myecl.o" "hello.o" -Wl,–rpath,/usr/lib/ecl/ -lecl -lgmp -lgc -ldl -lm |
| 360 |
#P"myecl" |
| 361 |
Top level. |
| 362 |
</screen>Now you can execute this program from your favorite shell.</para> |
| 363 |
<para role="continues"><screen> |
| 364 |
% <lineannotation>./myecl</lineannotation> |
| 365 |
Hello world! |
| 366 |
</screen></para> |
| 367 |
</section> |
| 368 |
|
| 369 |
<section> |
| 370 |
<title>Combining files into a larger FASL</title> |
| 371 |
<para>You can only perform the example in this section if your &ECL; image supports |
| 372 |
dynamically loading of object files. In this example we build a loadable |
| 373 |
library which prints the <literal>"Hello world!"</literal> message and does nothing |
| 374 |
else. First you must create the <filename>hello.lisp</filename> file shown above. Next you |
| 375 |
have to enter the &ECL; environment and type <literal>(compile-file "hello.lisp" |
| 376 |
:system-p t)</literal>. This produces an object file that can be linked to form a loadable |
| 377 |
library.</para> |
| 378 |
<para><screen> |
| 379 |
Type :h for Help. Top level. |
| 380 |
> (compile-file "hello.lisp" :system-p t) |
| 381 |
;;; Loading #P"/usr/lib/ecl/cmp.fas" |
| 382 |
;;; Loading #P"/usr/lib/ecl/sysfun.lsp" |
| 383 |
;;; Compiling hello.lisp. |
| 384 |
;;; End of Pass 1. |
| 385 |
;;; Calling the C compiler... |
| 386 |
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "hello.c" -o "hello.o" |
| 387 |
;;; OPTIMIZE levels: Safety=2, Space=0, Speed=3 |
| 388 |
;;; Finished compiling hello.lisp. |
| 389 |
#P"hello.o" |
| 390 |
</screen></para> |
| 391 |
<para role="continues">The final step is to build the library using the <literal>c:build-fasl</literal> |
| 392 |
instruction.<screen> |
| 393 |
> (c:build-fasl "myecl" :lisp-files '("hello.o")) |
| 394 |
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "myecl.c" -o "myecl.o" |
| 395 |
;;; Invoking external command: gcc -o "libmyecl.so" -L"/usr/lib/ecl/" "myecl.o" "hello.o" -Wl,–rpath,/usr/lib/ecl/ -shared -lecl -lgmp -lgc -ldl -lm |
| 396 |
#P"libmyecl.so" |
| 397 |
</screen>Now you can load this extension from any &ECL; image, even those you produce |
| 398 |
with <literal>c:build-program</literal>.</para> |
| 399 |
<para role="continues"><screen> |
| 400 |
<<<<<<<< THIS EXAMPLE IS WRONG?! >>>>>>>>> |
| 401 |
> (load "myecl") |
| 402 |
;;; Loading myecl.fas |
| 403 |
Hello world! |
| 404 |
Bye. |
| 405 |
</screen></para> |
| 406 |
<!-- --> |
| 407 |
</section> |
| 408 |
</section> |
| 409 |
</chapter> |
| 410 |
|
| 411 |
<chapter xml:id="Lisp-objects"> |
| 412 |
<title>Manipulating Lisp objects</title> |
| 413 |
<para>If you want to extend, fix or simply customize &ECL; for your own needs, |
| 414 |
you should understand how the implementation works.</para> |
| 415 |
|
| 416 |
<section xml:id="Internals-Objects-representation"> |
| 417 |
<title>Objects representation</title> |
| 418 |
<para>In &ECL; a lisp object is represented by a type called <literal>cl_object</literal>. This |
| 419 |
type is a word which is long enough to host both an integer and a pointer. The |
| 420 |
least significant bits of this word, also called the tag bits, determine |
| 421 |
whether it is a pointer to a C structure representing a complex object, or |
| 422 |
whether it is an immediate data, such as a fixnum or a character.</para> |
| 423 |
<screen><![CDATA[ |
| 424 |
|-------------------|--| |
| 425 |
| Fixnum value |01| |
| 426 |
|-------------------|--| |
| 427 |
|
| 428 |
|------------|------|--| |
| 429 |
| Unused bits| char |10| |
| 430 |
|------------|------|--| |
| 431 |
|
| 432 |
|----------------------| |--------|--------|-----|--------| |
| 433 |
| Pointer to cell |---->| word-1 | word-2 | ... | word-n | |
| 434 |
|----------------------| |--------|--------|-----|--------| |
| 435 |
| ...................00| | actual data of the object | |
| 436 |
|----------------------| |--------------------------------| |
| 437 |
]]></screen> |
| 438 |
<para>The fixnums and characters are called immediate datatypes, because they require |
| 439 |
no more than the <literal>cl_object</literal> datatype to store all information. All other |
| 440 |
&ECL; objects are non-immediate and they are represented by a pointer to a |
| 441 |
cell that is allocated on the heap. Each cell consists of several words of |
| 442 |
memory and contains all the information related to that object. By storing data |
| 443 |
in multiples of a word size, we make sure that the least significant bits of a |
| 444 |
pointer are zero, which distinguishes pointers from immediate data.</para> |
| 445 |
<para>In an immediate datatype, the tag bits determine the type of the object. In |
| 446 |
non-immediate datatypes, the first byte in the cell contains the secondary type |
| 447 |
indicator, and distinguishes between different types of non immediate data. The |
| 448 |
use of the remaining bytes differs for each type of object. For instance, a |
| 449 |
cons cell consists of three words:</para> |
| 450 |
<screen><![CDATA[ |
| 451 |
|---------|----------| |
| 452 |
|CONS| | | |
| 453 |
|---------|----------| |
| 454 |
| car-pointer | |
| 455 |
|--------------------| |
| 456 |
| cdr-pointer | |
| 457 |
|--------------------| |
| 458 |
]]></screen> |
| 459 |
<para>There is one important function which tells the type of an object, plus several |
| 460 |
macros which group several tests.</para> |
| 461 |
<blockquote> |
| 462 |
<screen><indexterm role="tp"><primary>cl_object</primary></indexterm> C type: <structname>cl_object</structname></screen> |
| 463 |
<para>This is the type of a lisp object. For your C/C++ program, a <literal>cl_object</literal> |
| 464 |
can be either a fixnum, a character, or a pointer to a union of structures (See |
| 465 |
the header <filename>object.h</filename>). The actual interpretation of that object can be |
| 466 |
guessed with the macro <literal>ecl_t_of</literal>.</para> |
| 467 |
<para>For example, if <replaceable>x</replaceable> is of type <literal>cl_object</literal>, and it is of type fixnum, |
| 468 |
we may retrieve its value</para> |
| 469 |
<screen> |
| 470 |
if (ecl_t_of(x) == t_fixnum) |
| 471 |
printf("Integer value: %d\n", fix(x)); |
| 472 |
</screen> |
| 473 |
<para role="continues">If <replaceable>x</replaceable> is of type <literal>cl_object</literal> and it does not contain an immediate |
| 474 |
datatype, you may inspect the cell associated to the lisp object using <replaceable>x</replaceable> |
| 475 |
as a pointer. For example,</para> |
| 476 |
<screen> |
| 477 |
if (ecl_t_of(x) == t_cons) |
| 478 |
printf("CAR = %x, CDR = %x\n", x->cons.car, x->cons.cdr); |
| 479 |
else if (ecl_t_of(x) == t_string) |
| 480 |
printf("String: %s\n", x->string.self); |
| 481 |
</screen> |
| 482 |
<para role="continues">You should see the following sections and the header <filename>object.h</filename> to learn |
| 483 |
how to use the different fields of a <literal>cl_object</literal> pointer.</para> |
| 484 |
</blockquote> |
| 485 |
<blockquote> |
| 486 |
<screen><indexterm role="tp"><primary>cl_type</primary></indexterm> C type: <structname>cl_type</structname></screen> |
| 487 |
<para>Enumeration type which distinguishes the different types of lisp objects. The |
| 488 |
most important values are t_cons, t_fixnum, t_character, t_bignum, t_ratio, |
| 489 |
t_singlefloat, t_doublefloat, t_complex, t_symbol, t_package, t_hashtable, |
| 490 |
t_array, t_vector, t_string, t_bitvector, t_stream, t_random, t_readtable, |
| 491 |
t_pathname, t_bytecodes, t_cfun, t_cclosure, t_gfun, t_instance, t_foreign and |
| 492 |
t_thread.</para> |
| 493 |
</blockquote> |
| 494 |
<blockquote> |
| 495 |
<screen><indexterm role="fn"><primary>ecl_t_of</primary></indexterm> Function: <returnvalue>cl_type</returnvalue> <function>ecl_t_of</function> (<type>cl_object</type> <varname>O</varname>)</screen> |
| 496 |
<para>If <replaceable>O</replaceable> is a valid lisp object, <literal>ecl_t_of(<replaceable>O</replaceable>)</literal> returns an integer |
| 497 |
denoting the type that lisp object. That integer is one of the values of the |
| 498 |
enumeration type <literal>cl_type</literal>.</para> |
| 499 |
</blockquote> |
| 500 |
<blockquote> |
| 501 |
<screen><indexterm role="fn"><primary>FIXNUMP</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>FIXNUMP</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 502 |
<screen><indexterm role="fn"><primary>CHARACTERP</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>CHARACTERP</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 503 |
<screen><indexterm role="fn"><primary>CONSP</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>CONSP</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 504 |
<screen><indexterm role="fn"><primary>LISTP</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>LISTP</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 505 |
<screen><indexterm role="fn"><primary>ATOM</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>ATOM</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 506 |
<screen><indexterm role="fn"><primary>ARRAYP</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>ARRAYP</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 507 |
<screen><indexterm role="fn"><primary>VECTORP</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>VECTORP</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 508 |
<screen><indexterm role="fn"><primary>STRINGP</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>STRINGP</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 509 |
<para>Different macros that check whether <replaceable>o</replaceable> belongs to the specified type. |
| 510 |
These checks have been optimized, and are preferred over several calls to |
| 511 |
<literal>ecl_t_of</literal>.</para> |
| 512 |
</blockquote> |
| 513 |
<blockquote> |
| 514 |
<screen><indexterm role="fn"><primary>IMMEDIATE</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>IMMEDIATE</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 515 |
<para>Tells whether <replaceable>o</replaceable> is an immediate datatype.</para> |
| 516 |
</blockquote> |
| 517 |
<!-- --> |
| 518 |
</section> |
| 519 |
|
| 520 |
<section xml:id="Internals-Constructing-objects"> |
| 521 |
<title>Constructing objects</title> |
| 522 |
<para>On each of the following sections we will document the standard interface for |
| 523 |
building objects of different types. For some objects, though, it is too |
| 524 |
difficult to make a C interface that resembles all of the functionality in the |
| 525 |
lisp environment. In those cases you need to</para> |
| 526 |
<orderedlist numeration="arabic"> |
| 527 |
<listitem> |
| 528 |
<para>build the objects from their textual representation, or</para> |
| 529 |
</listitem> |
| 530 |
<listitem> |
| 531 |
<para>use the evaluator to build these objects.</para> |
| 532 |
</listitem> |
| 533 |
</orderedlist> |
| 534 |
<para role="continues">The first way makes use of a C or Lisp string to construct an object. The two |
| 535 |
functions you need to know are the following ones.</para> |
| 536 |
<blockquote> |
| 537 |
<screen><indexterm role="fn"><primary>c_string_to_object</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>c_string_to_object</function> (<type>const</type> <varname>char</varname> <type>*</type><varname>s</varname>)</screen> |
| 538 |
<screen><indexterm role="fn"><primary>string_to_object</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>string_to_object</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 539 |
<para><literal>c_string_to_object</literal> builds a lisp object from a C string which contains a |
| 540 |
suitable representation of a lisp object. <literal>string_to_object</literal> performs the |
| 541 |
same task, but uses a lisp string, and therefore it is less useful. Two |
| 542 |
examples of their use</para> |
| 543 |
<screen> |
| 544 |
/* Using a C string */ |
| 545 |
cl_object array1 = c_string_to_object("#(1 2 3 4)"); |
| 546 |
|
| 547 |
/* Using a Lisp string */ |
| 548 |
cl_object string = make_simple_string("#(1 2 3 4)"); |
| 549 |
cl_object array2 = string_to_object(string); |
| 550 |
</screen> |
| 551 |
</blockquote> |
| 552 |
<!-- --> |
| 553 |
</section> |
| 554 |
|
| 555 |
<section xml:id="Internals-Integers"> |
| 556 |
<title>Integers</title> |
| 557 |
<para>Common-Lisp distinguishes two types of integer types: bignums and fixnums. A |
| 558 |
fixnum is a small integer, which ideally occupies only a word of memory and |
| 559 |
which is between the values <replaceable>MOST-NEGATIVE-FIXNUM</replaceable> and |
| 560 |
<replaceable>MOST-POSITIVE-FIXNUM</replaceable>. A bignum is any integer which is not a fixnum and |
| 561 |
it is only constrained by the amount of memory available to represent it.</para> |
| 562 |
<para>In &ECL; a fixnum is an integer that, together with the tag bits, fits in a |
| 563 |
word of memory. The size of a word, and thus the size of a fixnum, varies from |
| 564 |
one architecture to another, and you should refer to the types and constants in |
| 565 |
the <filename>ecl.h</filename> header to make sure that your C extensions are portable. |
| 566 |
All other integers are stored as bignums, they are not immediate objects, they |
| 567 |
take up a variable amount of memory and the GNU Multiprecision Library is |
| 568 |
required to create, manipulate and calculate with them.</para> |
| 569 |
<blockquote> |
| 570 |
<screen><indexterm role="tp"><primary>cl_fixnum</primary></indexterm> C type: <structname>cl_fixnum</structname></screen> |
| 571 |
<para>This is a C signed integer type capable of holding a whole fixnum without any |
| 572 |
loss of precision. The opposite is not true, and you may create a |
| 573 |
<literal>cl_fixnum</literal> which exceeds the limits of a fixnum and should be stored as a |
| 574 |
bignum.</para> |
| 575 |
</blockquote> |
| 576 |
<blockquote> |
| 577 |
<screen><indexterm role="tp"><primary>cl_index</primary></indexterm> C type: <structname>cl_index</structname></screen> |
| 578 |
<para>This is a C unsigned integer type capable of holding a nonnegative fixnum without |
| 579 |
loss of precision. Typically, a <literal>cl_index</literal> is used as an index into an array, |
| 580 |
or into a proper list, etc.</para> |
| 581 |
</blockquote> |
| 582 |
<blockquote> |
| 583 |
<screen><indexterm role="vr"><primary>MOST_NEGATIVE_FIXNUM</primary></indexterm> Constant: <varname>MOST_NEGATIVE_FIXNUM</varname></screen> |
| 584 |
<screen><indexterm role="vr"><primary>MOST_POSITIVE_FIXNUM</primary></indexterm> Constant: <varname>MOST_POSITIVE_FIXNUM</varname></screen> |
| 585 |
<para>These constants mark the limits of a fixnum.</para> |
| 586 |
</blockquote> |
| 587 |
<blockquote> |
| 588 |
<screen><indexterm role="fn"><primary>FIXNUM_MINUSP</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>FIXNUM_MINUSP</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 589 |
<screen><indexterm role="fn"><primary>FIXNUM_PLUSP</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>FIXNUM_PLUSP</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 590 |
<para>These functions perform the checks (<replaceable>o</replaceable> < 0) and (0 <= <replaceable>o</replaceable>), |
| 591 |
respectively.</para> |
| 592 |
</blockquote> |
| 593 |
<blockquote> |
| 594 |
<screen><indexterm role="fn"><primary>MAKE_FIXNUM</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>MAKE_FIXNUM</function> (<type>cl_fixnum</type> <varname>n</varname>)</screen> |
| 595 |
<screen><indexterm role="fn"><primary>fix</primary></indexterm> Function: <returnvalue>cl_fixnum</returnvalue> <function>fix</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 596 |
<para><literal>MAKE_FIXNUM</literal> and <literal>fix</literal> convert from an integer to a lisp object |
| 597 |
of fixnum type and vice versa. These functions no not check their arguments.</para> |
| 598 |
</blockquote> |
| 599 |
<blockquote> |
| 600 |
<screen><indexterm role="fn"><primary>fixint</primary></indexterm> Function: <returnvalue>cl_fixnum</returnvalue> <function>fixint</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 601 |
<para>Converts a lisp fixnum to a C integer of the appropriate size. Signals an error |
| 602 |
if <replaceable>o</replaceable> is not of fixnum type.</para> |
| 603 |
</blockquote> |
| 604 |
<blockquote> |
| 605 |
<screen><indexterm role="fn"><primary>fixnnint</primary></indexterm> Function: <returnvalue>cl_index</returnvalue> <function>fixnnint</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 606 |
<para>Similar to <literal>fixint</literal> but also ensures that <replaceable>o</replaceable> is not negative.</para> |
| 607 |
</blockquote> |
| 608 |
<!-- --> |
| 609 |
</section> |
| 610 |
|
| 611 |
<section xml:id="Internals-Characters"> |
| 612 |
<title>Characters</title> |
| 613 |
<para>&ECL; has only one type of characters, which fits in the C type <literal>char</literal>. |
| 614 |
The following constants and functions operate on characters.</para> |
| 615 |
<blockquote> |
| 616 |
<screen><indexterm role="vr"><primary>CHAR_CODE_LIMIT</primary></indexterm> Constant: <varname>CHAR_CODE_LIMIT</varname></screen> |
| 617 |
<para>Each character is assigned an integer code which ranges from 0 to |
| 618 |
(<replaceable>CHAR_CODE_LIMIT</replaceable>-1).</para> |
| 619 |
</blockquote> |
| 620 |
<blockquote> |
| 621 |
<screen><indexterm role="fn"><primary>CHAR_CODE</primary></indexterm> Function: <returnvalue>cl_fixnum</returnvalue> <function>CHAR_CODE</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 622 |
<screen><indexterm role="fn"><primary>char_code</primary></indexterm> Function: <returnvalue>cl_fixnum</returnvalue> <function>char_code</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 623 |
<para>Returns the integer code associated to a lisp character. Only <literal>char_code</literal> |
| 624 |
checks its arguments.</para> |
| 625 |
</blockquote> |
| 626 |
<blockquote> |
| 627 |
<screen><indexterm role="fn"><primary>CODE_CHAR</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>CODE_CHAR</function> (<type>cl_fixnum</type> <varname>o</varname>)</screen> |
| 628 |
<para>Returns the lisp character associated to an integer code. It does not check |
| 629 |
its arguments.</para> |
| 630 |
</blockquote> |
| 631 |
<blockquote> |
| 632 |
<screen><indexterm role="fn"><primary>coerce_to_character</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>coerce_to_character</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 633 |
<para>Coerces a lisp object to type character. Valid arguments are a character, |
| 634 |
or a string designator of length 1. In all other cases an error is signaled.</para> |
| 635 |
</blockquote> |
| 636 |
<blockquote> |
| 637 |
<screen><indexterm role="fn"><primary>char_eq</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>char_eq</function> (<type>cl_object</type> <varname>x</varname>, <type>cl_object</type> <varname>y</varname>)</screen> |
| 638 |
<screen><indexterm role="fn"><primary>char_equal</primary></indexterm> Function: <returnvalue>bool</returnvalue> <function>char_equal</function> (<type>cl_object</type> <varname>x</varname>, <type>cl_object</type> <varname>y</varname>)</screen> |
| 639 |
<para>Compare two characters for equality. <literal>char_eq</literal> take case into account and |
| 640 |
<literal>char_equal</literal> ignores it.</para> |
| 641 |
</blockquote> |
| 642 |
<blockquote> |
| 643 |
<screen><indexterm role="fn"><primary>char_cmp</primary></indexterm> Function: <returnvalue>int</returnvalue> <function>char_cmp</function> (<type>cl_object</type> <varname>x</varname>, <type>cl_object</type> <varname>y</varname>)</screen> |
| 644 |
<screen><indexterm role="fn"><primary>char_compare</primary></indexterm> Function: <returnvalue>int</returnvalue> <function>char_compare</function> (<type>cl_object</type> <varname>x</varname>, <type>cl_object</type> <varname>y</varname>)</screen> |
| 645 |
<para>Compare the relative order of two characters. <literal>char_cmp</literal> takes care of |
| 646 |
case and <literal>char_compare</literal> converts all characters to uppercase before |
| 647 |
comparing them.</para> |
| 648 |
</blockquote> |
| 649 |
<!-- --> |
| 650 |
</section> |
| 651 |
|
| 652 |
<section xml:id="Internals-Arrays"> |
| 653 |
<title>Arrays</title> |
| 654 |
<para>An array is an aggregate of data of a common type, which can be accessed with |
| 655 |
one or more nonnegative indices. &ECL; stores arrays as a C structure with a |
| 656 |
pointer to the region of memory which contains the actual data. The cell |
| 657 |
of an array datatype varies depending on whether it is a vector, a bytevector, |
| 658 |
a multidimensional array or a string.</para> |
| 659 |
<para>If <replaceable>x</replaceable> contains a vector, you can access the following fields:</para> |
| 660 |
<variablelist> |
| 661 |
<varlistentry> |
| 662 |
<term><literal>x->vector.elttype</literal></term> |
| 663 |
<listitem> |
| 664 |
<para>The type of the elements of the vector.</para> |
| 665 |
</listitem> |
| 666 |
</varlistentry> |
| 667 |
<varlistentry> |
| 668 |
<term><literal>x->vector.dim</literal></term> |
| 669 |
<listitem> |
| 670 |
<para>The maximum number of elements.</para> |
| 671 |
</listitem> |
| 672 |
</varlistentry> |
| 673 |
<varlistentry> |
| 674 |
<term><literal>x->vector.fillp</literal></term> |
| 675 |
<listitem> |
| 676 |
<para>Actual number of elements in the vector or "fill pointer".</para> |
| 677 |
</listitem> |
| 678 |
</varlistentry> |
| 679 |
<varlistentry> |
| 680 |
<term><literal>x->vector.self</literal></term> |
| 681 |
<listitem> |
| 682 |
<para>Union of pointers of different types. You should choose the right pointer |
| 683 |
depending on <literal>x->vector.elltype</literal></para> |
| 684 |
</listitem> |
| 685 |
</varlistentry> |
| 686 |
<varlistentry> |
| 687 |
<term><literal>x->vector.hasfillp</literal></term> |
| 688 |
<listitem> |
| 689 |
<para>Whether <literal>x->vector.fillp</literal> can be smaller than <literal>x->vector.dim</literal>.</para> |
| 690 |
</listitem> |
| 691 |
</varlistentry> |
| 692 |
</variablelist> |
| 693 |
<para>If <replaceable>x</replaceable> contains a multidimensional array, the cell elements become</para> |
| 694 |
<variablelist> |
| 695 |
<varlistentry> |
| 696 |
<term><literal>x->array.elttype</literal></term> |
| 697 |
<listitem> |
| 698 |
<para>The type of the elements of the array.</para> |
| 699 |
</listitem> |
| 700 |
</varlistentry> |
| 701 |
<varlistentry> |
| 702 |
<term><literal>x->array.dim</literal></term> |
| 703 |
<listitem> |
| 704 |
<para>Number of elements in the array.</para> |
| 705 |
</listitem> |
| 706 |
</varlistentry> |
| 707 |
<varlistentry> |
| 708 |
<term><literal>x->array.rank</literal></term> |
| 709 |
<listitem> |
| 710 |
<para>Number of dimensions of the array.</para> |
| 711 |
</listitem> |
| 712 |
</varlistentry> |
| 713 |
<varlistentry> |
| 714 |
<term><literal>x->array.dims[]</literal></term> |
| 715 |
<listitem> |
| 716 |
<para>Array with the dimensions of the array. The elements range from |
| 717 |
<literal>x->array.dim[0]</literal> to <literal>x->array.dim[x->array.rank-1]</literal>.</para> |
| 718 |
</listitem> |
| 719 |
</varlistentry> |
| 720 |
<varlistentry> |
| 721 |
<term><literal>x->array.self</literal></term> |
| 722 |
<listitem> |
| 723 |
<para>Union of pointers to the actual data. You should choose the right pointer |
| 724 |
depending on <literal>x->array.elltype</literal>.</para> |
| 725 |
</listitem> |
| 726 |
</varlistentry> |
| 727 |
<varlistentry> |
| 728 |
<term><literal>x->array.rank</literal></term> |
| 729 |
<listitem> |
| 730 |
<para>Whether <literal>x->vector.fillp</literal> can be smaller than <literal>x->vector.dim</literal>.</para> |
| 731 |
</listitem> |
| 732 |
</varlistentry> |
| 733 |
</variablelist> |
| 734 |
<para role="continues">Bitvectors and strings are treated separately.</para> |
| 735 |
<para>Each array is of an specialized type which is the type of the elements of the |
| 736 |
array. &ECL; has arrays only a few following specialized types, and for each |
| 737 |
of these types there is a C integer which is the corresponding value of |
| 738 |
<literal>x->array.elttype</literal> or <literal>x->vector.elttype</literal>. We list those types |
| 739 |
together with the C constant that denotes that type:</para> |
| 740 |
<variablelist> |
| 741 |
<varlistentry> |
| 742 |
<term><replaceable>T</replaceable></term> |
| 743 |
<listitem> |
| 744 |
<para><literal>aet_object</literal></para> |
| 745 |
</listitem> |
| 746 |
</varlistentry> |
| 747 |
<varlistentry> |
| 748 |
<term><replaceable>CHARACTER</replaceable></term> |
| 749 |
<listitem> |
| 750 |
<para><literal>aet_ch</literal></para> |
| 751 |
</listitem> |
| 752 |
</varlistentry> |
| 753 |
<varlistentry> |
| 754 |
<term><replaceable>FIXNUM</replaceable></term> |
| 755 |
<listitem> |
| 756 |
<para><literal>aet_fix</literal></para> |
| 757 |
</listitem> |
| 758 |
</varlistentry> |
| 759 |
<varlistentry> |
| 760 |
<term><replaceable>BIT</replaceable></term> |
| 761 |
<listitem> |
| 762 |
<para><literal>aet_bit</literal></para> |
| 763 |
</listitem> |
| 764 |
</varlistentry> |
| 765 |
<varlistentry> |
| 766 |
<term><replaceable>SINGLE-FLOAT</replaceable></term> |
| 767 |
<listitem> |
| 768 |
<para><literal>aet_sf</literal></para> |
| 769 |
</listitem> |
| 770 |
</varlistentry> |
| 771 |
<varlistentry> |
| 772 |
<term><replaceable>DOUBLE-FLOAT</replaceable></term> |
| 773 |
<listitem> |
| 774 |
<para><literal>aet_df</literal></para> |
| 775 |
</listitem> |
| 776 |
</varlistentry> |
| 777 |
</variablelist> |
| 778 |
<blockquote> |
| 779 |
<screen><indexterm role="fn"><primary>array_elttype</primary></indexterm> Function: <returnvalue>cl_elttype</returnvalue> <function>array_elttype</function> (<type>cl_object</type> <varname>o</varname>)</screen> |
| 780 |
<para>Returns the element type of the array <replaceable>o</replaceable>, which can be a string, a |
| 781 |
bitvector, vector, or a multidimensional array. For example, the code |
| 782 |
<literal>array_elttype(c_string_to_object("\"AAA\""))</literal> returns <literal>aet_ch</literal>, |
| 783 |
while the <literal>array_elttype(c_string_to_object("#(A B C)"))</literal> returns |
| 784 |
<literal>aet_object</literal>.</para> |
| 785 |
</blockquote> |
| 786 |
<blockquote> |
| 787 |
<screen><indexterm role="fn"><primary>aref</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>aref</function> (<type>cl_object</type> <varname>array</varname>, <type>cl_index</type> <varname>index</varname>)</screen> |
| 788 |
<screen><indexterm role="fn"><primary>aset</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>aset</function> (<type>cl_object</type> <varname>array</varname>, <type>cl_index</type> <varname>index</varname>, <type>cl_object</type> <varname>value</varname>)</screen> |
| 789 |
<para>These functions are used to retrieve and set the elements of an array. The |
| 790 |
elements are accessed with one index, <replaceable>index</replaceable>, as in the lisp function |
| 791 |
<literal>ROW-MAJOR-AREF</literal>. For example</para> |
| 792 |
<screen> |
| 793 |
cl_object array = c_string_to_object("#2A((1 2) (3 4))"); |
| 794 |
cl_object x = aref(array, 3); |
| 795 |
cl_print(1, x); /* Outputs 4 */ |
| 796 |
aset(array, 3, MAKE_FIXNUM(5)); |
| 797 |
cl_print(1, array); /* Outputs #2A((1 2) (3 5)) */ |
| 798 |
</screen> |
| 799 |
</blockquote> |
| 800 |
<blockquote> |
| 801 |
<screen><indexterm role="fn"><primary>aref1</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>aref1</function> (<type>cl_object</type> <varname>vector</varname>, <type>cl_index</type> <varname>index</varname>)</screen> |
| 802 |
<screen><indexterm role="fn"><primary>aset1</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>aset1</function> (<type>cl_object</type> <varname>vector</varname>, <type>cl_index</type> <varname>index</varname>, <type>cl_object</type> <varname>value</varname>)</screen> |
| 803 |
<para>These functions are similar to <literal>aref</literal> and <literal>aset</literal>, but they operate on |
| 804 |
vectors.</para> |
| 805 |
<screen> |
| 806 |
cl_object array = c_string_to_object("#(1 2 3 4)"); |
| 807 |
cl_object x = aref1(array, 3); |
| 808 |
cl_print(1, x); /* Outputs 4 */ |
| 809 |
aset1(array, 3, MAKE_FIXNUM(5)); |
| 810 |
cl_print(1, array); /* Outputs #(1 2 3 5) */ |
| 811 |
</screen> |
| 812 |
</blockquote> |
| 813 |
<!-- --> |
| 814 |
</section> |
| 815 |
|
| 816 |
<section xml:id="Internals-Strings"> |
| 817 |
<title>Strings</title> |
| 818 |
<para>A string, both in Common-Lisp and in &ECL; is nothing but a vector of |
| 819 |
characters. Therefore, almost everything mentioned in the section of arrays |
| 820 |
remains valid here. The only important difference is that &ECL; stores |
| 821 |
strings as a lisp object with a pointer to a zero terminated C string. Thus, if |
| 822 |
a string has <replaceable>n</replaceable> characters, &ECL; will reserve <replaceable>n</replaceable>+1 bytes for the |
| 823 |
string. This allows us to pass the string <literal>self</literal> pointer to any C |
| 824 |
routine.</para> |
| 825 |
<para>If <replaceable>x</replaceable> is a lisp object of type string, we can access the following fields:</para> |
| 826 |
<variablelist> |
| 827 |
<varlistentry> |
| 828 |
<term><literal>x->string.dim</literal></term> |
| 829 |
<listitem> |
| 830 |
<para>Maximum number of characters that it can contain.</para> |
| 831 |
</listitem> |
| 832 |
</varlistentry> |
| 833 |
<varlistentry> |
| 834 |
<term><literal>x->string.fillp</literal></term> |
| 835 |
<listitem> |
| 836 |
<para>Actual number of characters in the string.</para> |
| 837 |
</listitem> |
| 838 |
</varlistentry> |
| 839 |
<varlistentry> |
| 840 |
<term><literal>x->string.self</literal></term> |
| 841 |
<listitem> |
| 842 |
<para>Pointer to the characters.</para> |
| 843 |
</listitem> |
| 844 |
</varlistentry> |
| 845 |
<varlistentry> |
| 846 |
<term><literal>x->string.hasfillp</literal></term> |
| 847 |
<listitem> |
| 848 |
<para>True if <literal>x->string.fillp</literal> can be smaller than <literal>x->string.dim</literal>.</para> |
| 849 |
</listitem> |
| 850 |
</varlistentry> |
| 851 |
</variablelist> |
| 852 |
<blockquote> |
| 853 |
<screen><indexterm role="fn"><primary>make_simple_string</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>make_simple_string</function> (<type>char</type> <varname>*</varname><varname>s</varname>)</screen> |
| 854 |
<screen><indexterm role="fn"><primary>make_string_copy</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>make_string_copy</function> (<type>char</type> <varname>*</varname><varname>s</varname>)</screen> |
| 855 |
<para>Both routines build a lisp string from a C string. <literal>make_string_copy</literal> |
| 856 |
allocates new space and copies the content of the string to |
| 857 |
it. <literal>make_simple_string</literal> simply uses the memory pointed by <replaceable>s</replaceable>, which |
| 858 |
should not be deallocated. Both routines use <literal>strlen</literal> to calculate the |
| 859 |
length of the string.</para> |
| 860 |
</blockquote> |
| 861 |
</section> |
| 862 |
|
| 863 |
<section xml:id="Internals-Bitvectors"> |
| 864 |
<title>Bitvectors</title> |
| 865 |
</section> |
| 866 |
|
| 867 |
<section xml:id="Internals-Streams"> |
| 868 |
<title>Streams</title> |
| 869 |
</section> |
| 870 |
|
| 871 |
<section xml:id="Internals-Structures"> |
| 872 |
<title>Structures</title> |
| 873 |
</section> |
| 874 |
|
| 875 |
<section xml:id="Internals-Instances"> |
| 876 |
<title>Instances</title> |
| 877 |
<!-- --> |
| 878 |
</section> |
| 879 |
|
| 880 |
<section xml:id="Internals-Bytecodes"> |
| 881 |
<title>Bytecodes</title> |
| 882 |
<para>A bytecodes object is a lisp object with a piece of code that can be |
| 883 |
interpreted. The objects of type <literal>t_bytecode</literal> are implicitly constructed |
| 884 |
by a call to <literal>eval</literal>, but can also be explicitly constructed with the |
| 885 |
<literal>make_lambda</literal> function.</para> |
| 886 |
<blockquote> |
| 887 |
<screen><indexterm role="fn"><primary>cl_safe_eval</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>cl_safe_eval</function> (<type>cl_object</type> <varname>form</varname>, <type>cl_object</type> <varname>env</varname>, <type>cl_object</type> <varname>err_value</varname></screen> |
| 888 |
<screen><indexterm role="fn"><primary>cl_eval</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>cl_eval</function> (<type>cl_object</type> <varname>form</varname>)</screen> |
| 889 |
<para><literal>cl_safe_eval</literal> evaluates <replaceable>form</replaceable> in the lexical environment <replaceable>env</replaceable>, |
| 890 |
which can be <replaceable>nil</replaceable>. Before evaluating it, the expression <replaceable>form</replaceable> must |
| 891 |
be bytecompiled. <literal>cl_eval</literal> is the equivalent of <literal>cl_safe_eval</literal> but |
| 892 |
without environment and with <replaceable>err_value</replaceable> set to <replaceable>nil</replaceable>. It exists only |
| 893 |
for compatibility with previous versions.</para> |
| 894 |
<screen> |
| 895 |
cl_object form = c_string_to_object("(print 1)"); |
| 896 |
cl_safe_eval(form,Cnil); |
| 897 |
cl_safe_eval(form, Cnil); |
| 898 |
</screen> |
| 899 |
</blockquote> |
| 900 |
<blockquote> |
| 901 |
<screen><indexterm role="fn"><primary>si_make_lambda</primary></indexterm> Function: <returnvalue>cl_object</returnvalue> <function>si_make_lambda</function> (<type>cl_object</type> <varname>name</varname>, <type>cl_object</type> <varname>def</varname>)</screen> |
| 902 |
<para>Builds an interpreted lisp function with name given by the symbol <replaceable>name</replaceable> |
| 903 |
and body given by <replaceable>def</replaceable>. For instance, we would achieve the equivalent of</para> |
| 904 |
<programlisting> |
| 905 |
(funcall #'(lambda (x y) (block foo (+ x y))) |
| 906 |
1 2) |
| 907 |
</programlisting> |
| 908 |
<para role="continues">with the following code</para> |
| 909 |
<screen> |
| 910 |
cl_object def = c_string_to_object("((x y) (+ x y))"); |
| 911 |
cl_object name = _intern("foo") |
| 912 |
cl_object fun = si_make_lambda(name, def); |
| 913 |
return funcall(fun, MAKE_FIXNUM(1), MAKE_FIXNUM(2)); |
| 914 |
</screen> |
| 915 |
<para role="continues">Notice that <literal>si_safe_lambda</literal> performs a bytecodes compilation |
| 916 |
of the definition and thus it may signal some errors. Such errors are not |
| 917 |
handled by the routine itself you might consider using <literal>cl_safe_eval</literal> |
| 918 |
or <literal>cl_eval</literal> instead:</para> |
| 919 |
<screen> |
| 920 |
cl_object def = c_string_to_object("#'(lambda-block foo (x y) (+ x y))"); |
| 921 |
cl_object fun = cl_eval(def); |
| 922 |
return funcall(fun, MAKE_FIXNUM(1), MAKE_FIXNUM(2)); |
| 923 |
</screen> |
| 924 |
</blockquote> |
| 925 |
<!-- --> |
| 926 |
</section> |
| 927 |
</chapter> |
| 928 |
|
| 929 |
<chapter xml:id="Internals-the-interpreter"> |
| 930 |
<title>The interpreter</title> |
| 931 |
<section xml:id="Internals-ECL-stacks"> |
| 932 |
<title>&ECL; stacks</title> |
| 933 |
<para>&ECL; uses the following stacks:</para> |
| 934 |
<variablelist> |
| 935 |
<varlistentry> |
| 936 |
<term>Frame Stack</term> |
| 937 |
<listitem> |
| 938 |
<para>consisting of catch, block, tagbody frames</para> |
| 939 |
</listitem> |
| 940 |
</varlistentry> |
| 941 |
<varlistentry> |
| 942 |
<term>Bind Stack</term> |
| 943 |
<listitem> |
| 944 |
<para>for shallow binding of dynamic variables</para> |
| 945 |
</listitem> |
| 946 |
</varlistentry> |
| 947 |
<varlistentry> |
| 948 |
<term>Interpreter Stack</term> |
| 949 |
<listitem> |
| 950 |
<para>acts as a Forth data stack, keeping intermediate arguments to |
| 951 |
interpreted functions, plus a history of called functions.</para> |
| 952 |
</listitem> |
| 953 |
</varlistentry> |
| 954 |
<varlistentry> |
| 955 |
<term>C Control Stack</term> |
| 956 |
<listitem> |
| 957 |
<para>used for arguments/values passing, typed lexical variables, |
| 958 |
temporary values, and function invocation.</para> |
| 959 |
</listitem> |
| 960 |
</varlistentry> |
| 961 |
</variablelist> |
| 962 |
</section> |
| 963 |
|
| 964 |
<section xml:id="Internals-Procedure-Call-Conventions"> |
| 965 |
<title>Procedure Call Conventions</title> |
| 966 |
<para>&ECL; employs standard C calling conventions to achieve efficiency and |
| 967 |
interoperability with other languages. |
| 968 |
Each Lisp function is implemented as a C function which takes as many |
| 969 |
argument as the Lisp original plus one additional integer argument |
| 970 |
which holds the number of actual arguments. The function sets <literal>NValues</literal> |
| 971 |
to the number of Lisp values produced, it returns the first one and the |
| 972 |
remaining ones are kept in a global (per thread) array (<literal>VALUES</literal>).</para> |
| 973 |
<para>To show the argument/value passing mechanism, here we list the actual |
| 974 |
code for the Common-Lisp function <literal>cons</literal>.</para> |
| 975 |
<screen> |
| 976 |
cl_cons(int narg, object car, object cdr) |
| 977 |
{ object x; |
| 978 |
check_arg(2); |
| 979 |
x = alloc_object(t_cons); |
| 980 |
CAR(x) = car; |
| 981 |
CDR(x) = cdr; |
| 982 |
NValues = 1; |
| 983 |
return x; |
| 984 |
} |
| 985 |
</screen> |
| 986 |
<para>&ECL; adopts the convention that the name of a function that implements a |
| 987 |
Common-Lisp function begins with a short package name (<literal>cl</literal> for COMMON-LISP, |
| 988 |
<literal>si</literal> for SYSTEM, etc), followed by <literal>L</literal>, and followed by the name of |
| 989 |
the Common-Lisp function. (Strictly speaking, `<literal>-</literal>' and `<literal>*</literal>' in the |
| 990 |
Common-Lisp function name are replaced by `<literal>_</literal>' and `<literal>A</literal>', respectively, |
| 991 |
to obey the syntax of C.)</para> |
| 992 |
<para><literal>check_arg(2)</literal> in the code of <literal>cl_cons</literal> checks that exactly two |
| 993 |
arguments are supplied to <literal>cons</literal>. That is, it checks that <literal>narg</literal> is |
| 994 |
2, and otherwise, it causes an error. <literal>allocate_object(t_cons)</literal> allocates |
| 995 |
a cons cell in the heap and returns the pointer to the cell. After the |
| 996 |
<literal>CAR</literal> and the <literal>CDR</literal> fields of the cell are set, the cell pointer is |
| 997 |
returned directly. The number assigned to NValues set by the function (1 in |
| 998 |
this case) represents the number of values of the function.</para> |
| 999 |
<para>In general, if one is to play with the C kernel of &ECL; there is no need to |
| 1000 |
know about all these conventions. There is a preprocessor that takes care of |
| 1001 |
the details, by using a lisp representation of the statements that output |
| 1002 |
values, and of the function definitions. For instance, the actual source code |
| 1003 |
for <literal>cl_cons</literal> in <filename>src/c/lists.d</filename></para> |
| 1004 |
<screen> |
| 1005 |
@(defun cons (car cdr) |
| 1006 |
@ |
| 1007 |
@(return CONS(car, cdr)) |
| 1008 |
@) |
| 1009 |
</screen> |
| 1010 |
</section> |
| 1011 |
|
| 1012 |
<section xml:id="Internals-The-lexical-environment"> |
| 1013 |
<title>The lexical environment</title> |
| 1014 |
<para>The &ECL; interpreter uses two A-lists (Association lists) to |
| 1015 |
represent lexical environments.</para> |
| 1016 |
<itemizedlist mark="bullet"> |
| 1017 |
<listitem> |
| 1018 |
<para>One for variable bindings</para> |
| 1019 |
</listitem> |
| 1020 |
<listitem> |
| 1021 |
<para>One for local function/macro/tag/block bindings</para> |
| 1022 |
</listitem> |
| 1023 |
</itemizedlist> |
| 1024 |
<para>When a function closure is created, the current two A-lists are |
| 1025 |
saved in the closure along with the lambda expression. Later, when the |
| 1026 |
closure is invoked, the saved A-lists are |
| 1027 |
used to recover the lexical environment.</para> |
| 1028 |
</section> |
| 1029 |
|
| 1030 |
<section xml:id="Internals-The-interpreter-stack"> |
| 1031 |
<title>The interpreter stack</title> |
| 1032 |
<para>The bytecodes interpreter uses a stack of its own to save and restore values |
| 1033 |
from intermediate calculations. This Forth-like data stack is also used in |
| 1034 |
other parts of the C kernel for various purposes, such as saving compiled code, |
| 1035 |
keeping arguments to FORMAT, etc.</para> |
| 1036 |
<para>However, one of the most important roles of the Interpreter Stack is to keep a |
| 1037 |
log of the functions which are called during the execution of bytecodes. For |
| 1038 |
each function invoked, the interpreter keeps three lisp objects on the stack:</para> |
| 1039 |
<screen><![CDATA[ |
| 1040 |
+----------+------------------------------------------------+ |
| 1041 |
| function | lexical environment | index to previous record | |
| 1042 |
+----------+---------------------+--------------------------+ |
| 1043 |
]]></screen> |
| 1044 |
<para>The first item is the object which is funcalled. It can be a bytecodes object, |
| 1045 |
a compiled function or a generic function. In the last two cases the lexical |
| 1046 |
environment is just NIL. In the first case, the second item on the stack is |
| 1047 |
the lexical environment on which the code is executed. Each of these records |
| 1048 |
are popped out of the stack after function invocation.</para> |
| 1049 |
<para>Let us see how these invocation records are used for debugging.<screen> |
| 1050 |
>(defun fact (x) ;;; Wrong definition of the |
| 1051 |
(if (= x 0) ;;; factorial function. |
| 1052 |
one ;;; one should be 1. |
| 1053 |
(* x (fact (1- x))))) |
| 1054 |
FACT |
| 1055 |
|
| 1056 |
>(fact 3) ;;; Tries 3! |
| 1057 |
Error: The variable ONE is unbound. |
| 1058 |
Error signalled by IF. |
| 1059 |
Broken at IF. |
| 1060 |
>>:b ;;; Backtrace. |
| 1061 |
Backtrace: eval > fact > if > fact > if > fact > if > fact > IF |
| 1062 |
;;; Currently at the last IF. |
| 1063 |
>>:h ;;; Help. |
| 1064 |
|
| 1065 |
Break commands: |
| 1066 |
:q(uit) Return to some previous break level. |
| 1067 |
:pop Pop to previous break level. |
| 1068 |
:c(ontinue) Continue execution. |
| 1069 |
:b(acktrace) Print backtrace. |
| 1070 |
:f(unction) Show current function. |
| 1071 |
:p(revious) Go to previous function. |
| 1072 |
:n(ext) Go to next function. |
| 1073 |
:g(o) Go to next function. |
| 1074 |
:fs Search forward for function. |
| 1075 |
:bs Search backward for function. |
| 1076 |
:v(ariables) Show local variables, functions, blocks, and tags. |
| 1077 |
:l(ocal) Return the nth local value on the stack. |
| 1078 |
:hide Hide function. |
| 1079 |
:unhide Unhide function. |
| 1080 |
:hp Hide package. |
| 1081 |
:unhp Unhide package. |
| 1082 |
:unhide-all Unhide all variables and packages. |
| 1083 |
:bds Show binding stack. |
| 1084 |
:m(essage) Show error message. |
| 1085 |
:hs Help stack. |
| 1086 |
Top level commands: |
| 1087 |
:cf Compile file. |
| 1088 |
:exit or ^D Exit Lisp. |
| 1089 |
:ld Load file. |
| 1090 |
:step Single step form. |
| 1091 |
:tr(ace) Trace function. |
| 1092 |
:untr(ace) Untrace function. |
| 1093 |
|
| 1094 |
Help commands: |
| 1095 |
:apropos Apropos. |
| 1096 |
:doc(ument) Document. |
| 1097 |
:h(elp) or ? Help. Type ":help help" for more information. |
| 1098 |
|
| 1099 |
>>:p ;;; Move to the last call of FACT. |
| 1100 |
Broken at IF. |
| 1101 |
|
| 1102 |
>>:b |
| 1103 |
Backtrace: eval > fact > if > fact > if > fact > if > FACT > if |
| 1104 |
;;; Now at the last FACT. |
| 1105 |
>>:v ;;; The environment at the last call |
| 1106 |
Local variables: ;;; to FACT is recovered. |
| 1107 |
X: 0 ;;; X is the only bound variable. |
| 1108 |
Block names: FACT. ;;; The block FACT is established. |
| 1109 |
|
| 1110 |
>>x |
| 1111 |
0 ;;; The value of x is 0. |
| 1112 |
|
| 1113 |
>>(return-from fact 1) ;;; Return from the last call of |
| 1114 |
6 ;;; FACT with the value of 0. |
| 1115 |
;;; The execution is resumed and |
| 1116 |
> ;;; the value 6 is returned. |
| 1117 |
;;; Again at the top-level loop. |
| 1118 |
</screen></para> |
| 1119 |
<!-- --> |
| 1120 |
</section> |
| 1121 |
</chapter> |
| 1122 |
|
| 1123 |
<chapter xml:id="Internals-the-compiler"> |
| 1124 |
<title>The compiler</title> |
| 1125 |
<section xml:id="Internals-The-compiler-translates-to-C"> |
| 1126 |
<title>The compiler translates to C</title> |
| 1127 |
<para>The &ECL; compiler is essentially a translator from Common-Lisp to C. Given |
| 1128 |
a Lisp source file, the compiler first generates three intermediate |
| 1129 |
files:</para> |
| 1130 |
<itemizedlist mark="bullet"> |
| 1131 |
<listitem> |
| 1132 |
<para>a C-file which consists of the C version of the Lisp program</para> |
| 1133 |
</listitem> |
| 1134 |
<listitem> |
| 1135 |
<para>an H-file which consists of declarations referenced in the C-file</para> |
| 1136 |
</listitem> |
| 1137 |
<listitem> |
| 1138 |
<para>a Data-file which consists of Lisp data to be used at load time</para> |
| 1139 |
</listitem> |
| 1140 |
</itemizedlist> |
| 1141 |
<para>The &ECL; compiler then invokes the C compiler to compile the |
| 1142 |
C-file into an object file. Finally, the contents of the Data-file is |
| 1143 |
appended to the object file to make a <emphasis>Fasl-file</emphasis>. The generated |
| 1144 |
Fasl-file can be loaded into the &ECL; system by the Common-Lisp |
| 1145 |
function <literal>load</literal>. By default, the three intermediate files are |
| 1146 |
deleted after the compilation, but, if asked, the compiler leaves |
| 1147 |
them.</para> |
| 1148 |
<para>The merits of the use of C as the intermediate language are:</para> |
| 1149 |
<itemizedlist mark="-"> |
| 1150 |
<listitem> |
| 1151 |
<para>The &ECL; compiler is highly portable.</para> |
| 1152 |
</listitem> |
| 1153 |
<listitem> |
| 1154 |
<para>Cross compilation is possible, because the contents of the |
| 1155 |
intermediate files are common to all versions of &ECL;. For example, |
| 1156 |
one can compile his or her Lisp program by the &ECL; compiler on |
| 1157 |
a Sun, bring the intermediate files to DOS, compile the C-file with |
| 1158 |
the gcc compiler under DOS, and then append the Data-file to the object |
| 1159 |
file. This procedure generates the Fasl-file for the &ECL; system on |
| 1160 |
DOS. This kind of cross compilation makes it easier to port &ECL;.</para> |
| 1161 |
</listitem> |
| 1162 |
<listitem> |
| 1163 |
<para>Hardware-dependent optimizations such as register allocations |
| 1164 |
are done by the C compiler.</para> |
| 1165 |
</listitem> |
| 1166 |
</itemizedlist> |
| 1167 |
<para>The demerits are:</para> |
| 1168 |
<itemizedlist mark="-"> |
| 1169 |
<listitem> |
| 1170 |
<para>At those sites where no C compiler is available, |
| 1171 |
the users cannot compile their Lisp programs.</para> |
| 1172 |
</listitem> |
| 1173 |
<listitem> |
| 1174 |
<para>The compilation time is long. 70% to 80% of the |
| 1175 |
compilation time is used by the C compiler. The &ECL; compiler is |
| 1176 |
therefore slower than compiler generating machine code directly.</para> |
| 1177 |
</listitem> |
| 1178 |
</itemizedlist> |
| 1179 |
</section> |
| 1180 |
|
| 1181 |
<section xml:id="Internals-The-compiler-mimics-human-C-programmer"> |
| 1182 |
<title>The compiler mimics human C programmer</title> |
| 1183 |
<para>The format of the intermediate C code generated by the &ECL; compiler is the |
| 1184 |
same as the hand-coded C code of the &ECL; source programs. For example, |
| 1185 |
supposing that the Lisp source file contains the |
| 1186 |
following function definition:</para> |
| 1187 |
<programlisting> |
| 1188 |
(defvar *delta* 2) |
| 1189 |
(defun add1 (x) (+ *delta* x)) |
| 1190 |
</programlisting> |
| 1191 |
<para role="continues">The compiler generates the following intermediate C code.</para> |
| 1192 |
<screen> |
| 1193 |
/* function definition for ADD1 */ |
| 1194 |
static cl_object L1(cl_object V1) |
| 1195 |
{ VT2 VLEX2 CLSR2 |
| 1196 |
cl_object value0; |
| 1197 |
value0=number_plus(symbol_value(VV[0]),V1); NVALUES=1; |
| 1198 |
return value0; |
| 1199 |
} |
| 1200 |
/* initialization of this module */ |
| 1201 |
void init_CODE(cl_object flag) |
| 1202 |
{ VT1 CLSR1 |
| 1203 |
cl_object value0; |
| 1204 |
if (!FIXNUMP(flag)){ |
| 1205 |
Cblock=flag; |
| 1206 |
#ifndef ECL_DYNAMIC_VV |
| 1207 |
flag->cblock.data = VV; |
| 1208 |
#endif |
| 1209 |
flag->cblock.self_destruct=0; |
| 1210 |
flag->cblock.data_size = VM; |
| 1211 |
flag->cblock.data_text = compiler_data_text; |
| 1212 |
flag->cblock.data_text_size = compiler_data_text_size; |
| 1213 |
return;} |
| 1214 |
#ifdef ECL_DYNAMIC_VV |
| 1215 |
VV = Cblock->cblock.data; |
| 1216 |
#endif |
| 1217 |
T0= MAKE_FIXNUM(2); |
| 1218 |
si_Xmake_special(VV[0]) |
| 1219 |
if(SYM_VAL(T0)!=OBJNULL) cl_setq(VV[0],T0); |
| 1220 |
cl_def_c_function(VV[1],(void*)L1,1); |
| 1221 |
} |
| 1222 |
</screen> |
| 1223 |
<para>The C function <literal>L1</literal> implements the Lisp function <literal>add1</literal>. |
| 1224 |
This relation is established by <literal>cl_def_c_function</literal> in the |
| 1225 |
initialization function <literal>init_CODE</literal>, which is invoked at load |
| 1226 |
time. There, the vector <literal>VV</literal> consists of Lisp objects; |
| 1227 |
<literal>VV[0]</literal> and <literal>VV[1]</literal> in this example hold the Lisp symbols |
| 1228 |
<literal>*delta*</literal> and <literal>add1</literal>. <literal>VM</literal> in the definition of |
| 1229 |
<literal>L1</literal> is a C macro declared in the corresponding H-file. The |
| 1230 |
actual value of <literal>VM</literal> is the number of value stack locations used |
| 1231 |
by this module, i.e., 2 in this example. Thus the following macro |
| 1232 |
definition is found in the H-file.</para> |
| 1233 |
<screen> |
| 1234 |
#define VM 2 |
| 1235 |
</screen> |
| 1236 |
</section> |
| 1237 |
|
| 1238 |
<section xml:id="Internals-Implementation-of-Compiled-Closures"> |
| 1239 |
<title>Implementation of Compiled Closures</title> |
| 1240 |
<para>The &ECL; compiler takes two passes before it invokes the C |
| 1241 |
compiler. The major role of the first pass is to detect function |
| 1242 |
closures and to detect, for each function closure, those lexical |
| 1243 |
objects (i.e., lexical variable, local function definitions, tags, and |
| 1244 |
block-names) to be enclosed within the closure. This check must be |
| 1245 |
done before the C code generation in the second pass, because lexical |
| 1246 |
objects to be enclosed in function closures are treated in a different |
| 1247 |
way from those not enclosed.</para> |
| 1248 |
<para>Ordinarily, lexical variables in a compiled function <emphasis>f</emphasis> |
| 1249 |
are allocated on the C stack. However, if a lexical variable is |
| 1250 |
to be enclosed in function closures, it is allocated on a list, called |
| 1251 |
the "environment list", which is local to <emphasis>f</emphasis>. In addition, a |
| 1252 |
local variable is created which points to the lexical |
| 1253 |
variable's location (within the environment list), so that |
| 1254 |
the variable may be accessed through an indirection rather than by list |
| 1255 |
traversal.</para> |
| 1256 |
<para>The environment list is a pushdown list: It is empty when <emphasis>f</emphasis> is called. |
| 1257 |
An element is pushed on the environment list when a variable to be enclosed in |
| 1258 |
closures is bound, and is popped when the binding is no more in effect. That |
| 1259 |
is, at any moment during execution of <emphasis>f</emphasis>, the environment list contains |
| 1260 |
those lexical variables whose binding is still in effect and which should be |
| 1261 |
enclosed in closures. When a compiled closure is created during execution of |
| 1262 |
<emphasis>f</emphasis>, the compiled code for the closure is coupled with the environment |
| 1263 |
list at that moment to form the compiled closure.</para> |
| 1264 |
<para>Later, when the compiled closure is invoked, a pointer is set up to each |
| 1265 |
lexical variable in the environment list, so that each object may be referenced |
| 1266 |
through a memory indirection.</para> |
| 1267 |
<para>Let us see an example. Suppose the following function has been compiled.</para> |
| 1268 |
<programlisting> |
| 1269 |
(defun foo (x) |
| 1270 |
(let ((a #'(lambda () (incf x))) |
| 1271 |
(y x)) |
| 1272 |
(values a #'(lambda () (incf x y))))) |
| 1273 |
</programlisting> |
| 1274 |
<para><literal>foo</literal> returns two compiled closures. The first closure increments <replaceable>x</replaceable> |
| 1275 |
by one, whereas the second closure increments <replaceable>x</replaceable> by the initial value of |
| 1276 |
<replaceable>x</replaceable>. Both closures return the incremented value of <replaceable>x</replaceable>.</para> |
| 1277 |
<para><screen> |
| 1278 |
>(multiple-value-setq (f g) (foo 10)) |
| 1279 |
#<compiled-closure nil> |
| 1280 |
|
| 1281 |
>(funcall f) |
| 1282 |
11 |
| 1283 |
|
| 1284 |
>(funcall g) |
| 1285 |
21 |
| 1286 |
|
| 1287 |
> |
| 1288 |
</screen></para> |
| 1289 |
<para>After this, the two compiled closures look like:</para> |
| 1290 |
<screen><![CDATA[ |
| 1291 |
second closure y: x: |
| 1292 |
|-------|------| |-------|------| |------|------| |
| 1293 |
| ** | --|----->| 10 | --|------>| 21 | nil | |
| 1294 |
|-------|------| |-------|------| |------|------| |
| 1295 |
^ |
| 1296 |
first closure | |
| 1297 |
|-------|------| | |
| 1298 |
| * | --|----------| |
| 1299 |
|-------|------| |
| 1300 |
|
| 1301 |
* : address of the compiled code for #'(lambda () (incf x)) |
| 1302 |
** : address of the compiled code for #'(lambda () (incf x y)) |
| 1303 |
]]></screen> |
| 1304 |
</section> |
| 1305 |
|
| 1306 |
<section xml:id="Internals-Use-of-Declarations-to-Improve-Efficiency"> |
| 1307 |
<title>Use of Declarations to Improve Efficiency</title> |
| 1308 |
<para>Declarations, especially type and function declarations, |
| 1309 |
increase the efficiency of the compiled code. For example, for the |
| 1310 |
following Lisp source file, with two Common-Lisp declarations added,</para> |
| 1311 |
<programlisting> |
| 1312 |
(eval-when (compile) |
| 1313 |
(proclaim '(function tak (fixnum fixnum fixnum) fixnum)) |
| 1314 |
|
| 1315 |
(defun tak (x y z) |
| 1316 |
(declare (fixnum x y z)) |
| 1317 |
(if (not (< y x)) |
| 1318 |
z |
| 1319 |
(tak (tak (1- x) y z) |
| 1320 |
(tak (1- y) z x) |
| 1321 |
(tak (1- z) x y)))) |
| 1322 |
</programlisting> |
| 1323 |
<para>The compiler generates the following C code:</para> |
| 1324 |
<screen> |
| 1325 |
/* local entry for function TAK */ |
| 1326 |
static int LI1(register int V1,register int V2,register int V3) |
| 1327 |
{ VT3 VLEX3 CLSR3 |
| 1328 |
TTL: |
| 1329 |
if (V2 < V1) { |
| 1330 |
goto L2;} |
| 1331 |
return(V3); |
| 1332 |
L2: |
| 1333 |
{ int V5; |
| 1334 |
V5 = LI1((V1)-1,V2,V3); |
| 1335 |
{ int V6; |
| 1336 |
V6 = LI1((V2)-1,V3,V1); |
| 1337 |
V3 = LI1((V3)-1,V1,V2); |
| 1338 |
V2 = V6; |
| 1339 |
V1 = V5;}} |
| 1340 |
goto TTL; |
| 1341 |
;;; Note: Tail-recursive call of TAK was replaced by iteration. |
| 1342 |
} |
| 1343 |
</screen> |
| 1344 |
</section> |
| 1345 |
|
| 1346 |
<section xml:id="Internals-Inspecting-generated-C-code"> |
| 1347 |
<title>Inspecting generated C code</title> |
| 1348 |
<para>Common-Lisp defines a function disassemble, which is |
| 1349 |
supposed to disassemble a compiled function and to display the |
| 1350 |
assembler code. According to <emphasis>Common-Lisp: The Language</emphasis>,</para> |
| 1351 |
|
| 1352 |
<para><emphasis>This is primary useful for debugging the compiler</emphasis>, ..\\</para> |
| 1353 |
<!-- FIXME: Actually disassemble shows bytecode --> |
| 1354 |
<para>This is, however, <emphasis>useless</emphasis> in our case, because we are |
| 1355 |
not concerned with assembly language. Rather, we are interested in |
| 1356 |
the C code generated by the &ECL; compiler. Thus the disassemble |
| 1357 |
function in &ECL; accepts not-yet-compiled functions only and displays |
| 1358 |
the translated C code.</para> |
| 1359 |
<para><screen> |
| 1360 |
> (defun add1 (x) (1+ x)) |
| 1361 |
ADD1 |
| 1362 |
> (disassemble *) |
| 1363 |
;;; Compiling (DEFUN ADD1 ...). |
| 1364 |
;;; Emitting code for ADD1. |
| 1365 |
|
| 1366 |
/* function definition for ADD1 */ |
| 1367 |
static L1(int narg, object V1) |
| 1368 |
{ VT3 VLEX3 CLSR3 |
| 1369 |
TTL: |
| 1370 |
VALUES(0) = one_plus((V1)); |
| 1371 |
RETURN(1); |
| 1372 |
} |
| 1373 |
</screen></para> |
| 1374 |
</section> |
| 1375 |
</chapter> |
| 1376 |
|
| 1377 |
<chapter xml:id="Porting-ECL"> |
| 1378 |
<title>Porting &ECL;</title> |
| 1379 |
<para>To port &ECL; to a new architecture, the following steps are required:</para> |
| 1380 |
<orderedlist numeration="arabic"> |
| 1381 |
<listitem> |
| 1382 |
<para>Ensure that the GNU Multiprecision library supports this machine.</para> |
| 1383 |
</listitem> |
| 1384 |
<listitem> |
| 1385 |
<para>Ensure that the Boehm-Weiser garbage collector is supported by that |
| 1386 |
architecture. Alternatively, port ECL's own garbage collector |
| 1387 |
<filename>src/c/alloc.d</filename> and <filename>src/c/gbc.d</filename> to |
| 1388 |
that platform.</para> |
| 1389 |
</listitem> |
| 1390 |
<listitem> |
| 1391 |
<para>Fix <filename>src/aclocal.in</filename>, |
| 1392 |
<filename>src/h/config.h.in</filename> and |
| 1393 |
<filename>src/h/ecl.h</filename> so that they supply flags for the new |
| 1394 |
host machine.</para> |
| 1395 |
</listitem> |
| 1396 |
<listitem> |
| 1397 |
<para>Fix the machine dependent code in <filename>src/c/</filename>. The |
| 1398 |
most critical parts are in the <filename>unix*.d</filename> and |
| 1399 |
<filename>thread*.d</filename> files.</para> |
| 1400 |
</listitem> |
| 1401 |
<listitem> |
| 1402 |
<para>Compile as in any other platform.</para> |
| 1403 |
</listitem> |
| 1404 |
<listitem> |
| 1405 |
<para>Run the tests and compare to the results of other platforms.</para> |
| 1406 |
</listitem> |
| 1407 |
</orderedlist> |
| 1408 |
</chapter> |
| 1409 |
</part></book> |
| 1410 |
<!-- Keep this comment at the end of the file |
| 1411 |
Local variables: |
| 1412 |
mode: nxml |
| 1413 |
sgml-parent-document: "ecl.xml" |
| 1414 |
sgml-indent-step: 1 |
| 1415 |
nxml-child-indent: 1 |
| 1416 |
nxml-outline-child-indent: 1 |
| 1417 |
fill-column: 79 |
| 1418 |
End: |
| 1419 |
--> |