forked from filonenko-mikhail/cltl2-doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.tex
1947 lines (1599 loc) · 87.5 KB
/
misc.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%Part{MISC, Root = "CLM.MSS"}
%%%Chapter of Common Lisp Manual. Copyright 1984, 1988, 1989 Guy L. Steele Jr.
\clearpage\def\pagestatus{FINAL PROOF}
\chapter{Miscellaneous Features}
%\chapter{Разнообразные дополнительные возможности}
In this chapter are described various things that don't
seem to fit neatly anywhere else in this book:
the compiler, the \cdf{documentation}
function, debugging aids, environment inquiries (including facilities
for calculating and measuring time), and the \cdf{identity} function.
% В этой главе описываются различные вещи, которые не могли быть описаны в
% каком-либо другом месте книги:
% компилятор, функция поиска документации \cdf{documentation}, отладочный
% функционал, информация о среде выполнения (включая подсчет и измерение времени)
% и функция идентичности \cdf{identity}.
\section{The Compiler}
%\section{Компилятор}
\label{COMPILER-SECTION}
The compiler is a program that may make code run faster by translating
programs into an implementation-dependent form that can
be executed more efficiently by the computer. Most of the time
you can write programs without worrying about the compiler;
compiling a file of code should produce an equivalent but more
efficient program. When doing more esoteric things, you may need to
think carefully about what happens at ``compile time'' and what happens
at ``load time.'' Then the \cdf{eval-when} construct
becomes particularly useful.
% Компилятор --- это программа, которая выполняет преобразование кода для того,
% чтобы его выполнение было более быстрым. Способ преобразования зависит от
% реализации. Обычно вы можете писать программы, не беспокоясь о
% компиляторе. Компиляция файла с кодом должна создавать эквивалентную, но более
% быструю программу. При выполнении эзотерических вещей, вам необходимо подумать о
% том, что случается во <<время компиляции>> и, что случает во <<время
% загрузки>>. В таких случаях бывает полезна конструкция \cdf{eval-when}.
Most declarations are not used by the Common Lisp interpreter;
they may be used to give advice to the compiler. The compiler may attempt
to check your advice and warn you if it is inconsistent.
% Большинство деклараций не используются интерпретатором Common Lisp'а, они могут
% использования для советов компилятору. Компилятор может попытаться проверить
% ваш совет и уведомить вас, если совет противоречивый.
Unlike most other Lisp dialects, Common Lisp recognizes \cdf{special}
declarations in interpreted code as well as compiled code.
% В отличие от других диалектов Lisp'a, Common Lisp распознает декларацию
% \cdf{special} как в интерпретируемом, так и в скомпилированном коде.
The internal workings of a compiler will of course be highly
implementation-dependent. The following functions provide a standard
interface to the compiler, however.
% Внутренняя работа компилятора конечно же будет будет во многом зависеть от
% конкретной реализации. Однако, следующие функции предоставляют стандартный
% интерфейс доступа к компилятору.
\begin{defun}[Function]
compile name &optional definition
\begin{obsolete}\noindent
If \emph{definition} is supplied, it should be a lambda-expression,
the interpreted function to be compiled. If it is not supplied,
then \emph{name} should be a symbol with a definition that is a
lambda-expression; that definition is compiled
and the resulting compiled code is put back into the symbol
as its function definition.
\end{obsolete}
\emph{name} may be any function-name (a symbol or a list
whose car is \cdf{setf}---see section~\ref{FUNCTION-NAME-SECTION}).
One may write \cd{(compile '(setf cadr))} to compile the \cdf{setf}
expansion function for \cdf{cadr}.
If the optional \emph{definition} argument is supplied,
it may be either a lambda-expression (which is coerced to a function)
or a function to be compiled; if no \emph{definition} is supplied,
the \cdf{symbol-function} of the symbol is extracted and compiled.
It is permissible for the symbol to have a macro definition rather than
a function definition; both macros and functions may be compiled.
It is an error if the function to be compiled was defined interpretively
in a non-null lexical environment. (An implementation is free to extend
the behavior of \cdf{compile} to compile such functions properly, but
portable programs may not depend on this capability.) The consequences
of calling \cdf{compile} on a function that is already compiled
are unspecified.
\begin{obsolete}
The definition is compiled and a compiled-function object produced.
If \emph{name} is a non-{\nil}
symbol, then the compiled-function object is installed as the
global function definition of the symbol and the symbol is returned.
If \emph{name} is {\false}, then the compiled-function object itself is returned.
For example:
\begin{lisp}
\\
(defun foo ...) \EV\ foo~~~~~~~~\=;\textrm{A function definition} \\
(compile 'foo) \EV\ foo\>;\textrm{Compile it} \\
\>;\textrm{Now \cdf{foo} runs faster (maybe)} \\[4pt]
(compile {\false} \\
~~~~~~~~~'(lambda (a b c) (- (* b b) (* 4 a c)))) \\
~~~\EV\ \textrm{a compiled function of three arguments that computes $b^2-4ac$}
\end{lisp}
\end{obsolete}
\begin{newer}
X3J13 voted in June 1989 \issue{COMPILER-DIAGNOSTICS} to specify that
\cdf{compile} returns two additional values
indicating whether the compiler issued any diagnostics
(see section~\ref{COMPILER-DIAGNOSTICS-SECTION}).
\end{newer}
\end{defun}
X3J13 voted in March 1989 \issue{COMPILER-VERBOSITY} to add two new
keyword arguments \cd{:verbose} and \cd{:print}
to \cdf{compile-file} by analogy with \cdf{load}.
The new function definition is as follows.
\begin{defun}[Function]
compile-file input-pathname &key :output-file :verbose :print
The \emph{input-pathname} must be a valid file specifier, such as a pathname.
The defaults for \emph{input-filename} are taken from the variable
\cd{*default-pathname-defaults*}.
The file should be a Lisp source file;
its contents are compiled and written as a binary object file.
The \cd{:verbose} argument (which defaults to the value of
\cd{*compile-verbose*}), if true, permits \cdf{compile-file} to print a message
in the form of a comment to \cdf{*standard-output*} indicating what file is
being compiled and other useful information.
The \cd{:print} argument (which defaults to the value of \cd{*compile-print*}),
if true, causes information about top-level forms in the file being
compiled to be printed to \cdf{*standard-output*}. Exactly what is printed
is implementation-dependent; nevertheless something will be printed.
\end{defun}
\begin{new}
X3J13 voted in March 1988
\issue{PATHNAME-STREAM}
to specify exactly which streams may be used as pathnames
(see section~\ref{PATHNAME-FUNCTIONS}).
\end{new}
\begin{newer}
X3J13 voted in June 1989 \issue{PATHNAME-WILD}
to clarify that supplying a wild pathname
as the \emph{input-pathname} argument to \cdf{compile-file} has implementation-dependent consequences;
\cdf{compile-file} might signal an error, for example,
or might compile all files that match the wild pathname.
\end{newer}
\begin{newer}
X3J13 voted in June 1989 \issue{PATHNAME-LOGICAL} to require \cdf{compile-file}
to accept logical pathnames (see section~\ref{LOGICAL-PATHNAMES-SECTION}).
\end{newer}
The \cd{:output-file} argument may be used to specify an output pathname;
it defaults in a manner
appropriate to the implementation's file system conventions.
\begin{newer}
X3J13 voted in June 1989 \issue{COMPILER-DIAGNOSTICS} to specify that
\cdf{compile-file} returns three values: the \cdf{truename} of the output
file (or \cdf{nil} if the file could not be created) and two values
indicating whether the compiler issued any diagnostics
(see section~\ref{COMPILER-DIAGNOSTICS-SECTION}).
\end{newer}
\begin{newer}
X3J13 voted in October 1988 \issue{COMPILE-FILE-PACKAGE} to specify that
\cdf{compile-file}, like \cdf{load}, rebinds \cdf{*package*} to its current value. If
some form in the file changes the value of \cdf{*package*},
the old value will be restored when compilation is completed.
\end{newer}
\begin{newer}
X3J13 voted in June 1989 \issue{COMPILE-FILE-SYMBOL-HANDLING} to specify
restrictions on conforming programs to ensure consistent handling of symbols
and packages.
In order to guarantee that compiled files can be loaded correctly,
the user must ensure that the packages referenced in the file are defined
consistently at compile and load time. Conforming Common Lisp programs
must satisfy the following requirements.
\begin{itemize}
\item The value of \cdf{*package*} when a top-level form in the file is processed
by \cdf{compile-file} must be the same as the value of \cdf{*package*} when the
code corresponding to that top-level form in the compiled file is
executed by the loader. In particular,
any top-level form in a file that alters the value of \cdf{*package*}
must change it to a package of the same name at both compile and
load time; moreover, if the first non-atomic top-level form
in the file is not a call to
\cdf{in-package}, then the value of \cdf{*package*} at the time \cdf{load} is
called must be a package with the same name as the package that
was the value of \cdf{*package*} at the time \cdf{compile-file} was called.
\item For every symbol appearing lexically within a top-level form that
was accessible in the package that was the value of \cdf{*package*}
during processing of that top-level form at compile time, but
whose home package was another package, at load time there must
be a symbol with the same name that is accessible in both the
load-time \cdf{*package*} and in the package with the same name as the
compile-time home package.
\item For every symbol in the compiled file that was an external symbol in
its home package at compile time, there must be a symbol with the
same name that is an external symbol in the package with the same name
at load time.
\end{itemize}
If any of these conditions do not hold, the package in which \cdf{load} looks
for the affected symbols is unspecified. Implementations are permitted
to signal an error or otherwise define this behavior.
These requirements are merely an explicit statement of the status quo,
namely that users cannot depend on any particular behavior if the
package environment at load time is inconsistent with what existed
at compile time.
\end{newer}
\begin{newer}
X3J13 voted in March 1989 \issue{IN-SYNTAX}
to specify that \cdf{compile-file} must bind \cd{*readtable*} to its current value
at the time \cdf{compile-file} is called; the dynamic extent of the binding
should encompass all of the file-loading activity.
This allows a portable program to include forms such as
\begin{lisp}
(in-package "FOO") \\*
\\*
(eval-when (:execute :load-toplevel :compile-toplevel) \\*
~~(setq *readtable* foo:my-readtable))
\end{lisp}
without performing a net global side effect on the loading environment.
Such statements allow the remainder of such a file to be read either as
interpreted code or by \cdf{compile-file} in a syntax determined by
an alternative readtable.
\end{newer}
\begin{newer}
X3J13 voted in June 1989 \issue{LOAD-TRUENAME}
to require that \cdf{compile-file} bind two new variables
\cd{*compile-file-pathname*} and \cd{*compile-file-truename*}; the dynamic extent of the bindings
should encompass all of the file-compiling activity.
\end{newer}
\begin{defun}[Variable]
*compile-verbose*
This variable provides the default for the \cd{:verbose} argument
to \cdf{compile-file}. Its initial value is implementation-dependent.
\end{defun}
\begin{defun}[Variable]
*compile-print*
This variable provides the default for the \cd{:print} argument
to \cdf{compile-file}. Its initial value is implementation-dependent.
\end{defun}
\begin{defun}[Variable]
*compile-file-pathname*
X3J13 voted in June 1989 \issue{LOAD-TRUENAME} to introduce \cd{*compile-file-pathname*};
it is initially \cdf{nil} but \cdf{compile-file} binds it to a pathname that
represents the file name given as the first argument to \cdf{compile-file} merged
with the defaults (see \cdf{merge-pathname}).
\end{defun}
\begin{defun}[Variable]
*compile-file-truename*
Variable is initially \cdf{nil} but \cdf{compile-file} binds it to the ``true
name'' of the pathname of the file being compiled. See \cdf{truename}.
\end{defun}
\begin{defspec}
load-time-value form [read-only-p]
This is a mechanism for delaying evaluation of a \emph{form} until it can be
done in the run-time environment.
If a \cdf{load-time-value} expression is seen by \cdf{compile-file}, the compiler
performs its normal semantic processing (such as macro expansion and
translation into machine code) on the form, but arranges for the
execution of the \emph{form} to occur at load time in a null
lexical environment, with the result of this evaluation then being
treated as an immediate quantity (that is, as if originally quoted)
at run time. It is guaranteed that
the evaluation of the \emph{form} will take place only once when the file is
loaded, but the order of evaluation with respect to the execution
of top-level forms in the file is unspecified.
If a \cdf{load-time-value} expression appears within a function compiled
with \cdf{compile}, the \emph{form} is evaluated at compile time in a null lexical
environment. The result of this compile-time evaluation is treated as
an immediate quantity in the compiled code.
In interpreted code, \emph{form} is evaluated (by \cdf{eval}) in a null
lexical environment and one value is returned. Implementations that
implicitly compile (or partially compile) expressions passed to
\cdf{eval} may evaluate the \emph{form} only once, at the time this
compilation is performed. This is intentionally similar to the
freedom that implementations are given for the time of expanding
macros in interpreted code.
If the same (as determined by \cdf{eq}) list \cd{(load-time-value \emph{form})} is
evaluated or compiled more than once, it is unspecified whether the \emph{form}
is evaluated only once or is evaluated more than once. This can
happen both when an expression being evaluated or compiled shares
substructure and when the same expression is passed to \cdf{eval} or to
\cdf{compile} multiple times. Since a \cdf{load-time-value} expression may be
referenced in more than one place and may be evaluated multiple times
by the interpreter, it is unspecified whether each execution returns
a ``fresh'' object or returns the same object as some other execution.
Users must use caution when destructively modifying the resulting
object.
If two lists \cd{(load-time-value \emph{form})} are \cdf{equal} but not \cdf{eq}, their
values always come from distinct evaluations of \emph{form}. Coalescing
of these forms is not permitted.
The optional \emph{read-only-p} argument designates whether the result
may be considered a
read-only constant. If \cdf{nil} (the default), the result must be considered
ordinary, modifiable data. If \cdf{t}, the result is a read-only quantity
that may, as appropriate, be copied into read-only space and may,
as appropriate, be shared
with other programs. The \emph{read-only-p} argument is
not evaluated and only the literal symbols \cdf{t} and \cdf{nil} are permitted.
This new feature addresses the same set of needs as the now-defunct
\cd{\#,} reader syntax but in a cleaner and more general manner.
Note that \cd{\#,} syntax was reliably useful only inside quoted structure
(though this was not explicitly mentioned in the first edition),
whereas a \cdf{load-time-value} form must appear outside quoted structure in a
for-evaluation position.
See \cdf{make-load-form}.
\end{defspec}
\begin{defun}[Function]
disassemble name-or-compiled-function
The argument should be a function object, a lambda-expression, or
a symbol with a function definition. If the relevant function is not a
compiled function, it is first compiled. In any case, the compiled code
is then ``reverse-assembled'' and printed out in a symbolic format. This
is primarily useful for debugging the compiler, but also often of use to
the novice who wishes to understand the workings of compiled code.
\beforenoterule
\begin{implementation}
Implementors are encouraged to make the output
readable, preferably with helpful comments.
\end{implementation}
\afternoterule
When \cdf{disassemble} compiles a function, it never
installs the resulting compiled-function object in the
\cdf{symbol-function} of a symbol.
\emph{name} may be any function-name (a symbol or a list
whose car is \cdf{setf}---see section~\ref{FUNCTION-NAME-SECTION}).
Thus one may write \cd{(disassemble '(setf cadr))} to disassemble the \cdf{setf}
expansion function for \cdf{cadr}.
\end{defun}
\begin{defun}[Function]
function-lambda-expression fn
This function allows the
source code for a defined function to be recovered.
(The committee noted that the first edition provided no
portable way to recover a lambda-expression once it had
been compiled or evaluated to produce a function.)
This function takes one argument, which must be a function, and returns
three values.
The first value is the defining lambda-expression for the
function, or {\false} if that information is not available.
The lambda-expression may have been preprocessed in some ways
but should nevertheless be of a form suitable as an argument
to the function \cdf{compile} or for use in the \cdf{function} special operator.
The second value is {\false} if the function was definitely
produced by closing
a lambda-expression in the null lexical environment; it is some
non-{\false} value if the function might have been closed in some
non-null lexical environment.
The third value is the ``name'' of the function; this is {\false} if the
name is not available or if the function had no name.
The name is intended for debugging purposes only and may be
any Lisp object (not necessarily one that would be valid for use as a name
in a \cdf{defun} or \cdf{function} special operator, for example).
\beforenoterule
\begin{implementation}
An implementation is always free to return the values
{\false}, \cdf{t}, {\false} from this function but is encouraged to
make more useful information available as appropriate.
For example, it may not be desirable for files of compiled code
to retain the source lambda-expressions for use after the file is loaded,
but it is probably desirable for
functions produced by ``in-core'' calls to \cdf{eval},
\cdf{compile}, or \cdf{defun} to retain the defining lambda-expression
for debugging purposes. The function \cdf{function-lambda-expression}
makes this information, if retained, accessible in a standard and portable
manner.
\end{implementation}
\afternoterule
\end{defun}
\begin{defmac}
with-compilation-unit ({option-name option-value}*) {form}*
\cdf{with-compilation-unit} executes the body forms as an implicit \cdf{progn}. Within the dynamic context
of this form, warnings deferred by the compiler until ``the end of
compilation'' will be deferred until the end of the outermost call
to \cdf{with-compilation-unit}. The results are the same as those of
the last of the forms (or \cdf{nil} if there is no \emph{form}).
Each \emph{option-name} is an unevaluated keyword; each \emph{option-value}
is evaluated. The set of keywords permitted may be extended by the
implementation, but the only standard option keyword is \cd{:override};
the default value for this option is \cdf{nil}.
If \cdf{with-compilation-unit} forms are nested dynamically, only the outermost
such call has any effect unless the \cd{:override} value of an
inner call is true.
The function \cdf{compile-file} should
provide the effect of
\begin{lisp}
(with-compilation-unit (:override nil) ...)
\end{lisp}
around its code.
Any implementation-dependent extensions to this behavior may be provided only
as the result of an explicit programmer request by use of
an implementation-dependent keyword. It is forbidden for an implementation
to attach additional meaning to a conforming use of this
macro.
Note that not all compiler warnings are deferred. In some implementations,
it may be that none are deferred. This macro only creates an
interface to the capability where it exists, it does not require the
creation of the capability. An implementation that does not
defer any compiler warnings may correctly implement this macro
as an expansion into a simple \cdf{progn}.
\end{defmac}
\subsection{Compiler Diagnostics}
\label{COMPILER-DIAGNOSTICS-SECTION}
\cdf{compile} and \cdf{compile-file}
may output warning messages; any such messages should
go to the stream that is the value of \cdf{*error-output*}.
First, note that
\cdf{error} and \cdf{warning} conditions may be signaled either by the compiler itself
or by code being processed by the compiler (for example, arbitrary errors may
occur during compile-time macro expansion or processing of \cdf{eval-when}
forms).
Considering only those conditions signaled \emph{by the compiler} (as
opposed to \emph{during compilation}):
\begin{itemize}
\item Conditions of type \cdf{error} may be signaled by the compiler in
situations where the compilation cannot proceed without
intervention. Examples of such situations may include errors when opening
a file or syntax errors.
\item Conditions of type \cdf{warning} may be signaled by the compiler in
situations where the standard explicitly states that a warning must,
should, or may be signaled. They may also be signaled
when the compiler can determine
that a situation would result at runtime that would have
undefined consequences or would cause
an error to be signaled.
Examples of such situations may include
violations of type declarations,
altering or rebinding a constant defined with \cdf{defconstant},
calls to built-in Lisp functions with too few or too many arguments
or with malformed keyword argument lists,
referring to a variable declared \cdf{ignore}, or
unrecognized declaration specifiers.
\item The compiler is permitted to signal diagnostics about matters of
programming style as conditions of type \cdf{style-warning}, a subtype
of \cdf{warning}. Although
a \cdf{style-warning} condition \emph{may} be signaled in these situations, no
implementation is \emph{required} to do so. However, if an
implementation does choose to signal a condition, that condition
will be of type \cdf{style-warning} and will be signaled by a call to
the function \cdf{warn}.
Examples of such situations may include
redefinition of a function with an incompatible argument list,
calls to functions (other than built-in functions)
with too few or too many arguments
or with malformed keyword argument lists,
unreferenced local variables not declared \cdf{ignore}, or
standard declaration specifiers that are ignored by
the particular compiler in question.
\end{itemize}
Both \cdf{compile} and \cdf{compile-file} are permitted (but not
required) to establish a handler for conditions of type \cdf{error}.
Such a handler
might, for example, issue a warning and restart compilation from some
implementation-dependent point in order to let the compilation
proceed without manual intervention.
The functions \cdf{compile} and \cdf{compile-file} each return three values.
See the definitions of these functions for descriptions of the first value.
The second value is \cdf{nil} if no compiler diagnostics were issued, and
true otherwise.
The third value is \cdf{nil} if no compiler diagnostics other than style
warnings were issued; a non-\cdf{nil} value indicates that there were
``serious'' compiler diagnostics issued or that other conditions of
type \cdf{error} or \cdf{warning} (but not \cdf{style-warning}) were signaled during
compilation.
\subsection{Compiled Functions}
Certain requirements are imposed on the functions produced by the compilation
process.
If a function is of type \cdf{compiled-function}, then
all macro calls appearing lexically within the function have
already been expanded and will not be expanded again when the
function is called. The process of
compilation effectively turns every \cdf{macrolet} or \cdf{symbol-macrolet}
construct into a \cdf{progn} (or a \cdf{locally}) with all
instances of the local macros in the body fully expanded.
If a function is of type \cdf{compiled-function}, then
all \cdf{load-time-value} forms appearing lexically within the function have
already been pre-evaluated and will not be evaluated
again when the function is called.
Implementations are free to classify every function as
a \cdf{compiled-function} provided that all functions
satisfy the preceding requirements.
Conversely, it is permissible for a function that is
not a \cdf{compiled-function} to satisfy the preceding requirements.
If one or more functions are defined in a file that is compiled
with \cdf{compile-file} and the compiled file is subsequently loaded
by the function \cdf{load},
the resulting loaded function definitions must be of
type \cdf{compiled-function}.
The function \cdf{compile} must produce an object of type
\cdf{compiled-function} as the value that is either returned
or stored into the \cdf{symbol-function} of a symbol argument.
Note that none of these restrictions addresses questions of the compilation technology or
target instruction set. For example, a compiled function does not necessarily consist of
native machine instructions. These requirements merely specify the behavior of the type
system with respect to certain actions taken by \cdf{compile}, \cdf{compile-file}, and
\cdf{load}.
\subsection{Compilation Environment}
Following information must be available at compile time
for correct compilation
and what need not be available until run time.
The following information must be present in the compile-time
environment for a program to be compiled correctly. This
information need not also be present in the run-time environment.
\begin{itemize}
\item In conforming code, macros referenced in the code being compiled
must have been previously defined in the compile-time environment.
The compiler must treat as a function call any form that is a list whose \emph{car} is
a symbol that does not name a macro or special operator.
(This implies that \cdf{setf} methods must also be available at
compile time.)
\item In conforming code, proclamations for \cdf{special} variables must
be made in the compile-time environment before any bindings of
those variables are processed by the compiler. The compiler
must treat any binding of an undeclared variable as a lexical
binding.
\end{itemize}
The compiler may incorporate the following kinds of information
into the code it produces, if the information is present in the
compile-time environment and is referenced within the code being
compiled; however, the compiler is not required to do so.
When compile-time and run-time definitions differ, it is
unspecified which will prevail within the compiled code
(unless some other behavior is explicitly specified below). It is also
permissible for an implementation to signal an error at run time on
detecting such a discrepancy. In all cases, the absence of the
information at compile time is not an error, but its presence may
enable the compiler to generate more efficient code.
\begin{itemize}
\item The compiler may assume that functions that are defined and
declared \cdf{inline} in the compile-time environment will retain the
same definitions at run time.
\item The compiler may assume that, within a named function, a
recursive call to a function of the same name refers to the
same function, unless that function has been declared \cdf{notinline}.
(This permits tail-recursive calls of a function to itself
to be compiled as jumps, for example, thereby turning certain recursive
schemas into efficient loops.)
\item In the absence of \cdf{notinline}
declarations to the contrary,
\cdf{compile-file} may assume that a call within the file being compiled to a named
function that is defined in that file refers to that function.
(This rule permits \emph{block compilation} of files.) The behavior of
the program is unspecified if functions are redefined individually
at run time.
\item The compiler may assume that the signature (or ``interface contract'') of
all built-in Common Lisp functions will not change. In addition,
the compiler may treat all built-in Common Lisp functions as if
they had been proclaimed \cdf{inline}.
\item The compiler may assume that the signature (or ``interface contract'') of
functions with \cdf{ftype} information available will not change.
\item The compiler may ``wire in'' (that is, open-code or inline)
the values of symbolic constants
that have been defined with \cdf{defconstant} in the compile-time
environment.
\item The compiler may assume that any type definition made with \cdf{defstruct}
or \cdf{deftype} in the compile-time environment will retain the same
definition in the run-time environment. It may also assume that
a class defined by \cdf{defclass} in the compile-time environment will
be defined in the run-time environment in such a way as to have
the same superclasses and metaclass. This implies that
subtype/supertype relationships of type specifiers will not
change between compile time and run time. (Note that it is not
an error for an unknown type to appear in a declaration at
compile time, although it is reasonable for the compiler to
emit a warning in such a case.)
\item The compiler may assume that if type declarations are present
in the compile-time environment, the corresponding variables and
functions present in the run-time environment will actually be of
those types. If this assumption is violated, the run-time behavior of the program is
undefined.
\end{itemize}
The compiler must not make any additional assumptions about
consistency between the compile-time and run-time environments. In
particular, the compiler may not assume that functions that are defined
in the compile-time environment will retain either the
same definition or the same signature at run time, except
as described above.
Similarly,
the compiler may not signal an error if it sees a call to a
function that is not defined at compile time, since that function
may be provided at run time.
X3J13 voted in January 1989 \issue{COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS}
to specify the compile-time side effects of processing various macro forms.
Calls to defining macros such as \cdf{defmacro} or \cdf{defvar} appearing
within a file being processed by \cdf{compile-file} normally have
compile-time side effects that affect how subsequent forms in the
same file are compiled. A convenient model for explaining how these
side effects happen is that each defining macro expands into one or
more \cdf{eval-when} forms and that compile-time
side effects are caused by calls occurring in the body of an
\cd{(eval-when (:compile-toplevel) ...)} form.
The affected defining macros and their specific side effects are
as follows. In each case, it is identified what a user must do to
ensure that a program is conforming, and what a compiler must do
in order to correctly process a conforming program.
\begin{flushdesc}
\item[\cdf{deftype}]
The user must ensure that the body of a \cdf{deftype} form is
evaluable at compile time if the type is referenced in subsequent type
declarations. The compiler must ensure that a type
specifier defined by \cdf{deftype}
is recognized in subsequent type declarations. If the
expansion of a type specifier is not defined fully at compile time
(perhaps because it expands into an unknown type specifier or a
\cdf{satisfies} of a named function that isn't defined in the compile-time
environment), an implementation may ignore any references to this type
in declarations and may signal a warning.
\item[\cdf{defmacro} and \cdf{define-modify-macro}]
The compiler must store macro
definitions at compile time, so that occurrences of the macro later on
in the file can be expanded correctly. The user must ensure that the
body of the macro is evaluable at compile time if it is referenced
within the file being compiled.
\item[\cdf{defun}]
No required compile-time side effects are associated with \cdf{defun} forms.
In particular, \cdf{defun} does not make the function definition available
at compile time. An implementation may choose to store information
about the function for the purposes of compile-time error checking
(such as checking the number of arguments on calls) or to permit later
\cdf{inline} expansion of the function.
\item[\cdf{defvar} and \cdf{defparameter}]
The compiler must recognize that the variables
named by these forms have been proclaimed \cdf{special}. However, it must
not evaluate the \emph{initial-value} form or \cdf{set} the variable at compile
time.
\item[\cdf{defconstant}]
The compiler must recognize that the symbol names a
constant. An implementation may choose to evaluate the \emph{value-form} at
compile time, load time, or both. Therefore the user must ensure that
the \emph{value-form} is evaluable at compile time (regardless of whether or
not references to the constant appear in the file) and that it always
evaluates to the same value.
(There has been considerable
variance among implementations on this point. The effect of this specification is
to legitimize all of the implementation variants by requiring care of the user.)
\item[\cdf{defsetf} and \cdf{define-setf-method}]
The compiler must make \cdf{setf} methods
available so that they may be used to expand calls to \cdf{setf} later on in
the file. Users must ensure that the body of a call
to \cdf{define-setf-method} or
the complex form of \cdf{defsetf} is evaluable at compile time if the
corresponding place is referred to in a subsequent \cdf{setf} in the same
file. The compiler must make these \cdf{setf} methods available to
compile-time calls to \cdf{get-setf-method} when its environment argument is
a value received as the \cd{\&environment} parameter of a macro.
\item[\cdf{defstruct}]
The compiler must make the structure type name recognized
as a valid type name in subsequent declarations (as described above
for \cdf{deftype}) and
make the structure slot accessors known to \cdf{setf}.
In addition, the
compiler must save enough information so that
further \cdf{defstruct} definitions can include (with the \cd{:include}
option) a structure type defined
earlier in the file being compiled. The functions that \cdf{defstruct}
generates are not defined in the compile-time environment, although
the compiler may save enough information about the functions to allow
\cdf{inline} expansion of
subsequent calls to these functions. The \cd{\#S} reader syntax may or may not be
available for that structure type at compile time.
\item[\cdf{define-condition}]
The rules are essentially the same as those for
\cdf{defstruct}. The compiler must make the condition type recognizable as a
valid type name, and it must be possible to reference the condition
type as the \emph{parent-type} of another condition type in a subsequent
\cdf{define-condition} form in the file being compiled.
\item[\cdf{defpackage}]
All of the actions normally performed by the \cdf{defpackage} macro at load
time must also be performed at compile time.
\end{flushdesc}
Compile-time side effects may cause information about a
definition to be stored in a different manner from
information about definitions
processed either interpretively or by loading
a compiled file.
In particular, the information stored by a defining macro at
compile time may or may not be available to the interpreter (either
during or after compilation) or during subsequent calls to \cdf{compile} or
\cdf{compile-file}. For example, the following code is not portable because
it assumes that the compiler stores the macro definition of \cdf{foo} where
it is available to the interpreter.
\begin{lisp}
(defmacro foo (x) {\Xbq}(car ,x)) \\
\\
(eval-when (:execute :compile-toplevel :load-toplevel) \\*
~~(print (foo '(a b c))))~~~~~;\textrm{Wrong}
\end{lisp}
The goal may be accomplished portably by including the macro
definition within the \cdf{eval-when} form:
\begin{lisp}
(eval-when (eval compile load) \\*
~~(defmacro foo (x) {\Xbq}(car ,x)) \\*
~~(print (foo '(a b c))))~~~~~;\textrm{Right}
\end{lisp}
\begin{flushdesc}
\item[\cdf{declaim}]
X3J13 voted in June 1989 \issue{PROCLAIM-ETC-IN-COMPILE-FILE}
to add a new macro \cdf{declaim} for making proclamations recognizable
at compile time. The declaration specifiers in the \cdf{declaim} form
are effectively proclaimed at compile time so as to affect
compilation of subsequent forms. (Note that compiler processing
of a call to \cdf{proclaim}
does not have any compile-time side effects, for \cdf{proclaim}
is a function.)
\end{flushdesc}
\begin{flushdesc}
\item[\cdf{in-package}]
X3J13 voted in March 1989 \issue{IN-PACKAGE-FUNCTIONALITY} to specify that
all of the actions normally performed by the \cdf{in-package} macro at load
time must also be performed at compile time.
\end{flushdesc}
X3J13 voted in June 1989 \issue{CLOS-MACRO-COMPILATION}
to specify the compile-time side effects of processing various CLOS-related
macro forms. Top-level calls to the CLOS defining macros have the
following compile-time side effects; any other compile-time behavior
is explicitly left unspecified.
\begin{flushdesc}
\item[\cdf{defclass}]
The class name may appear in subsequent type declarations and
can be used as a specializer in subsequent \cdf{defmethod} forms.
Thus the compile-time behavior of \cdf{defclass} is similar to that of
\cdf{deftype} or \cdf{defstruct}.
\item[\cdf{defgeneric}]
The generic function can be referenced in subsequent \cdf{defmethod} forms,
but the compiler does not arrange for the generic function to be callable
at compile time.
\item[\cdf{defmethod}]
The compiler does not arrange for the method to be callable at compile
time. If there is a generic function with the same name defined at
compile time, compiling a \cdf{defmethod} form does not add the method to that
generic function; the method is added to the generic
function only when the \cdf{defmethod} form is actually executed.
The error-signaling behavior described in the specification of
\cdf{defmethod} in chapter~\ref{CLOS} (if the function isn't a generic function
or if the lambda-list is not congruent) occurs only when the defining
form is executed, not at compile time.
The forms in \cdf{eql} parameter specializers are evaluated when the \cdf{defmethod}
form is executed. The compiler is permitted to build in knowledge
about what the form in an \cdf{eql} specializer will evaluate to in cases
where the ultimate result can be syntactically inferred without
actually evaluating it.
\item[\cdf{define-method-combination}]
The method combination can be used in subsequent \cdf{defgeneric} forms.
The body of a \cdf{define-method-combination} form is evaluated no earlier
than when the defining macro is executed and possibly as late as
generic function invocation time. The compiler may attempt to
evaluate these forms at compile time but must not depend on being able
to do so.
\end{flushdesc}
\subsection{Similarity of Constants}
\label{SIMILAR-AS-A-CONSTANT-SECTION}
Following paragraphs specifies what objects can be in compiled constants and
what relationship there must be between a constant
passed to the compiler and the one that is established by compiling it
and then loading its file.
The key is a definition of an equivalence relationship called
``similarity as constants''
between Lisp
objects. Code passed through the file
compiler and then loaded must behave as though quoted constants in it
are similar in this sense to quoted constants in the corresponding source code.
An object may be used as a quoted constant processed by \cdf{compile-file}
if and only if the compiler can guarantee that the resulting constant established
by loading the compiled file is ``similar as a constant'' to the
original. Specific requirements are spelled out below.
Some types of objects, such as streams, are not supported in constants
processed by the file compiler. Such objects may not portably appear
as constants in code processed with \cdf{compile-file}. Conforming
implementations are required to handle such objects either by having
the compiler or loader reconstruct an equivalent copy of the
object in some implementation-specific manner or by having the
compiler signal an error.
Of the types supported in constants, some are treated as aggregate
objects. For these types, being similar as constants is defined
recursively. We say that an object of such a type has certain ``basic
attributes''; to be similar as a constant to another object, the
values of the corresponding attributes of the two objects must also be
similar as constants.
A definition of this recursive form has problems with any circular or infinitely
recursive object such as a list that is an element of itself. We use
the idea of depth-limited comparison and say that two objects are
similar as constants if they are similar at all finite levels. This
idea is implicit in the definitions below, and it applies in all the
places where attributes of two objects are required to be similar as
constants. The question of handling circular constants is the subject
of a separate vote by X3J13 (see below).
The following terms are used throughout this section.
The term \emph{constant} refers to a quoted or self-evaluating constant,
not a named constant defined by \cdf{defconstant}.
The term \emph{source code} is used to refer to the objects constructed
when \cdf{compile-file} calls \cdf{read} (or the equivalent) and to
additional objects constructed by
macro expansion during file compilation.
The term \emph{compiled code} is used to refer to objects constructed by
\cdf{load}.
Two objects are \emph{similar as a constant} if and only if
they are both of one of the types listed below and satisfy the
additional requirements listed for that type.
\begin{flushdesc}
\item[\cdf{number}]
Two numbers are similar as constants if they are of the same type
and represent the same mathematical value.
\item[\cdf{character}]
Two characters are similar as constants if they both represent
the same character. (The intent is that this be compatible with
how \cdf{eql} is defined on characters.)
\item[\cdf{symbol}]
X3J13 voted in June 1989 \issue{COMPILE-FILE-SYMBOL-HANDLING}
to define similarity as a constant for interned symbols.
A symbol $\emph{S}$ appearing in the source code is similar as a constant to
a symbol $\emph{S}'$ in the compiled code if their print names are similar as constants
and either of the following conditions holds:
\begin{itemize}
\item $\emph{S}$ is accessible in \cdf{*package*} at compile time and $\emph{S}'$ is accessible in
\cdf{*package*} at load time.
\item $\emph{S}'$ is accessible in the package that is similar as a constant to the
home package of symbol \emph{S}.
\end{itemize}
The ``similar as constants'' relationship for interned symbols has nothing
to do with \cd{*readtable*} or how the function \cdf{read} would parse the
characters in the print name of the symbol.
An uninterned symbol in the source code is similar as a constant
to an uninterned symbol in the compiled code if their print names
are similar as constants.
\item[\cdf{package}]
A package in the source code is similar as a constant to a package in
the compiled code if their names are similar as constants. Note that
the loader finds the corresponding package object as if by calling
\cdf{find-package} with the package name as an argument. An error is
signaled if no package of that name exists at load time.
\item[\cdf{random-state}]
We say that two \cdf{random-state} objects are \emph{functionally equivalent} if
applying \cdf{random} to them repeatedly always produces the same
pseudo-random numbers in the same order.
Two random-states are similar as constants if and only if copies of
them made via \cdf{make-random-state} are functionally equivalent.
(Note that a constant \cdf{random-state} object cannot be used as the \emph{state}
argument to the function \cdf{random} because \cdf{random} performs
a side effect on that argument.)
\item[\cdf{cons}]
Two conses are similar as constants if the values of their respective
\emph{car} and \emph{cdr} attributes are similar as constants.
\item[\cdf{array}]
Two arrays are similar as constants if the corresponding values of each
of the following attributes are similar as constants:
for vectors (one-dimensional arrays), the \cdf{length} and \cdf{element-type}
and the result of \cdf{elt} for all valid indices;
for all other arrays, the \cdf{array-rank}, the result of \cdf{array-dimension}
for all valid axis numbers, the \cdf{array-element-type},
and the result of \cdf{aref} for all valid indices. (The point of
distinguishing vectors is to take any fill pointers into account.)
If the array in the source code is a \cdf{simple-array}, then
the corresponding array in the compiled code must also be a
\cdf{simple-array}, but if the array in the source code is displaced, has a
fill pointer, or is adjustable, the corresponding array in the
compiled code is permitted to lack any or all of these qualities.