Debian Codegraph @

src/doc/old-manual/declarations.xmlf

raw
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="Declarations">
8 <title>Declarations</title>
9 <para>&ECL; supports all kinds of declarations described in the
10 &Steele84;. Any valid declaration will affect the &ECL; environment
11 in some way or another, although information obtained by declarations, other
12 than special declarations, is mainly used by the &ECL; compiler.</para>
13 <para>As described in &Steele84;, Common-Lisp declarations are divided into two
14 classes: <replaceable>proclamations</replaceable> and others. A proclamation is a global
15 declaration given by the function <literal>proclaim</literal>, the top-level <emphasis>macro</emphasis>
16 <literal>defvar</literal>, or the top-level macro <literal>defparameter</literal>. Once given, a
17 proclamation remains effective during the &ECL; session unless it is shadowed
18 by a local declaration or is canceled by another proclamation. Any other
19 declaration is a <emphasis>local declaration</emphasis> and is given only by the special form
20 <literal>declare</literal>. A local declaration remains in effect only within the body of
21 the construct that surrounds the declaration.</para>
22 <para>In the following nonsensical example borrowed from Chapter 9 of
23 &Steele84;,</para>
24 <programlisting>
25 (defun nonsense (k x z)
26 (foo z x)
27 (let ((j (foo k x))
28 (x (* k k)))
29 (declare (inline foo) (special x z))
30 (foo x j z)))
31 </programlisting>
32 <para role="continues">the <literal>inline</literal> and the special declarations both remain in effect within the
33 surrounding <literal>let</literal> form. In this case, we say that the <literal>let</literal> form is
34 the <emphasis>surrounding construct</emphasis> of these declarations.</para>
35 <blockquote>
36 <screen><indexterm role="fn"><primary>the</primary></indexterm>&#151; Special Form: <function>the</function> <varname>value-type form</varname></screen>
37 <para>The &ECL; interpreter does actually check whether the value of the
38 <emphasis>form</emphasis> conforms to the data type specified by <emphasis>value-type</emphasis> and
39 signals an error if the value does not. The type checking is performed by
40 the function <literal>typep</literal>. For example,</para>
41 </blockquote>
42 <programlisting>(the fixnum (foo))
43 </programlisting>
44 <para role="continues">is equivalent to</para>
45 <programlisting>
46 (let ((values (multiple-value-list (foo))))
47 (cond ((endp values) (error ``Too few return values."))
48 ((not (endp (cdr values)))
49 (error ``Too many return values."))
50 ((typep (car values) 'fixnum) (car values))
51 (t (error ``~s is not of type fixnum." (car values)))))
52 </programlisting>
53 <para>On the other hand, the &ECL; compiler uses the special form to
54 obtain type information for compiled code optimization. No code for
55 runtime type-checking is embedded in the compiled code.</para>
56
57 <section xml:id="Declaration-specifiers">
58 <title>Declaration Specifiers</title>
59 <para>&ECL; recognizes all declaration specifiers defined in &Steele84;.
60 The syntax of each such declaration specifier is exactly the same as defined in
61 &Steele84;. In addition, &ECL; recognizes the <literal>object</literal>
62 declaration specifier which is specific to &ECL;.</para>
63 <blockquote>
64 <screen><indexterm role="fn"><primary>special</primary></indexterm>&#151; Declaration: <function>special</function> <varname>{</varname><varname>variable-name</varname><varname>}</varname><varname>*</varname></screen>
65 <para>The interpreter and the compiler of &ECL; both treat special declarations
66 exactly as described in &Steele84;.</para>
67 </blockquote>
68 <blockquote>
69 <screen><indexterm role="fn"><primary>type</primary></indexterm>&#151; Declaration: <function>type</function> <varname>type</varname> <varname>{</varname><varname>variable-name</varname><varname>}</varname><varname>*</varname></screen>
70 <para>A <literal>type</literal> proclamation <literal>(type
71 <replaceable>type var1 var2</replaceable> ...)</literal></para>
72 <para>specifies
73 that the dynamic values of the named variables are of the type <emphasis>type</emphasis>. A
74 local <literal>type</literal> declaration specifies that the variables mentioned are bound
75 by the surrounding construct and have values of the type <emphasis>type</emphasis> during
76 execution of the surrounding construct. The compiler issues a warning if one
77 of the named variables is not bound by the surrounding construct. The
78 information given by <literal>type</literal> declarations is used by the compiler to
79 optimize the compiled code. The behavior of the compiled code is unpredictable
80 if a wrong <literal>type</literal> declaration is supplied. The compiler detects certain
81 wrong <literal>type</literal> declarations at compile time.</para>
82 </blockquote>
83 <para>For example,<screen>
84 &gt;(defun foo (x y)
85 (declare (fixnum x) (character y))
86 (setq x y)
87 ...))
88 foo
89
90 &gt;(compile 'foo)
91
92 ; (defun foo ...) is being compiled.
93 ;; Warning: Type mismatches between x and y.
94 </screen></para>
95 <para>See Section 7.3 for further information on <literal>type</literal> declarations.</para>
96 <blockquote>
97 <screen><indexterm role="fn"><primary>type</primary></indexterm>&#151; Declaration: <function>type</function> <varname>{</varname><varname>variable-name</varname><varname>}</varname><varname>*</varname></screen>
98 <para>(<replaceable>type var1 var2</replaceable> ...) is equivalent to <literal>(type <replaceable>type var1 var2</replaceable>
99 ...)</literal>, provided that <replaceable>type</replaceable> is one of the symbols in Table 4-1 of
100 &Steele84;, other than <literal>function</literal>. Declaration specifications
101 that begin with <literal>function</literal> are regarded as <literal>function</literal> declarations
102 (see below).</para>
103 </blockquote>
104 <blockquote>
105 <screen><indexterm role="fn"><primary>function</primary></indexterm>&#151; Declaration: <function>function</function> <varname>function-name</varname> <varname>argument-types</varname> <varname>.</varname> <varname>return-types</varname></screen>
106 <para>A <literal>function</literal> declaration is used to obtain type information for function
107 call forms. That is, a <literal>function</literal> declaration specifies the argument and
108 the return types of each form that calls the named function.</para>
109 </blockquote>
110 <programlisting>(defun foo ()
111 (declare (function bar (character) fixnum))
112 (+ (bar (atcholi1)) (bar (atcholi2))))
113 </programlisting>
114 <para>In this example, the <literal>function</literal> declaration specifies that the two
115 functions <literal>atcholi1</literal> and <literal>atcholi2</literal> both return character objects
116 when called within the body of <literal>foo</literal>, and that the function bar returns
117 fixnum objects when called within the body of <literal>foo</literal>. The type information
118 given by function declarations is used by the compiler to optimize the compiled
119 code. The behavior of the compiled code is unpredictable if a wrong
120 <literal>function</literal> declaration is supplied. The compiler detects certain wrong
121 <literal>function</literal> declarations at compile time.</para>
122 <para>For example,</para>
123 <para><screen>
124 &gt;(defun foo (x)
125 (declare (fixnum x)
126 (function bar (character) fixnum))
127 (bar x))
128 foo
129
130 &gt;(compile 'foo)
131
132 ; (defun foo ...) is being compiled.
133 ;; Warning: The type of the form x is not character.
134 </screen></para>
135 <para>However, the compiler does not check the number of arguments, and thus, the
136 following function definition will be compiled successfully without any
137 warnings.</para>
138 <programlisting>(defun foo ()
139 (declare (function bar (character character) fixnum))
140 (+ (bar (atcholi1)) (bar (atcholi2) (atcholi3) (atcholi4))))
141 </programlisting>
142 <para>For this definition, the compiler assumes that the three functions
143 <literal>atcholi1</literal>, <literal>atcholi2</literal>, and <literal>atcholi3</literal> will return fixnum
144 objects. The return type of <literal>atcholi4</literal> is unknown at compile time.</para>
145 <para>The complete syntax of a function declaration is:</para>
146 <screen>(function function-name
147 ({type}* [{&amp;optional | &amp;rest | &amp;key} {thing}*])
148 {(values {type}* ) | {type}*}
149 )
150 </screen>
151 <para>Although &amp;optional, &amp;rest, and &amp;key markers may appear in the list of
152 argument types, only those <replaceable>types</replaceable> are recognized that appear before any
153 such markers and the rest of the list is simply ignored. Note that functions
154 with &amp;optional, &amp;rest, or &amp;key parameters may still be declared by
155 <literal>function</literal> declarations because of the use of <literal>function</literal> declarations
156 mentioned above.</para>
157 <para>The <literal>values</literal> construct in the specification of return types is almost
158 useless: <literal>(function <replaceable>function-name argument-types</replaceable> (<replaceable>values</replaceable>
159 <replaceable>type1 type2</replaceable> &hellip;))</literal> is equivalent to <literal>(function
160 <replaceable>function-name argment-types type1 type2</replaceable> &hellip;)</literal>.</para>
161 <para>See Section 7.3 for further information on <literal>function</literal> declarations.</para>
162 <blockquote>
163 <screen><indexterm role="fn"><primary>ftype</primary></indexterm>&#151; Declaration: <function>ftype</function> <varname>function-type</varname> <varname>{</varname><varname>function-name</varname><varname>}</varname><varname>*</varname></screen>
164 <para><replaceable>function-type</replaceable> must be a list whose first element is the symbol
165 <literal>function</literal>. <literal>(ftype (function . <replaceable>rest</replaceable>) <replaceable>function-name-1</replaceable>
166 ... <replaceable>function-name-n</replaceable>)</literal> is equivalent to <replaceable>n</replaceable> consecutive
167 <literal>function</literal> declarations <literal>(function <replaceable>function-name-1</replaceable>
168 . <replaceable>rest</replaceable>)</literal> ... <literal>(function <replaceable>function-name-n</replaceable> . <replaceable>rest</replaceable>)</literal>.</para>
169 </blockquote>
170 <blockquote>
171 <screen><indexterm role="fn"><primary>notinline</primary></indexterm>&#151; Declaration: <function>notinline</function> <varname>{</varname><varname>function-name</varname><varname>}</varname><varname>*</varname></screen>
172 <para><literal>(notinline <replaceable>function1 function2</replaceable> ...)</literal> specifies that the
173 compiler should not compile the named functions in-line. Calls to the
174 named functions can be traced and an event (see Section 5.4) is pushed
175 on the event stack when any one of the named functions is invoked.</para>
176 </blockquote>
177 <blockquote>
178 <screen><indexterm role="fn"><primary>inline</primary></indexterm>&#151; Declaration: <function>inline</function> <varname>{</varname><varname>function-name</varname><varname>}</varname><varname>*</varname></screen>
179 <para>An <literal>inline</literal> proclamation cancels currently effective <literal>notinline</literal>
180 proclamations, and a local <literal>inline</literal> declaration locally shadows currently
181 effective <literal>notinline</literal> declarations.</para>
182 </blockquote>
183 <para><screen>
184 &gt;(defun foo (x)
185 (cons (car x)
186 (locally (declare (inline car)) (car x))))
187 foo
188 &gt;(defun bar (x)
189 (cons (car x)
190 (locally (declare (inline car)) (car x))))
191 foo
192 &gt;(proclaim '(notinline car))
193 nil
194 &gt;(compile 'foo)
195 ...
196 &gt;(proclaim '(inline car))
197 nil
198 &gt;(compile 'bar)
199 ...
200 </screen></para>
201 <para>Usually, primitive functions such as <literal>car</literal> are compiled in-line.
202 Therefore, in this example, only the first call to <literal>car</literal> within <literal>foo</literal>
203 is compiled not in-line.</para>
204 <para>In general, the &ECL; compiler compiles functions in-line whenever possible.
205 Thus an <literal>inline</literal> declaration <literal>(inline <replaceable>function1 function2</replaceable> ...)</literal>
206 is worthless if none of the named functions have previously been declared to be
207 <literal>notinline</literal>.</para>
208 <blockquote>
209 <screen><indexterm role="fn"><primary>ignore</primary></indexterm>&#151; Declaration: <function>ignore</function> <varname>{</varname><varname>variable-name</varname><varname>}</varname><varname>*</varname></screen>
210 <para>Usually, the compiler issues a warning if a lexical variable is never referred
211 to. <literal>(ignore <replaceable>var1 ... varn</replaceable>)</literal> causes the compiler not to issue a
212 warning even if the named variables are never referred to. The compiler issues
213 a warning if one of the named variables is not bound by the surrounding
214 construct, or if a named variable is actually referred to. <literal>ignore</literal>
215 proclamations are simply ignored.</para>
216 </blockquote>
217 <blockquote>
218 <screen><indexterm role="fn"><primary>optimize</primary></indexterm>&#151; Declaration: <function>optimize</function> <varname>{</varname>(<varname>quality</varname> <varname>value</varname>) <varname>|</varname> <varname>quality</varname><varname>}</varname><varname>*</varname></screen>
219 <para>&ECL; supports the four <literal>optimize</literal> qualities listed in the
220 &Steele84;.</para>
221 <para><literal>speed</literal> and <literal>compilation-speed</literal> are used to set up the optimization
222 switch of the C language compiler which is invoked to compile the C-language
223 code generated by the &ECL; compiler (see Chapter 6). <literal>(optimize (speed
224 <replaceable>n</replaceable>))</literal> and <literal>(optimize (compilation-speed <replaceable>m</replaceable>))</literal> are equivalent,
225 where <replaceable>n</replaceable> and <replaceable>m</replaceable> are integers between 0 and 3, and <replaceable>m</replaceable> is equal to
226 3-<replaceable>n</replaceable>. When a &ECL; session is started, the <literal>speed</literal> quality is set
227 to 3. That is, by default, the compiler generates the fastest code in the
228 longest compilation time. The <literal>space</literal> quality specifies whether the code
229 size is important or not: The compiled code is a little bit larger and faster
230 when compiled with the space quality 0, than when compiled with the space
231 quality 1, 2, or 3. When a &ECL; session is started, the <literal>space</literal>
232 quality is set to 0. The <literal>safety</literal> quality determines how much runtime
233 error checking code should be embedded in the compiled code. If the
234 <literal>safety</literal> quality is 0, the compiled code scarcely does runtime error
235 checking. If the <literal>safety</literal> quality is 1, then the compiled code for a
236 function will check the number of arguments to the function at runtime. If the
237 <literal>safety</literal> quality is 2 or 3, then the compiled code does full runtime error
238 checking. In addition, the highest quality value 3 causes the compiler to
239 treat all functions as if they were declared to be <literal>notinline</literal>. When a
240 &ECL; session is started, the <literal>safety</literal> quality is set to 0.</para>
241 </blockquote>
242 <blockquote>
243 <screen><indexterm role="fn"><primary>declaration</primary></indexterm>&#151; Declaration: <function>declaration</function> <varname>{</varname><varname>name</varname><varname>}</varname><varname>*</varname></screen>
244 <para>A <literal>declaration</literal> declaration is used exactly as specified in the
245 &Steele84;.</para>
246 </blockquote>
247 <blockquote>
248 <screen><indexterm role="fn"><primary>object</primary></indexterm>&#151; Declaration: <function>object</function> <varname>{</varname><varname>variable-name</varname><varname>}</varname><varname>*</varname></screen>
249 <para>This is the only declaration specifier that is specific to &ECL;.
250 <literal>(object <replaceable>var1 ... varn</replaceable>)</literal> affects only variable bindings and
251 specifies that the named variables can be allocated in the C stack (see Section
252 7.3). The compiler issues a warning if one of the named variables is not bound
253 by the surrounding construct. <literal>object</literal> proclamations are simply ignored.</para>
254 </blockquote>
255 </section>
256
257 <section xml:id="Type-specifiers">
258 <title>Significant Type Specifiers</title>
259 <para>Whenever a declaration is encountered, each type specifier (if any) in the
260 declaration is converted to one of the following type specifiers, which are
261 collectively called the <emphasis>significant type specifiers</emphasis>.</para>
262 <screen><![CDATA[
263 |------------ fixnum
264 |------------ character
265 |------------ short-float
266 |------------ long-float
267 t --|---- (array t) -------------- (vector t)
268 |---- (array fixnum) --------- (vector fixnum)
269 |---- (array string-char) --- string
270 |---- (array short-float) --- (vector short-float)
271 |---- (array long-float) --- (vector long-float)
272 |---- (array bit) ----------- bit-vector
273 ]]></screen>
274 <para>Here, the lines indicate subtype relations; the right type is a subtype of the
275 left type. For instance, <literal>(vector t)</literal> is a subtype of <literal>(array t)</literal>
276 and <replaceable>T</replaceable>, and <literal>(array t)</literal> itself is a subtype of <replaceable>T</replaceable>. However,
277 <literal>(array t)</literal> and <literal>(array string-char)</literal> are disjoint types.</para>
278 <para>The function <literal>subtypep</literal> is used for the conversion to significant type
279 specifiers: If the first value of <literal>(subtypep <replaceable>raw-type type</replaceable>)</literal> is
280 <replaceable>T</replaceable> for one of the significant type specifiers <replaceable>type</replaceable>, then the type
281 specifier <replaceable>raw-type</replaceable> in the declaration is converted to <replaceable>type</replaceable>. If
282 there are more than one such significant type specifiers, then the type
283 specifier that is a subtype of other specifiers is selected. For example, type
284 specifiers fixnum, <literal>(mod 3)</literal>, and <literal>(member 0 1)</literal> are all
285 converted to fixnum, though they are also subtypes of <replaceable>T</replaceable>.</para>
286 <para>Because of this type specifier conversion, &ECL; may sometimes regard two
287 seemingly distinct declarations as the same. For example, the following
288 <literal>type</literal> declarations are completely equivalent, internally in &ECL;.</para>
289 <programlisting>
290 (declare (type fixnum x)))
291
292 (declare (type (mod 3) x))
293
294 (declare (type (member 0 1) x))
295 </programlisting>
296 <para>Type specifiers in declaration specifications passed to the &ECL; specific
297 function <literal>proclamation</literal> are also converted to significant type specifiers.
298 Thus, for example,</para>
299 <para><screen>
300 &gt;(proclaim '(function foo (fixnum) fixnum))
301 nil
302 &gt;(proclamation '(function foo ((mod 3)) (member 0 1)))
303 t
304 &gt;(proclamation '(function foo (number) character))
305 nil
306 </screen></para>
307 <para>The first call to <literal>proclamation</literal> returns <replaceable>T</replaceable> because both <literal>(mod
308 3)</literal> and <literal>(member 0 1)</literal> are converted to fixnum before the
309 function type of <literal>foo</literal> is checked.</para>
310 </section>
311
312 <section xml:id="Type-declarations">
313 <title>Treatment of Type Declarations</title>
314 <para>&ECL; uses several runtime stacks.</para>
315 <para>Arguments to functions, lexical and temporary variables are allocated on the C
316 stack. Temporary values saved on the C stack may sometimes be represented as
317 <replaceable>raw data</replaceable> instead of pointers to heap-allocated cells. Accessing such raw
318 data on the C stack results in faster compiled code, partly because no pointer
319 dereferencing operation is necessary, and partly because no cell is newly
320 allocated on the heap when a new object is created. This is particularly
321 helpful for numeric code which computes with floating point numbers.</para>
322 <para>&ECL; uses a conservative garbage collector to scan the C stack and find
323 references to live object.</para>
324 </section>
325
326 <section xml:id="Variable-allocations">
327 <title>Variable Allocations</title>
328 <para>If a lexical variable is declared to be of fixnum, <literal>character</literal>,
329 <literal>short-float</literal>, <literal>long-float</literal>, or their subtypes, then it is allocated
330 on the C stack rather than on the value stack. In addition, the variable
331 always has a raw datum as its value: 32 bit signed integer for fixnums, 8 bit
332 character code with 24 bit padding for characters (remember that the font and
333 bit fields of &ECL; characters are always 0), 32 bit floating point
334 representation for short-floats, and 64 bit floating point representation for
335 long-floats. Similarly, if a lexical variable is named in an <literal>object</literal>
336 declaration (see Section 7.1), then it is allocated on the C stack but, in this
337 case, the variable always has a cell pointer as its value. The user is
338 strongly recommended to make sure that objects stored in such an <literal>object</literal>
339 variable may never be garbage collected unexpectedly. For example,</para>
340 <programlisting>
341 (do ((x (foo) (cdr x)))
342 ((endp x))
343 (let ((y (car x)))
344 (declare (object y))
345 (bar y)))
346 </programlisting>
347 <para role="continues">this <literal>object</literal> declaration is completely safe because the value of the
348 variable <replaceable>y</replaceable> is always a substructure of the value of <replaceable>x</replaceable>, which in
349 turn is protected against garbage collection. Incidentally, loop variables of
350 <literal>dolist</literal> may always be declared as object variables, since the
351 <literal>dolist</literal> form has essentially the same control structure as the <literal>do</literal>
352 form above. On the other hand, the result of evaluation of the following form
353 is unpredictable, because the cons cell pointed to from the <literal>object</literal>
354 variable <replaceable>z</replaceable> may be garbage collected before <literal>bar</literal> is called.</para>
355 <programlisting>
356 (let ((z (cons x y)))
357 (declare (object z))
358 (foo (cons x y))
359 (bar z))
360 </programlisting>
361 <para>Lexical variables that are not declared to be of fixnum,
362 character, short-float, long-float, or their
363 subtypes, and that are not named in <literal>object</literal> declarations are usually
364 allocated on the value stack, but may possibly be allocated on the C stack
365 automatically by the compiler.</para>
366 </section>
367
368 <section xml:id="Raw-data-functions">
369 <title>Built-in Functions that Operate on Raw Data Directly</title>
370 <para>Some built-in Common-Lisp functions can directly operate on raw data, if
371 appropriate declarations are supplied. The addition function <literal>+</literal> is among
372 such functions.</para>
373 <programlisting>
374 (let ((x 1))
375 (declare (fixnum x))
376 ...
377 (setq x (+ x 2))
378 ...
379 )
380 </programlisting>
381 <para>In the compiled code for this <literal>let</literal> form, the raw fixnum datum (i.e., the
382 32 bit signed integer) stored in <replaceable>x</replaceable> is simply incremented by 2 and the
383 resulting 32 bit signed integer is stored back into <replaceable>x</replaceable>. The compiler is
384 sure that the addition for 32 bit signed integers will be performed on the call
385 to <literal>+</literal>, because the arguments are both fixnums and the return value must
386 be also a fixnum since the value is to be assigned to the fixnum
387 variable. The knowledge of both the argument types and the return type is
388 necessary for this decision: Addition of two fixnums may possibly produce a
389 bignum and addition of two bignums may happen to produce a fixnum value. If
390 either the argument type or the return type were not known to the compiler, the
391 general addition function would be called to handle the general case. In the
392 following form, for example, the compiler cannot be sure that the return value
393 of the multiplication is a fixnum or that the arguments of the addition are
394 fixnums.</para>
395 <programlisting>
396 (setq x (+ (* x 3) 2))
397 </programlisting>
398 <para>In order to obtain the optimal code, a <literal>the</literal> special form should
399 surround the multiplication.</para>
400 <programlisting>
401 (setq x (+ (the fixnum (* x 3)) 2))
402 </programlisting>
403 <para>Built-in Common-Lisp functions that can directly operate on raw data are:</para>
404 <orderedlist numeration="arabic">
405 <listitem>
406 <para>arithmetic functions such as <literal>+</literal>, <literal>-</literal>,
407 <literal>1+</literal>, <literal>1-</literal>, <literal>*</literal>, <literal>floor</literal>, <literal>mod</literal>, <literal>/</literal>, and
408 <literal>expt</literal>.</para>
409 </listitem>
410 <listitem>
411 <para>predicates such as <literal>eq</literal>, <literal>eql</literal>, <literal>equal</literal>,
412 <literal>zerop</literal>, <literal>plusp</literal>, <literal>minusp</literal>, <literal>=</literal>, <literal>/=</literal>, <literal>&lt;</literal>,
413 <literal>&lt;=</literal>, <literal>&gt;</literal>, <literal>&gt;=</literal>, <literal>char=</literal>, <literal>char/=</literal>, <literal>char&lt;</literal>,
414 <literal>char&lt;=</literal>, <literal>char&gt;</literal>, and <literal>char&gt;=</literal>.</para>
415 </listitem>
416 <listitem>
417 <para>sequence processing functions that receive or return one or more
418 fixnum values, such as <literal>nth</literal>, <literal>nthcdr</literal>, <literal>length</literal>, and
419 <literal>elt</literal>.</para>
420 </listitem>
421 <listitem>
422 <para>array access functions such as <literal>svref</literal>, <literal>char</literal>, <literal>schar</literal>,
423 and <literal>aref</literal> (see below).</para>
424 </listitem>
425 <listitem>
426 <para>system-internal functions for array update (see below).</para>
427 </listitem>
428 <listitem>
429 <para>type-specific functions such as <literal>char-code</literal>, <literal>code-char</literal>,
430 and <literal>float</literal>.</para>
431 </listitem>
432 </orderedlist>
433 <para>As mentioned in Section 2.5.1, array elements are represented in one of six
434 ways depending on the type of the array. By supplying appropriate array type
435 declarations, array access and update operations can handle raw data stored in
436 arrays. For example,</para>
437 <programlisting>
438 (let ((a (make-array n :element-type 'fixnum))
439 (sum 0))
440 (declare (type (array fixnum) a)
441 (fixnum sum))
442 (dotimes (i n) ;;; Array initialization.
443 (declare (fixnum i))
444 (setf (aref a i) i))
445 ....
446 (dotimes (i n) ;;; Summing up the elements.
447 (declare (fixnum i))
448 (setq sum (+ (aref a i) sum)))
449 ....
450 )
451 </programlisting>
452 <para>The <literal>setf</literal> form replaces the <literal>i-th</literal> element of the array a by the raw
453 fixnum value of <literal>i</literal>. The <literal>aref</literal> form retrieves the raw fixnum datum
454 stored in <literal>a</literal>. This raw datum is then added to the raw fixnum value of
455 the fixnum variable <literal>sum</literal>, producing the raw fixnum datum to be stored in
456 <literal>sum</literal>. Similar raw data handling is possible for arrays of types
457 <literal>(array fixnum), (vector fixnum),
458 (array string-char), string,
459 (array short-float), (vector short-float),
460 (array long-float)</literal>, and <literal>(vector long-float)</literal>.</para>
461 </section>
462
463 <section xml:id="Arguments-Values-passing">
464 <title>Arguments/Values Passing</title>
465 <para>Function proclamations <literal>(function <replaceable>function-name</replaceable> (<replaceable>arg-type1</replaceable>
466 <replaceable>arg-type2</replaceable> ...) <replaceable>return-type</replaceable>)</literal> or its equivalents give the compiler
467 the chance to generate compiled code so that arguments to the named functions
468 and resulting values of the named functions will be passed via the C stack,
469 thus increasing the efficiency of calls to these functions. Such
470 arguments/values passing via the C stack is possible only if the called
471 function is also defined in the same source file. This is because the code for
472 the called function must have two entries: One entry for C arguments/values
473 passing and another for ordinary Lisp arguments/values passing. (An ordinary
474 function has only the latter entry.) When the latter entry is used, the
475 arguments are <emphasis>unboxed</emphasis> and passed to the former entry. On return from
476 the function, the resulting value is cast into a Lisp data type.</para>
477 <para>A good example of this follows.</para>
478 <programlisting>
479 (eval-when (compile)
480 (proclaim '(function tak (fixnum fixnum fixnum) fixnum)))
481
482 (defun tak (x y z)
483 (declare (fixnum x y z))
484 (if (not (&lt; y x))
485 z
486 (tak (tak (1- x) y z)
487 (tak (1- y) z x)
488 (tak (1- z) x y))))
489
490 ;;; Call (tak 18 12 6).
491 </programlisting>
492 <para>When <literal>tak</literal> is called with the arguments <literal>18, 12</literal>, and <literal>6</literal>, the
493 raw fixnum data of the arguments are set to the parameters <literal>x</literal>, <literal>y</literal>,
494 <literal>z</literal>. After that, only raw C data are used to perform the execution: No
495 cell pointers are newly allocated nor even referenced. The built-in functions
496 <literal>&lt;</literal> and <literal>1-</literal> directly operate on the raw data. Only at the return from
497 the top-level call of <literal>tak</literal>, the resulting raw data value (which happens
498 to be <literal>7</literal>) is reallocated on the heap. Note that both the <literal>function</literal>
499 proclamation and the local fixnum declaration are necessary to
500 obtain the optimal code. The <literal>function</literal> proclamation is necessary for
501 arguments/values passing via the C stack and the fixnum declaration
502 is necessary to unbox the parameters into C variables.</para>
503 </section>
504 </chapter>
505 <!-- Keep this comment at the end of the file
506 Local variables:
507 sgml-parent-document: "ecl.xml"
508 sgml-indent-step: 1
509 nxml-child-indent: 1
510 nxml-outline-child-indent: 1
511 fill-column: 79
512 End:
513 --></book>