0.pre7.38:
[sbcl.git] / src / compiler / main.lisp
1 ;;;; the top-level interfaces to the compiler, plus some other
2 ;;;; compiler-related stuff (e.g. CL:CALL-ARGUMENTS-LIMIT) which
3 ;;;; doesn't obviously belong anywhere else
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!C")
15
16 ;;; FIXME: Doesn't this belong somewhere else, like early-c.lisp?
17 (declaim (special *constants* *free-variables* *component-being-compiled*
18                   *code-vector* *next-location* *result-fixups*
19                   *free-functions* *source-paths*
20                   *seen-blocks* *seen-functions* *list-conflicts-table*
21                   *continuation-number* *continuation-numbers*
22                   *number-continuations* *tn-id* *tn-ids* *id-tns*
23                   *label-ids* *label-id* *id-labels*
24                   *undefined-warnings* *compiler-error-count*
25                   *compiler-warning-count* *compiler-style-warning-count*
26                   *compiler-note-count*
27                   *compiler-error-bailout*
28                   #!+sb-show *compiler-trace-output*
29                   *last-source-context* *last-original-source*
30                   *last-source-form* *last-format-string* *last-format-args*
31                   *last-message-count* *lexenv*))
32
33 ;;; FIXME: byte compiler to be removed completely
34 (defvar *byte-compile-default* nil
35   #!+sb-doc
36   "the default value for the :BYTE-COMPILE argument to COMPILE-FILE")
37
38 (defvar *byte-compile-top-level*
39   #|
40   #-sb-xc-host t
41   #+sb-xc-host nil ; since the byte compiler isn't supported in cross-compiler
42   |#
43   nil ; FIXME: byte compiler to be removed completely
44   #!+sb-doc
45   "Similar to *BYTE-COMPILE-DEFAULT*, but controls the compilation of top-level
46    forms (evaluated at load-time) when the :BYTE-COMPILE argument is :MAYBE
47    (the default.)  When true, we decide to byte-compile.")
48
49 ;;; the value of the :BYTE-COMPILE argument which was passed to the
50 ;;; compiler
51 (defvar *byte-compile*
52   nil #|:maybe|#) ; FIXME: byte compiler to be removed completely
53
54 ;;; Bound by COMPILE-COMPONENT to T when byte-compiling, and NIL when
55 ;;; native compiling. During IR1 conversion this can also be :MAYBE,
56 ;;; in which case we must look at the policy; see #'BYTE-COMPILING.
57 (defvar *byte-compiling*
58   nil #|:maybe|#) ; FIXME: byte compiler to be removed completely
59
60 (declaim (type (member t nil :maybe)
61                *byte-compile*
62                *byte-compiling*
63                *byte-compile-default*))
64
65 (defvar *check-consistency* nil)
66 (defvar *all-components*)
67
68 ;;; Bind this to a stream to capture various internal debugging output.
69 (defvar *compiler-trace-output* nil)
70
71 ;;; The current block compilation state. These are initialized to the
72 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
73 ;;; called with.
74 ;;;
75 ;;; *BLOCK-COMPILE-ARGUMENT* holds the original value of the
76 ;;; :BLOCK-COMPILE argument, which overrides any internal
77 ;;; declarations.
78 (defvar *block-compile*)
79 (defvar *block-compile-argument*)
80 (declaim (type (member nil t :specified)
81                *block-compile* *block-compile-argument*))
82 (defvar *entry-points*)
83 (declaim (list *entry-points*))
84
85 ;;; When block compiling, used by PROCESS-FORM to accumulate top-level
86 ;;; lambdas resulting from compiling subforms. (In reverse order.)
87 (defvar *top-level-lambdas*)
88 (declaim (list *top-level-lambdas*))
89
90 (defvar sb!xc:*compile-verbose* t
91   #!+sb-doc
92   "The default for the :VERBOSE argument to COMPILE-FILE.")
93 (defvar sb!xc:*compile-print* t
94   #!+sb-doc
95   "The default for the :PRINT argument to COMPILE-FILE.")
96 (defvar *compile-progress* nil
97   #!+sb-doc
98   "When this is true, the compiler prints to *ERROR-OUTPUT* progress
99   information about the phases of compilation of each function. (This
100   is useful mainly in large block compilations.)")
101
102 (defvar sb!xc:*compile-file-pathname* nil
103   #!+sb-doc
104   "The defaulted pathname of the file currently being compiled, or NIL if not
105   compiling.")
106 (defvar sb!xc:*compile-file-truename* nil
107   #!+sb-doc
108   "The TRUENAME of the file currently being compiled, or NIL if not
109   compiling.")
110
111 (declaim (type (or pathname null)
112                sb!xc:*compile-file-pathname*
113                sb!xc:*compile-file-truename*))
114
115 ;;; the SOURCE-INFO structure for the current compilation. This is
116 ;;; null globally to indicate that we aren't currently in any
117 ;;; identifiable compilation.
118 (defvar *source-info* nil)
119
120 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
121 ;;; normally causes nested uses to be no-ops).
122 (defvar *in-compilation-unit* nil)
123
124 ;;; Count of the number of compilation units dynamically enclosed by
125 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
126 (defvar *aborted-compilation-unit-count*)
127
128 ;;; Mumble conditional on *COMPILE-PROGRESS*.
129 (defun maybe-mumble (&rest foo)
130   (when *compile-progress*
131     (compiler-mumble "~&")
132     (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
133        (apply #'compiler-mumble foo))))
134
135 (deftype object () '(or fasl-output core-object null))
136
137 (defvar *compile-object* nil)
138 (declaim (type object *compile-object*))
139 \f
140 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
141
142 (defmacro sb!xc:with-compilation-unit (options &body body)
143   #!+sb-doc
144   "WITH-COMPILATION-UNIT ({Key Value}*) Form*
145   This form affects compilations that take place within its dynamic extent. It
146   is intended to be wrapped around the compilation of all files in the same
147   system. These keywords are defined:
148     :OVERRIDE Boolean-Form
149         One of the effects of this form is to delay undefined warnings
150         until the end of the form, instead of giving them at the end of each
151         compilation. If OVERRIDE is NIL (the default), then the outermost
152         WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
153         OVERRIDE true causes that form to grab any enclosed warnings, even if
154         it is enclosed by another WITH-COMPILATION-UNIT."
155   `(%with-compilation-unit (lambda () ,@body) ,@options))
156
157 (defun %with-compilation-unit (fn &key override)
158   (let ((succeeded-p nil))
159     (if (and *in-compilation-unit* (not override))
160         ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
161         ;; ordinarily (unless OVERRIDE) basically a no-op.
162         (unwind-protect
163             (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
164           (unless succeeded-p
165             (incf *aborted-compilation-unit-count*)))
166         ;; FIXME: Now *COMPILER-FOO-COUNT* stuff is bound in more than
167         ;; one place. If we can get rid of the IR1 interpreter, this
168         ;; should be easier to clean up.
169         (let ((*aborted-compilation-unit-count* 0)
170               (*compiler-error-count* 0)
171               (*compiler-warning-count* 0)
172               (*compiler-style-warning-count* 0)
173               (*compiler-note-count* 0)
174               (*undefined-warnings* nil)
175               (*in-compilation-unit* t))
176           (handler-bind ((parse-unknown-type
177                           (lambda (c)
178                             (note-undefined-reference
179                              (parse-unknown-type-specifier c)
180                              :type))))
181             (unwind-protect
182                 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
183               (unless succeeded-p
184                 (incf *aborted-compilation-unit-count*))
185               (summarize-compilation-unit (not succeeded-p))))))))
186
187 ;;; This is to be called at the end of a compilation unit. It signals
188 ;;; any residual warnings about unknown stuff, then prints the total
189 ;;; error counts. ABORT-P should be true when the compilation unit was
190 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
191 ;;; enclosed nested compilation units that were aborted.
192 (defun summarize-compilation-unit (abort-p)
193   (unless abort-p
194     (handler-bind ((style-warning #'compiler-style-warning-handler)
195                    (warning #'compiler-warning-handler))
196
197       (let ((undefs (sort *undefined-warnings* #'string<
198                           :key #'(lambda (x)
199                                    (let ((x (undefined-warning-name x)))
200                                      (if (symbolp x)
201                                          (symbol-name x)
202                                          (prin1-to-string x)))))))
203         (dolist (undef undefs)
204           (let ((name (undefined-warning-name undef))
205                 (kind (undefined-warning-kind undef))
206                 (warnings (undefined-warning-warnings undef))
207                 (undefined-warning-count (undefined-warning-count undef)))
208             (dolist (*compiler-error-context* warnings)
209               (compiler-style-warning "undefined ~(~A~): ~S" kind name))
210             (let ((warn-count (length warnings)))
211               (when (and warnings (> undefined-warning-count warn-count))
212                 (let ((more (- undefined-warning-count warn-count)))
213                   (compiler-style-warning
214                    "~D more use~:P of undefined ~(~A~) ~S"
215                    more kind name))))))
216         
217         (dolist (kind '(:variable :function :type))
218           (let ((summary (mapcar #'undefined-warning-name
219                                  (remove kind undefs :test-not #'eq
220                                          :key #'undefined-warning-kind))))
221             (when summary
222               (compiler-style-warning
223                "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
224                 ~%  ~{~<~%  ~1:;~S~>~^ ~}"
225                (cdr summary) kind summary)))))))
226
227   (unless (and (not abort-p)
228                (zerop *aborted-compilation-unit-count*)
229                (zerop *compiler-error-count*)
230                (zerop *compiler-warning-count*)
231                (zerop *compiler-style-warning-count*)
232                (zerop *compiler-note-count*))
233     (format *error-output* "~&")
234     (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
235       (compiler-mumble "compilation unit ~:[finished~;aborted~]~
236                        ~[~:;~:*~&  caught ~D fatal ERROR condition~:P~]~
237                        ~[~:;~:*~&  caught ~D ERROR condition~:P~]~
238                        ~[~:;~:*~&  caught ~D WARNING condition~:P~]~
239                        ~[~:;~:*~&  caught ~D STYLE-WARNING condition~:P~]~
240                        ~[~:;~:*~&  printed ~D note~:P~]"
241                        abort-p
242                        *aborted-compilation-unit-count*
243                        *compiler-error-count*
244                        *compiler-warning-count*
245                        *compiler-style-warning-count*
246                        *compiler-note-count*)))
247   (format *error-output* "~&"))
248
249 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
250 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
251 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
252 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
253 (defmacro with-compilation-values (&body body)
254   `(with-ir1-namespace
255     (let ((*warnings-p* nil)
256           (*failure-p* nil))
257       (values (progn ,@body)
258               *warnings-p*
259               *failure-p*))))
260 \f
261 ;;;; component compilation
262
263 (defparameter *max-optimize-iterations* 3 ; ARB
264   #!+sb-doc
265   "The upper limit on the number of times that we will consecutively do IR1
266   optimization that doesn't introduce any new code. A finite limit is
267   necessary, since type inference may take arbitrarily long to converge.")
268
269 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
270 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
271
272 ;;; Repeatedly optimize COMPONENT until no further optimizations can
273 ;;; be found or we hit our iteration limit. When we hit the limit, we
274 ;;; clear the component and block REOPTIMIZE flags to discourage the
275 ;;; next optimization attempt from pounding on the same code.
276 (defun ir1-optimize-until-done (component)
277   (declare (type component component))
278   (maybe-mumble "opt")
279   (event ir1-optimize-until-done)
280   (let ((count 0)
281         (cleared-reanalyze nil))
282     (loop
283       (when (component-reanalyze component)
284         (setq count 0)
285         (setq cleared-reanalyze t)
286         (setf (component-reanalyze component) nil))
287       (setf (component-reoptimize component) nil)
288       (ir1-optimize component)
289       (cond ((component-reoptimize component)
290              (incf count)
291              (when (= count *max-optimize-iterations*)
292                (maybe-mumble "*")
293                (cond ((retry-delayed-ir1-transforms :optimize)
294                       (maybe-mumble "+")
295                       (setq count 0))
296                      (t
297                       (event ir1-optimize-maxed-out)
298                       (setf (component-reoptimize component) nil)
299                       (do-blocks (block component)
300                         (setf (block-reoptimize block) nil))
301                       (return)))))
302             ((retry-delayed-ir1-transforms :optimize)
303              (setf count 0)
304              (maybe-mumble "+"))
305             (t
306              (maybe-mumble " ")
307              (return)))
308       (maybe-mumble "."))
309     (when cleared-reanalyze
310       (setf (component-reanalyze component) t)))
311   (values))
312
313 (defparameter *constraint-propagate* t)
314
315 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
316 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
317 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
318 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
319 ;;;
320 ;;; FIXME: It would be good to document why it's important to have a
321 ;;; large value here, and what the drawbacks of an excessively large
322 ;;; value are; and it might also be good to make it depend on
323 ;;; optimization policy.
324 (defparameter *reoptimize-after-type-check-max* 10)
325
326 (defevent reoptimize-maxed-out
327   "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
328
329 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
330 (defun dfo-as-needed (component)
331   (declare (type component component))
332   (when (component-reanalyze component)
333     (maybe-mumble "DFO")
334     (loop
335       (find-dfo component)
336       (unless (component-reanalyze component)
337         (maybe-mumble " ")
338         (return))
339       (maybe-mumble ".")))
340   (values))
341
342 ;;; Do all the IR1 phases for a non-top-level component.
343 (defun ir1-phases (component)
344   (declare (type component component))
345   (let ((*constraint-number* 0)
346         (loop-count 1)
347         (*delayed-ir1-transforms* nil))
348     (declare (special *constraint-number* *delayed-ir1-transforms*))
349     (loop
350       (ir1-optimize-until-done component)
351       (when (or (component-new-functions component)
352                 (component-reanalyze-functions component))
353         (maybe-mumble "locall ")
354         (local-call-analyze component))
355       (dfo-as-needed component)
356       (when *constraint-propagate*
357         (maybe-mumble "constraint ")
358         (constraint-propagate component))
359       (when (retry-delayed-ir1-transforms :constraint)
360         (maybe-mumble "Rtran "))
361       ;; Delay the generation of type checks until the type
362       ;; constraints have had time to propagate, else the compiler can
363       ;; confuse itself.
364       (unless (and (or (component-reoptimize component)
365                        (component-reanalyze component)
366                        (component-new-functions component)
367                        (component-reanalyze-functions component))
368                    (< loop-count (- *reoptimize-after-type-check-max* 4)))
369         (maybe-mumble "type ")
370         (generate-type-checks component)
371         (unless (or (component-reoptimize component)
372                     (component-reanalyze component)
373                     (component-new-functions component)
374                     (component-reanalyze-functions component))
375           (return)))
376       (when (>= loop-count *reoptimize-after-type-check-max*)
377         (maybe-mumble "[reoptimize limit]")
378         (event reoptimize-maxed-out)
379         (return))
380       (incf loop-count)))
381
382   (ir1-finalize component)
383   (values))
384
385 (defun native-compile-component (component)
386   (/show "entering NATIVE-COMPILE-COMPONENT")
387   (let ((*code-segment* nil)
388         (*elsewhere* nil))
389     (maybe-mumble "GTN ")
390     (gtn-analyze component)
391     (maybe-mumble "LTN ")
392     (ltn-analyze component)
393     (dfo-as-needed component)
394     (maybe-mumble "control ")
395     (control-analyze component #'make-ir2-block)
396
397     (when (ir2-component-values-receivers (component-info component))
398       (maybe-mumble "stack ")
399       (stack-analyze component)
400       ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
401       ;; stack analysis. There shouldn't be any unreachable code after
402       ;; control, so this won't delete anything.
403       (dfo-as-needed component))
404
405     (unwind-protect
406         (progn
407           (maybe-mumble "IR2tran ")
408           (init-assembler)
409           (entry-analyze component)
410           (ir2-convert component)
411
412           (when (policy *lexenv* (>= speed compilation-speed))
413             (maybe-mumble "copy ")
414             (copy-propagate component))
415
416           (select-representations component)
417
418           (when *check-consistency*
419             (maybe-mumble "check2 ")
420             (check-ir2-consistency component))
421
422           (delete-unreferenced-tns component)
423
424           (maybe-mumble "life ")
425           (lifetime-analyze component)
426
427           (when *compile-progress*
428             (compiler-mumble "") ; Sync before doing more output.
429             (pre-pack-tn-stats component *error-output*))
430
431           (when *check-consistency*
432             (maybe-mumble "check-life ")
433             (check-life-consistency component))
434
435           (maybe-mumble "pack ")
436           (pack component)
437
438           (when *check-consistency*
439             (maybe-mumble "check-pack ")
440             (check-pack-consistency component))
441
442           (when *compiler-trace-output*
443             (describe-component component *compiler-trace-output*)
444             (describe-ir2-component component *compiler-trace-output*))
445
446           (maybe-mumble "code ")
447           (multiple-value-bind (code-length trace-table fixups)
448               (generate-code component)
449
450             (when *compiler-trace-output*
451               (format *compiler-trace-output*
452                       "~|~%disassembly of code for ~S~2%" component)
453               (sb!disassem:disassemble-assem-segment *code-segment*
454                                                      *compiler-trace-output*))
455
456             (etypecase *compile-object*
457               (fasl-output
458                (maybe-mumble "fasl")
459                (fasl-dump-component component
460                                     *code-segment*
461                                     code-length
462                                     trace-table
463                                     fixups
464                                     *compile-object*))
465               (core-object
466                (maybe-mumble "core")
467                (make-core-component component
468                                     *code-segment*
469                                     code-length
470                                     trace-table
471                                     fixups
472                                     *compile-object*))
473               (null))))))
474
475   ;; We're done, so don't bother keeping anything around.
476   (setf (component-info component) nil)
477
478   (/show "leaving NATIVE-COMPILE-COMPONENT")
479   (values))
480
481 (defun policy-byte-compile-p (thing)
482   nil
483   ;; FIXME: byte compiler to be removed completely
484   #|
485   (policy thing
486           (and (zerop speed)
487                (<= debug 1)))
488   |#)
489
490 ;;; Return our best guess for whether we will byte compile code
491 ;;; currently being IR1 converted. This is only a guess because the
492 ;;; decision is made on a per-component basis.
493 ;;;
494 ;;; FIXME: This should be called something more mnemonic, e.g.
495 ;;; PROBABLY-BYTE-COMPILING
496 (defun byte-compiling ()
497   nil
498   ;; FIXME: byte compiler to be removed completely
499   #|
500   (if (eq *byte-compiling* :maybe)
501       (or (eq *byte-compile* t)
502           (policy-byte-compile-p *lexenv*))
503       (and *byte-compile* *byte-compiling*))
504   |#)
505
506 ;;; Delete components with no external entry points before we try to
507 ;;; generate code. Unreachable closures can cause IR2 conversion to
508 ;;; puke on itself, since it is the reference to the closure which
509 ;;; normally causes the components to be combined.
510 (defun delete-if-no-entries (component)
511   (dolist (fun (component-lambdas component) (delete-component component))
512     (when (functional-has-external-references-p fun)
513       (return))
514     (case (functional-kind fun)
515       (:top-level (return))
516       (:external
517        (unless (every (lambda (ref)
518                         (eq (block-component (node-block ref))
519                             component))
520                       (leaf-refs fun))
521          (return))))))
522
523 (defun byte-compile-this-component-p (component)
524   nil
525   ;; FIXME: byte compiler to be removed completely
526   #|
527   (ecase *byte-compile*
528     ((t) t)
529     ((nil) nil)
530     ((:maybe)
531      (every #'policy-byte-compile-p (component-lambdas component))))
532   |#)
533
534 (defun compile-component (component)
535   (let* ((*component-being-compiled* component)
536          (*byte-compiling* (byte-compile-this-component-p component)))
537     (when sb!xc:*compile-print*
538       (compiler-mumble "~&; ~:[~;byte ~]compiling ~A: "
539                        *byte-compiling*
540                        (component-name component)))
541
542     (ir1-phases component)
543
544     ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
545     (maybe-mumble "env ")
546     (environment-analyze component)
547     (dfo-as-needed component)
548
549     (delete-if-no-entries component)
550
551     (unless (eq (block-next (component-head component))
552                 (component-tail component))
553       (if *byte-compiling*
554           (byte-compile-component component)
555           (native-compile-component component))))
556
557   (clear-constant-info)
558
559   (when sb!xc:*compile-print*
560     (compiler-mumble "~&"))
561
562   (values))
563 \f
564 ;;;; clearing global data structures
565 ;;;;
566 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
567 ;;;; global data structures entirely when possible and consing up the
568 ;;;; others from scratch instead of clearing and reusing them?
569
570 ;;; Clear the INFO in constants in the *FREE-VARIABLES*, etc. In
571 ;;; addition to allowing stuff to be reclaimed, this is required for
572 ;;; correct assignment of constant offsets, since we need to assign a
573 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
574 ;;; slots, since they are used to keep track of functions across
575 ;;; component boundaries.
576 (defun clear-constant-info ()
577   (maphash #'(lambda (k v)
578                (declare (ignore k))
579                (setf (leaf-info v) nil))
580            *constants*)
581   (maphash #'(lambda (k v)
582                (declare (ignore k))
583                (when (constant-p v)
584                  (setf (leaf-info v) nil)))
585            *free-variables*)
586   (values))
587
588 ;;; Blow away the REFS for all global variables, and let COMPONENT
589 ;;; be recycled.
590 (defun clear-ir1-info (component)
591   (declare (type component component))
592   (labels ((blast (x)
593              (maphash #'(lambda (k v)
594                           (declare (ignore k))
595                           (when (leaf-p v)
596                             (setf (leaf-refs v)
597                                   (delete-if #'here-p (leaf-refs v)))
598                             (when (basic-var-p v)
599                               (setf (basic-var-sets v)
600                                     (delete-if #'here-p (basic-var-sets v))))))
601                       x))
602            (here-p (x)
603              (eq (block-component (node-block x)) component)))
604     (blast *free-variables*)
605     (blast *free-functions*)
606     (blast *constants*))
607   (values))
608
609 ;;; Clear global variables used by the compiler.
610 ;;;
611 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
612 ;;; and it adds overhead even when people aren't using the compiler.
613 ;;; Perhaps we could make these global vars unbound except when
614 ;;; actually in use, so that this function could go away.
615 (defun clear-stuff (&optional (debug-too t))
616
617   ;; Clear global tables.
618   (when (boundp '*free-functions*)
619     (clrhash *free-functions*)
620     (clrhash *free-variables*)
621     (clrhash *constants*))
622
623   ;; Clear debug counters and tables.
624   (clrhash *seen-blocks*)
625   (clrhash *seen-functions*)
626   (clrhash *list-conflicts-table*)
627
628   (when debug-too
629     (clrhash *continuation-numbers*)
630     (clrhash *number-continuations*)
631     (setq *continuation-number* 0)
632     (clrhash *tn-ids*)
633     (clrhash *id-tns*)
634     (setq *tn-id* 0)
635     (clrhash *label-ids*)
636     (clrhash *id-labels*)
637     (setq *label-id* 0)
638
639     ;; Clear some PACK data structures (for GC purposes only).
640     (aver (not *in-pack*))
641     (dolist (sb *backend-sb-list*)
642       (when (finite-sb-p sb)
643         (fill (finite-sb-live-tns sb) nil))))
644
645   ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
646   ;; Superficially, this seemed harmful -- the user could reasonably be
647   ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
648   ;; compiled. A closer inspection showed that this actually turned out to be
649   ;; harmless in practice, because CLEAR-STUFF was only called from within
650   ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
651   ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
652   ;; practice, it was also useless in practice. So we don't do it any more.)
653
654   (values))
655 \f
656 ;;;; trace output
657
658 ;;; Print out some useful info about Component to Stream.
659 (defun describe-component (component *standard-output*)
660   (declare (type component component))
661   (format t "~|~%;;;; component: ~S~2%" (component-name component))
662   (print-blocks component)
663   (values))
664
665 (defun describe-ir2-component (component *standard-output*)
666   (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
667   (format t "entries:~%")
668   (dolist (entry (ir2-component-entries (component-info component)))
669     (format t "~4TL~D: ~S~:[~; [closure]~]~%"
670             (label-id (entry-info-offset entry))
671             (entry-info-name entry)
672             (entry-info-closure-p entry)))
673   (terpri)
674   (pre-pack-tn-stats component *standard-output*)
675   (terpri)
676   (print-ir2-blocks component)
677   (terpri)
678   (values))
679 \f
680 ;;;; file reading
681 ;;;;
682 ;;;; When reading from a file, we have to keep track of some source
683 ;;;; information. We also exploit our ability to back up for printing
684 ;;;; the error context and for recovering from errors.
685 ;;;;
686 ;;;; The interface we provide to this stuff is the stream-oid
687 ;;;; Source-Info structure. The bookkeeping is done as a side-effect
688 ;;;; of getting the next source form.
689
690 ;;; A FILE-INFO structure holds all the source information for a
691 ;;; given file.
692 (defstruct (file-info (:copier nil))
693   ;; If a file, the truename of the corresponding source file. If from
694   ;; a Lisp form, :LISP. If from a stream, :STREAM.
695   (name (required-argument) :type (or pathname (member :lisp :stream)))
696   ;; the defaulted, but not necessarily absolute file name (i.e. prior
697   ;; to TRUENAME call.) Null if not a file. This is used to set
698   ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
699   ;; debug-info.
700   (untruename nil :type (or pathname null))
701   ;; the file's write date (if relevant)
702   (write-date nil :type (or unsigned-byte null))
703   ;; the source path root number of the first form in this file (i.e.
704   ;; the total number of forms converted previously in this
705   ;; compilation)
706   (source-root 0 :type unsigned-byte)
707   ;; parallel vectors containing the forms read out of the file and
708   ;; the file positions that reading of each form started at (i.e. the
709   ;; end of the previous form)
710   (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
711   (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
712
713 ;;; The SOURCE-INFO structure provides a handle on all the source
714 ;;; information for an entire compilation.
715 (defstruct (source-info
716             #-no-ansi-print-object
717             (:print-object (lambda (s stream)
718                              (print-unreadable-object (s stream :type t))))
719             (:copier nil))
720   ;; the UT that compilation started at
721   (start-time (get-universal-time) :type unsigned-byte)
722   ;; the FILE-INFO structure for this compilation
723   (file-info nil :type (or file-info null))
724   ;; the stream that we are using to read the FILE-INFO, or NIL if
725   ;; no stream has been opened yet
726   (stream nil :type (or stream null)))
727
728 ;;; Given a pathname, return a SOURCE-INFO structure.
729 (defun make-file-source-info (file)
730   (let ((file-info (make-file-info :name (truename file)
731                                    :untruename file
732                                    :write-date (file-write-date file))))
733
734     (make-source-info :file-info file-info)))
735
736 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM. 
737 (defun make-lisp-source-info (form)
738   (make-source-info :start-time (get-universal-time)
739                     :file-info (make-file-info :name :lisp
740                                                :forms (vector form)
741                                                :positions '#(0))))
742
743 ;;; Return a SOURCE-INFO which will read from STREAM.
744 (defun make-stream-source-info (stream)
745   (let ((file-info (make-file-info :name :stream)))
746     (make-source-info :file-info file-info
747                       :stream stream)))
748
749 ;;; Return a form read from STREAM; or for EOF use the trick,
750 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
751 ;;; error happens, then convert it to standard abort-the-compilation
752 ;;; error condition (possibly recording some extra location
753 ;;; information).
754 (defun read-for-compile-file (stream position)
755   (handler-case (read stream nil stream)
756     (reader-error (condition)
757      (error 'input-error-in-compile-file
758             :error condition
759             ;; We don't need to supply :POSITION here because
760             ;; READER-ERRORs already know their position in the file.
761             ))
762     ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
763     ;; (and that this is not a READER-ERROR) when it encounters end of
764     ;; file in the middle of something it's trying to read.
765     (end-of-file (condition)
766      (error 'input-error-in-compile-file
767             :error condition
768             ;; We need to supply :POSITION here because the END-OF-FILE
769             ;; condition doesn't carry the position that the user
770             ;; probably cares about, where the failed READ began.
771             :position position))))
772
773 ;;; If STREAM is present, return it, otherwise open a stream to the
774 ;;; current file. There must be a current file.
775 ;;;
776 ;;; FIXME: This is probably an unnecessarily roundabout way to do
777 ;;; things now that we process a single file in COMPILE-FILE (unlike
778 ;;; the old CMU CL code, which accepted multiple files). Also, the old
779 ;;; comment said
780 ;;;   When we open a new file, we also reset *PACKAGE* and policy.
781 ;;;   This gives the effect of rebinding around each file.
782 ;;; which doesn't seem to be true now. Check to make sure that if
783 ;;; such rebinding is necessary, it's still done somewhere.
784 (defun get-source-stream (info)
785   (declare (type source-info info))
786   (or (source-info-stream info)
787       (let* ((file-info (source-info-file-info info))
788              (name (file-info-name file-info)))
789         (setf sb!xc:*compile-file-truename* name
790               sb!xc:*compile-file-pathname* (file-info-untruename file-info)
791               (source-info-stream info) (open name :direction :input)))))
792
793 ;;; Close the stream in INFO if it is open.
794 (defun close-source-info (info)
795   (declare (type source-info info))
796   (let ((stream (source-info-stream info)))
797     (when stream (close stream)))
798   (setf (source-info-stream info) nil)
799   (values))
800
801 ;;; Read and compile the source file.
802 (defun sub-sub-compile-file (info)
803   (let* ((file-info (source-info-file-info info))
804          (stream (get-source-stream info)))
805     (loop
806      (let* ((pos (file-position stream))
807             (form (read-for-compile-file stream pos)))
808        (if (eq form stream) ; i.e., if EOF
809            (return)
810            (let* ((forms (file-info-forms file-info))
811                   (current-idx (+ (fill-pointer forms)
812                                   (file-info-source-root file-info))))
813              (vector-push-extend form forms)
814              (vector-push-extend pos (file-info-positions file-info))
815              (find-source-paths form current-idx)
816              (process-top-level-form form
817                                      `(original-source-start 0 ,current-idx)
818                                      nil)))))))
819
820 ;;; Return the INDEX'th source form read from INFO and the position
821 ;;; where it was read.
822 (defun find-source-root (index info)
823   (declare (type index index) (type source-info info))
824   (let ((file-info (source-info-file-info info)))
825     (values (aref (file-info-forms file-info) index)
826             (aref (file-info-positions file-info) index))))
827 \f
828 ;;;; top-level form processing
829
830 ;;; This is called by top-level form processing when we are ready to
831 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
832 ;;; convert the form, but delay compilation, pushing the result on
833 ;;; *TOP-LEVEL-LAMBDAS* instead.
834 (defun convert-and-maybe-compile (form path)
835   (declare (list path))
836   (let* ((*lexenv* (make-lexenv :policy *policy*))
837          (tll (ir1-top-level form path nil)))
838     (cond ((eq *block-compile* t) (push tll *top-level-lambdas*))
839           (t (compile-top-level (list tll) nil)))))
840
841 ;;; Macroexpand FORM in the current environment with an error handler.
842 ;;; We only expand one level, so that we retain all the intervening
843 ;;; forms in the source path.
844 (defun preprocessor-macroexpand (form)
845   (handler-case (sb!xc:macroexpand-1 form *lexenv*)
846     (error (condition)
847        (compiler-error "(during macroexpansion)~%~A" condition))))
848
849 ;;; Process a PROGN-like portion of a top-level form. FORMS is a list of
850 ;;; the forms, and PATH is the source path of the FORM they came out of.
851 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
852 (defun process-top-level-progn (forms path compile-time-too)
853   (declare (list forms) (list path))
854   (dolist (form forms)
855     (process-top-level-form form path compile-time-too)))
856
857 ;;; Process a top-level use of LOCALLY, or anything else (e.g.
858 ;;; MACROLET) at top-level which has declarations and ordinary forms.
859 ;;; We parse declarations and then recursively process the body.
860 (defun process-top-level-locally (body path compile-time-too)
861   (declare (list path))
862   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
863     (let* ((*lexenv*
864             (process-decls decls nil nil (make-continuation)))
865            ;; Binding *POLICY* is pretty much of a hack, since it
866            ;; causes LOCALLY to "capture" enclosed proclamations. It
867            ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
868            ;; value of *POLICY* as the policy. The need for this hack
869            ;; is due to the quirk that there is no way to represent in
870            ;; a POLICY that an optimize quality came from the default.
871            ;;
872            ;; FIXME: Ideally, something should be done so that DECLAIM
873            ;; inside LOCALLY works OK. Failing that, at least we could
874            ;; issue a warning instead of silently screwing up.
875            (*policy* (lexenv-policy *lexenv*)))
876       (process-top-level-progn forms path compile-time-too))))
877
878 ;;; Parse an EVAL-WHEN situations list, returning three flags,
879 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
880 ;;; the types of situations present in the list.
881 (defun parse-eval-when-situations (situations)
882   (when (or (not (listp situations))
883             (set-difference situations
884                             '(:compile-toplevel
885                               compile
886                               :load-toplevel
887                               load
888                               :execute
889                               eval)))
890     (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
891   (let ((deprecated-names (intersection situations '(compile load eval))))
892     (when deprecated-names
893       (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
894                   deprecated-names)))
895   (values (intersection '(:compile-toplevel compile)
896                         situations)
897           (intersection '(:load-toplevel load) situations)
898           (intersection '(:execute eval) situations)))
899
900
901 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
902 (defun clambda-component (clambda)
903   (block-component (node-block (lambda-bind clambda))))
904 (defun functional-components (f)
905   (declare (type functional f))
906   (etypecase f
907     (clambda (list (clambda-component f)))
908     (optional-dispatch (let ((result nil))
909                          (labels ((frob (clambda)
910                                     (pushnew (clambda-component clambda)
911                                              result))
912                                   (maybe-frob (maybe-clambda)
913                                     (when maybe-clambda
914                                       (frob maybe-clambda))))
915                            (mapc #'frob (optional-dispatch-entry-points f))
916                            (maybe-frob (optional-dispatch-more-entry f))
917                            (maybe-frob (optional-dispatch-main-entry f)))))))
918
919 (defun make-functional-from-top-level-lambda (definition
920                                               &key
921                                               name
922                                               (path
923                                                ;; I'd thought NIL should
924                                                ;; work, but it doesn't.
925                                                ;; -- WHN 2001-09-20
926                                                (required-argument)))
927   (let* ((*current-path* path)
928          (component (make-empty-component))
929          (*current-component* component))
930     (setf (component-name component)
931           (format nil "~S initial component" name))
932     (setf (component-kind component) :initial)
933     (let* ((locall-fun (ir1-convert-lambda definition
934                                            (format nil "locall ~S" name)))
935            (fun (ir1-convert-lambda (make-xep-lambda locall-fun) name)))
936       (setf (functional-entry-function fun) locall-fun
937             (functional-kind fun) :external
938             (functional-has-external-references-p fun) t)
939       fun)))
940
941 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
942 ;;; description of the result.
943 ;;;   * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
944 ;;;     into core and return the compiled FUNCTION value.
945 ;;;   * If *COMPILE-OBJECT* is a fasl file, then write the function
946 ;;;     into the fasl file and return a dump handle.
947 ;;;
948 ;;; If NAME is provided, then we try to use it as the name of the
949 ;;; function for debugging/diagnostic information.
950 (defun %compile (lambda-expression
951                  *compile-object*
952                  &key
953                  name
954                  (path
955                   ;; This magical idiom seems to be the appropriate
956                   ;; path for compiling standalone LAMBDAs, judging
957                   ;; from the CMU CL code and experiment, so it's a
958                   ;; nice default for things where we don't have a
959                   ;; real source path (as in e.g. inside CL:COMPILE).
960                   '(original-source-start 0 0)))
961   (/show "entering %COMPILE" name)
962   (unless (or (null name) (legal-function-name-p name))
963     (error "not a legal function name: ~S" name))
964   (let* ((*lexenv* (make-lexenv :policy *policy*))
965          (fun (make-functional-from-top-level-lambda lambda-expression
966                                                      :name name
967                                                      :path path)))
968
969     (/noshow fun)
970
971     ;; FIXME: The compile-it code from here on is sort of a
972     ;; twisted version of the code in COMPILE-TOP-LEVEL. It'd be
973     ;; better to find a way to share the code there; or
974     ;; alternatively, to use this code to replace the code there.
975     ;; (The second alternative might be pretty easy if we used
976     ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
977     ;; whole FUNCTIONAL-KIND=:TOP-LEVEL case could go away..)
978
979     (/show "about to LOCAL-CALL-ANALYZE-UNTIL-DONE")
980     (local-call-analyze-until-done (list fun))
981
982     (multiple-value-bind (components-from-dfo top-components hairy-top)
983         (find-initial-dfo (list fun))
984
985       (let ((*all-components* (append components-from-dfo top-components)))
986         (/noshow components-from-dfo top-components *all-components*)
987         (mapc #'preallocate-environments-for-top-levelish-lambdas
988               (append hairy-top top-components))
989         (dolist (component-from-dfo components-from-dfo)
990           (/show "compiling a COMPONENT-FROM-DFO")
991           (compile-component component-from-dfo)
992           (/show "about to REPLACE-TOP-LEVEL-XEPS")
993           (replace-top-level-xeps component-from-dfo)))
994
995       (/show "about to go into PROG1")
996       (prog1
997           (let ((entry-table (etypecase *compile-object*
998                                (fasl-output (fasl-output-entry-table
999                                              *compile-object*))
1000                                (core-object (core-object-entry-table
1001                                              *compile-object*)))))
1002             (multiple-value-bind (result found-p)
1003                 (gethash (leaf-info fun) entry-table)
1004               (aver found-p)
1005               result))
1006         (mapc #'clear-ir1-info components-from-dfo)
1007         (clear-stuff)
1008         (/show "returning from %COMPILE")))))
1009
1010 (defun process-top-level-cold-fset (name lambda-expression path)
1011   (/show "entering PROCESS-TOP-LEVEL-COLD-FSET" name)
1012   (unless (producing-fasl-file)
1013     (error "can't COLD-FSET except in a fasl file"))
1014   (unless (legal-function-name-p name)
1015     (error "not a legal function name: ~S" name))
1016   (fasl-dump-cold-fset name
1017                        (%compile lambda-expression
1018                                  *compile-object*
1019                                  :name name
1020                                  :path path)
1021                        *compile-object*)
1022   (/show "finished with PROCESS-TOP-LEVEL-COLD-FSET" name)
1023   (values))
1024
1025 ;;; Process a top-level FORM with the specified source PATH.
1026 ;;;  * If this is a magic top-level form, then do stuff.
1027 ;;;  * If this is a macro, then expand it.
1028 ;;;  * Otherwise, just compile it.
1029 ;;;
1030 ;;; COMPILE-TIME-TOO is as defined in ANSI
1031 ;;; "3.2.3.1 Processing of Top Level Forms".
1032 (defun process-top-level-form (form path compile-time-too)
1033
1034   (declare (list path))
1035
1036   (catch 'process-top-level-form-error-abort
1037     (let* ((path (or (gethash form *source-paths*) (cons form path)))
1038            (*compiler-error-bailout*
1039             (lambda ()
1040               (convert-and-maybe-compile
1041                `(error "execution of a form compiled with errors:~% ~S"
1042                        ',form)
1043                path)
1044               (throw 'process-top-level-form-error-abort nil))))
1045
1046       (if (atom form)
1047           ;; (There are no EVAL-WHEN issues in the ATOM case until
1048           ;; SBCL gets smart enough to handle global
1049           ;; DEFINE-SYMBOL-MACRO.)
1050           (convert-and-maybe-compile form path)
1051           (flet ((need-at-least-one-arg (form)
1052                    (unless (cdr form)
1053                      (compiler-error "~S form is too short: ~S"
1054                                      (car form)
1055                                      form))))
1056             (case (car form)
1057               ;; In the cross-compiler, top level COLD-FSET arranges
1058               ;; for static linking at cold init time.
1059               #+sb-xc-host
1060               ((cold-fset)
1061                (aver (not compile-time-too))
1062                (destructuring-bind (cold-fset fun-name lambda-expression) form
1063                  (declare (ignore cold-fset))
1064                  (process-top-level-cold-fset fun-name
1065                                               lambda-expression
1066                                               path)))
1067               ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1068                (need-at-least-one-arg form)
1069                (destructuring-bind (special-operator magic &rest body) form
1070                  (ecase special-operator
1071                    ((eval-when)
1072                     ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1073                     ;; "3.2.3.1 Processing of Top Level Forms".
1074                     (multiple-value-bind (ct lt e)
1075                         (parse-eval-when-situations magic)
1076                       (let ((new-compile-time-too (or ct
1077                                                       (and compile-time-too
1078                                                            e))))
1079                         (cond (lt (process-top-level-progn
1080                                    body path new-compile-time-too))
1081                               (new-compile-time-too (eval
1082                                                      `(progn ,@body)))))))
1083                    ((macrolet)
1084                     (funcall-in-macrolet-lexenv
1085                      magic
1086                      (lambda ()
1087                        (process-top-level-locally body
1088                                                   path
1089                                                   compile-time-too))))
1090                    ((symbol-macrolet)
1091                     (funcall-in-symbol-macrolet-lexenv
1092                      magic
1093                      (lambda ()
1094                        (process-top-level-locally body
1095                                                   path
1096                                                   compile-time-too)))))))
1097               ((locally)
1098                (process-top-level-locally (rest form) path compile-time-too))
1099               ((progn)
1100                (process-top-level-progn (rest form) path compile-time-too))
1101               ;; When we're cross-compiling, consider: what should we
1102               ;; do when we hit e.g.
1103               ;;   (EVAL-WHEN (:COMPILE-TOPLEVEL)
1104               ;;     (DEFUN FOO (X) (+ 7 X)))?
1105               ;; DEFUN has a macro definition in the cross-compiler,
1106               ;; and a different macro definition in the target
1107               ;; compiler. The only sensible thing is to use the
1108               ;; target compiler's macro definition, since the
1109               ;; cross-compiler's macro is in general into target
1110               ;; functions which can't meaningfully be executed at
1111               ;; cross-compilation time. So make sure we do the EVAL
1112               ;; here, before we macroexpand.
1113               ;;
1114               ;; Then things get even dicier with something like
1115               ;;   (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1116               ;; where we have to make sure that we don't uncross
1117               ;; the SB!XC: prefix before we do EVAL, because otherwise
1118               ;; we'd be trying to redefine the cross-compilation host's
1119               ;; constants.
1120               ;;
1121               ;; (Isn't it fun to cross-compile Common Lisp?:-)
1122               #+sb-xc-host
1123               (t
1124                (when compile-time-too
1125                  (eval form)) ; letting xc host EVAL do its own macroexpansion
1126                (let* (;; (We uncross the operator name because things
1127                       ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1128                       ;; should be equivalent to their CL: counterparts
1129                       ;; when being compiled as target code. We leave
1130                       ;; the rest of the form uncrossed because macros
1131                       ;; might yet expand into EVAL-WHEN stuff, and
1132                       ;; things inside EVAL-WHEN can't be uncrossed
1133                       ;; until after we've EVALed them in the
1134                       ;; cross-compilation host.)
1135                       (slightly-uncrossed (cons (uncross (first form))
1136                                                 (rest form)))
1137                       (expanded (preprocessor-macroexpand slightly-uncrossed)))
1138                  (if (eq expanded slightly-uncrossed)
1139                      ;; (Now that we're no longer processing toplevel
1140                      ;; forms, and hence no longer need to worry about
1141                      ;; EVAL-WHEN, we can uncross everything.)
1142                      (convert-and-maybe-compile expanded path)
1143                      ;; (We have to demote COMPILE-TIME-TOO to NIL
1144                      ;; here, no matter what it was before, since
1145                      ;; otherwise we'd tend to EVAL subforms more than
1146                      ;; once, because of WHEN COMPILE-TIME-TOO form
1147                      ;; above.)
1148                      (process-top-level-form expanded path nil))))
1149               ;; When we're not cross-compiling, we only need to
1150               ;; macroexpand once, so we can follow the 1-thru-6
1151               ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1152               ;; Top Level Forms".
1153               #-sb-xc-host
1154               (t
1155                (let ((expanded (preprocessor-macroexpand form)))
1156                  (cond ((eq expanded form)
1157                         (when compile-time-too
1158                           (eval form))
1159                         (convert-and-maybe-compile form path))
1160                        (t
1161                         (process-top-level-form expanded
1162                                                 path
1163                                                 compile-time-too))))))))))
1164
1165   (values))
1166 \f
1167 ;;;; load time value support
1168 ;;;;
1169 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1170
1171 ;;; Return T if we are currently producing a fasl file and hence
1172 ;;; constants need to be dumped carefully.
1173 (defun producing-fasl-file ()
1174   (fasl-output-p *compile-object*))
1175
1176 ;;; Compile FORM and arrange for it to be called at load-time. Return
1177 ;;; the dumper handle and our best guess at the type of the object.
1178 (defun compile-load-time-value
1179        (form &optional
1180              (name (let ((*print-level* 2) (*print-length* 3))
1181                      (format nil "load time value of ~S"
1182                              (if (and (listp form)
1183                                       (eq (car form) 'make-value-cell))
1184                                  (second form)
1185                                  form)))))
1186   (let ((lambda (compile-load-time-stuff form name t)))
1187     (values
1188      (fasl-dump-load-time-value-lambda lambda *compile-object*)
1189      (let ((type (leaf-type lambda)))
1190        (if (function-type-p type)
1191            (single-value-type (function-type-returns type))
1192            *wild-type*)))))
1193
1194 ;;; Compile the FORMS and arrange for them to be called (for effect,
1195 ;;; not value) at load time.
1196 (defun compile-make-load-form-init-forms (forms name)
1197   (let ((lambda (compile-load-time-stuff `(progn ,@forms) name nil)))
1198     (fasl-dump-top-level-lambda-call lambda *compile-object*)))
1199
1200 ;;; Does the actual work of COMPILE-LOAD-TIME-VALUE or
1201 ;;; COMPILE-MAKE-LOAD-FORM- INIT-FORMS.
1202 (defun compile-load-time-stuff (form name for-value)
1203   (with-ir1-namespace
1204    (let* ((*lexenv* (make-null-lexenv))
1205           (lambda (ir1-top-level form *current-path* for-value)))
1206      (setf (leaf-name lambda) name)
1207      (compile-top-level (list lambda) t)
1208      lambda)))
1209
1210 ;;; This is called by COMPILE-TOP-LEVEL when it was passed T for
1211 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1212 ;;; don't try to combine this component with anything else and frob
1213 ;;; the name. If not in a :TOP-LEVEL component, then don't bother
1214 ;;; compiling, because it was merged with a run-time component.
1215 (defun compile-load-time-value-lambda (lambdas)
1216   (aver (null (cdr lambdas)))
1217   (let* ((lambda (car lambdas))
1218          (component (block-component (node-block (lambda-bind lambda)))))
1219     (when (eql (component-kind component) :top-level)
1220       (setf (component-name component) (leaf-name lambda))
1221       (compile-component component)
1222       (clear-ir1-info component))))
1223 \f
1224 ;;;; COMPILE-FILE
1225
1226 ;;; We build a list of top-level lambdas, and then periodically smash
1227 ;;; them together into a single component and compile it.
1228 (defvar *pending-top-level-lambdas*)
1229
1230 ;;; The maximum number of top-level lambdas we put in a single
1231 ;;; top-level component.
1232 ;;;
1233 ;;; CMU CL 18b used this nontrivially by default (setting it to 10)
1234 ;;; but consequently suffered from the inability to execute some
1235 ;;; troublesome constructs correctly, e.g. inability to load a fasl
1236 ;;; file compiled from the source file
1237 ;;;   (defpackage "FOO" (:use "CL"))
1238 ;;;   (print 'foo::bar)
1239 ;;; because it would dump data-setup fops (including a FOP-PACKAGE for
1240 ;;; "FOO") for the second form before dumping the the code in the
1241 ;;; first form, or the fop to execute the code in the first form. By
1242 ;;; setting this value to 0 by default, we avoid this badness. This
1243 ;;; increases the number of toplevel form functions, and so increases
1244 ;;; the size of object files.
1245 ;;;
1246 ;;; The variable is still supported because when we are compiling the
1247 ;;; SBCL system itself, which is known not contain any troublesome
1248 ;;; constructs, we can set it to a nonzero value, which reduces the
1249 ;;; number of toplevel form objects, reducing the peak memory usage in
1250 ;;; GENESIS, which is desirable, since at least for SBCL version
1251 ;;; 0.6.7, this is the high water mark for memory usage during system
1252 ;;; construction.
1253 (defparameter *top-level-lambda-max* 0)
1254
1255 (defun object-call-top-level-lambda (tll)
1256   (declare (type functional tll))
1257   (let ((object *compile-object*))
1258     (etypecase object
1259       (fasl-output
1260        (fasl-dump-top-level-lambda-call tll object))
1261       (core-object
1262        (core-call-top-level-lambda tll object))
1263       (null))))
1264
1265 ;;; Add LAMBDAS to the pending lambdas. If this leaves more than
1266 ;;; *TOP-LEVEL-LAMBDA-MAX* lambdas in the list, or if FORCE-P is true,
1267 ;;; then smash the lambdas into a single component, compile it, and
1268 ;;; call the resulting function.
1269 (defun sub-compile-top-level-lambdas (lambdas force-p)
1270   (declare (list lambdas))
1271   (setq *pending-top-level-lambdas*
1272         (append *pending-top-level-lambdas* lambdas))
1273   (let ((pending *pending-top-level-lambdas*))
1274     (when (and pending
1275                (or (> (length pending) *top-level-lambda-max*)
1276                    force-p))
1277       (multiple-value-bind (component tll) (merge-top-level-lambdas pending)
1278         (setq *pending-top-level-lambdas* ())
1279         (let ((*byte-compile* (if (eq *byte-compile* :maybe)
1280                                   *byte-compile-top-level*
1281                                   *byte-compile*)))
1282           (compile-component component))
1283         (clear-ir1-info component)
1284         (object-call-top-level-lambda tll))))
1285   (values))
1286
1287 ;;; Compile top-level code and call the top-level lambdas. We pick off
1288 ;;; top-level lambdas in non-top-level components here, calling
1289 ;;; SUB-c-t-l-l on each subsequence of normal top-level lambdas.
1290 (defun compile-top-level-lambdas (lambdas force-p)
1291   (declare (list lambdas))
1292   (let ((len (length lambdas)))
1293     (flet ((loser (start)
1294              (or (position-if (lambda (x)
1295                                 (not (eq (component-kind
1296                                           (block-component
1297                                            (node-block
1298                                             (lambda-bind x))))
1299                                          :top-level)))
1300                               lambdas
1301                               :start start)
1302                  len)))
1303       (do* ((start 0 (1+ loser))
1304             (loser (loser start) (loser start)))
1305            ((>= start len)
1306             (when force-p
1307               (sub-compile-top-level-lambdas nil t)))
1308         (sub-compile-top-level-lambdas (subseq lambdas start loser)
1309                                        (or force-p (/= loser len)))
1310         (unless (= loser len)
1311           (object-call-top-level-lambda (elt lambdas loser))))))
1312   (values))
1313
1314 ;;; Compile LAMBDAS (a list of CLAMBDAs for top-level forms) into the
1315 ;;; object file. 
1316 ;;;
1317 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1318 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1319 (defun compile-top-level (lambdas load-time-value-p)
1320   (declare (list lambdas))
1321
1322   (maybe-mumble "locall ")
1323   (local-call-analyze-until-done lambdas)
1324
1325   (maybe-mumble "IDFO ")
1326   (multiple-value-bind (components top-components hairy-top)
1327       (find-initial-dfo lambdas)
1328     (let ((*all-components* (append components top-components))
1329           (top-level-closure nil))
1330       (when *check-consistency*
1331         (maybe-mumble "[check]~%")
1332         (check-ir1-consistency *all-components*))
1333
1334       (dolist (component (append hairy-top top-components))
1335         (when (pre-environment-analyze-top-level component)
1336           (setq top-level-closure t)))
1337
1338       (let ((*byte-compile*
1339              (if (and top-level-closure (eq *byte-compile* :maybe))
1340                  nil
1341                  *byte-compile*)))
1342         (dolist (component components)
1343           (compile-component component)
1344           (when (replace-top-level-xeps component)
1345             (setq top-level-closure t)))
1346         
1347         (when *check-consistency*
1348           (maybe-mumble "[check]~%")
1349           (check-ir1-consistency *all-components*))
1350         
1351         (if load-time-value-p
1352             (compile-load-time-value-lambda lambdas)
1353             (compile-top-level-lambdas lambdas top-level-closure)))
1354
1355       (mapc #'clear-ir1-info components)
1356       (clear-stuff)))
1357   (values))
1358
1359 ;;; Actually compile any stuff that has been queued up for block
1360 ;;; compilation.
1361 (defun finish-block-compilation ()
1362   (when *block-compile*
1363     (when *top-level-lambdas*
1364       (compile-top-level (nreverse *top-level-lambdas*) nil)
1365       (setq *top-level-lambdas* ()))
1366     (setq *block-compile* nil)
1367     (setq *entry-points* nil)))
1368
1369 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1370 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1371 (defun sub-compile-file (info)
1372   (declare (type source-info info))
1373   (let* ((*block-compile* *block-compile-argument*)
1374          (*package* (sane-package))
1375          (*policy* *policy*)
1376          (*lexenv* (make-null-lexenv))
1377          (*source-info* info)
1378          (sb!xc:*compile-file-pathname* nil)
1379          (sb!xc:*compile-file-truename* nil)
1380          (*top-level-lambdas* ())
1381          (*pending-top-level-lambdas* ())
1382          (*compiler-error-bailout*
1383           (lambda ()
1384             (compiler-mumble "~2&; fatal error, aborting compilation~%")
1385             (return-from sub-compile-file (values nil t t))))
1386          (*current-path* nil)
1387          (*last-source-context* nil)
1388          (*last-original-source* nil)
1389          (*last-source-form* nil)
1390          (*last-format-string* nil)
1391          (*last-format-args* nil)
1392          (*last-message-count* 0)
1393          ;; FIXME: Do we need this rebinding here? It's a literal
1394          ;; translation of the old CMU CL rebinding to
1395          ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1396          ;; and it's not obvious whether the rebinding to itself is
1397          ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1398          (*info-environment* *info-environment*)
1399          (*gensym-counter* 0))
1400     (handler-case
1401         (with-compilation-values
1402          (sb!xc:with-compilation-unit ()
1403            (clear-stuff)
1404
1405            (sub-sub-compile-file info)
1406
1407            (finish-block-compilation)
1408            (compile-top-level-lambdas () t)
1409            (let ((object *compile-object*))
1410              (etypecase object
1411                (fasl-output (fasl-dump-source-info info object))
1412                (core-object (fix-core-source-info info object))
1413                (null)))
1414            nil))
1415       ;; Some errors are sufficiently bewildering that we just fail
1416       ;; immediately, without trying to recover and compile more of
1417       ;; the input file.
1418       (input-error-in-compile-file (condition)
1419        (format *error-output*
1420                "~@<compilation aborted because of input error: ~2I~_~A~:>"
1421                condition)
1422        (values nil t t)))))
1423
1424 ;;; Return a pathname for the named file. The file must exist.
1425 (defun verify-source-file (pathname-designator)
1426   (let* ((pathname (pathname pathname-designator))
1427          (default-host (make-pathname :host (pathname-host pathname))))
1428     (flet ((try-with-type (path type error-p)
1429              (let ((new (merge-pathnames
1430                          path (make-pathname :type type
1431                                              :defaults default-host))))
1432                (if (probe-file new)
1433                    new
1434                    (and error-p (truename new))))))
1435       (cond ((typep pathname 'logical-pathname)
1436              (try-with-type pathname "LISP" t))
1437             ((probe-file pathname) pathname)
1438             ((try-with-type pathname "lisp"  nil))
1439             ((try-with-type pathname "lisp"  t))))))
1440
1441 (defun elapsed-time-to-string (tsec)
1442   (multiple-value-bind (tmin sec) (truncate tsec 60)
1443     (multiple-value-bind (thr min) (truncate tmin 60)
1444       (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1445
1446 ;;; Print some junk at the beginning and end of compilation.
1447 (defun start-error-output (source-info)
1448   (declare (type source-info source-info))
1449   (let ((file-info (source-info-file-info source-info)))
1450     (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1451                      (namestring (file-info-name file-info))
1452                      (sb!int:format-universal-time nil
1453                                                    (file-info-write-date
1454                                                     file-info)
1455                                                    :style :government
1456                                                    :print-weekday nil
1457                                                    :print-timezone nil)))
1458   (values))
1459 (defun finish-error-output (source-info won)
1460   (declare (type source-info source-info))
1461   (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1462                    won
1463                    (elapsed-time-to-string
1464                     (- (get-universal-time)
1465                        (source-info-start-time source-info))))
1466   (values))
1467
1468 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1469 ;;; out of the compile, then abort the writing of the output file, so
1470 ;;; that we don't overwrite it with known garbage.
1471 (defun sb!xc:compile-file
1472     (input-file
1473      &key
1474
1475      ;; ANSI options
1476      (output-file (cfp-output-file-default input-file))
1477      ;; FIXME: ANSI doesn't seem to say anything about
1478      ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1479      ;; function..
1480      ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1481      ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1482      (external-format :default)
1483
1484      ;; extensions
1485      (trace-file nil) 
1486      ((:block-compile *block-compile-argument*) nil)
1487      ;; FIXME: byte compiler to be removed completely
1488      #+nil ((:byte-compile *byte-compile*) *byte-compile-default*))
1489
1490   #!+sb-doc
1491   "Compile INPUT-FILE, producing a corresponding fasl file and returning
1492    its filename. Besides the ANSI &KEY arguments :OUTPUT-FILE, :VERBOSE,
1493    :PRINT, and :EXTERNAL-FORMAT,the following extensions are supported:
1494      :TRACE-FILE
1495         If given, internal data structures are dumped to the specified
1496         file, or if a value of T is given, to a file of *.trace type
1497         derived from the input file name.
1498    Also, as a workaround for vaguely-non-ANSI behavior, the :BLOCK-COMPILE
1499    argument is quasi-supported, to determine whether multiple
1500    functions are compiled together as a unit, resolving function
1501    references at compile time. NIL means that global function names
1502    are never resolved at compilation time. Currently NIL is the
1503    default behavior, because although section 3.2.2.3, \"Semantic
1504    Constraints\", of the ANSI spec allows this behavior under all
1505    circumstances, the compiler's runtime scales badly when it
1506    tries to do this for large files. If/when this performance
1507    problem is fixed, the block compilation default behavior will
1508    probably be made dependent on the SPEED and COMPILATION-SPEED
1509    optimization values, and the :BLOCK-COMPILE argument will probably
1510    become deprecated."
1511
1512   (unless (eq external-format :default)
1513     (error "Non-:DEFAULT EXTERNAL-FORMAT values are not supported."))
1514   (let* ((fasl-output nil)
1515          (output-file-name nil)
1516          (compile-won nil)
1517          (warnings-p nil)
1518          (failure-p t) ; T in case error keeps this from being set later
1519          (input-pathname (verify-source-file input-file))
1520          (source-info (make-file-source-info input-pathname))
1521          (*compiler-trace-output* nil)) ; might be modified below
1522                                 
1523     (unwind-protect
1524         (progn
1525           (when output-file
1526             (setq output-file-name
1527                   (sb!xc:compile-file-pathname input-file
1528                                                :output-file output-file))
1529             (setq fasl-output
1530                   (open-fasl-output output-file-name
1531                                     (namestring input-pathname)
1532                                     (eq *byte-compile* t))))
1533           (when trace-file
1534             (let* ((default-trace-file-pathname
1535                      (make-pathname :type "trace" :defaults input-pathname))
1536                    (trace-file-pathname
1537                     (if (eql trace-file t)
1538                         default-trace-file-pathname
1539                         (merge-pathnames trace-file
1540                                          default-trace-file-pathname))))
1541               (setf *compiler-trace-output*
1542                     (open trace-file-pathname
1543                           :if-exists :supersede
1544                           :direction :output))))
1545
1546           (when sb!xc:*compile-verbose*
1547             (start-error-output source-info))
1548           (let ((*compile-object* fasl-output)
1549                 dummy)
1550             (multiple-value-setq (dummy warnings-p failure-p)
1551               (sub-compile-file source-info)))
1552           (setq compile-won t))
1553
1554       (close-source-info source-info)
1555
1556       (when fasl-output
1557         (close-fasl-output fasl-output (not compile-won))
1558         (setq output-file-name
1559               (pathname (fasl-output-stream fasl-output)))
1560         (when (and compile-won sb!xc:*compile-verbose*)
1561           (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1562
1563       (when sb!xc:*compile-verbose*
1564         (finish-error-output source-info compile-won))
1565
1566       (when *compiler-trace-output*
1567         (close *compiler-trace-output*)))
1568
1569     (values (if output-file
1570                 ;; Hack around filesystem race condition...
1571                 (or (probe-file output-file-name) output-file-name)
1572                 nil)
1573             warnings-p
1574             failure-p)))
1575 \f
1576 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1577 ;;; the OUTPUT-FILE argument
1578 ;;;
1579 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1580 ;;; that results from merging the INPUT-FILE with the value of
1581 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1582 ;;; default to the appropriate implementation-defined default type for
1583 ;;; compiled files.
1584 (defun cfp-output-file-default (input-file)
1585   (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1586          (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1587     retyped))
1588         
1589 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1590 ;;;   If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1591 ;;;   the result is a logical pathname. If INPUT-FILE is a logical
1592 ;;;   pathname, it is translated into a physical pathname as if by
1593 ;;;   calling TRANSLATE-LOGICAL-PATHNAME.
1594 ;;; So I haven't really tried to make this precisely ANSI-compatible
1595 ;;; at the level of e.g. whether it returns logical pathname or a
1596 ;;; physical pathname. Patches to make it more correct are welcome.
1597 ;;; -- WHN 2000-12-09
1598 (defun sb!xc:compile-file-pathname (input-file
1599                                     &key
1600                                     (output-file (cfp-output-file-default
1601                                                   input-file))
1602                                     &allow-other-keys)
1603   #!+sb-doc
1604   "Return a pathname describing what file COMPILE-FILE would write to given
1605    these arguments."
1606   (pathname output-file))
1607 \f
1608 ;;;; MAKE-LOAD-FORM stuff
1609
1610 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1611 ;;; finds a constant structure, it invokes this to arrange for proper
1612 ;;; dumping. If it turns out that the constant has already been
1613 ;;; dumped, then we don't need to do anything.
1614 ;;;
1615 ;;; If the constant hasn't been dumped, then we check to see whether
1616 ;;; we are in the process of creating it. We detect this by
1617 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1618 ;;; the constants we are in the process of creating. Actually, each
1619 ;;; entry is a list of the constant and any init forms that need to be
1620 ;;; processed on behalf of that constant.
1621 ;;;
1622 ;;; It's not necessarily an error for this to happen. If we are
1623 ;;; processing the init form for some object that showed up *after*
1624 ;;; the original reference to this constant, then we just need to
1625 ;;; defer the processing of that init form. To detect this, we
1626 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1627 ;;; constants created since the last time we started processing an
1628 ;;; init form. If the constant passed to emit-make-load-form shows up
1629 ;;; in this list, then there is a circular chain through creation
1630 ;;; forms, which is an error.
1631 ;;;
1632 ;;; If there is some intervening init form, then we blow out of
1633 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1634 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1635 ;;; offending init form can be tacked onto the init forms for the
1636 ;;; circular object.
1637 ;;;
1638 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1639 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1640 ;;; whether the creation form is the magic value
1641 ;;; :JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1642 ;;; dumper will eventually get its hands on the object and use the
1643 ;;; normal structure dumping noise on it.
1644 ;;;
1645 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1646 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1647 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1648 ;;; dumper to use that result instead whenever it sees this constant.
1649 ;;;
1650 ;;; Now we try to compile the init form. We bind
1651 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1652 ;;; form (and any init forms that were added because of circularity
1653 ;;; detection). If this works, great. If not, we add the init forms to
1654 ;;; the init forms for the object that caused the problems and let it
1655 ;;; deal with it.
1656 (defvar *constants-being-created* nil)
1657 (defvar *constants-created-since-last-init* nil)
1658 ;;; FIXME: Shouldn't these^ variables be bound in LET forms?
1659 (defun emit-make-load-form (constant)
1660   (aver (fasl-output-p *compile-object*))
1661   (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1662               ;; KLUDGE: This special hack is because I was too lazy
1663               ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1664               ;; function of LAYOUT returns nontrivial forms when
1665               ;; building the cross-compiler but :IGNORE-IT when
1666               ;; cross-compiling or running under the target Lisp. --
1667               ;; WHN 19990914
1668               #+sb-xc-host (typep constant 'layout))
1669     (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1670       (when circular-ref
1671         (when (find constant *constants-created-since-last-init* :test #'eq)
1672           (throw constant t))
1673         (throw 'pending-init circular-ref)))
1674     (multiple-value-bind (creation-form init-form)
1675         (handler-case
1676             (sb!xc:make-load-form constant (make-null-lexenv))
1677           (error (condition)
1678                  (compiler-error "(while making load form for ~S)~%~A"
1679                                  constant
1680                                  condition)))
1681       (case creation-form
1682         (:just-dump-it-normally
1683          (fasl-validate-structure constant *compile-object*)
1684          t)
1685         (:ignore-it
1686          nil)
1687         (t
1688          (compile-top-level-lambdas () t)
1689          (when (fasl-constant-already-dumped-p constant *compile-object*)
1690            (return-from emit-make-load-form nil))
1691          (let* ((name (let ((*print-level* 1) (*print-length* 2))
1692                         (with-output-to-string (stream)
1693                           (write constant :stream stream))))
1694                 (info (if init-form
1695                           (list constant name init-form)
1696                           (list constant))))
1697            (let ((*constants-being-created*
1698                   (cons info *constants-being-created*))
1699                  (*constants-created-since-last-init*
1700                   (cons constant *constants-created-since-last-init*)))
1701              (when
1702                  (catch constant
1703                    (fasl-note-handle-for-constant
1704                     constant
1705                     (compile-load-time-value
1706                      creation-form
1707                      (format nil "creation form for ~A" name))
1708                     *compile-object*)
1709                    nil)
1710                (compiler-error "circular references in creation form for ~S"
1711                                constant)))
1712            (when (cdr info)
1713              (let* ((*constants-created-since-last-init* nil)
1714                     (circular-ref
1715                      (catch 'pending-init
1716                        (loop for (name form) on (cdr info) by #'cddr
1717                          collect name into names
1718                          collect form into forms
1719                          finally
1720                          (compile-make-load-form-init-forms
1721                           forms
1722                           (format nil "init form~:[~;s~] for ~{~A~^, ~}"
1723                                   (cdr forms) names)))
1724                        nil)))
1725                (when circular-ref
1726                  (setf (cdr circular-ref)
1727                        (append (cdr circular-ref) (cdr info))))))))))))