cac86ea111e78764145ad35ccb6e83b9c5291acb
[sbcl.git] / doc / compiler.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1//EN"
3                "http://www.oasis-open.org/docbook/xml/4.1/docbookx.dtd" [
4 <!ENTITY % myents SYSTEM "entities.inc">
5 %myents;
6 ]>
7
8 <chapter id="compiler"><title>The Compiler</title>
9
10 <para>This chapter will discuss most compiler issues other than
11 efficiency, including compiler error messages, the &SBCL; compiler's
12 unusual approach to type safety in the presence of type declarations,
13 the effects of various compiler optimization policies, and the way
14 that inlining and open coding may cause optimized code to differ from
15 a naive translation. Efficiency issues are sufficiently varied and
16 separate that they have <link linkend="efficiency">their own
17 chapter</link>.</para>
18
19 <sect1 id="error-messages"><title>Error Messages</title>
20 <!--INDEX {error messages}{compiler}-->
21 <!--INDEX {compiler error messages}-->
22
23 <para>The compiler supplies a large amount of source location
24 information in error messages. The error messages contain a lot of
25 detail in a terse format, so they may be confusing at first. Error
26 messages will be illustrated using this example program:
27 <programlisting>(defmacro zoq (x)
28 `(roq (ploq (+ ,x 3))))
29
30 (defun foo (y)
31 (declare (symbol y))
32 (zoq y))</programlisting>
33 The main problem with this program is that it is trying to add
34 <literal>3</literal> to a symbol. Note also that the functions
35 <function>roq</function> and <function>ploq</function> aren't defined anywhere.
36 </para>
37
38 <sect2><title>The Parts of the Error Message</title>
39
40 <para>When processing this program, the compiler will produce this
41 warning: 
42
43 <screen>file: /tmp/foo.lisp
44
45 in: DEFUN FOO
46 (ZOQ Y)
47 --> ROQ PLOQ + 
48 ==>
49 Y
50 caught WARNING:
51 Result is a SYMBOL, not a NUMBER.</screen>
52 In this example we see each of the six possible parts of a compiler error
53 message:
54 <orderedlist>
55   <listitem><para><computeroutput>File: /tmp/foo.lisp</computeroutput>
56   This is the name of the file that the compiler read the relevant
57   code from.  The file name is displayed because it may not be
58   immediately obvious when there is an error during compilation of a
59   large system, especially when
60   <function>with-compilation-unit</function> is used to delay
61   undefined warnings.</para></listitem>
62   <listitem><para><computeroutput>in: DEFUN FOO</computeroutput> This is the
63   definition top level form responsible for the error. It is
64   obtained by taking the first two elements of the enclosing form
65   whose first element is a symbol beginning with <quote><literal>def</literal></quote>.
66   If there is no such enclosing <quote><literal>def</literal></quote> form, then the 
67   outermost form is used.  If there are multiple <literal>def</literal>
68   forms, then they are all printed from the outside in, separated by
69   <literal>=></literal>'s.  In this example, the problem was in the
70   <function>defun</function> for <function>foo</function>.</para></listitem>
71   <listitem><para><computeroutput>(ZOQ Y)</computeroutput> This is the
72   <emphasis>original source</emphasis> form responsible for the error.
73   Original source means that the form directly appeared in the
74   original input to the compiler, i.e. in the lambda passed to
75   <function>compile</function> or in the top level form read from the
76   source file. In this example, the expansion of the <function>zoq</function>
77   macro was responsible for the error.</para></listitem>
78   <listitem><para><computeroutput>--> ROQ PLOQ +</computeroutput> This is the
79   <emphasis>processing path</emphasis> that the compiler used to produce
80   the errorful code.  The processing path is a representation of
81   the evaluated forms enclosing the actual source that the
82   compiler encountered when processing the original source.
83   The path is the first element of each form, or the form itself
84   if the form is not a list.  These forms result from the
85   expansion of macros or source-to-source transformation done
86   by the compiler.  In this example, the enclosing evaluated forms
87   are the calls to <function>roq</function>, <function>ploq</function> and
88   <function>+</function>.  These calls resulted from the expansion of
89   the <function>zoq</function> macro.</para></listitem>
90   <listitem><para><computeroutput>==> Y</computeroutput> This is the
91   <emphasis>actual source</emphasis> responsible for the error. If
92   the actual source appears in the explanation, then
93   we print the next enclosing evaluated form, instead of
94   printing the actual source twice.  (This is the form
95   that would otherwise have been the last form of the processing
96   path.) In this example, the problem is with the evaluation of
97   the reference to the variable <varname>y</varname>.</para></listitem>
98   <listitem><para>
99   <computeroutput>caught WARNING: Result is a SYMBOL, not a NUMBER.</computeroutput>
100   This is the <emphasis>explanation</emphasis> of the problem. In this
101   example, the problem is that <varname>y</varname> evaluates to a symbol,
102   but is in a context where a number is required (the argument
103   to <function>+</function>).</para></listitem>
104 </orderedlist>
105
106 Note that each part of the error message is distinctively marked:
107
108 <itemizedlist>
109   <listitem><para> <computeroutput>file:</computeroutput> and <computeroutput>in:</computeroutput>
110   mark the file and definition, respectively.</para></listitem>
111   <listitem><para> The original source is an indented form with no
112   prefix.</para></listitem>
113   <listitem><para> Each line of the processing path is prefixed with
114   <computeroutput>--></computeroutput></para></listitem>
115   <listitem><para> The actual source form is indented like the original
116   source, but is marked by a preceding <computeroutput>==></computeroutput> line.
117   </para></listitem>
118   <listitem><para> The explanation is prefixed with the error
119   severity, which can be <computeroutput>caught ERROR:</computeroutput>,
120   <computeroutput>caught WARNING:</computeroutput>,
121   <computeroutput>caught STYLE-WARNING:</computeroutput>, or
122   <computeroutput>note:</computeroutput>. </para></listitem>
123 </itemizedlist>
124 </para>
125
126 <para>Each part of the error message is more specific than the preceding
127 one.  If consecutive error messages are for nearby locations, then the
128 front part of the error messages would be the same.  In this case, the
129 compiler omits as much of the second message as in common with the
130 first.  For example:
131 <screen>file: /tmp/foo.lisp
132
133 in: DEFUN FOO
134 (ZOQ Y)
135 --> ROQ
136 ==>
137 (PLOQ (+ Y 3))
138 caught STYLE-WARNING:
139 undefined function: PLOQ
140
141 ==>
142 (ROQ (PLOQ (+ Y 3)))
143 caught STYLE-WARNING:
144 undefined function: ROQ</screen>
145 In this example, the file, definition and original source are
146 identical for the two messages, so the compiler omits them in the
147 second message.  If consecutive messages are entirely identical, then
148 the compiler prints only the first message, followed by:
149 <computeroutput>[Last message occurs <replaceable>repeats</replaceable> times]</computeroutput>
150 where <replaceable>repeats</replaceable> is the number of times the message
151 was given.</para>
152
153 <para>If the source was not from a file, then no file line is printed.
154 If the actual source is the same as the original source, then the
155 processing path and actual source will be omitted. If no forms
156 intervene between the original source and the actual source, then the
157 processing path will also be omitted.</para>
158
159 </sect2>
160
161 <sect2><title>The Original and Actual Source</title>
162
163 <para>The <emphasis>original source</emphasis> displayed will almost always be
164 a list. If the actual source for an error message is a symbol, the
165 original source will be the immediately enclosing evaluated list form.
166 So even if the offending symbol does appear in the original source,
167 the compiler will print the enclosing list and then print the symbol
168 as the actual source (as though the symbol were introduced by a
169 macro.)</para>
170
171 <para>When the <emphasis>actual source</emphasis> is displayed
172 (and is not a symbol), it will always
173 be code that resulted from the expansion of a macro or a source-to-source
174 compiler optimization.  This is code that did not appear in the original
175 source program; it was introduced by the compiler.</para>
176
177 <para>Keep in mind that when the compiler displays a source form
178 in an error message, it always displays the most specific (innermost)
179 responsible form.  For example, compiling this function
180 <programlisting>(defun bar (x)
181 (let (a)
182 (declare (fixnum a))
183 (setq a (foo x))
184 a))</programlisting>
185 gives this error message
186 <screen>in: DEFUN BAR
187 (LET (A) (DECLARE (FIXNUM A)) (SETQ A (FOO X)) A)
188 caught WARNING: The binding of A is not a FIXNUM:
189 NIL</screen>
190 This error message is not saying <quote>there is a problem somewhere in
191 this <function>let</function></quote> &mdash; it is saying that there is a
192 problem with the <function>let</function> itself. In this example, the problem
193 is that <varname>a</varname>'s <literal>nil</literal> initial value is not a
194 <type>fixnum</type>.</para>
195
196 </sect2>
197
198 <sect2><title>The Processing Path</title>
199 <!--INDEX processing path-->
200 <!--INDEX macroexpansion-->
201 <!--INDEX source-to-source transformation-->
202
203 <para>The processing path is mainly useful for debugging macros, so if
204 you don't write macros, you can probably ignore it. Consider this
205 example:
206
207 <programlisting>(defun foo (n)
208 (dotimes (i n *undefined*)))
209 </programlisting>
210
211 Compiling results in this error message:
212
213 <screen>in: DEFUN FOO
214 (DOTIMES (I N *UNDEFINED*))
215 --> DO BLOCK LET TAGBODY RETURN-FROM
216 ==>
217 (PROGN *UNDEFINED*)
218 caught STYLE-WARNING:
219 undefined variable: *UNDEFINED*</screen>
220
221 Note that <function>do</function> appears in the processing path. This is because
222 <function>dotimes</function> expands into:
223
224 <programlisting>(do ((i 0 (1+ i)) (#:g1 n))
225 ((>= i #:g1) *undefined*)
226 (declare (type unsigned-byte i)))</programlisting>
227
228 The rest of the processing path results from the expansion
229 of <function>do</function>:
230
231 <programlisting>
232   (block nil
233   (let ((i 0) (#:g1 n))
234   (declare (type unsigned-byte i))
235   (tagbody (go #:g3)
236   #:g2    (psetq i (1+ i))
237   #:g3    (unless (>= i #:g1) (go #:g2))
238   (return-from nil (progn *undefined*)))))
239 </programlisting>
240
241 In this example, the compiler descended into the <function>block</function>,
242 <function>let</function>, <function>tagbody</function> and <function>return-from</function> to
243 reach the <function>progn</function> printed as the actual source. This is a
244 place where the <quote>actual source appears in explanation</quote> rule
245 was applied. The innermost actual source form was the symbol
246 <varname>*undefined*</varname> itself, but that also appeared in the
247 explanation, so the compiler backed out one level.</para>
248
249 </sect2>
250
251 <sect2><title>Error Severity</title>
252 <!--INDEX severity of compiler errors -->
253 <!--INDEX compiler error severity -->
254
255 <para>There are four levels of compiler error severity:
256 <wordasword>error</wordasword>, <wordasword>warning</wordasword>, <wordasword>style
257 warning</wordasword>, and <wordasword>note</wordasword>. The first three levels correspond
258 to condition classes which are defined in the &ANSI; standard for
259 &CommonLisp; and which have special significance to the
260 <function>compile</function> and <function>compile-file</function> functions. These
261 levels of compiler error severity occur when the compiler handles
262 conditions of these classes. The fourth level of compiler error
263 severity, <wordasword>note</wordasword>, is used for problems which are too mild
264 for the standard condition classes, typically hints about how
265 efficiency might be improved.</para>
266
267 </sect2>
268
269 <sect2><title>Errors During Macroexpansion</title>
270 <!--INDEX {macroexpansion}{errors during}-->
271
272 <para>The compiler handles errors that happen during macroexpansion,
273 turning them into compiler errors. If you want to debug the error (to
274 debug a macro), you can set <varname>*break-on-signals*</varname> to
275 <literal>error</literal>. For example, this definition:
276
277 <programlisting>(defun foo (e l)
278 (do ((current l (cdr current))
279 ((atom current) nil))
280 (when (eq (car current) e) (return current))))</programlisting>
281
282 gives this error:
283
284 <screen>in: DEFUN FOO
285 (DO ((CURRENT L #) (# NIL)) (WHEN (EQ # E) (RETURN CURRENT)) )
286 caught ERROR: 
287 (in macroexpansion of (DO # #))
288 (hint: For more precise location, try *BREAK-ON-SIGNALS*.)
289 DO step variable is not a symbol: (ATOM CURRENT)</screen>
290 </para>
291
292 </sect2>
293
294 <sect2><title>Read Errors</title>
295 <!--INDEX {read errors}{compiler}-->
296
297 <para>&SBCL;'s compiler (unlike &CMUCL;'s) does not attempt to recover
298 from read errors when reading a source file, but instead just reports
299 the offending character position and gives up on the entire source
300 file.</para>
301
302 </sect2>
303
304 <!-- FIXME: How much control over error messages is in SBCL?
305      _     How much should be? How much of this documentation should
306      _     we save or adapt? 
307      _ 
308      _ %%\node Error Message Parameterization,  , Read Errors, Interpreting Error Messages
309      _ \subsection{Error Message Parameterization}
310      _ \cpsubindex{error messages}{verbosity}
311      _ \cpsubindex{verbosity}{of error messages}
312      _ 
313      _ There is some control over the verbosity of error messages.  See also
314      _ \varref{undefined-warning-limit}, \code{*efficiency-note-limit*} and
315      _ \varref{efficiency-note-cost-threshold}.
316      _ 
317      _ \begin{defvar}{}{enclosing-source-cutoff}
318      _ 
319      _   This variable specifies the number of enclosing actual source forms
320      _   that are printed in full, rather than in the abbreviated processing
321      _   path format.  Increasing the value from its default of \code{1}
322      _   allows you to see more of the guts of the macroexpanded source,
323      _   which is useful when debugging macros.
324      _ \end{defvar}
325      _ 
326      _ \begin{defvar}{}{error-print-length}
327      _   \defvarx{error-print-level}
328      _ 
329      _   These variables are the print level and print length used in
330      _   printing error messages.  The default values are \code{5} and
331      _   \code{3}.  If null, the global values of \code{*print-level*} and
332      _   \code{*print-length*} are used.
333      _ \end{defvar}
334      _ 
335      _ \begin{defmac}{extensions:}{define-source-context}{%
336      _     \args{\var{name} \var{lambda-list} \mstar{form}}}
337      _ 
338      _   This macro defines how to extract an abbreviated source context from
339      _   the \var{name}d form when it appears in the compiler input.
340      _   \var{lambda-list} is a \code{defmacro} style lambda-list used to
341      _   parse the arguments.  The \var{body} should return a list of
342      _   subforms that can be printed on about one line.  There are
343      _   predefined methods for \code{defstruct}, \code{defmethod}, etc.  If
344      _   no method is defined, then the first two subforms are returned.
345      _   Note that this facility implicitly determines the string name
346      _   associated with anonymous functions.
347      _ \end{defmac}
348      _ 
349      _ -->
350
351 </sect1>
352
353 <sect1 id="compiler-types"><title>The Compiler's Handling of Types</title>
354
355 <para>The most unusual features of the &SBCL; compiler (which is
356 very similar to the original &CMUCL; compiler, also known as
357 &Python;) is its unusually sophisticated understanding of the
358 &CommonLisp; type system and its unusually conservative approach to
359 the implementation of type declarations. These two features reward the
360 use of type declarations throughout development, even when high
361 performance is not a concern. (Also, as discussed <link
362 linkend="efficiency">in the chapter on performance</link>, the use of
363 appropriate type declarations can be very important for performance as
364 well.)</para>
365
366 <para>The &SBCL; compiler, like the related compiler in &CMUCL;,
367 treats type declarations much differently than other Lisp compilers.
368 By default (<emphasis>i.e.</emphasis>, at ordinary levels of the
369 <parameter>safety</parameter> compiler optimization parameter), the compiler
370 doesn't blindly believe most type declarations; it considers them
371 assertions about the program that should be checked.</para>
372
373 <para>The &SBCL; compiler also has a greater knowledge of the
374 &CommonLisp; type system than other compilers.  Support is incomplete
375 only for the <type>not</type>, <type>and</type> and <type>satisfies</type>
376 types.
377 <!-- FIXME: See also sections \ref{advanced-type-stuff}
378      and \ref{type-inference}, once we snarf them from the
379      CMU CL manual. -->
380 </para>
381
382 <sect2 id="compiler-impl-limitations"><title>Implementation Limitations</title>
383
384 <para>
385   Ideally, the compiler would consider <emphasis>all</emphasis> type declarations to
386   be assertions, so that adding type declarations to a program, no
387   matter how incorrect they might be, would <emphasis>never</emphasis> cause
388   undefined behavior. As of &SBCL; version 0.8.1, the compiler is known to
389   fall short of this goal in two areas:
390   <itemizedlist>
391     <listitem><para><function>Proclaim</function>ed constraints on argument and
392     result types of a function are supposed to be checked by the
393     function. If the function type is proclaimed before function
394     definition, type checks are inserted by the compiler, but the
395     standard allows the reversed order, in which case the compiler
396     will trust the declaration.</para></listitem>
397     <listitem><para>The compiler cannot check types of an unknown number
398     of values; if the number of generated values is unknown, but the
399     number of consumed is known, only consumed values are
400     checked.</para></listitem>
401     <listitem><para>There are a few poorly characterized but apparently
402     very uncommon situations where a type declaration in an unexpected
403     location will be trusted and never checked by the
404     compiler.</para></listitem>
405 </itemizedlist></para>
406
407 <para>These are important bugs, but are not necessarily easy to fix,
408 so they may, alas, remain in the system for a while.</para>
409
410 </sect2>
411
412 <sect2><title>Type Errors at Compile Time</title>
413 <!--INDEX compile time type errors-->
414 <!--INDEX type checking}{at compile time}-->
415
416 <para>If the compiler can prove at compile time that some portion of
417 the program cannot be executed without a type error, then it will give
418 a warning at compile time. It is possible that the offending code
419 would never actually be executed at run-time due to some higher level
420 consistency constraint unknown to the compiler, so a type warning
421 doesn't always indicate an incorrect program. For example, consider
422 this code fragment:
423
424 <programlisting>(defun raz (foo)
425 (let ((x (case foo
426 (:this 13)
427 (:that 9)
428 (:the-other 42))))
429 (declare (fixnum x))
430 (foo x)))
431 </programlisting>
432
433 Compilation produces this warning:
434
435 <screen>in: DEFUN RAZ
436 (CASE FOO (:THIS 13) (:THAT 9) (:THE-OTHER 42))
437 --> LET COND IF COND IF COND IF
438 ==>
439 (COND)
440 caught WARNING: This is not a FIXNUM:
441 NIL</screen>
442
443 In this case, the warning means that if <varname>foo</varname> isn't any of
444 <literal>:this</literal>, <literal>:that</literal> or <literal>:the-other</literal>, then
445 <varname>x</varname> will be initialized to <literal>nil</literal>, which the
446 <type>fixnum</type> declaration makes illegal. The warning will go away if
447 <function>ecase</function> is used instead of <function>case</function>, or if
448 <literal>:the-other</literal> is changed to <literal>t</literal>.</para>
449
450 <para>This sort of spurious type warning happens moderately often in
451 the expansion of complex macros and in inline functions. In such
452 cases, there may be dead code that is impossible to correctly execute.
453 The compiler can't always prove this code is dead (could never be
454 executed), so it compiles the erroneous code (which will always signal
455 an error if it is executed) and gives a warning.</para>
456
457 <para>
458   Type warnings are inhibited when the
459   <parameter>sb-ext:inhibit-warnings</parameter> optimization quality is
460   <literal>3</literal>. (See <link linkend="compiler-policy">the section
461   on compiler policy</link>.) This can be used in a local declaration
462   to inhibit type warnings in a code fragment that has spurious
463 warnings.</para>
464
465 </sect2>
466
467 <sect2 id="precisetypechecking"><title>Precise Type Checking</title>
468 <!--INDEX precise type checking-->
469 <!--INDEX {type checking}{precise}-->
470
471 <para>With the default compilation policy, all type declarations are
472 precisely checked, except in a few situations where they are simply
473 ignored instead. Precise checking means that the check is done as
474 though <function>typep</function> had been called with the exact type
475 specifier that appeared in the declaration. In &SBCL;, adding type
476 declarations makes code safer. (Except that as noted <link
477 linkend="compiler-impl-limitations">elsewhere</link>, remaining bugs
478 in the compiler's handling of types unfortunately provide some
479 exceptions to this rule.)</para>
480
481 <para>If a variable is declared to be
482 <type>(integer 3 17)</type> then its value must always be an integer
483 between <literal>3</literal> and <literal>17</literal>. If multiple type
484 declarations apply to a single variable, then all the declarations
485 must be correct; it is as though all the types were intersected
486 producing a single <type>and</type> type specifier.</para>
487
488 <para>Argument and result type declarations are automatically
489 enforced. If you declare the type of a function argument, a type check
490 will be done when that function is called. In a function call, the
491 called function does the argument type checking.</para>
492
493 <para>The types of structure slots are also checked. The value of a
494 structure slot must always be of the type indicated in any
495 <literal>:type</literal> slot option. </para>
496
497 <para>In traditional &CommonLisp; compilers, not all type assertions
498 are checked, and type checks are not precise. Traditional compilers
499 blindly trust explicit type declarations, but may check the argument
500 type assertions for built-in functions. Type checking is not precise,
501 since the argument type checks will be for the most general type legal
502 for that argument. In many systems, type declarations suppress what
503 little type checking is being done, so adding type declarations makes
504 code unsafe. This is a problem since it discourages writing type
505 declarations during initial coding. In addition to being more error
506 prone, adding type declarations during tuning also loses all the
507 benefits of debugging with checked type assertions.</para>
508
509 <para>To gain maximum benefit from the compiler's type checking, you
510 should always declare the types of function arguments and structure
511 slots as precisely as possible. This often involves the use of
512 <type>or</type>, <type>member</type>, and other list-style type specifiers.</para>
513
514 </sect2>
515
516 <sect2 id="weakened-type-checking"><title>Weakened Type Checking</title>
517 <!--INDEX weakened type checking-->
518 <!--INDEX {type checking}{weakened}-->
519
520 <para>At one time, &CMUCL; supported another level of type checking,
521 <quote>weakened type checking</quote>, when the value for the
522 <parameter>speed</parameter> optimization quality is greater than
523 <parameter>safety</parameter>, and <parameter>safety</parameter> is not <literal>0</literal>.
524 The &CMUCL; manual still has a description of it, but even the CMU CL
525 code no longer corresponds to the manual. Some of this partial safety
526 checking lingers on in SBCL, but it's not a supported feature, and 
527 should not be relied on. If you ask the compiler to optimize
528 <parameter>speed</parameter> to a higher level than <parameter>safety</parameter>,
529 your program is performing without a safety net, because &SBCL; may
530 at its option believe any or all type declarations with either partial
531 or nonexistent runtime checking.</para>
532
533 </sect2>
534
535 <sect2><title>Getting Existing Programs to Run</title>
536 <!--INDEX {existing programs}{to run}-->
537 <!--INDEX {types}{portability}-->
538 <!--INDEX {compatibility with other Lisps}
539      (should also have an entry in the non-&ANSI;-isms section)-->
540
541 <para>Since &SBCL;'s compiler, like &CMUCL;'s compiler, does much more
542 comprehensive type checking than most Lisp compilers, &SBCL; may
543 detect type errors in programs that have been debugged using other
544 compilers. These errors are mostly incorrect declarations, although
545 compile-time type errors can find actual bugs if parts of the program
546 have never been tested.</para>
547
548 <para>Some incorrect declarations can only be detected by run-time
549 type checking. It is very important to initially compile a program
550 with full type checks (high <parameter>safety</parameter> optimization) and
551 then test this safe version. After the checking version has been
552 tested, then you can consider weakening or eliminating type checks.
553 <emphasis>This applies even to previously debugged
554 programs,</emphasis> because the &SBCL; compiler does much more type
555 inference than other &CommonLisp; compilers, so an incorrect
556 declaration can do more damage.</para>
557
558 <para>The most common problem is with variables whose constant initial
559 value doesn't match the type declaration. Incorrect constant initial
560 values will always be flagged by a compile-time type error, and they
561 are simple to fix once located. Consider this code fragment:
562
563 <programlisting>(prog (foo)
564 (declare (fixnum foo))
565 (setq foo ...)
566 ...)</programlisting>
567
568 Here <varname>foo</varname> is given an initial value of <literal>nil</literal>, but
569 is declared to be a <type>fixnum</type>.  Even if it is never read, the
570 initial value of a variable must match the declared type.  There are
571 two ways to fix this problem. Change the declaration
572
573 <programlisting>(prog (foo)
574 (declare (type (or fixnum null) foo))
575 (setq foo ...)
576 ...)</programlisting>
577
578 or change the initial value
579
580 <programlisting>(prog ((foo 0))
581 (declare (fixnum foo))
582 (setq foo ...)
583 ...)</programlisting>
584
585 It is generally preferable to change to a legal initial value rather
586 than to weaken the declaration, but sometimes it is simpler to weaken
587 the declaration than to try to make an initial value of the
588 appropriate type.</para>
589
590 <para>Another declaration problem occasionally encountered is
591 incorrect declarations on <function>defmacro</function> arguments. This can happen
592 when a function is converted into a macro. Consider this macro:
593
594 <programlisting>(defmacro my-1+ (x)
595 (declare (fixnum x))
596 `(the fixnum (1+ ,x)))</programlisting>
597
598 Although legal and well-defined &CommonLisp; code, this meaning of
599 this definition is almost certainly not what the writer intended. For
600 example, this call is illegal:
601
602 <programlisting>(my-1+ (+ 4 5))</programlisting>
603
604 This call is illegal because the argument to the macro is
605 <literal>(+ 4 5)</literal>, which is a <type>list</type>, not a
606 <type>fixnum</type>.  Because of
607 macro semantics, it is hardly ever useful to declare the types of
608 macro arguments.  If you really want to assert something about the
609 type of the result of evaluating a macro argument, then put a
610 <function>the</function> in the expansion:
611
612 <programlisting>(defmacro my-1+ (x)
613 `(the fixnum (1+ (the fixnum ,x))))</programlisting>
614
615 In this case, it would be stylistically preferable to change this
616 macro back to a function and declare it inline. 
617 <!--FIXME: <xref>inline-expansion, once we crib the 
618      relevant text from the CMU CL manual.-->
619 </para>
620
621 <para>
622   Some more subtle problems are caused by incorrect declarations that
623   can't be detected at compile time.  Consider this code:
624
625   <programlisting>(do ((pos 0 (position #\a string :start (1+ pos))))
626   ((null pos))
627   (declare (fixnum pos))
628   ...)</programlisting>
629
630   Although <varname>pos</varname> is almost always a <varname>fixnum</varname>, it is
631   <literal>nil</literal> at the end of the loop. If this example is compiled
632   with full type checks (the default), then running it will signal a
633   type error at the end of the loop. If compiled without type checks,
634   the program will go into an infinite loop (or perhaps
635   <function>position</function> will complain because <literal>(1+ nil)</literal> isn't
636   a sensible start.) Why? Because if you compile without type checks,
637   the compiler just quietly believes the type declaration. Since the
638   compiler believes that <varname>pos</varname> is always a <type>fixnum</type>, it
639   believes that <varname>pos</varname> is never <literal>nil</literal>, so
640   <literal>(null pos)</literal> is never true, and the loop exit test is
641   optimized away. Such errors are sometimes flagged by unreachable code
642   notes, but it is still important to initially compile and test any
643   system with full type checks, even if the system works fine when
644 compiled using other compilers.</para>
645
646 <para>In this case, the fix is to weaken the type declaration to
647 <type>(or fixnum null)</type>.
648 <footnote><para>Actually, this declaration is 
649 unnecessary in &SBCL;, since it already knows that <function>position</function>
650 returns a non-negative <type>fixnum</type> or <literal>nil</literal>.
651 </para></footnote>
652
653 Note that there is usually little performance penalty for weakening a
654 declaration in this way. Any numeric operations in the body can still
655 assume that the variable is a <type>fixnum</type>, since <literal>nil</literal>
656 is not a legal numeric argument. Another possible fix would be to say:
657
658 <programlisting>(do ((pos 0 (position #\a string :start (1+ pos))))
659 ((null pos))
660 (let ((pos pos))
661 (declare (fixnum pos))
662 ...))</programlisting>
663
664 This would be preferable in some circumstances, since it would allow a
665 non-standard representation to be used for the local <varname>pos</varname>
666 variable in the loop body.
667 <!-- FIXME: <xref>ND-variables, once we crib the text from the 
668      CMU CL manual. -->
669 </para>
670
671 </sect2>
672
673 </sect1>
674
675 <sect1 id="compiler-policy"><title>Compiler Policy</title>
676
677 <para>As of version 0.6.4, &SBCL; still uses most of the &CMUCL; code
678 for compiler policy. The &CMUCL; code has many features and high-quality
679 documentation, but the two unfortunately do not match. So this area of
680 the compiler and its interface needs to be cleaned up. Meanwhile, here
681 is some rudimentary documentation on the current behavior of the
682 system.</para>
683
684 <para>Compiler policy is controlled by the <parameter>optimize</parameter>
685 declaration. The compiler supports the &ANSI; optimization qualities,
686 and also an extension <parameter>sb-ext:inhibit-warnings</parameter>.</para>
687
688 <para>Ordinarily, when the <parameter>speed</parameter> quality is high, the
689 compiler emits notes to notify the programmer about its inability to
690 apply various optimizations. Setting
691 <parameter>sb-ext:inhibit-warnings</parameter> to a value at least as large as
692 the <parameter>speed</parameter> quality inhibits this notification. This can
693 be useful to suppress notes about code which is known to be
694 unavoidably inefficient. (For example, the compiler issues notes about
695 having to use generic arithmetic instead of fixnum arithmetic, which
696 is not helpful for code which by design supports arbitrary-sized
697 integers instead of being limited to fixnums.)</para>
698
699 <note><para>The basic functionality of the <parameter>optimize
700 inhibit-warnings</parameter> extension will probably be supported in all future
701 versions of the system, but it will probably be renamed when the
702 compiler and its interface are cleaned up. The current name is
703 misleading, because it mostly inhibits optimization notes, not
704 warnings. And making it an optimization quality is misleading, because
705 it shouldn't affect the resulting code at all. It may become a
706 declaration identifier with a name like
707 <parameter>sb-ext:inhibit-notes</parameter>, so that what's currently written
708
709 <programlisting>(declaim (optimize (sb-ext:inhibit-warnings 2)))</programlisting>
710
711 would become something like
712
713 <programlisting>(declaim (sb-ext:inhibit-notes 2))</programlisting>
714
715 </para></note>
716
717 <para> (In early versions of SBCL, a <parameter>speed</parameter> value of zero
718 was used to enable byte compilation, but since version 0.7.0, SBCL
719 only supports native compilation.)</para>
720
721 <para>When <parameter>safety</parameter> is zero, almost all runtime checking
722 of types, array bounds, and so forth is suppressed.</para>
723
724 <para>When <parameter>safety</parameter> is less than <parameter>speed</parameter>, any
725 and all type checks may be suppressed. At some point in the past,
726 &CMUCL; had <link linkend="weakened-type-checking">a more nuanced
727 interpretation of this</link>. However, &SBCL; doesn't support that
728 interpretation, and setting <parameter>safety</parameter> less than
729 <parameter>speed</parameter> may have roughly the same effect as setting
730 <parameter>safety</parameter> to zero.</para>
731
732 <para>The value of <parameter>space</parameter> mostly influences the
733 compiler's decision whether to inline operations, which tend to
734 increase the size of programs. Use the value <literal>0</literal> with
735 caution, since it can cause the compiler to inline operations so
736 indiscriminately that the net effect is to slow the program by causing
737 cache misses or swapping.</para>
738
739 <!-- FIXME: old CMU CL compiler policy, should perhaps be adapted
740      _    for SBCL. (Unfortunately, the CMU CL docs are out of sync with the
741      _    CMU CL code, so adapting this requires not only reformatting
742      _    the documentation, but rooting out code rot.)
743      _
744      _<sect2 id="compiler-policy"><title>Compiler Policy</1000
745      _  INDEX {policy}{compiler}
746      _  INDEX compiler policy
747      _
748      _<para>The policy is what tells the compiler <emphasis>how</emphasis> to
749      _compile a program. This is logically (and often textually) distinct
750      _from the program itself. Broad control of policy is provided by the
751      _<parameter>optimize</parameter> declaration; other declarations and variables
752      _control more specific aspects of compilation.</para>
753      _
754      _\begin{comment}
755      _* The Optimize Declaration::
756      _* The Optimize-Interface Declaration::
757      _\end{comment}
758      _
759      _%%\node The Optimize Declaration, The Optimize-Interface Declaration, Compiler Policy, Compiler Policy
760      _\subsection{The Optimize Declaration}
761      _\label{optimize-declaration}
762      _\cindex{optimize declaration}
763      _\cpsubindex{declarations}{\code{optimize}}
764      _
765      _The \code{optimize} declaration recognizes six different
766      _\var{qualities}.  The qualities are conceptually independent aspects
767      _of program performance.  In reality, increasing one quality tends to
768      _have adverse effects on other qualities.  The compiler compares the
769      _relative values of qualities when it needs to make a trade-off; i.e.,
770      _if \code{speed} is greater than \code{safety}, then improve speed at
771      _the cost of safety.
772      _
773      _The default for all qualities (except \code{debug}) is \code{1}.
774      _Whenever qualities are equal, ties are broken according to a broad
775      _idea of what a good default environment is supposed to be.  Generally
776      _this downplays \code{speed}, \code{compile-speed} and \code{space} in
777      _favor of \code{safety} and \code{debug}.  Novice and casual users
778      _should stick to the default policy.  Advanced users often want to
779      _improve speed and memory usage at the cost of safety and
780      _debuggability.
781      _
782      _If the value for a quality is \code{0} or \code{3}, then it may have a
783      _special interpretation.  A value of \code{0} means ``totally
784      _unimportant'', and a \code{3} means ``ultimately important.''  These
785      _extreme optimization values enable ``heroic'' compilation strategies
786      _that are not always desirable and sometimes self-defeating.
787      _Specifying more than one quality as \code{3} is not desirable, since
788      _it doesn't tell the compiler which quality is most important.
789      _
790      _
791      _These are the optimization qualities:
792      _\begin{Lentry}
793      _
794      _\item[\code{speed}] \cindex{speed optimization quality}How fast the
795      _  program should is run.  \code{speed 3} enables some optimizations
796      _  that hurt debuggability.
797      _
798      _\item[\code{compilation-speed}] \cindex{compilation-speed optimization
799      _    quality}How fast the compiler should run.  Note that increasing
800      _  this above \code{safety} weakens type checking.
801      _
802      _\item[\code{space}] \cindex{space optimization quality}How much space
803      _  the compiled code should take up.  Inline expansion is mostly
804      _  inhibited when \code{space} is greater than \code{speed}.  A value
805      _  of \code{0} enables indiscriminate inline expansion.  Wide use of a
806      _  \code{0} value is not recommended, as it may waste so much space
807      _  that run time is slowed.  \xlref{inline-expansion} for a discussion
808      _  of inline expansion.
809      _
810      _\item[\code{debug}] \cindex{debug optimization quality}How debuggable
811      _  the program should be.  The quality is treated differently from the
812      _  other qualities: each value indicates a particular level of debugger
813      _  information; it is not compared with the other qualities.
814      _  \xlref{debugger-policy} for more details.
815      _
816      _\item[\code{safety}] \cindex{safety optimization quality}How much
817      _  error checking should be done.  If \code{speed}, \code{space} or
818      _  \code{compilation-speed} is more important than \code{safety}, then
819      _  type checking is weakened (\pxlref{weakened-type-checks}).  If
820      _  \code{safety} if \code{0}, then no run time error checking is done.
821      _  In addition to suppressing type checks, \code{0} also suppresses
822      _  argument count checking, unbound-symbol checking and array bounds
823      _  checks.
824      _
825      _\item[\code{extensions:inhibit-warnings}] \cindex{inhibit-warnings
826      _    optimization quality}This is a CMU extension that determines how
827      _  little (or how much) diagnostic output should be printed during
828      _  compilation.  This quality is compared to other qualities to
829      _  determine whether to print style notes and warnings concerning those
830      _  qualities.  If \code{speed} is greater than \code{inhibit-warnings},
831      _  then notes about how to improve speed will be printed, etc.  The
832      _  default value is \code{1}, so raising the value for any standard
833      _  quality above its default enables notes for that quality.  If
834      _  \code{inhibit-warnings} is \code{3}, then all notes and most
835      _  non-serious warnings are inhibited.  This is useful with
836      _  \code{declare} to suppress warnings about unavoidable problems.
837      _\end{Lentry}
838      _
839      _%%\node The Optimize-Interface Declaration,  , The Optimize Declaration, Compiler Policy
840      _\subsection{The Optimize-Interface Declaration}
841      _\label{optimize-interface-declaration}
842      _\cindex{optimize-interface declaration}
843      _\cpsubindex{declarations}{\code{optimize-interface}}
844      _
845      _The \code{extensions:optimize-interface} declaration is identical in
846      _syntax to the \code{optimize} declaration, but it specifies the policy
847      _used during compilation of code the compiler automatically generates
848      _to check the number and type of arguments supplied to a function.  It
849      _is useful to specify this policy separately, since even thoroughly
850      _debugged functions are vulnerable to being passed the wrong arguments.
851      _The \code{optimize-interface} declaration can specify that arguments
852      _should be checked even when the general \code{optimize} policy is
853      _unsafe.
854      _
855      _Note that this argument checking is the checking of user-supplied
856      _arguments to any functions defined within the scope of the
857      _declaration, \code{not} the checking of arguments to \llisp{}
858      _primitives that appear in those definitions.
859      _
860      _The idea behind this declaration is that it allows the definition of
861      _functions that appear fully safe to other callers, but that do no
862      _internal error checking.  Of course, it is possible that arguments may
863      _be invalid in ways other than having incorrect type.  Functions
864      _compiled unsafely must still protect themselves against things like
865      _user-supplied array indices that are out of bounds and improper lists.
866      _See also the \kwd{context-declarations} option to
867      _\macref{with-compilation-unit}.
868      _
869      _(end of section on compiler policy)
870      _-->
871
872 </sect1>
873
874 <sect1 id="open-coding"><title>Open Coding and Inline Expansion</title>
875 <!--INDEX open-coding-->
876 <!--INDEX inline expansion-->
877 <!--INDEX static functions-->
878
879 <para>Since &CommonLisp; forbids the redefinition of standard
880 functions, the compiler can have special knowledge of these standard
881 functions embedded in it. This special knowledge is used in various
882 ways (open coding, inline expansion, source transformation), but the
883 implications to the user are basically the same:
884 <itemizedlist>
885   <listitem><para> Attempts to redefine standard functions may
886   be frustrated, since the function may never be called. Although
887   it is technically illegal to redefine standard functions, users
888   sometimes want to implicitly redefine these functions when they
889   are debugging using the <function>trace</function> macro.  Special-casing
890   of standard functions can be inhibited using the
891   <parameter>notinline</parameter> declaration.</para></listitem>
892   <listitem><para> The compiler can have multiple alternate
893   implementations of standard functions that implement different
894   trade-offs of speed, space and safety.  This selection is
895   based on the <link linkend="compiler-policy">compiler policy</link>.
896   </para></listitem>
897 </itemizedlist>
898 </para>
899
900 <para>When a function call is <emphasis>open coded</emphasis>, inline code whose
901 effect is equivalent to the function call is substituted for that
902 function call. When a function call is <emphasis>closed coded</emphasis>, it
903 is usually left as is, although it might be turned into a call to a
904 different function with different arguments. As an example, if
905 <function>nthcdr</function> were to be open coded, then
906
907 <programlisting>(nthcdr 4 foobar)</programlisting>
908
909 might turn into
910
911 <programlisting>(cdr (cdr (cdr (cdr foobar))))</programlisting>
912
913 or even
914
915 <programlisting>(do ((i 0 (1+ i))
916 (list foobar (cdr foobar)))
917 ((= i 4) list))</programlisting>
918
919 If <function>nth</function> is closed coded, then
920
921 <programlisting>
922   (nth x l)
923 </programlisting>
924
925 might stay the same, or turn into something like
926
927 <programlisting>
928   (car (nthcdr x l))
929 </programlisting>
930 </para>
931
932 <para>In general, open coding sacrifices space for speed, but some
933 functions (such as <function>car</function>) are so simple that they are always
934 open-coded. Even when not open-coded, a call to a standard function
935 may be transformed into a different function call (as in the last
936 example) or compiled as <emphasis>static call</emphasis>. Static function call
937 uses a more efficient calling convention that forbids
938 redefinition.</para>
939
940 </sect1>
941
942 </chapter>