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