2 @comment node-name, next, previous, up
5 This chapter will discuss most compiler issues other than
6 efficiency, including compiler error messages, the SBCL compiler's
7 unusual approach to type safety in the presence of type declarations,
8 the effects of various compiler optimization policies, and the way
9 that inlining and open coding may cause optimized code to differ from
10 a naive translation. Efficiency issues are sufficiently varied and
11 separate that they have their own chapter, @ref{Efficiency}.
17 * Open Coding and Inline Expansion::
21 @comment node-name, next, previous, up
22 @section Error Messages
23 @cindex Error messages, Compiler
24 @cindex Compiler error messages
26 The compiler supplies a large amount of source location information in
27 error messages. The error messages contain a lot of detail in a terse
28 format, so they may be confusing at first. Error messages will be
29 illustrated using this example program:
33 `(roq (ploq (+ ,x 3))))
40 The main problem with this program is that it is trying to add
41 @code{3} to a symbol. Note also that the functions @code{roq} and
42 @code{ploq} aren't defined anywhere.
46 * The Parts of the Error Message::
47 * The Original and Actual Source::
49 * Errors During Macroexpansion::
53 @node The Parts of the Error Message
54 @comment node-name, next, previous, up
55 @subsection The Parts of the Error Message
57 When processing this program, the compiler will produce this warning:
68 ; Asserted type NUMBER conflicts with derived type (VALUES SYMBOL &OPTIONAL).
71 In this example we see each of the six possible parts of a compiler
77 @findex with-compilation-unit
78 @samp{file: /tmp/foo.lisp} This is the name of the file that the
79 compiler read the relevant code from. The file name is displayed
80 because it may not be immediately obvious when there is an error
81 during compilation of a large system, especially when
82 @code{with-compilation-unit} is used to delay undefined warnings.
85 @samp{in: DEFUN FOO} This is the definition top level form responsible
86 for the error. It is obtained by taking the first two elements of the
87 enclosing form whose first element is a symbol beginning with
88 ``@samp{def}''. If there is no such enclosing ``@samp{def}'' form,
89 then the outermost form is used. If there are multiple @samp{def}
90 forms, then they are all printed from the outside in, separated by
91 @samp{=>}'s. In this example, the problem was in the @code{defun} for
95 @cindex Original Source
96 @samp{(ZOQ Y)} This is the @dfn{original source} form responsible for
97 the error. Original source means that the form directly appeared in
98 the original input to the compiler, i.e. in the lambda passed to
99 @code{compile} or in the top level form read from the source file. In
100 this example, the expansion of the @code{zoq} macro was responsible
104 @cindex Processing Path
105 @samp{--> ROQ PLOQ} This is the @dfn{processing path} that the
106 compiler used to produce the errorful code. The processing path is a
107 representation of the evaluated forms enclosing the actual source that
108 the compiler encountered when processing the original source. The
109 path is the first element of each form, or the form itself if the form
110 is not a list. These forms result from the expansion of macros or
111 source-to-source transformation done by the compiler. In this
112 example, the enclosing evaluated forms are the calls to @code{roq} and
113 @code{ploq}. These calls resulted from the expansion of the
117 @cindex Actual Source
118 @samp{==> (+ Y 3)} This is the @dfn{actual source} responsible for the
119 error. If the actual source appears in the explanation, then we print
120 the next enclosing evaluated form, instead of printing the actual
121 source twice. (This is the form that would otherwise have been the
122 last form of the processing path.) In this example, the problem is
123 with the evaluation of the reference to the variable @code{y}.
126 @samp{caught WARNING: Asserted type NUMBER conflicts with derived type
127 (VALUES SYMBOL &OPTIONAL).} This is the @dfn{explanation} of the
128 problem. In this example, the problem is that, while the call to
129 @code{+} requires that its arguments are all of type @code{number},
130 the compiler has derived that @code{y} will evaluate to a
131 @code{symbol}. Note that @samp{(VALUES SYMBOL &OPTIONAL)} expresses
132 that @code{y} evaluates to precisely one value.
136 Note that each part of the error message is distinctively marked:
141 @samp{file:} and @samp{in:} mark the file and definition,
145 The original source is an indented form with no prefix.
148 Each line of the processing path is prefixed with @samp{-->}
151 The actual source form is indented like the original source, but is
152 marked by a preceding @samp{==>} line.
153 @comment no it isn't.
156 The explanation is prefixed with the error severity, which can be
157 @samp{caught ERROR:}, @samp{caught WARNING:}, @samp{caught
158 STYLE-WARNING:}, or @samp{note:}.
162 Each part of the error message is more specific than the preceding
163 one. If consecutive error messages are for nearby locations, then the
164 front part of the error messages would be the same. In this case, the
165 compiler omits as much of the second message as in common with the
169 ; file: /tmp/foo.lisp
176 ; caught STYLE-WARNING:
177 ; undefined function: PLOQ
180 ; (ROQ (PLOQ (+ Y 3)))
182 ; caught STYLE-WARNING:
183 ; undefined function: ROQ
185 @comment fixing that weird blank line might be good
187 In this example, the file, definition and original source are
188 identical for the two messages, so the compiler omits them in the
189 second message. If consecutive messages are entirely identical, then
190 the compiler prints only the first message, followed by: @samp{[Last
191 message occurs @var{repeats} times]} where @var{repeats} is the number
192 of times the message was given.
194 If the source was not from a file, then no file line is printed. If
195 the actual source is the same as the original source, then the
196 processing path and actual source will be omitted. If no forms
197 intervene between the original source and the actual source, then the
198 processing path will also be omitted.
201 @node The Original and Actual Source
202 @comment node-name, next, previous, up
203 @subsection The Original and Actual Source
204 @cindex Original Source
205 @cindex Actual Source
207 The @emph{original source} displayed will almost always be a list. If
208 the actual source for an error message is a symbol, the original
209 source will be the immediately enclosing evaluated list form. So even
210 if the offending symbol does appear in the original source, the
211 compiler will print the enclosing list and then print the symbol as
212 the actual source (as though the symbol were introduced by a macro.)
214 When the @emph{actual source} is displayed (and is not a symbol), it
215 will always be code that resulted from the expansion of a macro or a
216 source-to-source compiler optimization. This is code that did not
217 appear in the original source program; it was introduced by the
220 Keep in mind that when the compiler displays a source form in an error
221 message, it always displays the most specific (innermost) responsible
222 form. For example, compiling this function
232 gives this error message
235 ; file: /tmp/foo.lisp
238 ; (DECLARE (FIXNUM A))
243 ; Asserted type FIXNUM conflicts with derived type (VALUES NULL &OPTIONAL).
246 This error message is not saying ``there is a problem somewhere in
247 this @code{let}'' -- it is saying that there is a problem with the
248 @code{let} itself. In this example, the problem is that @code{a}'s
249 @code{nil} initial value is not a @code{fixnum}.
252 @subsection The Processing Path
253 @cindex Processing path
254 @cindex Macroexpansion
255 @cindex Source-to-source transformation
257 The processing path is mainly useful for debugging macros, so if you
258 don't write macros, you can probably ignore it. Consider this example:
262 (dotimes (i n *undefined*)))
265 Compiling results in this error message:
269 ; (DOTIMES (I N *UNDEFINED*))
270 ; --> DO BLOCK LET TAGBODY RETURN-FROM
272 ; (PROGN *UNDEFINED*)
275 ; undefined variable: *UNDEFINED*
278 Note that @code{do} appears in the processing path. This is because
279 @code{dotimes} expands into:
282 (do ((i 0 (1+ i)) (#:g1 n))
283 ((>= i #:g1) *undefined*)
284 (declare (type unsigned-byte i)))
287 The rest of the processing path results from the expansion of
292 (let ((i 0) (#:g1 n))
293 (declare (type unsigned-byte i))
295 #:g2 (psetq i (1+ i))
296 #:g3 (unless (>= i #:g1) (go #:g2))
297 (return-from nil (progn *undefined*)))))
300 In this example, the compiler descended into the @code{block},
301 @code{let}, @code{tagbody} and @code{return-from} to
302 reach the @code{progn} printed as the actual source. This is a
303 place where the ``actual source appears in explanation'' rule
304 was applied. The innermost actual source form was the symbol
305 @code{*undefined*} itself, but that also appeared in the
306 explanation, so the compiler backed out one level.
310 @comment node-name, next, previous, up
311 @subsection Error Severity
312 @cindex Severity of compiler errors
313 @cindex compiler error severity
315 There are four levels of compiler error severity: @emph{error},
316 @emph{warning}, @emph{style warning}, and @emph{note}. The first three
317 levels correspond to condition classes which are defined in the ANSI
318 standard for Common Lisp and which have special significance to the
319 @code{compile} and @code{compile-file} functions. These levels of
320 compiler error severity occur when the compiler handles conditions of
321 these classes. The fourth level of compiler error severity,
322 @emph{note}, is used for problems which are too mild for the standard
323 condition classes, typically hints about how efficiency might be
325 @comment mention sb-ext:compiler-note
327 @node Errors During Macroexpansion
328 @comment node-name, next, previous, up
329 @subsection Errors During Macroexpansion
330 @cindex Macroexpansion, errors during
332 The compiler handles errors that happen during macroexpansion, turning
333 them into compiler errors. If you want to debug the error (to debug a
334 macro), you can set @code{*break-on-signals*} to @code{error}. For
335 example, this definition:
339 (do ((current l (cdr current))
340 ((atom current) nil))
341 (when (eq (car current) e) (return current))))
348 ; (DO ((CURRENT L (CDR CURRENT))
349 ; ((ATOM CURRENT) NIL))
350 ; (WHEN (EQ (CAR CURRENT) E) (RETURN CURRENT)))
353 ; (in macroexpansion of (DO # #))
354 ; (hint: For more precise location, try *BREAK-ON-SIGNALS*.)
355 ; DO step variable is not a symbol: (ATOM CURRENT)
360 @comment node-name, next, previous, up
361 @subsection Read Errors
362 @cindex Read errors, compiler
364 SBCL's compiler does not attempt to recover from read errors when
365 reading a source file, but instead just reports the offending
366 character position and gives up on the entire source file.
369 @c <!-- FIXME: How much control over error messages is in SBCL?
370 @c _ How much should be? How much of this documentation should
371 @c _ we save or adapt?
373 @c _ %%\node Error Message Parameterization, , Read Errors, Interpreting Error Messages
374 @c _ \subsection{Error Message Parameterization}
375 @c _ \cpsubindex{error messages}{verbosity}
376 @c _ \cpsubindex{verbosity}{of error messages}
378 @c _ There is some control over the verbosity of error messages. See also
379 @c _ \varref{undefined-warning-limit}, \code{*efficiency-note-limit*} and
380 @c _ \varref{efficiency-note-cost-threshold}.
382 @c _ \begin{defvar}{}{enclosing-source-cutoff}
384 @c _ This variable specifies the number of enclosing actual source forms
385 @c _ that are printed in full, rather than in the abbreviated processing
386 @c _ path format. Increasing the value from its default of \code{1}
387 @c _ allows you to see more of the guts of the macroexpanded source,
388 @c _ which is useful when debugging macros.
391 @c _ \begin{defvar}{}{error-print-length}
392 @c _ \defvarx{error-print-level}
394 @c _ These variables are the print level and print length used in
395 @c _ printing error messages. The default values are \code{5} and
396 @c _ \code{3}. If null, the global values of \code{*print-level*} and
397 @c _ \code{*print-length*} are used.
400 @c _ \begin{defmac}{extensions:}{define-source-context}{%
401 @c _ \args{\var{name} \var{lambda-list} \mstar{form}}}
403 @c _ This macro defines how to extract an abbreviated source context from
404 @c _ the \var{name}d form when it appears in the compiler input.
405 @c _ \var{lambda-list} is a \code{defmacro} style lambda-list used to
406 @c _ parse the arguments. The \var{body} should return a list of
407 @c _ subforms that can be printed on about one line. There are
408 @c _ predefined methods for \code{defstruct}, \code{defmethod}, etc. If
409 @c _ no method is defined, then the first two subforms are returned.
410 @c _ Note that this facility implicitly determines the string name
411 @c _ associated with anonymous functions.
417 @node Handling of Types
418 @comment node-name, next, previous, up
419 @section The Compiler's Handling of Types
421 The most unusual features of the SBCL compiler (which is very similar
422 to the original CMUCL compiler, also known as @dfn{Python}) is its
423 unusually sophisticated understanding of the Common Lisp type system
424 and its unusually conservative approach to the implementation of type
425 declarations. These two features reward the use of type declarations
426 throughout development, even when high performance is not a
427 concern. Also, as discussed in the chapter on performance
428 (@pxref{Efficiency}), the use of appropriate type declarations can be
429 very important for performance as well.
432 The SBCL compiler treats type declarations differently from most other
433 Lisp compilers. By default (@emph{i.e.}, at ordinary levels of the
434 @code{safety} compiler optimization parameter), the compiler doesn't
435 blindly believe most type declarations; it considers them assertions
436 about the program that should be checked.
439 The SBCL compiler also has a greater knowledge of the
440 Common Lisp type system than other compilers. Support is incomplete
441 only for types involving the @code{satisfies} type specifier.
443 @c <!-- FIXME: See also sections \ref{advanced-type-stuff}
444 @c and \ref{type-inference}, once we snarf them from the
445 @c CMU CL manual. -->
447 @c Also see my paper on improving Baker, when I get round to it.
450 * Type Errors at Compile Time::
451 * Precise Type Checking::
452 * Weakened Type Checking::
453 * Getting Existing Programs to Run::
454 * Implementation Limitations::
458 @node Type Errors at Compile Time
459 @comment node-name, next, previous, up
460 @subsection Type Errors at Compile Time
461 @cindex Compile time type errors
462 @cindex Type checking, at compile time
464 If the compiler can prove at compile time that some portion of the
465 program cannot be executed without a type error, then it will give a
466 warning at compile time. It is possible that the offending code would
467 never actually be executed at run-time due to some higher level
468 consistency constraint unknown to the compiler, so a type warning
469 doesn't always indicate an incorrect program. For example, consider
483 Compilation produces this warning:
487 ; (CASE FOO (:THIS 13) (:THAT 9) (:THE-OTHER 42))
488 ; --> LET COND IF COND IF COND IF
493 ; This is not a FIXNUM:
497 In this case, the warning means that if @code{foo} isn't any of
498 @code{:this}, @code{:that} or @code{:the-other}, then @code{x} will be
499 initialized to @code{nil}, which the @code{fixnum} declaration makes
500 illegal. The warning will go away if @code{ecase} is used instead of
501 @code{case}, or if @code{:the-other} is changed to @code{t}.
503 This sort of spurious type warning happens moderately often in the
504 expansion of complex macros and in inline functions. In such cases,
505 there may be dead code that is impossible to correctly execute. The
506 compiler can't always prove this code is dead (could never be
507 executed), so it compiles the erroneous code (which will always signal
508 an error if it is executed) and gives a warning.
510 Type warnings are inhibited when the @code{sb-ext:inhibit-warnings}
511 optimization quality is @code{3}. @xref{Compiler Policy}. This
512 can be used in a local declaration to inhibit type warnings in a code
513 fragment that has spurious warnings.
516 @node Precise Type Checking
517 @comment node-name, next, previous, up
518 @subsection Precise Type Checking
519 @cindex Precise type checking
520 @cindex Type checking, precise
522 With the default compilation policy, all type declarations are
523 precisely checked, except in a few situations where they are simply
524 ignored instead. Precise checking means that the check is done as
525 though @code{typep} had been called with the exact type specifier that
526 appeared in the declaration. In SBCL, adding type declarations makes
527 code safer. (Except that remaining bugs in the compiler's handling of
528 types unfortunately provide some exceptions to this rule, see
529 @ref{Implementation Limitations}).
531 If a variable is declared to be @code{(integer 3 17)} then its value
532 must always be an integer between @code{3} and @code{17}. If multiple
533 type declarations apply to a single variable, then all the
534 declarations must be correct; it is as though all the types were
535 intersected producing a single @code{and} type specifier.
537 Argument and result type declarations are automatically enforced. If
538 you declare the type of a function argument, a type check will be done
539 when that function is called. In a function call, the called function
540 does the argument type checking.
542 The types of structure slots are also checked. The value of a
543 structure slot must always be of the type indicated in any
544 @code{:type} slot option.
546 In traditional Common Lisp compilers, not all type assertions are
547 checked, and type checks are not precise. Traditional compilers
548 blindly trust explicit type declarations, but may check the argument
549 type assertions for built-in functions. Type checking is not precise,
550 since the argument type checks will be for the most general type legal
551 for that argument. In many systems, type declarations suppress what
552 little type checking is being done, so adding type declarations makes
553 code unsafe. This is a problem since it discourages writing type
554 declarations during initial coding. In addition to being more error
555 prone, adding type declarations during tuning also loses all the
556 benefits of debugging with checked type assertions.
558 To gain maximum benefit from the compiler's type checking, you should
559 always declare the types of function arguments and structure slots as
560 precisely as possible. This often involves the use of @code{or},
561 @code{member}, and other list-style type specifiers.
564 @node Weakened Type Checking
565 @comment node-name, next, previous, up
566 @subsection Weakened Type Checking
567 @cindex Weakened type checking
568 @cindex Type checking, weakened
570 At one time, CMUCL supported another level of type checking,
571 ``weakened type checking'', when the value for the @code{speed}
572 optimization quality is greater than @code{safety}, and @code{safety}
573 is not @code{0}. The CMUCL manual still has a description of it, but
574 even the CMU CL code no longer corresponds to the manual. Some of this
575 partial safety checking lingers on in SBCL, but it's not a supported
576 feature, and should not be relied on. If you ask the compiler to
577 optimize @code{speed} to a higher level than @code{safety}, your
578 program is performing without a safety net, because SBCL may at its
579 option believe any or all type declarations with either partial or
580 nonexistent runtime checking.
583 @node Getting Existing Programs to Run
584 @comment node-name, next, previous, up
585 @subsection Getting Existing Programs to Run
586 @cindex Existing programs, to run
587 @cindex Types, portability
588 @cindex Compatibility with other Lisps
589 @c (should also have an entry in the non-ANSI-isms section)-->
591 Since SBCL's compiler does much more comprehensive type checking than
592 most Lisp compilers, SBCL may detect type errors in programs that have
593 been debugged using other compilers. These errors are mostly incorrect
594 declarations, although compile-time type errors can find actual bugs
595 if parts of the program have never been tested.
597 Some incorrect declarations can only be detected by run-time type
598 checking. It is very important to initially compile a program with
599 full type checks (high @code{safety} optimization) and then test this
600 safe version. After the checking version has been tested, then you can
601 consider weakening or eliminating type checks. @emph{This applies
602 even to previously debugged programs,} because the SBCL compiler does
603 much more type inference than other Common Lisp compilers, so an
604 incorrect declaration can do more damage.
606 The most common problem is with variables whose constant initial value
607 doesn't match the type declaration. Incorrect constant initial values
608 will always be flagged by a compile-time type error, and they are
609 simple to fix once located. Consider this code fragment:
613 (declare (fixnum foo))
618 Here @code{foo} is given an initial value of @code{nil}, but is
619 declared to be a @code{fixnum}. Even if it is never read, the initial
620 value of a variable must match the declared type. There are two ways
621 to fix this problem. Change the declaration
625 (declare (type (or fixnum null) foo))
630 or change the initial value
634 (declare (fixnum foo))
639 It is generally preferable to change to a legal initial value rather
640 than to weaken the declaration, but sometimes it is simpler to weaken
641 the declaration than to try to make an initial value of the
644 Another declaration problem occasionally encountered is incorrect
645 declarations on @code{defmacro} arguments. This can happen when a
646 function is converted into a macro. Consider this macro:
651 `(the fixnum (1+ ,x)))
654 Although legal and well-defined Common Lisp code, this meaning of this
655 definition is almost certainly not what the writer intended. For
656 example, this call is illegal:
662 This call is illegal because the argument to the macro is @code{(+ 4
663 5)}, which is a @code{list}, not a @code{fixnum}. Because of macro
664 semantics, it is hardly ever useful to declare the types of macro
665 arguments. If you really want to assert something about the type of
666 the result of evaluating a macro argument, then put a @code{the} in
671 `(the fixnum (1+ (the fixnum ,x))))
674 In this case, it would be stylistically preferable to change this
675 macro back to a function and declare it inline.
676 @c <!--FIXME: <xref>inline-expansion, once we crib the
677 @c relevant text from the CMU CL manual.-->
679 Some more subtle problems are caused by incorrect declarations that
680 can't be detected at compile time. Consider this code:
683 (do ((pos 0 (position #\a string :start (1+ pos))))
685 (declare (fixnum pos))
689 Although @code{pos} is almost always a @code{fixnum}, it is @code{nil}
690 at the end of the loop. If this example is compiled with full type
691 checks (the default), then running it will signal a type error at the
692 end of the loop. If compiled without type checks, the program will go
693 into an infinite loop (or perhaps @code{position} will complain
694 because @code{(1+ nil)} isn't a sensible start.) Why? Because if you
695 compile without type checks, the compiler just quietly believes the
696 type declaration. Since the compiler believes that @code{pos} is
697 always a @code{fixnum}, it believes that @code{pos} is never
698 @code{nil}, so @code{(null pos)} is never true, and the loop exit test
699 is optimized away. Such errors are sometimes flagged by unreachable
700 code notes, but it is still important to initially compile and test
701 any system with full type checks, even if the system works fine when
702 compiled using other compilers.
704 In this case, the fix is to weaken the type declaration to @code{(or
705 fixnum null)} @footnote{Actually, this declaration is unnecessary in
706 SBCL, since it already knows that @code{position} returns a
707 non-negative @code{fixnum} or @code{nil}.}.
709 Note that there is usually little performance penalty for weakening a
710 declaration in this way. Any numeric operations in the body can still
711 assume that the variable is a @code{fixnum}, since @code{nil} is not a
712 legal numeric argument. Another possible fix would be to say:
715 (do ((pos 0 (position #\a string :start (1+ pos))))
718 (declare (fixnum pos))
722 This would be preferable in some circumstances, since it would allow a
723 non-standard representation to be used for the local @code{pos}
724 variable in the loop body.
725 @c <!-- FIXME: <xref>ND-variables, once we crib the text from the
726 @c CMU CL manual. -->
728 @node Implementation Limitations
729 @comment node-name, next, previous, up
730 @subsection Implementation Limitations
733 Ideally, the compiler would consider @emph{all} type declarations to
734 be assertions, so that adding type declarations to a program, no
735 matter how incorrect they might be, would @emph{never} cause undefined
736 behavior. As of SBCL version 0.8.1, the compiler is known to fall
737 short of this goal in two areas:
742 @code{Proclaim}ed constraints on argument and result types of a
743 function are supposed to be checked by the function. If the function
744 type is proclaimed before function definition, type checks are
745 inserted by the compiler, but the standard allows the reversed order,
746 in which case the compiler will trust the declaration.
749 The compiler cannot check types of an unknown number of values; if the
750 number of generated values is unknown, but the number of consumed is
751 known, only consumed values are checked.
754 There are a few poorly characterized but apparently very uncommon
755 situations where a type declaration in an unexpected location will be
756 trusted and never checked by the compiler.
760 These are important bugs, but are not necessarily easy to fix, so they
761 may, alas, remain in the system for a while.
764 @node Compiler Policy
765 @comment node-name, next, previous, up
766 @section Compiler Policy
768 As of version 0.6.4, SBCL still uses most of the CMUCL code for
769 compiler policy. The CMUCL code has many features and high-quality
770 documentation, but the two unfortunately do not match. So this area of
771 the compiler and its interface needs to be cleaned up. Meanwhile, here
772 is some rudimentary documentation on the current behavior of the
775 Compiler policy is controlled by the @code{optimize} declaration. The
776 compiler supports the ANSI optimization qualities, and also an
777 extension @code{sb-ext:inhibit-warnings}.
779 Ordinarily, when the @code{speed} quality is high, the compiler emits
780 notes to notify the programmer about its inability to apply various
781 optimizations. Setting @code{sb-ext:inhibit-warnings} to a value at
782 least as large as the @code{speed} quality inhibits this
783 notification. This can be useful to suppress notes about code which is
784 known to be unavoidably inefficient. (For example, the compiler issues
785 notes about having to use generic arithmetic instead of fixnum
786 arithmetic, which is not helpful for code which by design supports
787 arbitrary-sized integers instead of being limited to fixnums.)
790 Note: The basic functionality of the @code{optimize
791 inhibit-warnings} extension will probably be supported in all future
792 versions of the system, but it will probably be renamed when the
793 compiler and its interface are cleaned up. The current name is
794 misleading, because it mostly inhibits optimization notes, not
795 warnings. And making it an optimization quality is misleading, because
796 it shouldn't affect the resulting code at all. It may become a
797 declaration identifier with a name like
798 @code{sb-ext:inhibit-notes}, so that what's currently written.
801 (declaim (optimize (sb-ext:inhibit-warnings 2)))
804 would become something like
807 (declaim (sb-ext:inhibit-notes 2))
812 In early versions of SBCL, a @code{speed} value of zero was used to
813 enable byte compilation, but since version 0.7.0, SBCL only supports
816 When @code{safety} is zero, almost all runtime checking of types,
817 array bounds, and so forth is suppressed.
819 When @code{safety} is less than @code{speed}, any and all type checks
820 may be suppressed. At some point in the past, CMUCL had a more nuanced
821 interpretation of this (@pxref{Weakened Type Checking}). However, SBCL
822 doesn't support that interpretation, and setting @code{safety} less
823 than @code{speed} may have roughly the same effect as setting
824 @code{safety} to zero.
826 The value of @code{space} mostly influences the compiler's decision
827 whether to inline operations, which tend to increase the size of
828 programs. Use the value @code{0} with caution, since it can cause the
829 compiler to inline operations so indiscriminately that the net effect
830 is to slow the program by causing cache misses or even swapping.
832 @c <!-- FIXME: old CMU CL compiler policy, should perhaps be adapted
833 @c _ for SBCL. (Unfortunately, the CMU CL docs are out of sync with the
834 @c _ CMU CL code, so adapting this requires not only reformatting
835 @c _ the documentation, but rooting out code rot.)
837 @c _<sect2 id="compiler-policy"><title>Compiler Policy</1000
838 @c _ INDEX {policy}{compiler}
839 @c _ INDEX compiler policy
841 @c _<para>The policy is what tells the compiler <emphasis>how</emphasis> to
842 @c _compile a program. This is logically (and often textually) distinct
843 @c _from the program itself. Broad control of policy is provided by the
844 @c _<parameter>optimize</parameter> declaration; other declarations and variables
845 @c _control more specific aspects of compilation.
848 @c _* The Optimize Declaration::
849 @c _* The Optimize-Interface Declaration::
852 @c _%%\node The Optimize Declaration, The Optimize-Interface Declaration, Compiler Policy, Compiler Policy
853 @c _\subsection{The Optimize Declaration}
854 @c _\label{optimize-declaration}
855 @c _\cindex{optimize declaration}
856 @c _\cpsubindex{declarations}{\code{optimize}}
858 @c _The \code{optimize} declaration recognizes six different
859 @c _\var{qualities}. The qualities are conceptually independent aspects
860 @c _of program performance. In reality, increasing one quality tends to
861 @c _have adverse effects on other qualities. The compiler compares the
862 @c _relative values of qualities when it needs to make a trade-off; i.e.,
863 @c _if \code{speed} is greater than \code{safety}, then improve speed at
864 @c _the cost of safety.
866 @c _The default for all qualities (except \code{debug}) is \code{1}.
867 @c _Whenever qualities are equal, ties are broken according to a broad
868 @c _idea of what a good default environment is supposed to be. Generally
869 @c _this downplays \code{speed}, \code{compile-speed} and \code{space} in
870 @c _favor of \code{safety} and \code{debug}. Novice and casual users
871 @c _should stick to the default policy. Advanced users often want to
872 @c _improve speed and memory usage at the cost of safety and
875 @c _If the value for a quality is \code{0} or \code{3}, then it may have a
876 @c _special interpretation. A value of \code{0} means ``totally
877 @c _unimportant'', and a \code{3} means ``ultimately important.'' These
878 @c _extreme optimization values enable ``heroic'' compilation strategies
879 @c _that are not always desirable and sometimes self-defeating.
880 @c _Specifying more than one quality as \code{3} is not desirable, since
881 @c _it doesn't tell the compiler which quality is most important.
884 @c _These are the optimization qualities:
887 @c _\item[\code{speed}] \cindex{speed optimization quality}How fast the
888 @c _ program should is run. \code{speed 3} enables some optimizations
889 @c _ that hurt debuggability.
891 @c _\item[\code{compilation-speed}] \cindex{compilation-speed optimization
892 @c _ quality}How fast the compiler should run. Note that increasing
893 @c _ this above \code{safety} weakens type checking.
895 @c _\item[\code{space}] \cindex{space optimization quality}How much space
896 @c _ the compiled code should take up. Inline expansion is mostly
897 @c _ inhibited when \code{space} is greater than \code{speed}. A value
898 @c _ of \code{0} enables indiscriminate inline expansion. Wide use of a
899 @c _ \code{0} value is not recommended, as it may waste so much space
900 @c _ that run time is slowed. \xlref{inline-expansion} for a discussion
901 @c _ of inline expansion.
903 @c _\item[\code{debug}] \cindex{debug optimization quality}How debuggable
904 @c _ the program should be. The quality is treated differently from the
905 @c _ other qualities: each value indicates a particular level of debugger
906 @c _ information; it is not compared with the other qualities.
907 @c _ \xlref{debugger-policy} for more details.
909 @c _\item[\code{safety}] \cindex{safety optimization quality}How much
910 @c _ error checking should be done. If \code{speed}, \code{space} or
911 @c _ \code{compilation-speed} is more important than \code{safety}, then
912 @c _ type checking is weakened (\pxlref{weakened-type-checks}). If
913 @c _ \code{safety} if \code{0}, then no run time error checking is done.
914 @c _ In addition to suppressing type checks, \code{0} also suppresses
915 @c _ argument count checking, unbound-symbol checking and array bounds
918 @c _\item[\code{extensions:inhibit-warnings}] \cindex{inhibit-warnings
919 @c _ optimization quality}This is a CMU extension that determines how
920 @c _ little (or how much) diagnostic output should be printed during
921 @c _ compilation. This quality is compared to other qualities to
922 @c _ determine whether to print style notes and warnings concerning those
923 @c _ qualities. If \code{speed} is greater than \code{inhibit-warnings},
924 @c _ then notes about how to improve speed will be printed, etc. The
925 @c _ default value is \code{1}, so raising the value for any standard
926 @c _ quality above its default enables notes for that quality. If
927 @c _ \code{inhibit-warnings} is \code{3}, then all notes and most
928 @c _ non-serious warnings are inhibited. This is useful with
929 @c _ \code{declare} to suppress warnings about unavoidable problems.
932 @c _%%\node The Optimize-Interface Declaration, , The Optimize Declaration, Compiler Policy
933 @c _\subsection{The Optimize-Interface Declaration}
934 @c _\label{optimize-interface-declaration}
935 @c _\cindex{optimize-interface declaration}
936 @c _\cpsubindex{declarations}{\code{optimize-interface}}
938 @c _The \code{extensions:optimize-interface} declaration is identical in
939 @c _syntax to the \code{optimize} declaration, but it specifies the policy
940 @c _used during compilation of code the compiler automatically generates
941 @c _to check the number and type of arguments supplied to a function. It
942 @c _is useful to specify this policy separately, since even thoroughly
943 @c _debugged functions are vulnerable to being passed the wrong arguments.
944 @c _The \code{optimize-interface} declaration can specify that arguments
945 @c _should be checked even when the general \code{optimize} policy is
948 @c _Note that this argument checking is the checking of user-supplied
949 @c _arguments to any functions defined within the scope of the
950 @c _declaration, \code{not} the checking of arguments to \llisp{}
951 @c _primitives that appear in those definitions.
953 @c _The idea behind this declaration is that it allows the definition of
954 @c _functions that appear fully safe to other callers, but that do no
955 @c _internal error checking. Of course, it is possible that arguments may
956 @c _be invalid in ways other than having incorrect type. Functions
957 @c _compiled unsafely must still protect themselves against things like
958 @c _user-supplied array indices that are out of bounds and improper lists.
959 @c _See also the \kwd{context-declarations} option to
960 @c _\macref{with-compilation-unit}.
962 @c _(end of section on compiler policy)
966 @node Open Coding and Inline Expansion
967 @comment node-name, next, previous, up
968 @section Open Coding and Inline Expansion
970 @cindex inline expansion
971 @cindex static functions
973 Since Common Lisp forbids the redefinition of standard functions, the
974 compiler can have special knowledge of these standard functions
975 embedded in it. This special knowledge is used in various ways (open
976 coding, inline expansion, source transformation), but the implications
977 to the user are basically the same:
982 Attempts to redefine standard functions may be frustrated, since the
983 function may never be called. Although it is technically illegal to
984 redefine standard functions, users sometimes want to implicitly
985 redefine these functions when they are debugging using the
986 @code{trace} macro. Special-casing of standard functions can be
987 inhibited using the @code{notinline} declaration, but even then some
988 phases of analysis such as type inferencing are applied by the
992 The compiler can have multiple alternate implementations of standard
993 functions that implement different trade-offs of speed, space and
994 safety. This selection is based on the compiler policy, @ref{Compiler
999 When a function call is @emph{open coded}, inline code whose effect is
1000 equivalent to the function call is substituted for that function
1001 call. When a function call is @emph{closed coded}, it is usually left
1002 as is, although it might be turned into a call to a different function
1003 with different arguments. As an example, if @code{nthcdr} were to be
1013 (cdr (cdr (cdr (cdr foobar))))
1020 (list foobar (cdr foobar)))
1024 If @code{nth} is closed coded, then
1030 might stay the same, or turn into something like
1036 In general, open coding sacrifices space for speed, but some functions
1037 (such as @code{car}) are so simple that they are always
1038 open-coded. Even when not open-coded, a call to a standard function
1039 may be transformed into a different function call (as in the last
1040 example) or compiled as @emph{static call}. Static function call uses
1041 a more efficient calling convention that forbids redefinition.