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
5 ;;;; This software is part of the SBCL system. See the README file for
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.
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*
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*))
33 (defvar *byte-compile-default* :maybe
35 "the default value for the :BYTE-COMPILE argument to COMPILE-FILE")
37 (defvar *byte-compile-top-level*
39 #+sb-xc-host nil ; since the byte compiler isn't supported in cross-compiler
41 "Similar to *BYTE-COMPILE-DEFAULT*, but controls the compilation of top-level
42 forms (evaluated at load-time) when the :BYTE-COMPILE argument is :MAYBE
43 (the default.) When true, we decide to byte-compile.")
45 ;;; the value of the :BYTE-COMPILE argument which was passed to the
47 (defvar *byte-compile* :maybe)
49 ;;; Bound by COMPILE-COMPONENT to T when byte-compiling, and NIL when
50 ;;; native compiling. During IR1 conversion this can also be :MAYBE,
51 ;;; in which case we must look at the policy, see (byte-compiling).
52 (defvar *byte-compiling* :maybe)
53 (declaim (type (member t nil :maybe) *byte-compile* *byte-compiling*
54 *byte-compile-default*))
56 (defvar *check-consistency* nil)
57 (defvar *all-components*)
59 ;;; Bind this to a stream to capture various internal debugging output.
60 (defvar *compiler-trace-output* nil)
62 ;;; The current block compilation state. These are initialized to the
63 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
66 ;;; *BLOCK-COMPILE-ARGUMENT* holds the original value of the
67 ;;; :BLOCK-COMPILE argument, which overrides any internal
69 (defvar *block-compile*)
70 (defvar *block-compile-argument*)
71 (declaim (type (member nil t :specified)
72 *block-compile* *block-compile-argument*))
73 (defvar *entry-points*)
74 (declaim (list *entry-points*))
76 ;;; When block compiling, used by PROCESS-FORM to accumulate top-level
77 ;;; lambdas resulting from compiling subforms. (In reverse order.)
78 (defvar *top-level-lambdas*)
79 (declaim (list *top-level-lambdas*))
81 (defvar sb!xc:*compile-verbose* t
83 "The default for the :VERBOSE argument to COMPILE-FILE.")
84 (defvar sb!xc:*compile-print* t
86 "The default for the :PRINT argument to COMPILE-FILE.")
87 (defvar *compile-progress* nil
89 "When this is true, the compiler prints to *ERROR-OUTPUT* progress
90 information about the phases of compilation of each function. (This
91 is useful mainly in large block compilations.)")
93 (defvar sb!xc:*compile-file-pathname* nil
95 "The defaulted pathname of the file currently being compiled, or NIL if not
97 (defvar sb!xc:*compile-file-truename* nil
99 "The TRUENAME of the file currently being compiled, or NIL if not
102 (declaim (type (or pathname null)
103 sb!xc:*compile-file-pathname*
104 sb!xc:*compile-file-truename*))
106 ;;; the SOURCE-INFO structure for the current compilation. This is
107 ;;; null globally to indicate that we aren't currently in any
108 ;;; identifiable compilation.
109 (defvar *source-info* nil)
111 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
112 ;;; normally causes nested uses to be no-ops).
113 (defvar *in-compilation-unit* nil)
115 ;;; Count of the number of compilation units dynamically enclosed by
116 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
117 (defvar *aborted-compilation-unit-count*)
119 ;;; Mumble conditional on *COMPILE-PROGRESS*.
120 (defun maybe-mumble (&rest foo)
121 (when *compile-progress*
122 (compiler-mumble "~&")
123 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
124 (apply #'compiler-mumble foo))))
126 (deftype object () '(or fasl-output core-object null))
128 (defvar *compile-object* nil)
129 (declaim (type object *compile-object*))
131 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
133 (defmacro sb!xc:with-compilation-unit (options &body body)
135 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
136 This form affects compilations that take place within its dynamic extent. It
137 is intended to be wrapped around the compilation of all files in the same
138 system. These keywords are defined:
139 :OVERRIDE Boolean-Form
140 One of the effects of this form is to delay undefined warnings
141 until the end of the form, instead of giving them at the end of each
142 compilation. If OVERRIDE is NIL (the default), then the outermost
143 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
144 OVERRIDE true causes that form to grab any enclosed warnings, even if
145 it is enclosed by another WITH-COMPILATION-UNIT."
146 `(%with-compilation-unit (lambda () ,@body) ,@options))
148 (defun %with-compilation-unit (fn &key override)
149 (let ((succeeded-p nil))
150 (if (and *in-compilation-unit* (not override))
151 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
152 ;; ordinarily (unless OVERRIDE) basically a no-op.
154 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
156 (incf *aborted-compilation-unit-count*)))
157 ;; FIXME: Now *COMPILER-FOO-COUNT* stuff is bound in more than
158 ;; one place. If we can get rid of the IR1 interpreter, this
159 ;; should be easier to clean up.
160 (let ((*aborted-compilation-unit-count* 0)
161 (*compiler-error-count* 0)
162 (*compiler-warning-count* 0)
163 (*compiler-style-warning-count* 0)
164 (*compiler-note-count* 0)
165 (*undefined-warnings* nil)
166 (*in-compilation-unit* t))
167 (handler-bind ((parse-unknown-type
169 (note-undefined-reference
170 (parse-unknown-type-specifier c)
173 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
175 (incf *aborted-compilation-unit-count*))
176 (summarize-compilation-unit (not succeeded-p))))))))
178 ;;; This is to be called at the end of a compilation unit. It signals
179 ;;; any residual warnings about unknown stuff, then prints the total
180 ;;; error counts. ABORT-P should be true when the compilation unit was
181 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
182 ;;; enclosed nested compilation units that were aborted.
183 (defun summarize-compilation-unit (abort-p)
185 (handler-bind ((style-warning #'compiler-style-warning-handler)
186 (warning #'compiler-warning-handler))
188 (let ((undefs (sort *undefined-warnings* #'string<
190 (let ((x (undefined-warning-name x)))
193 (prin1-to-string x)))))))
194 (unless *converting-for-interpreter*
195 (dolist (undef undefs)
196 (let ((name (undefined-warning-name undef))
197 (kind (undefined-warning-kind undef))
198 (warnings (undefined-warning-warnings undef))
199 (undefined-warning-count (undefined-warning-count undef)))
200 (dolist (*compiler-error-context* warnings)
201 (compiler-style-warning "undefined ~(~A~): ~S" kind name))
203 (let ((warn-count (length warnings)))
204 (when (and warnings (> undefined-warning-count warn-count))
205 (let ((more (- undefined-warning-count warn-count)))
206 (compiler-style-warning
207 "~D more use~:P of undefined ~(~A~) ~S"
208 more kind name)))))))
210 (dolist (kind '(:variable :function :type))
211 (let ((summary (mapcar #'undefined-warning-name
212 (remove kind undefs :test-not #'eq
213 :key #'undefined-warning-kind))))
215 (compiler-style-warning
216 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
217 ~% ~{~<~% ~1:;~S~>~^ ~}"
218 (cdr summary) kind summary)))))))
220 (unless (or *converting-for-interpreter*
222 (zerop *aborted-compilation-unit-count*)
223 (zerop *compiler-error-count*)
224 (zerop *compiler-warning-count*)
225 (zerop *compiler-style-warning-count*)
226 (zerop *compiler-note-count*)))
227 (format *error-output* "~&")
228 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
229 (compiler-mumble "compilation unit ~:[finished~;aborted~]~
230 ~[~:;~:*~& caught ~D fatal ERROR condition~:P~]~
231 ~[~:;~:*~& caught ~D ERROR condition~:P~]~
232 ~[~:;~:*~& caught ~D WARNING condition~:P~]~
233 ~[~:;~:*~& caught ~D STYLE-WARNING condition~:P~]~
234 ~[~:;~:*~& printed ~D note~:P~]"
236 *aborted-compilation-unit-count*
237 *compiler-error-count*
238 *compiler-warning-count*
239 *compiler-style-warning-count*
240 *compiler-note-count*)))
241 (format *error-output* "~&"))
243 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
244 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
245 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
246 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
247 (defmacro with-compilation-values (&body body)
249 (let ((*warnings-p* nil)
251 (values (progn ,@body)
255 ;;;; component compilation
257 (defparameter *max-optimize-iterations* 3 ; ARB
259 "The upper limit on the number of times that we will consecutively do IR1
260 optimization that doesn't introduce any new code. A finite limit is
261 necessary, since type inference may take arbitrarily long to converge.")
263 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
264 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
266 ;;; Repeatedly optimize COMPONENT until no further optimizations can
267 ;;; be found or we hit our iteration limit. When we hit the limit, we
268 ;;; clear the component and block REOPTIMIZE flags to discourage the
269 ;;; next optimization attempt from pounding on the same code.
270 (defun ir1-optimize-until-done (component)
271 (declare (type component component))
273 (event ir1-optimize-until-done)
275 (cleared-reanalyze nil))
277 (when (component-reanalyze component)
279 (setq cleared-reanalyze t)
280 (setf (component-reanalyze component) nil))
281 (setf (component-reoptimize component) nil)
282 (ir1-optimize component)
283 (cond ((component-reoptimize component)
285 (when (= count *max-optimize-iterations*)
287 (cond ((retry-delayed-ir1-transforms :optimize)
291 (event ir1-optimize-maxed-out)
292 (setf (component-reoptimize component) nil)
293 (do-blocks (block component)
294 (setf (block-reoptimize block) nil))
296 ((retry-delayed-ir1-transforms :optimize)
303 (when cleared-reanalyze
304 (setf (component-reanalyze component) t)))
307 (defparameter *constraint-propagate* t)
309 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
310 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
311 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
312 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
314 ;;; FIXME: It would be good to document why it's important to have a
315 ;;; large value here, and what the drawbacks of an excessively large
316 ;;; value are; and it might also be good to make it depend on
317 ;;; optimization policy.
318 (defparameter *reoptimize-after-type-check-max* 10)
320 (defevent reoptimize-maxed-out
321 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
323 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
324 (defun dfo-as-needed (component)
325 (declare (type component component))
326 (when (component-reanalyze component)
330 (unless (component-reanalyze component)
336 ;;; Do all the IR1 phases for a non-top-level component.
337 (defun ir1-phases (component)
338 (declare (type component component))
339 (let ((*constraint-number* 0)
341 (*delayed-ir1-transforms* nil))
342 (declare (special *constraint-number* *delayed-ir1-transforms*))
344 (ir1-optimize-until-done component)
345 (when (or (component-new-functions component)
346 (component-reanalyze-functions component))
347 (maybe-mumble "locall ")
348 (local-call-analyze component))
349 (dfo-as-needed component)
350 (when *constraint-propagate*
351 (maybe-mumble "constraint ")
352 (constraint-propagate component))
353 (when (retry-delayed-ir1-transforms :constraint)
354 (maybe-mumble "Rtran "))
355 ;; Delay the generation of type checks until the type
356 ;; constraints have had time to propagate, else the compiler can
358 (unless (and (or (component-reoptimize component)
359 (component-reanalyze component)
360 (component-new-functions component)
361 (component-reanalyze-functions component))
362 (< loop-count (- *reoptimize-after-type-check-max* 4)))
363 (maybe-mumble "type ")
364 (generate-type-checks component)
365 (unless (or (component-reoptimize component)
366 (component-reanalyze component)
367 (component-new-functions component)
368 (component-reanalyze-functions component))
370 (when (>= loop-count *reoptimize-after-type-check-max*)
371 (maybe-mumble "[reoptimize limit]")
372 (event reoptimize-maxed-out)
376 (ir1-finalize component)
379 (defun native-compile-component (component)
380 (let ((*code-segment* nil)
382 (maybe-mumble "GTN ")
383 (gtn-analyze component)
384 (maybe-mumble "LTN ")
385 (ltn-analyze component)
386 (dfo-as-needed component)
387 (maybe-mumble "control ")
388 (control-analyze component #'make-ir2-block)
390 (when (ir2-component-values-receivers (component-info component))
391 (maybe-mumble "stack ")
392 (stack-analyze component)
393 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
394 ;; stack analysis. There shouldn't be any unreachable code after
395 ;; control, so this won't delete anything.
396 (dfo-as-needed component))
400 (maybe-mumble "IR2tran ")
402 (entry-analyze component)
403 (ir2-convert component)
405 (when (policy *lexenv* (>= speed compilation-speed))
406 (maybe-mumble "copy ")
407 (copy-propagate component))
409 (select-representations component)
411 (when *check-consistency*
412 (maybe-mumble "check2 ")
413 (check-ir2-consistency component))
415 (delete-unreferenced-tns component)
417 (maybe-mumble "life ")
418 (lifetime-analyze component)
420 (when *compile-progress*
421 (compiler-mumble "") ; Sync before doing more output.
422 (pre-pack-tn-stats component *error-output*))
424 (when *check-consistency*
425 (maybe-mumble "check-life ")
426 (check-life-consistency component))
428 (maybe-mumble "pack ")
431 (when *check-consistency*
432 (maybe-mumble "check-pack ")
433 (check-pack-consistency component))
435 (when *compiler-trace-output*
436 (describe-component component *compiler-trace-output*)
437 (describe-ir2-component component *compiler-trace-output*))
439 (maybe-mumble "code ")
440 (multiple-value-bind (code-length trace-table fixups)
441 (generate-code component)
443 (when *compiler-trace-output*
444 (format *compiler-trace-output*
445 "~|~%disassembly of code for ~S~2%" component)
446 (sb!disassem:disassemble-assem-segment *code-segment*
447 *compiler-trace-output*))
449 (etypecase *compile-object*
451 (maybe-mumble "fasl")
452 (fasl-dump-component component
459 (maybe-mumble "core")
460 (make-core-component component
468 ;; We're done, so don't bother keeping anything around.
469 (setf (component-info component) nil)
473 (defun policy-byte-compile-p (thing)
478 ;;; Return our best guess for whether we will byte compile code
479 ;;; currently being IR1 converted. This is only a guess because the
480 ;;; decision is made on a per-component basis.
482 ;;; FIXME: This should be called something more mnemonic, e.g.
483 ;;; PROBABLY-BYTE-COMPILING
484 (defun byte-compiling ()
485 (if (eq *byte-compiling* :maybe)
486 (or (eq *byte-compile* t)
487 (policy-byte-compile-p *lexenv*))
488 (and *byte-compile* *byte-compiling*)))
490 ;;; Delete components with no external entry points before we try to
491 ;;; generate code. Unreachable closures can cause IR2 conversion to
492 ;;; puke on itself, since it is the reference to the closure which
493 ;;; normally causes the components to be combined.
495 ;;; FIXME: The original CMU CL comment said "This doesn't really cover
496 ;;; all cases..." That's a little scary.
497 (defun delete-if-no-entries (component)
498 (dolist (fun (component-lambdas component)
499 (delete-component component))
500 (case (functional-kind fun)
501 (:top-level (return))
503 (unless (every (lambda (ref)
504 (eq (block-component (node-block ref))
509 (defun byte-compile-this-component-p (component)
510 (ecase *byte-compile*
514 (every #'policy-byte-compile-p (component-lambdas component)))))
516 (defun compile-component (component)
517 (let* ((*component-being-compiled* component)
518 (*byte-compiling* (byte-compile-this-component-p component)))
519 (when sb!xc:*compile-print*
520 (compiler-mumble "~&; ~:[~;byte ~]compiling ~A: "
522 (component-name component)))
524 (ir1-phases component)
526 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
527 (maybe-mumble "env ")
528 (environment-analyze component)
529 (dfo-as-needed component)
531 (delete-if-no-entries component)
533 (unless (eq (block-next (component-head component))
534 (component-tail component))
536 (byte-compile-component component)
537 (native-compile-component component))))
539 (clear-constant-info)
541 (when sb!xc:*compile-print*
542 (compiler-mumble "~&"))
546 ;;;; clearing global data structures
548 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
549 ;;;; global data structures entirely when possible and consing up the
550 ;;;; others from scratch instead of clearing and reusing them?
552 ;;; Clear the INFO in constants in the *FREE-VARIABLES*, etc. In
553 ;;; addition to allowing stuff to be reclaimed, this is required for
554 ;;; correct assignment of constant offsets, since we need to assign a
555 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
556 ;;; slots, since they are used to keep track of functions across
557 ;;; component boundaries.
558 (defun clear-constant-info ()
559 (maphash #'(lambda (k v)
561 (setf (leaf-info v) nil))
563 (maphash #'(lambda (k v)
566 (setf (leaf-info v) nil)))
570 ;;; Blow away the REFS for all global variables, and let COMPONENT
572 (defun clear-ir1-info (component)
573 (declare (type component component))
575 (maphash #'(lambda (k v)
579 (delete-if #'here-p (leaf-refs v)))
580 (when (basic-var-p v)
581 (setf (basic-var-sets v)
582 (delete-if #'here-p (basic-var-sets v))))))
585 (eq (block-component (node-block x)) component)))
586 (blast *free-variables*)
587 (blast *free-functions*)
591 ;;; Clear global variables used by the compiler.
593 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
594 ;;; and it adds overhead even when people aren't using the compiler.
595 ;;; Perhaps we could make these global vars unbound except when
596 ;;; actually in use, so that this function could go away.
597 (defun clear-stuff (&optional (debug-too t))
599 ;; Clear global tables.
600 (when (boundp '*free-functions*)
601 (clrhash *free-functions*)
602 (clrhash *free-variables*)
603 (clrhash *constants*))
605 ;; Clear debug counters and tables.
606 (clrhash *seen-blocks*)
607 (clrhash *seen-functions*)
608 (clrhash *list-conflicts-table*)
611 (clrhash *continuation-numbers*)
612 (clrhash *number-continuations*)
613 (setq *continuation-number* 0)
617 (clrhash *label-ids*)
618 (clrhash *id-labels*)
621 ;; Clear some PACK data structures (for GC purposes only).
622 (aver (not *in-pack*))
623 (dolist (sb *backend-sb-list*)
624 (when (finite-sb-p sb)
625 (fill (finite-sb-live-tns sb) nil))))
627 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
628 ;; Superficially, this seemed harmful -- the user could reasonably be
629 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
630 ;; compiled. A closer inspection showed that this actually turned out to be
631 ;; harmless in practice, because CLEAR-STUFF was only called from within
632 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
633 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
634 ;; practice, it was also useless in practice. So we don't do it any more.)
640 ;;; Print out some useful info about Component to Stream.
641 (defun describe-component (component *standard-output*)
642 (declare (type component component))
643 (format t "~|~%;;;; component: ~S~2%" (component-name component))
644 (print-blocks component)
647 (defun describe-ir2-component (component *standard-output*)
648 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
649 (format t "entries:~%")
650 (dolist (entry (ir2-component-entries (component-info component)))
651 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
652 (label-id (entry-info-offset entry))
653 (entry-info-name entry)
654 (entry-info-closure-p entry)))
656 (pre-pack-tn-stats component *standard-output*)
658 (print-ir2-blocks component)
664 ;;;; When reading from a file, we have to keep track of some source
665 ;;;; information. We also exploit our ability to back up for printing
666 ;;;; the error context and for recovering from errors.
668 ;;;; The interface we provide to this stuff is the stream-oid
669 ;;;; Source-Info structure. The bookkeeping is done as a side-effect
670 ;;;; of getting the next source form.
672 ;;; A FILE-INFO structure holds all the source information for a
674 (defstruct (file-info (:copier nil))
675 ;; If a file, the truename of the corresponding source file. If from
676 ;; a Lisp form, :LISP. If from a stream, :STREAM.
677 (name (required-argument) :type (or pathname (member :lisp :stream)))
678 ;; the defaulted, but not necessarily absolute file name (i.e. prior
679 ;; to TRUENAME call.) Null if not a file. This is used to set
680 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
682 (untruename nil :type (or pathname null))
683 ;; the file's write date (if relevant)
684 (write-date nil :type (or unsigned-byte null))
685 ;; the source path root number of the first form in this file (i.e.
686 ;; the total number of forms converted previously in this
688 (source-root 0 :type unsigned-byte)
689 ;; parallel vectors containing the forms read out of the file and
690 ;; the file positions that reading of each form started at (i.e. the
691 ;; end of the previous form)
692 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
693 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
695 ;;; The SOURCE-INFO structure provides a handle on all the source
696 ;;; information for an entire compilation.
697 (defstruct (source-info
698 #-no-ansi-print-object
699 (:print-object (lambda (s stream)
700 (print-unreadable-object (s stream :type t))))
702 ;; the UT that compilation started at
703 (start-time (get-universal-time) :type unsigned-byte)
704 ;; the FILE-INFO structure for this compilation
705 (file-info nil :type (or file-info null))
706 ;; the stream that we are using to read the FILE-INFO, or NIL if
707 ;; no stream has been opened yet
708 (stream nil :type (or stream null)))
710 ;;; Given a pathname, return a SOURCE-INFO structure.
711 (defun make-file-source-info (file)
712 (let ((file-info (make-file-info :name (truename file)
714 :write-date (file-write-date file))))
716 (make-source-info :file-info file-info)))
718 ;;; Return a SOURCE-INFO to describe the incremental compilation of
719 ;;; FORM. Also used by SB!EVAL:INTERNAL-EVAL.
720 (defun make-lisp-source-info (form)
721 (make-source-info :start-time (get-universal-time)
722 :file-info (make-file-info :name :lisp
726 ;;; Return a SOURCE-INFO which will read from STREAM.
727 (defun make-stream-source-info (stream)
728 (let ((file-info (make-file-info :name :stream)))
729 (make-source-info :file-info file-info
732 ;;; Return a form read from STREAM; or for EOF use the trick,
733 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
734 ;;; error happens, then convert it to standard abort-the-compilation
735 ;;; error condition (possibly recording some extra location
737 (defun read-for-compile-file (stream position)
738 (handler-case (read stream nil stream)
739 (reader-error (condition)
740 (error 'input-error-in-compile-file
742 ;; We don't need to supply :POSITION here because
743 ;; READER-ERRORs already know their position in the file.
745 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
746 ;; (and that this is not a READER-ERROR) when it encounters end of
747 ;; file in the middle of something it's trying to read.
748 (end-of-file (condition)
749 (error 'input-error-in-compile-file
751 ;; We need to supply :POSITION here because the END-OF-FILE
752 ;; condition doesn't carry the position that the user
753 ;; probably cares about, where the failed READ began.
754 :position position))))
756 ;;; If STREAM is present, return it, otherwise open a stream to the
757 ;;; current file. There must be a current file.
759 ;;; FIXME: This is probably an unnecessarily roundabout way to do
760 ;;; things now that we process a single file in COMPILE-FILE (unlike
761 ;;; the old CMU CL code, which accepted multiple files). Also, the old
763 ;;; When we open a new file, we also reset *PACKAGE* and policy.
764 ;;; This gives the effect of rebinding around each file.
765 ;;; which doesn't seem to be true now. Check to make sure that if
766 ;;; such rebinding is necessary, it's still done somewhere.
767 (defun get-source-stream (info)
768 (declare (type source-info info))
769 (or (source-info-stream info)
770 (let* ((file-info (source-info-file-info info))
771 (name (file-info-name file-info)))
772 (setf sb!xc:*compile-file-truename* name
773 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
774 (source-info-stream info) (open name :direction :input)))))
776 ;;; Close the stream in INFO if it is open.
777 (defun close-source-info (info)
778 (declare (type source-info info))
779 (let ((stream (source-info-stream info)))
780 (when stream (close stream)))
781 (setf (source-info-stream info) nil)
784 ;;; Read and compile the source file.
785 (defun sub-sub-compile-file (info)
786 (let* ((file-info (source-info-file-info info))
787 (stream (get-source-stream info)))
789 (let* ((pos (file-position stream))
790 (form (read-for-compile-file stream pos)))
791 (if (eq form stream) ; i.e., if EOF
793 (let* ((forms (file-info-forms file-info))
794 (current-idx (+ (fill-pointer forms)
795 (file-info-source-root file-info))))
796 (vector-push-extend form forms)
797 (vector-push-extend pos (file-info-positions file-info))
798 (clrhash *source-paths*)
799 (find-source-paths form current-idx)
800 (process-top-level-form form
801 `(original-source-start 0 ,current-idx)
804 ;;; Return the INDEX'th source form read from INFO and the position
805 ;;; where it was read.
806 (defun find-source-root (index info)
807 (declare (type index index) (type source-info info))
808 (let ((file-info (source-info-file-info info)))
809 (values (aref (file-info-forms file-info) index)
810 (aref (file-info-positions file-info) index))))
812 ;;;; top-level form processing
814 ;;; This is called by top-level form processing when we are ready to
815 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
816 ;;; convert the form, but delay compilation, pushing the result on
817 ;;; *TOP-LEVEL-LAMBDAS* instead.
818 (defun convert-and-maybe-compile (form path)
819 (declare (list path))
820 (let* ((*lexenv* (make-lexenv :policy *policy*))
821 (tll (ir1-top-level form path nil)))
822 (cond ((eq *block-compile* t) (push tll *top-level-lambdas*))
823 (t (compile-top-level (list tll) nil)))))
825 ;;; Macroexpand FORM in the current environment with an error handler.
826 ;;; We only expand one level, so that we retain all the intervening
827 ;;; forms in the source path.
828 (defun preprocessor-macroexpand (form)
829 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
831 (compiler-error "(during macroexpansion)~%~A" condition))))
833 ;;; Process a PROGN-like portion of a top-level form. FORMS is a list of
834 ;;; the forms, and PATH is the source path of the FORM they came out of.
835 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
836 (defun process-top-level-progn (forms path compile-time-too)
837 (declare (list forms) (list path))
839 (process-top-level-form form path compile-time-too)))
841 ;;; Process a top-level use of LOCALLY, or anything else (e.g.
842 ;;; MACROLET) at top-level which has declarations and ordinary forms.
843 ;;; We parse declarations and then recursively process the body.
844 (defun process-top-level-locally (body path compile-time-too)
845 (declare (list path))
846 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
848 (process-decls decls nil nil (make-continuation)))
849 ;; Binding *POLICY* is pretty much of a hack, since it
850 ;; causes LOCALLY to "capture" enclosed proclamations. It
851 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
852 ;; value of *POLICY* as the policy. The need for this hack
853 ;; is due to the quirk that there is no way to represent in
854 ;; a POLICY that an optimize quality came from the default.
856 ;; FIXME: Ideally, something should be done so that DECLAIM
857 ;; inside LOCALLY works OK. Failing that, at least we could
858 ;; issue a warning instead of silently screwing up.
859 (*policy* (lexenv-policy *lexenv*)))
860 (process-top-level-progn forms path compile-time-too))))
862 ;;; Force any pending top-level forms to be compiled and dumped so
863 ;;; that they will be evaluated in the correct package environment.
864 ;;; Dump the form to be evaled at (cold) load time, and if EVAL is
865 ;;; true, eval the form immediately.
866 (defun process-cold-load-form (form path eval)
867 (let ((object *compile-object*))
870 (compile-top-level-lambdas () t)
871 (fasl-dump-cold-load-form form object))
872 ((or null core-object)
873 (convert-and-maybe-compile form path)))
877 ;;; Parse an EVAL-WHEN situations list, returning three flags,
878 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
879 ;;; the types of situations present in the list.
880 (defun parse-eval-when-situations (situations)
881 (when (or (not (listp situations))
882 (set-difference situations
889 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
890 (let ((deprecated-names (intersection situations '(compile load eval))))
891 (when deprecated-names
892 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
894 (values (intersection '(:compile-toplevel compile)
896 (intersection '(:load-toplevel load) situations)
897 (intersection '(:execute eval) situations)))
899 ;;; Process a top-level FORM with the specified source PATH.
900 ;;; * If this is a magic top-level form, then do stuff.
901 ;;; * If this is a macro, then expand it.
902 ;;; * Otherwise, just compile it.
904 ;;; COMPILE-TIME-TOO is as defined in ANSI
905 ;;; "3.2.3.1 Processing of Top Level Forms".
906 (defun process-top-level-form (form path compile-time-too)
908 (declare (list path))
910 (catch 'process-top-level-form-error-abort
911 (let* ((path (or (gethash form *source-paths*) (cons form path)))
912 (*compiler-error-bailout*
914 (convert-and-maybe-compile
915 `(error "execution of a form compiled with errors:~% ~S"
918 (throw 'process-top-level-form-error-abort nil))))
921 ;; (There are no EVAL-WHEN issues in the ATOM case until
922 ;; SBCL gets smart enough to handle global
923 ;; DEFINE-SYMBOL-MACRO.)
924 (convert-and-maybe-compile form path)
925 (flet ((need-at-least-one-arg (form)
927 (compiler-error "~S form is too short: ~S"
931 ;; FIXME: It's not clear to me why we would want this
932 ;; special case; it might have been needed for some
933 ;; variation of the old GENESIS system, but it certainly
934 ;; doesn't seem to be needed for ours. Sometime after the
935 ;; system is running I'd like to remove it tentatively and
936 ;; see whether anything breaks, and if nothing does break,
937 ;; remove it permanently. (And if we *do* want special
938 ;; treatment of all these, we probably want to treat WARN
940 ((error cerror break signal)
941 (process-cold-load-form form path nil))
942 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
943 (need-at-least-one-arg form)
944 (destructuring-bind (special-operator magic &rest body) form
945 (ecase special-operator
947 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
948 ;; "3.2.3.1 Processing of Top Level Forms".
949 (multiple-value-bind (ct lt e)
950 (parse-eval-when-situations magic)
951 (let ((new-compile-time-too (or ct
952 (and compile-time-too
954 (cond (lt (process-top-level-progn
955 body path new-compile-time-too))
956 (new-compile-time-too (eval
957 `(progn ,@body)))))))
959 (funcall-in-macrolet-lexenv
962 (process-top-level-locally body
966 (funcall-in-symbol-macrolet-lexenv
969 (process-top-level-locally body
971 compile-time-too)))))))
973 (process-top-level-locally (rest form) path compile-time-too))
975 (process-top-level-progn (rest form) path compile-time-too))
977 ;; Consider: What should we do when we hit e.g.
978 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
979 ;; (DEFUN FOO (X) (+ 7 X)))?
980 ;; DEFUN has a macro definition in the cross-compiler,
981 ;; and a different macro definition in the target
982 ;; compiler. The only sensible thing is to use the
983 ;; target compiler's macro definition, since the
984 ;; cross-compiler's macro is in general into target
985 ;; functions which can't meaningfully be executed at
986 ;; cross-compilation time. So make sure we do the EVAL
987 ;; here, before we macroexpand.
989 ;; (Isn't it fun to cross-compile Common Lisp?:-)
991 (when compile-time-too
992 (eval form)) ; letting xc host EVAL do its own macroexpansion
993 (let* ((uncrossed (uncross form))
994 ;; letting our cross-compiler do its macroexpansion too
995 (expanded (preprocessor-macroexpand uncrossed)))
996 (if (eq expanded uncrossed)
997 (convert-and-maybe-compile expanded path)
998 ;; Note that we also have to demote
999 ;; COMPILE-TIME-TOO to NIL, no matter what it was
1000 ;; before, since otherwise we'd tend to EVAL
1001 ;; subforms more than once.
1002 (process-top-level-form expanded path nil))))
1003 ;; When we're not cross-compiling, we only need to
1004 ;; macroexpand once, so we can follow the 1-thru-6
1005 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1006 ;; Top Level Forms".
1009 (let ((expanded (preprocessor-macroexpand form)))
1010 (cond ((eq expanded form)
1011 (when compile-time-too
1013 (convert-and-maybe-compile form path))
1015 (process-top-level-form expanded
1017 compile-time-too))))))))))
1021 ;;;; load time value support
1023 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1025 ;;; Returns T if we are currently producing a fasl file and hence
1026 ;;; constants need to be dumped carefully.
1027 (defun producing-fasl-file ()
1028 (unless *converting-for-interpreter*
1029 (fasl-output-p *compile-object*)))
1031 ;;; Compile FORM and arrange for it to be called at load-time. Return
1032 ;;; the dumper handle and our best guess at the type of the object.
1033 (defun compile-load-time-value
1035 (name (let ((*print-level* 2) (*print-length* 3))
1036 (format nil "load time value of ~S"
1037 (if (and (listp form)
1038 (eq (car form) 'make-value-cell))
1041 (let ((lambda (compile-load-time-stuff form name t)))
1043 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1044 (let ((type (leaf-type lambda)))
1045 (if (function-type-p type)
1046 (single-value-type (function-type-returns type))
1049 ;;; Compile the FORMS and arrange for them to be called (for effect,
1050 ;;; not value) at load time.
1051 (defun compile-make-load-form-init-forms (forms name)
1052 (let ((lambda (compile-load-time-stuff `(progn ,@forms) name nil)))
1053 (fasl-dump-top-level-lambda-call lambda *compile-object*)))
1055 ;;; Does the actual work of COMPILE-LOAD-TIME-VALUE or
1056 ;;; COMPILE-MAKE-LOAD-FORM- INIT-FORMS.
1057 (defun compile-load-time-stuff (form name for-value)
1059 (let* ((*lexenv* (make-null-lexenv))
1060 (lambda (ir1-top-level form *current-path* for-value)))
1061 (setf (leaf-name lambda) name)
1062 (compile-top-level (list lambda) t)
1065 ;;; Called by COMPILE-TOP-LEVEL when it was pased T for
1066 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1067 ;;; don't try to combine this component with anything else and frob
1068 ;;; the name. If not in a :TOP-LEVEL component, then don't bother
1069 ;;; compiling, because it was merged with a run-time component.
1070 (defun compile-load-time-value-lambda (lambdas)
1071 (aver (null (cdr lambdas)))
1072 (let* ((lambda (car lambdas))
1073 (component (block-component (node-block (lambda-bind lambda)))))
1074 (when (eq (component-kind component) :top-level)
1075 (setf (component-name component) (leaf-name lambda))
1076 (compile-component component)
1077 (clear-ir1-info component))))
1081 ;;; We build a list of top-level lambdas, and then periodically smash
1082 ;;; them together into a single component and compile it.
1083 (defvar *pending-top-level-lambdas*)
1085 ;;; The maximum number of top-level lambdas we put in a single
1086 ;;; top-level component.
1088 ;;; CMU CL 18b used this nontrivially by default (setting it to 10)
1089 ;;; but consequently suffered from the inability to execute some
1090 ;;; troublesome constructs correctly, e.g. inability to load a fasl
1091 ;;; file compiled from the source file
1092 ;;; (defpackage "FOO" (:use "CL"))
1093 ;;; (print 'foo::bar)
1094 ;;; because it would dump data-setup fops (including a FOP-PACKAGE for
1095 ;;; "FOO") for the second form before dumping the the code in the
1096 ;;; first form, or the fop to execute the code in the first form. By
1097 ;;; setting this value to 0 by default, we avoid this badness. This
1098 ;;; increases the number of toplevel form functions, and so increases
1099 ;;; the size of object files.
1101 ;;; The variable is still supported because when we are compiling the
1102 ;;; SBCL system itself, which is known not contain any troublesome
1103 ;;; constructs, we can set it to a nonzero value, which reduces the
1104 ;;; number of toplevel form objects, reducing the peak memory usage in
1105 ;;; GENESIS, which is desirable, since at least for SBCL version
1106 ;;; 0.6.7, this is the high water mark for memory usage during system
1108 (defparameter *top-level-lambda-max* 0)
1110 (defun object-call-top-level-lambda (tll)
1111 (declare (type functional tll))
1112 (let ((object *compile-object*))
1115 (fasl-dump-top-level-lambda-call tll object))
1117 (core-call-top-level-lambda tll object))
1120 ;;; Add LAMBDAS to the pending lambdas. If this leaves more than
1121 ;;; *TOP-LEVEL-LAMBDA-MAX* lambdas in the list, or if FORCE-P is true,
1122 ;;; then smash the lambdas into a single component, compile it, and
1123 ;;; call the resulting function.
1124 (defun sub-compile-top-level-lambdas (lambdas force-p)
1125 (declare (list lambdas))
1126 (setq *pending-top-level-lambdas*
1127 (append *pending-top-level-lambdas* lambdas))
1128 (let ((pending *pending-top-level-lambdas*))
1130 (or (> (length pending) *top-level-lambda-max*)
1132 (multiple-value-bind (component tll) (merge-top-level-lambdas pending)
1133 (setq *pending-top-level-lambdas* ())
1134 (let ((*byte-compile* (if (eq *byte-compile* :maybe)
1135 *byte-compile-top-level*
1137 (compile-component component))
1138 (clear-ir1-info component)
1139 (object-call-top-level-lambda tll))))
1142 ;;; Compile top-level code and call the top-level lambdas. We pick off
1143 ;;; top-level lambdas in non-top-level components here, calling
1144 ;;; SUB-c-t-l-l on each subsequence of normal top-level lambdas.
1145 (defun compile-top-level-lambdas (lambdas force-p)
1146 (declare (list lambdas))
1147 (let ((len (length lambdas)))
1148 (flet ((loser (start)
1149 (or (position-if (lambda (x)
1150 (not (eq (component-kind
1158 (do* ((start 0 (1+ loser))
1159 (loser (loser start) (loser start)))
1162 (sub-compile-top-level-lambdas nil t)))
1163 (sub-compile-top-level-lambdas (subseq lambdas start loser)
1164 (or force-p (/= loser len)))
1165 (unless (= loser len)
1166 (object-call-top-level-lambda (elt lambdas loser))))))
1169 ;;; Compile LAMBDAS (a list of the lambdas for top-level forms) into
1170 ;;; the object file. We loop doing local call analysis until it
1171 ;;; converges, since a single pass might miss something due to
1172 ;;; components being joined by LET conversion.
1174 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1175 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1176 (defun compile-top-level (lambdas load-time-value-p)
1177 (declare (list lambdas))
1178 (maybe-mumble "locall ")
1180 (let ((did-something nil))
1181 (dolist (lambda lambdas)
1182 (let* ((component (block-component (node-block (lambda-bind lambda))))
1183 (*all-components* (list component)))
1184 (when (component-new-functions component)
1185 (setq did-something t)
1186 (local-call-analyze component))))
1187 (unless did-something (return))))
1189 (maybe-mumble "IDFO ")
1190 (multiple-value-bind (components top-components hairy-top)
1191 (find-initial-dfo lambdas)
1192 (let ((*all-components* (append components top-components))
1193 (top-level-closure nil))
1194 (when *check-consistency*
1195 (maybe-mumble "[check]~%")
1196 (check-ir1-consistency *all-components*))
1198 (dolist (component (append hairy-top top-components))
1199 (when (pre-environment-analyze-top-level component)
1200 (setq top-level-closure t)))
1202 (let ((*byte-compile*
1203 (if (and top-level-closure (eq *byte-compile* :maybe))
1206 (dolist (component components)
1207 (compile-component component)
1208 (when (replace-top-level-xeps component)
1209 (setq top-level-closure t)))
1211 (when *check-consistency*
1212 (maybe-mumble "[check]~%")
1213 (check-ir1-consistency *all-components*))
1215 (if load-time-value-p
1216 (compile-load-time-value-lambda lambdas)
1217 (compile-top-level-lambdas lambdas top-level-closure)))
1219 (dolist (component components)
1220 (clear-ir1-info component))
1224 ;;; Actually compile any stuff that has been queued up for block
1226 (defun finish-block-compilation ()
1227 (when *block-compile*
1228 (when *top-level-lambdas*
1229 (compile-top-level (nreverse *top-level-lambdas*) nil)
1230 (setq *top-level-lambdas* ()))
1231 (setq *block-compile* nil)
1232 (setq *entry-points* nil)))
1234 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1235 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1236 (defun sub-compile-file (info)
1237 (declare (type source-info info))
1238 (let* (;; These are bound in WITH-COMPILATION-UNIT now. -- WHN 20000308
1239 #+nil (*compiler-error-count* 0)
1240 #+nil (*compiler-warning-count* 0)
1241 #+nil (*compiler-style-warning-count* 0)
1242 #+nil (*compiler-note-count* 0)
1243 (*block-compile* *block-compile-argument*)
1244 (*package* (sane-package))
1246 (*lexenv* (make-null-lexenv))
1247 (*converting-for-interpreter* nil)
1248 (*source-info* info)
1249 (sb!xc:*compile-file-pathname* nil)
1250 (sb!xc:*compile-file-truename* nil)
1251 (*top-level-lambdas* ())
1252 (*pending-top-level-lambdas* ())
1253 (*compiler-error-bailout*
1255 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1256 (return-from sub-compile-file (values nil t t))))
1257 (*current-path* nil)
1258 (*last-source-context* nil)
1259 (*last-original-source* nil)
1260 (*last-source-form* nil)
1261 (*last-format-string* nil)
1262 (*last-format-args* nil)
1263 (*last-message-count* 0)
1264 ;; FIXME: Do we need this rebinding here? It's a literal
1265 ;; translation of the old CMU CL rebinding to
1266 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1267 ;; and it's not obvious whether the rebinding to itself is
1268 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1269 (*info-environment* *info-environment*)
1270 (*gensym-counter* 0))
1272 (with-compilation-values
1273 (sb!xc:with-compilation-unit ()
1276 (sub-sub-compile-file info)
1278 (finish-block-compilation)
1279 (compile-top-level-lambdas () t)
1280 (let ((object *compile-object*))
1282 (fasl-output (fasl-dump-source-info info object))
1283 (core-object (fix-core-source-info info object))
1286 ;; Some errors are sufficiently bewildering that we just fail
1287 ;; immediately, without trying to recover and compile more of
1289 (input-error-in-compile-file (condition)
1290 (format *error-output*
1291 "~@<compilation aborted because of input error: ~2I~_~A~:>"
1293 (values nil t t)))))
1295 ;;; Return a pathname for the named file. The file must exist.
1296 (defun verify-source-file (pathname-designator)
1297 (let* ((pathname (pathname pathname-designator))
1298 (default-host (make-pathname :host (pathname-host pathname))))
1299 (flet ((try-with-type (path type error-p)
1300 (let ((new (merge-pathnames
1301 path (make-pathname :type type
1302 :defaults default-host))))
1303 (if (probe-file new)
1305 (and error-p (truename new))))))
1306 (cond ((typep pathname 'logical-pathname)
1307 (try-with-type pathname "LISP" t))
1308 ((probe-file pathname) pathname)
1309 ((try-with-type pathname "lisp" nil))
1310 ((try-with-type pathname "lisp" t))))))
1312 (defun elapsed-time-to-string (tsec)
1313 (multiple-value-bind (tmin sec) (truncate tsec 60)
1314 (multiple-value-bind (thr min) (truncate tmin 60)
1315 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1317 ;;; Print some junk at the beginning and end of compilation.
1318 (defun start-error-output (source-info)
1319 (declare (type source-info source-info))
1320 (let ((file-info (source-info-file-info source-info)))
1321 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1322 (namestring (file-info-name file-info))
1323 (sb!int:format-universal-time nil
1324 (file-info-write-date
1328 :print-timezone nil)))
1330 (defun finish-error-output (source-info won)
1331 (declare (type source-info source-info))
1332 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1334 (elapsed-time-to-string
1335 (- (get-universal-time)
1336 (source-info-start-time source-info))))
1339 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1340 ;;; out of the compile, then abort the writing of the output file, so
1341 ;;; that we don't overwrite it with known garbage.
1342 (defun sb!xc:compile-file
1347 (output-file (cfp-output-file-default input-file))
1348 ;; FIXME: ANSI doesn't seem to say anything about
1349 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1351 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1352 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1353 (external-format :default)
1357 ((:block-compile *block-compile-argument*) nil)
1358 ((:byte-compile *byte-compile*) *byte-compile-default*))
1361 "Compile INPUT-FILE, producing a corresponding fasl file and returning
1362 its filename. Besides the ANSI &KEY arguments :OUTPUT-FILE, :VERBOSE,
1363 :PRINT, and :EXTERNAL-FORMAT,the following extensions are supported:
1365 If given, internal data structures are dumped to the specified
1366 file, or if a value of T is given, to a file of *.trace type
1367 derived from the input file name.
1368 :BYTE-COMPILE {T | NIL | :MAYBE}
1369 Determines whether to compile into interpreted byte code instead of
1370 machine instructions. Byte code is several times smaller, but much
1371 slower. If :MAYBE, then only byte-compile when SPEED is 0 and
1372 DEBUG <= 1. The default is the value of SB-EXT:*BYTE-COMPILE-DEFAULT*,
1373 which is initially :MAYBE. (This option will probably become
1374 formally deprecated starting around sbcl-0.7.0, when various
1375 cleanups related to the byte interpreter are planned.)
1376 Also, as a workaround for vaguely-non-ANSI behavior, the :BLOCK-COMPILE
1377 argument is quasi-supported, to determine whether multiple
1378 functions are compiled together as a unit, resolving function
1379 references at compile time. NIL means that global function names
1380 are never resolved at compilation time. Currently NIL is the
1381 default behavior, because although section 3.2.2.3, \"Semantic
1382 Constraints\", of the ANSI spec allows this behavior under all
1383 circumstances, the compiler's runtime scales badly when it
1384 tries to do this for large files. If/when this performance
1385 problem is fixed, the block compilation default behavior will
1386 probably be made dependent on the SPEED and COMPILATION-SPEED
1387 optimization values, and the :BLOCK-COMPILE argument will probably
1390 (unless (eq external-format :default)
1391 (error "Non-:DEFAULT EXTERNAL-FORMAT values are not supported."))
1392 (let* ((fasl-output nil)
1393 (output-file-name nil)
1396 (failure-p t) ; T in case error keeps this from being set later
1397 (input-pathname (verify-source-file input-file))
1398 (source-info (make-file-source-info input-pathname))
1399 (*compiler-trace-output* nil)) ; might be modified below
1404 (setq output-file-name
1405 (sb!xc:compile-file-pathname input-file
1406 :output-file output-file))
1408 (open-fasl-output output-file-name
1409 (namestring input-pathname)
1410 (eq *byte-compile* t))))
1412 (let* ((default-trace-file-pathname
1413 (make-pathname :type "trace" :defaults input-pathname))
1414 (trace-file-pathname
1415 (if (eql trace-file t)
1416 default-trace-file-pathname
1417 (merge-pathnames trace-file
1418 default-trace-file-pathname))))
1419 (setf *compiler-trace-output*
1420 (open trace-file-pathname
1421 :if-exists :supersede
1422 :direction :output))))
1424 (when sb!xc:*compile-verbose*
1425 (start-error-output source-info))
1426 (let ((*compile-object* fasl-output)
1428 (multiple-value-setq (dummy warnings-p failure-p)
1429 (sub-compile-file source-info)))
1430 (setq compile-won t))
1432 (close-source-info source-info)
1435 (close-fasl-output fasl-output (not compile-won))
1436 (setq output-file-name
1437 (pathname (fasl-output-stream fasl-output)))
1438 (when (and compile-won sb!xc:*compile-verbose*)
1439 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1441 (when sb!xc:*compile-verbose*
1442 (finish-error-output source-info compile-won))
1444 (when *compiler-trace-output*
1445 (close *compiler-trace-output*)))
1447 (values (if output-file
1448 ;; Hack around filesystem race condition...
1449 (or (probe-file output-file-name) output-file-name)
1454 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1455 ;;; the OUTPUT-FILE argument
1457 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1458 ;;; that results from merging the INPUT-FILE with the value of
1459 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1460 ;;; default to the appropriate implementation-defined default type for
1462 (defun cfp-output-file-default (input-file)
1463 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1464 (retyped (make-pathname :type *backend-fasl-file-type*
1465 :defaults defaults)))
1468 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1469 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1470 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1471 ;;; pathname, it is translated into a physical pathname as if by
1472 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1473 ;;; So I haven't really tried to make this precisely ANSI-compatible
1474 ;;; at the level of e.g. whether it returns logical pathname or a
1475 ;;; physical pathname. Patches to make it more correct are welcome.
1476 ;;; -- WHN 2000-12-09
1477 (defun sb!xc:compile-file-pathname (input-file
1479 (output-file (cfp-output-file-default
1483 "Return a pathname describing what file COMPILE-FILE would write to given
1485 (pathname output-file))
1487 ;;;; MAKE-LOAD-FORM stuff
1489 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1490 ;;; finds a constant structure, it invokes this to arrange for proper
1491 ;;; dumping. If it turns out that the constant has already been
1492 ;;; dumped, then we don't need to do anything.
1494 ;;; If the constant hasn't been dumped, then we check to see whether
1495 ;;; we are in the process of creating it. We detect this by
1496 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1497 ;;; the constants we are in the process of creating. Actually, each
1498 ;;; entry is a list of the constant and any init forms that need to be
1499 ;;; processed on behalf of that constant.
1501 ;;; It's not necessarily an error for this to happen. If we are
1502 ;;; processing the init form for some object that showed up *after*
1503 ;;; the original reference to this constant, then we just need to
1504 ;;; defer the processing of that init form. To detect this, we
1505 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1506 ;;; constants created since the last time we started processing an
1507 ;;; init form. If the constant passed to emit-make-load-form shows up
1508 ;;; in this list, then there is a circular chain through creation
1509 ;;; forms, which is an error.
1511 ;;; If there is some intervening init form, then we blow out of
1512 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1513 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1514 ;;; offending init form can be tacked onto the init forms for the
1515 ;;; circular object.
1517 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1518 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1519 ;;; whether the creation form is the magic value
1520 ;;; :JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1521 ;;; dumper will eventually get its hands on the object and use the
1522 ;;; normal structure dumping noise on it.
1524 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1525 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1526 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1527 ;;; dumper to use that result instead whenever it sees this constant.
1529 ;;; Now we try to compile the init form. We bind
1530 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1531 ;;; form (and any init forms that were added because of circularity
1532 ;;; detection). If this works, great. If not, we add the init forms to
1533 ;;; the init forms for the object that caused the problems and let it
1535 (defvar *constants-being-created* nil)
1536 (defvar *constants-created-since-last-init* nil)
1537 ;;; FIXME: Shouldn't these^ variables be bound in LET forms?
1538 (defun emit-make-load-form (constant)
1539 (aver (fasl-output-p *compile-object*))
1540 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1541 ;; KLUDGE: This special hack is because I was too lazy
1542 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1543 ;; function of LAYOUT returns nontrivial forms when
1544 ;; building the cross-compiler but :IGNORE-IT when
1545 ;; cross-compiling or running under the target Lisp. --
1547 #+sb-xc-host (typep constant 'layout))
1548 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1550 (when (find constant *constants-created-since-last-init* :test #'eq)
1552 (throw 'pending-init circular-ref)))
1553 (multiple-value-bind (creation-form init-form)
1555 (sb!xc:make-load-form constant (make-null-lexenv))
1557 (compiler-error "(while making load form for ~S)~%~A"
1561 (:just-dump-it-normally
1562 (fasl-validate-structure constant *compile-object*)
1567 (compile-top-level-lambdas () t)
1568 (when (fasl-constant-already-dumped-p constant *compile-object*)
1569 (return-from emit-make-load-form nil))
1570 (let* ((name (let ((*print-level* 1) (*print-length* 2))
1571 (with-output-to-string (stream)
1572 (write constant :stream stream))))
1574 (list constant name init-form)
1576 (let ((*constants-being-created*
1577 (cons info *constants-being-created*))
1578 (*constants-created-since-last-init*
1579 (cons constant *constants-created-since-last-init*)))
1582 (fasl-note-handle-for-constant
1584 (compile-load-time-value
1586 (format nil "creation form for ~A" name))
1589 (compiler-error "circular references in creation form for ~S"
1592 (let* ((*constants-created-since-last-init* nil)
1594 (catch 'pending-init
1595 (loop for (name form) on (cdr info) by #'cddr
1596 collect name into names
1597 collect form into forms
1599 (compile-make-load-form-init-forms
1601 (format nil "init form~:[~;s~] for ~{~A~^, ~}"
1602 (cdr forms) names)))
1605 (setf (cdr circular-ref)
1606 (append (cdr circular-ref) (cdr info))))))))))))