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 (dolist (undef undefs)
195 (let ((name (undefined-warning-name undef))
196 (kind (undefined-warning-kind undef))
197 (warnings (undefined-warning-warnings undef))
198 (undefined-warning-count (undefined-warning-count undef)))
199 (dolist (*compiler-error-context* warnings)
200 (compiler-style-warning "undefined ~(~A~): ~S" kind name))
201 (let ((warn-count (length warnings)))
202 (when (and warnings (> undefined-warning-count warn-count))
203 (let ((more (- undefined-warning-count warn-count)))
204 (compiler-style-warning
205 "~D more use~:P of undefined ~(~A~) ~S"
208 (dolist (kind '(:variable :function :type))
209 (let ((summary (mapcar #'undefined-warning-name
210 (remove kind undefs :test-not #'eq
211 :key #'undefined-warning-kind))))
213 (compiler-style-warning
214 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
215 ~% ~{~<~% ~1:;~S~>~^ ~}"
216 (cdr summary) kind summary)))))))
218 (unless (and (not abort-p)
219 (zerop *aborted-compilation-unit-count*)
220 (zerop *compiler-error-count*)
221 (zerop *compiler-warning-count*)
222 (zerop *compiler-style-warning-count*)
223 (zerop *compiler-note-count*))
224 (format *error-output* "~&")
225 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
226 (compiler-mumble "compilation unit ~:[finished~;aborted~]~
227 ~[~:;~:*~& caught ~D fatal ERROR condition~:P~]~
228 ~[~:;~:*~& caught ~D ERROR condition~:P~]~
229 ~[~:;~:*~& caught ~D WARNING condition~:P~]~
230 ~[~:;~:*~& caught ~D STYLE-WARNING condition~:P~]~
231 ~[~:;~:*~& printed ~D note~:P~]"
233 *aborted-compilation-unit-count*
234 *compiler-error-count*
235 *compiler-warning-count*
236 *compiler-style-warning-count*
237 *compiler-note-count*)))
238 (format *error-output* "~&"))
240 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
241 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
242 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
243 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
244 (defmacro with-compilation-values (&body body)
246 (let ((*warnings-p* nil)
248 (values (progn ,@body)
252 ;;;; component compilation
254 (defparameter *max-optimize-iterations* 3 ; ARB
256 "The upper limit on the number of times that we will consecutively do IR1
257 optimization that doesn't introduce any new code. A finite limit is
258 necessary, since type inference may take arbitrarily long to converge.")
260 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
261 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
263 ;;; Repeatedly optimize COMPONENT until no further optimizations can
264 ;;; be found or we hit our iteration limit. When we hit the limit, we
265 ;;; clear the component and block REOPTIMIZE flags to discourage the
266 ;;; next optimization attempt from pounding on the same code.
267 (defun ir1-optimize-until-done (component)
268 (declare (type component component))
270 (event ir1-optimize-until-done)
272 (cleared-reanalyze nil))
274 (when (component-reanalyze component)
276 (setq cleared-reanalyze t)
277 (setf (component-reanalyze component) nil))
278 (setf (component-reoptimize component) nil)
279 (ir1-optimize component)
280 (cond ((component-reoptimize component)
282 (when (= count *max-optimize-iterations*)
284 (cond ((retry-delayed-ir1-transforms :optimize)
288 (event ir1-optimize-maxed-out)
289 (setf (component-reoptimize component) nil)
290 (do-blocks (block component)
291 (setf (block-reoptimize block) nil))
293 ((retry-delayed-ir1-transforms :optimize)
300 (when cleared-reanalyze
301 (setf (component-reanalyze component) t)))
304 (defparameter *constraint-propagate* t)
306 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
307 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
308 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
309 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
311 ;;; FIXME: It would be good to document why it's important to have a
312 ;;; large value here, and what the drawbacks of an excessively large
313 ;;; value are; and it might also be good to make it depend on
314 ;;; optimization policy.
315 (defparameter *reoptimize-after-type-check-max* 10)
317 (defevent reoptimize-maxed-out
318 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
320 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
321 (defun dfo-as-needed (component)
322 (declare (type component component))
323 (when (component-reanalyze component)
327 (unless (component-reanalyze component)
333 ;;; Do all the IR1 phases for a non-top-level component.
334 (defun ir1-phases (component)
335 (declare (type component component))
336 (let ((*constraint-number* 0)
338 (*delayed-ir1-transforms* nil))
339 (declare (special *constraint-number* *delayed-ir1-transforms*))
341 (ir1-optimize-until-done component)
342 (when (or (component-new-functions component)
343 (component-reanalyze-functions component))
344 (maybe-mumble "locall ")
345 (local-call-analyze component))
346 (dfo-as-needed component)
347 (when *constraint-propagate*
348 (maybe-mumble "constraint ")
349 (constraint-propagate component))
350 (when (retry-delayed-ir1-transforms :constraint)
351 (maybe-mumble "Rtran "))
352 ;; Delay the generation of type checks until the type
353 ;; constraints have had time to propagate, else the compiler can
355 (unless (and (or (component-reoptimize component)
356 (component-reanalyze component)
357 (component-new-functions component)
358 (component-reanalyze-functions component))
359 (< loop-count (- *reoptimize-after-type-check-max* 4)))
360 (maybe-mumble "type ")
361 (generate-type-checks component)
362 (unless (or (component-reoptimize component)
363 (component-reanalyze component)
364 (component-new-functions component)
365 (component-reanalyze-functions component))
367 (when (>= loop-count *reoptimize-after-type-check-max*)
368 (maybe-mumble "[reoptimize limit]")
369 (event reoptimize-maxed-out)
373 (ir1-finalize component)
376 (defun native-compile-component (component)
377 (let ((*code-segment* nil)
379 (maybe-mumble "GTN ")
380 (gtn-analyze component)
381 (maybe-mumble "LTN ")
382 (ltn-analyze component)
383 (dfo-as-needed component)
384 (maybe-mumble "control ")
385 (control-analyze component #'make-ir2-block)
387 (when (ir2-component-values-receivers (component-info component))
388 (maybe-mumble "stack ")
389 (stack-analyze component)
390 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
391 ;; stack analysis. There shouldn't be any unreachable code after
392 ;; control, so this won't delete anything.
393 (dfo-as-needed component))
397 (maybe-mumble "IR2tran ")
399 (entry-analyze component)
400 (ir2-convert component)
402 (when (policy *lexenv* (>= speed compilation-speed))
403 (maybe-mumble "copy ")
404 (copy-propagate component))
406 (select-representations component)
408 (when *check-consistency*
409 (maybe-mumble "check2 ")
410 (check-ir2-consistency component))
412 (delete-unreferenced-tns component)
414 (maybe-mumble "life ")
415 (lifetime-analyze component)
417 (when *compile-progress*
418 (compiler-mumble "") ; Sync before doing more output.
419 (pre-pack-tn-stats component *error-output*))
421 (when *check-consistency*
422 (maybe-mumble "check-life ")
423 (check-life-consistency component))
425 (maybe-mumble "pack ")
428 (when *check-consistency*
429 (maybe-mumble "check-pack ")
430 (check-pack-consistency component))
432 (when *compiler-trace-output*
433 (describe-component component *compiler-trace-output*)
434 (describe-ir2-component component *compiler-trace-output*))
436 (maybe-mumble "code ")
437 (multiple-value-bind (code-length trace-table fixups)
438 (generate-code component)
440 (when *compiler-trace-output*
441 (format *compiler-trace-output*
442 "~|~%disassembly of code for ~S~2%" component)
443 (sb!disassem:disassemble-assem-segment *code-segment*
444 *compiler-trace-output*))
446 (etypecase *compile-object*
448 (maybe-mumble "fasl")
449 (fasl-dump-component component
456 (maybe-mumble "core")
457 (make-core-component component
465 ;; We're done, so don't bother keeping anything around.
466 (setf (component-info component) nil)
470 (defun policy-byte-compile-p (thing)
475 ;;; Return our best guess for whether we will byte compile code
476 ;;; currently being IR1 converted. This is only a guess because the
477 ;;; decision is made on a per-component basis.
479 ;;; FIXME: This should be called something more mnemonic, e.g.
480 ;;; PROBABLY-BYTE-COMPILING
481 (defun byte-compiling ()
482 (if (eq *byte-compiling* :maybe)
483 (or (eq *byte-compile* t)
484 (policy-byte-compile-p *lexenv*))
485 (and *byte-compile* *byte-compiling*)))
487 ;;; Delete components with no external entry points before we try to
488 ;;; generate code. Unreachable closures can cause IR2 conversion to
489 ;;; puke on itself, since it is the reference to the closure which
490 ;;; normally causes the components to be combined.
492 ;;; FIXME: The original CMU CL comment said "This doesn't really cover
493 ;;; all cases..." That's a little scary.
494 (defun delete-if-no-entries (component)
495 (dolist (fun (component-lambdas component)
496 (delete-component component))
497 (case (functional-kind fun)
498 (:top-level (return))
500 (unless (every (lambda (ref)
501 (eq (block-component (node-block ref))
506 (defun byte-compile-this-component-p (component)
507 (ecase *byte-compile*
511 (every #'policy-byte-compile-p (component-lambdas component)))))
513 (defun compile-component (component)
514 (let* ((*component-being-compiled* component)
515 (*byte-compiling* (byte-compile-this-component-p component)))
516 (when sb!xc:*compile-print*
517 (compiler-mumble "~&; ~:[~;byte ~]compiling ~A: "
519 (component-name component)))
521 (ir1-phases component)
523 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
524 (maybe-mumble "env ")
525 (environment-analyze component)
526 (dfo-as-needed component)
528 (delete-if-no-entries component)
530 (unless (eq (block-next (component-head component))
531 (component-tail component))
533 (byte-compile-component component)
534 (native-compile-component component))))
536 (clear-constant-info)
538 (when sb!xc:*compile-print*
539 (compiler-mumble "~&"))
543 ;;;; clearing global data structures
545 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
546 ;;;; global data structures entirely when possible and consing up the
547 ;;;; others from scratch instead of clearing and reusing them?
549 ;;; Clear the INFO in constants in the *FREE-VARIABLES*, etc. In
550 ;;; addition to allowing stuff to be reclaimed, this is required for
551 ;;; correct assignment of constant offsets, since we need to assign a
552 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
553 ;;; slots, since they are used to keep track of functions across
554 ;;; component boundaries.
555 (defun clear-constant-info ()
556 (maphash #'(lambda (k v)
558 (setf (leaf-info v) nil))
560 (maphash #'(lambda (k v)
563 (setf (leaf-info v) nil)))
567 ;;; Blow away the REFS for all global variables, and let COMPONENT
569 (defun clear-ir1-info (component)
570 (declare (type component component))
572 (maphash #'(lambda (k v)
576 (delete-if #'here-p (leaf-refs v)))
577 (when (basic-var-p v)
578 (setf (basic-var-sets v)
579 (delete-if #'here-p (basic-var-sets v))))))
582 (eq (block-component (node-block x)) component)))
583 (blast *free-variables*)
584 (blast *free-functions*)
588 ;;; Clear global variables used by the compiler.
590 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
591 ;;; and it adds overhead even when people aren't using the compiler.
592 ;;; Perhaps we could make these global vars unbound except when
593 ;;; actually in use, so that this function could go away.
594 (defun clear-stuff (&optional (debug-too t))
596 ;; Clear global tables.
597 (when (boundp '*free-functions*)
598 (clrhash *free-functions*)
599 (clrhash *free-variables*)
600 (clrhash *constants*))
602 ;; Clear debug counters and tables.
603 (clrhash *seen-blocks*)
604 (clrhash *seen-functions*)
605 (clrhash *list-conflicts-table*)
608 (clrhash *continuation-numbers*)
609 (clrhash *number-continuations*)
610 (setq *continuation-number* 0)
614 (clrhash *label-ids*)
615 (clrhash *id-labels*)
618 ;; Clear some PACK data structures (for GC purposes only).
619 (aver (not *in-pack*))
620 (dolist (sb *backend-sb-list*)
621 (when (finite-sb-p sb)
622 (fill (finite-sb-live-tns sb) nil))))
624 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
625 ;; Superficially, this seemed harmful -- the user could reasonably be
626 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
627 ;; compiled. A closer inspection showed that this actually turned out to be
628 ;; harmless in practice, because CLEAR-STUFF was only called from within
629 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
630 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
631 ;; practice, it was also useless in practice. So we don't do it any more.)
637 ;;; Print out some useful info about Component to Stream.
638 (defun describe-component (component *standard-output*)
639 (declare (type component component))
640 (format t "~|~%;;;; component: ~S~2%" (component-name component))
641 (print-blocks component)
644 (defun describe-ir2-component (component *standard-output*)
645 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
646 (format t "entries:~%")
647 (dolist (entry (ir2-component-entries (component-info component)))
648 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
649 (label-id (entry-info-offset entry))
650 (entry-info-name entry)
651 (entry-info-closure-p entry)))
653 (pre-pack-tn-stats component *standard-output*)
655 (print-ir2-blocks component)
661 ;;;; When reading from a file, we have to keep track of some source
662 ;;;; information. We also exploit our ability to back up for printing
663 ;;;; the error context and for recovering from errors.
665 ;;;; The interface we provide to this stuff is the stream-oid
666 ;;;; Source-Info structure. The bookkeeping is done as a side-effect
667 ;;;; of getting the next source form.
669 ;;; A FILE-INFO structure holds all the source information for a
671 (defstruct (file-info (:copier nil))
672 ;; If a file, the truename of the corresponding source file. If from
673 ;; a Lisp form, :LISP. If from a stream, :STREAM.
674 (name (required-argument) :type (or pathname (member :lisp :stream)))
675 ;; the defaulted, but not necessarily absolute file name (i.e. prior
676 ;; to TRUENAME call.) Null if not a file. This is used to set
677 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
679 (untruename nil :type (or pathname null))
680 ;; the file's write date (if relevant)
681 (write-date nil :type (or unsigned-byte null))
682 ;; the source path root number of the first form in this file (i.e.
683 ;; the total number of forms converted previously in this
685 (source-root 0 :type unsigned-byte)
686 ;; parallel vectors containing the forms read out of the file and
687 ;; the file positions that reading of each form started at (i.e. the
688 ;; end of the previous form)
689 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
690 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
692 ;;; The SOURCE-INFO structure provides a handle on all the source
693 ;;; information for an entire compilation.
694 (defstruct (source-info
695 #-no-ansi-print-object
696 (:print-object (lambda (s stream)
697 (print-unreadable-object (s stream :type t))))
699 ;; the UT that compilation started at
700 (start-time (get-universal-time) :type unsigned-byte)
701 ;; the FILE-INFO structure for this compilation
702 (file-info nil :type (or file-info null))
703 ;; the stream that we are using to read the FILE-INFO, or NIL if
704 ;; no stream has been opened yet
705 (stream nil :type (or stream null)))
707 ;;; Given a pathname, return a SOURCE-INFO structure.
708 (defun make-file-source-info (file)
709 (let ((file-info (make-file-info :name (truename file)
711 :write-date (file-write-date file))))
713 (make-source-info :file-info file-info)))
715 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
716 (defun make-lisp-source-info (form)
717 (make-source-info :start-time (get-universal-time)
718 :file-info (make-file-info :name :lisp
722 ;;; Return a SOURCE-INFO which will read from STREAM.
723 (defun make-stream-source-info (stream)
724 (let ((file-info (make-file-info :name :stream)))
725 (make-source-info :file-info file-info
728 ;;; Return a form read from STREAM; or for EOF use the trick,
729 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
730 ;;; error happens, then convert it to standard abort-the-compilation
731 ;;; error condition (possibly recording some extra location
733 (defun read-for-compile-file (stream position)
734 (handler-case (read stream nil stream)
735 (reader-error (condition)
736 (error 'input-error-in-compile-file
738 ;; We don't need to supply :POSITION here because
739 ;; READER-ERRORs already know their position in the file.
741 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
742 ;; (and that this is not a READER-ERROR) when it encounters end of
743 ;; file in the middle of something it's trying to read.
744 (end-of-file (condition)
745 (error 'input-error-in-compile-file
747 ;; We need to supply :POSITION here because the END-OF-FILE
748 ;; condition doesn't carry the position that the user
749 ;; probably cares about, where the failed READ began.
750 :position position))))
752 ;;; If STREAM is present, return it, otherwise open a stream to the
753 ;;; current file. There must be a current file.
755 ;;; FIXME: This is probably an unnecessarily roundabout way to do
756 ;;; things now that we process a single file in COMPILE-FILE (unlike
757 ;;; the old CMU CL code, which accepted multiple files). Also, the old
759 ;;; When we open a new file, we also reset *PACKAGE* and policy.
760 ;;; This gives the effect of rebinding around each file.
761 ;;; which doesn't seem to be true now. Check to make sure that if
762 ;;; such rebinding is necessary, it's still done somewhere.
763 (defun get-source-stream (info)
764 (declare (type source-info info))
765 (or (source-info-stream info)
766 (let* ((file-info (source-info-file-info info))
767 (name (file-info-name file-info)))
768 (setf sb!xc:*compile-file-truename* name
769 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
770 (source-info-stream info) (open name :direction :input)))))
772 ;;; Close the stream in INFO if it is open.
773 (defun close-source-info (info)
774 (declare (type source-info info))
775 (let ((stream (source-info-stream info)))
776 (when stream (close stream)))
777 (setf (source-info-stream info) nil)
780 ;;; Read and compile the source file.
781 (defun sub-sub-compile-file (info)
782 (let* ((file-info (source-info-file-info info))
783 (stream (get-source-stream info)))
785 (let* ((pos (file-position stream))
786 (form (read-for-compile-file stream pos)))
787 (if (eq form stream) ; i.e., if EOF
789 (let* ((forms (file-info-forms file-info))
790 (current-idx (+ (fill-pointer forms)
791 (file-info-source-root file-info))))
792 (vector-push-extend form forms)
793 (vector-push-extend pos (file-info-positions file-info))
794 (clrhash *source-paths*)
795 (find-source-paths form current-idx)
796 (process-top-level-form form
797 `(original-source-start 0 ,current-idx)
800 ;;; Return the INDEX'th source form read from INFO and the position
801 ;;; where it was read.
802 (defun find-source-root (index info)
803 (declare (type index index) (type source-info info))
804 (let ((file-info (source-info-file-info info)))
805 (values (aref (file-info-forms file-info) index)
806 (aref (file-info-positions file-info) index))))
808 ;;;; top-level form processing
810 ;;; This is called by top-level form processing when we are ready to
811 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
812 ;;; convert the form, but delay compilation, pushing the result on
813 ;;; *TOP-LEVEL-LAMBDAS* instead.
814 (defun convert-and-maybe-compile (form path)
815 (declare (list path))
816 (let* ((*lexenv* (make-lexenv :policy *policy*))
817 (tll (ir1-top-level form path nil)))
818 (cond ((eq *block-compile* t) (push tll *top-level-lambdas*))
819 (t (compile-top-level (list tll) nil)))))
821 ;;; Macroexpand FORM in the current environment with an error handler.
822 ;;; We only expand one level, so that we retain all the intervening
823 ;;; forms in the source path.
824 (defun preprocessor-macroexpand (form)
825 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
827 (compiler-error "(during macroexpansion)~%~A" condition))))
829 ;;; Process a PROGN-like portion of a top-level form. FORMS is a list of
830 ;;; the forms, and PATH is the source path of the FORM they came out of.
831 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
832 (defun process-top-level-progn (forms path compile-time-too)
833 (declare (list forms) (list path))
835 (process-top-level-form form path compile-time-too)))
837 ;;; Process a top-level use of LOCALLY, or anything else (e.g.
838 ;;; MACROLET) at top-level which has declarations and ordinary forms.
839 ;;; We parse declarations and then recursively process the body.
840 (defun process-top-level-locally (body path compile-time-too)
841 (declare (list path))
842 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
844 (process-decls decls nil nil (make-continuation)))
845 ;; Binding *POLICY* is pretty much of a hack, since it
846 ;; causes LOCALLY to "capture" enclosed proclamations. It
847 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
848 ;; value of *POLICY* as the policy. The need for this hack
849 ;; is due to the quirk that there is no way to represent in
850 ;; a POLICY that an optimize quality came from the default.
852 ;; FIXME: Ideally, something should be done so that DECLAIM
853 ;; inside LOCALLY works OK. Failing that, at least we could
854 ;; issue a warning instead of silently screwing up.
855 (*policy* (lexenv-policy *lexenv*)))
856 (process-top-level-progn forms path compile-time-too))))
858 ;;; Force any pending top-level forms to be compiled and dumped so
859 ;;; that they will be evaluated in the correct package environment.
860 ;;; Dump the form to be evaled at (cold) load time, and if EVAL is
861 ;;; true, eval the form immediately.
862 (defun process-cold-load-form (form path eval)
863 (let ((object *compile-object*))
866 (compile-top-level-lambdas () t)
867 (fasl-dump-cold-load-form form object))
868 ((or null core-object)
869 (convert-and-maybe-compile form path)))
873 ;;; Parse an EVAL-WHEN situations list, returning three flags,
874 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
875 ;;; the types of situations present in the list.
876 (defun parse-eval-when-situations (situations)
877 (when (or (not (listp situations))
878 (set-difference situations
885 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
886 (let ((deprecated-names (intersection situations '(compile load eval))))
887 (when deprecated-names
888 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
890 (values (intersection '(:compile-toplevel compile)
892 (intersection '(:load-toplevel load) situations)
893 (intersection '(:execute eval) situations)))
895 ;;; Process a top-level FORM with the specified source PATH.
896 ;;; * If this is a magic top-level form, then do stuff.
897 ;;; * If this is a macro, then expand it.
898 ;;; * Otherwise, just compile it.
900 ;;; COMPILE-TIME-TOO is as defined in ANSI
901 ;;; "3.2.3.1 Processing of Top Level Forms".
902 (defun process-top-level-form (form path compile-time-too)
904 (declare (list path))
906 (catch 'process-top-level-form-error-abort
907 (let* ((path (or (gethash form *source-paths*) (cons form path)))
908 (*compiler-error-bailout*
910 (convert-and-maybe-compile
911 `(error "execution of a form compiled with errors:~% ~S"
914 (throw 'process-top-level-form-error-abort nil))))
917 ;; (There are no EVAL-WHEN issues in the ATOM case until
918 ;; SBCL gets smart enough to handle global
919 ;; DEFINE-SYMBOL-MACRO.)
920 (convert-and-maybe-compile form path)
921 (flet ((need-at-least-one-arg (form)
923 (compiler-error "~S form is too short: ~S"
927 ;; FIXME: It's not clear to me why we would want this
928 ;; special case; it might have been needed for some
929 ;; variation of the old GENESIS system, but it certainly
930 ;; doesn't seem to be needed for ours. Sometime after the
931 ;; system is running I'd like to remove it tentatively and
932 ;; see whether anything breaks, and if nothing does break,
933 ;; remove it permanently. (And if we *do* want special
934 ;; treatment of all these, we probably want to treat WARN
936 ((error cerror break signal)
937 (process-cold-load-form form path nil))
938 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
939 (need-at-least-one-arg form)
940 (destructuring-bind (special-operator magic &rest body) form
941 (ecase special-operator
943 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
944 ;; "3.2.3.1 Processing of Top Level Forms".
945 (multiple-value-bind (ct lt e)
946 (parse-eval-when-situations magic)
947 (let ((new-compile-time-too (or ct
948 (and compile-time-too
950 (cond (lt (process-top-level-progn
951 body path new-compile-time-too))
952 (new-compile-time-too (eval
953 `(progn ,@body)))))))
955 (funcall-in-macrolet-lexenv
958 (process-top-level-locally body
962 (funcall-in-symbol-macrolet-lexenv
965 (process-top-level-locally body
967 compile-time-too)))))))
969 (process-top-level-locally (rest form) path compile-time-too))
971 (process-top-level-progn (rest form) path compile-time-too))
972 ;; When we're cross-compiling, consider: what should we
973 ;; do when we hit e.g.
974 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
975 ;; (DEFUN FOO (X) (+ 7 X)))?
976 ;; DEFUN has a macro definition in the cross-compiler,
977 ;; and a different macro definition in the target
978 ;; compiler. The only sensible thing is to use the
979 ;; target compiler's macro definition, since the
980 ;; cross-compiler's macro is in general into target
981 ;; functions which can't meaningfully be executed at
982 ;; cross-compilation time. So make sure we do the EVAL
983 ;; here, before we macroexpand.
985 ;; Then things get even dicier with something like
986 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
987 ;; where we have to make sure that we don't uncross
988 ;; the SB!XC: prefix before we do EVAL, because otherwise
989 ;; we'd be trying to redefine the cross-compilation host's
992 ;; (Isn't it fun to cross-compile Common Lisp?:-)
995 (when compile-time-too
996 (eval form)) ; letting xc host EVAL do its own macroexpansion
997 (let* (;; (We uncross the operator name because things
998 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
999 ;; should be equivalent to their CL: counterparts
1000 ;; when being compiled as target code. We leave
1001 ;; the rest of the form uncrossed because macros
1002 ;; might yet expand into EVAL-WHEN stuff, and
1003 ;; things inside EVAL-WHEN can't be uncrossed
1004 ;; until after we've EVALed them in the
1005 ;; cross-compilation host.)
1006 (slightly-uncrossed (cons (uncross (first form))
1008 (expanded (preprocessor-macroexpand slightly-uncrossed)))
1009 (if (eq expanded slightly-uncrossed)
1010 ;; (Now that we're no longer processing toplevel
1011 ;; forms, and hence no longer need to worry about
1012 ;; EVAL-WHEN, we can uncross everything.)
1013 (convert-and-maybe-compile expanded path)
1014 ;; (We have to demote COMPILE-TIME-TOO to NIL
1015 ;; here, no matter what it was before, since
1016 ;; otherwise we'd tend to EVAL subforms more than
1017 ;; once, because of WHEN COMPILE-TIME-TOO form
1019 (process-top-level-form expanded path nil))))
1020 ;; When we're not cross-compiling, we only need to
1021 ;; macroexpand once, so we can follow the 1-thru-6
1022 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1023 ;; Top Level Forms".
1026 (let ((expanded (preprocessor-macroexpand form)))
1027 (cond ((eq expanded form)
1028 (when compile-time-too
1030 (convert-and-maybe-compile form path))
1032 (process-top-level-form expanded
1034 compile-time-too))))))))))
1038 ;;;; load time value support
1040 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1042 ;;; Return T if we are currently producing a fasl file and hence
1043 ;;; constants need to be dumped carefully.
1044 (defun producing-fasl-file ()
1045 (fasl-output-p *compile-object*))
1047 ;;; Compile FORM and arrange for it to be called at load-time. Return
1048 ;;; the dumper handle and our best guess at the type of the object.
1049 (defun compile-load-time-value
1051 (name (let ((*print-level* 2) (*print-length* 3))
1052 (format nil "load time value of ~S"
1053 (if (and (listp form)
1054 (eq (car form) 'make-value-cell))
1057 (let ((lambda (compile-load-time-stuff form name t)))
1059 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1060 (let ((type (leaf-type lambda)))
1061 (if (function-type-p type)
1062 (single-value-type (function-type-returns type))
1065 ;;; Compile the FORMS and arrange for them to be called (for effect,
1066 ;;; not value) at load time.
1067 (defun compile-make-load-form-init-forms (forms name)
1068 (let ((lambda (compile-load-time-stuff `(progn ,@forms) name nil)))
1069 (fasl-dump-top-level-lambda-call lambda *compile-object*)))
1071 ;;; Does the actual work of COMPILE-LOAD-TIME-VALUE or
1072 ;;; COMPILE-MAKE-LOAD-FORM- INIT-FORMS.
1073 (defun compile-load-time-stuff (form name for-value)
1075 (let* ((*lexenv* (make-null-lexenv))
1076 (lambda (ir1-top-level form *current-path* for-value)))
1077 (setf (leaf-name lambda) name)
1078 (compile-top-level (list lambda) t)
1081 ;;; Called by COMPILE-TOP-LEVEL when it was pased T for
1082 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1083 ;;; don't try to combine this component with anything else and frob
1084 ;;; the name. If not in a :TOP-LEVEL component, then don't bother
1085 ;;; compiling, because it was merged with a run-time component.
1086 (defun compile-load-time-value-lambda (lambdas)
1087 (aver (null (cdr lambdas)))
1088 (let* ((lambda (car lambdas))
1089 (component (block-component (node-block (lambda-bind lambda)))))
1090 (when (eq (component-kind component) :top-level)
1091 (setf (component-name component) (leaf-name lambda))
1092 (compile-component component)
1093 (clear-ir1-info component))))
1097 ;;; We build a list of top-level lambdas, and then periodically smash
1098 ;;; them together into a single component and compile it.
1099 (defvar *pending-top-level-lambdas*)
1101 ;;; The maximum number of top-level lambdas we put in a single
1102 ;;; top-level component.
1104 ;;; CMU CL 18b used this nontrivially by default (setting it to 10)
1105 ;;; but consequently suffered from the inability to execute some
1106 ;;; troublesome constructs correctly, e.g. inability to load a fasl
1107 ;;; file compiled from the source file
1108 ;;; (defpackage "FOO" (:use "CL"))
1109 ;;; (print 'foo::bar)
1110 ;;; because it would dump data-setup fops (including a FOP-PACKAGE for
1111 ;;; "FOO") for the second form before dumping the the code in the
1112 ;;; first form, or the fop to execute the code in the first form. By
1113 ;;; setting this value to 0 by default, we avoid this badness. This
1114 ;;; increases the number of toplevel form functions, and so increases
1115 ;;; the size of object files.
1117 ;;; The variable is still supported because when we are compiling the
1118 ;;; SBCL system itself, which is known not contain any troublesome
1119 ;;; constructs, we can set it to a nonzero value, which reduces the
1120 ;;; number of toplevel form objects, reducing the peak memory usage in
1121 ;;; GENESIS, which is desirable, since at least for SBCL version
1122 ;;; 0.6.7, this is the high water mark for memory usage during system
1124 (defparameter *top-level-lambda-max* 0)
1126 (defun object-call-top-level-lambda (tll)
1127 (declare (type functional tll))
1128 (let ((object *compile-object*))
1131 (fasl-dump-top-level-lambda-call tll object))
1133 (core-call-top-level-lambda tll object))
1136 ;;; Add LAMBDAS to the pending lambdas. If this leaves more than
1137 ;;; *TOP-LEVEL-LAMBDA-MAX* lambdas in the list, or if FORCE-P is true,
1138 ;;; then smash the lambdas into a single component, compile it, and
1139 ;;; call the resulting function.
1140 (defun sub-compile-top-level-lambdas (lambdas force-p)
1141 (declare (list lambdas))
1142 (setq *pending-top-level-lambdas*
1143 (append *pending-top-level-lambdas* lambdas))
1144 (let ((pending *pending-top-level-lambdas*))
1146 (or (> (length pending) *top-level-lambda-max*)
1148 (multiple-value-bind (component tll) (merge-top-level-lambdas pending)
1149 (setq *pending-top-level-lambdas* ())
1150 (let ((*byte-compile* (if (eq *byte-compile* :maybe)
1151 *byte-compile-top-level*
1153 (compile-component component))
1154 (clear-ir1-info component)
1155 (object-call-top-level-lambda tll))))
1158 ;;; Compile top-level code and call the top-level lambdas. We pick off
1159 ;;; top-level lambdas in non-top-level components here, calling
1160 ;;; SUB-c-t-l-l on each subsequence of normal top-level lambdas.
1161 (defun compile-top-level-lambdas (lambdas force-p)
1162 (declare (list lambdas))
1163 (let ((len (length lambdas)))
1164 (flet ((loser (start)
1165 (or (position-if (lambda (x)
1166 (not (eq (component-kind
1174 (do* ((start 0 (1+ loser))
1175 (loser (loser start) (loser start)))
1178 (sub-compile-top-level-lambdas nil t)))
1179 (sub-compile-top-level-lambdas (subseq lambdas start loser)
1180 (or force-p (/= loser len)))
1181 (unless (= loser len)
1182 (object-call-top-level-lambda (elt lambdas loser))))))
1185 ;;; Compile LAMBDAS (a list of the lambdas for top-level forms) into
1186 ;;; the object file. We loop doing local call analysis until it
1187 ;;; converges, since a single pass might miss something due to
1188 ;;; components being joined by LET conversion.
1190 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1191 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1192 (defun compile-top-level (lambdas load-time-value-p)
1193 (declare (list lambdas))
1194 (maybe-mumble "locall ")
1196 (let ((did-something nil))
1197 (dolist (lambda lambdas)
1198 (let* ((component (block-component (node-block (lambda-bind lambda))))
1199 (*all-components* (list component)))
1200 (when (component-new-functions component)
1201 (setq did-something t)
1202 (local-call-analyze component))))
1203 (unless did-something (return))))
1205 (maybe-mumble "IDFO ")
1206 (multiple-value-bind (components top-components hairy-top)
1207 (find-initial-dfo lambdas)
1208 (let ((*all-components* (append components top-components))
1209 (top-level-closure nil))
1210 (when *check-consistency*
1211 (maybe-mumble "[check]~%")
1212 (check-ir1-consistency *all-components*))
1214 (dolist (component (append hairy-top top-components))
1215 (when (pre-environment-analyze-top-level component)
1216 (setq top-level-closure t)))
1218 (let ((*byte-compile*
1219 (if (and top-level-closure (eq *byte-compile* :maybe))
1222 (dolist (component components)
1223 (compile-component component)
1224 (when (replace-top-level-xeps component)
1225 (setq top-level-closure t)))
1227 (when *check-consistency*
1228 (maybe-mumble "[check]~%")
1229 (check-ir1-consistency *all-components*))
1231 (if load-time-value-p
1232 (compile-load-time-value-lambda lambdas)
1233 (compile-top-level-lambdas lambdas top-level-closure)))
1235 (dolist (component components)
1236 (clear-ir1-info component))
1240 ;;; Actually compile any stuff that has been queued up for block
1242 (defun finish-block-compilation ()
1243 (when *block-compile*
1244 (when *top-level-lambdas*
1245 (compile-top-level (nreverse *top-level-lambdas*) nil)
1246 (setq *top-level-lambdas* ()))
1247 (setq *block-compile* nil)
1248 (setq *entry-points* nil)))
1250 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1251 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1252 (defun sub-compile-file (info)
1253 (declare (type source-info info))
1254 (let* (;; These are bound in WITH-COMPILATION-UNIT now. -- WHN 20000308
1255 #+nil (*compiler-error-count* 0)
1256 #+nil (*compiler-warning-count* 0)
1257 #+nil (*compiler-style-warning-count* 0)
1258 #+nil (*compiler-note-count* 0)
1259 (*block-compile* *block-compile-argument*)
1260 (*package* (sane-package))
1262 (*lexenv* (make-null-lexenv))
1263 (*source-info* info)
1264 (sb!xc:*compile-file-pathname* nil)
1265 (sb!xc:*compile-file-truename* nil)
1266 (*top-level-lambdas* ())
1267 (*pending-top-level-lambdas* ())
1268 (*compiler-error-bailout*
1270 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1271 (return-from sub-compile-file (values nil t t))))
1272 (*current-path* nil)
1273 (*last-source-context* nil)
1274 (*last-original-source* nil)
1275 (*last-source-form* nil)
1276 (*last-format-string* nil)
1277 (*last-format-args* nil)
1278 (*last-message-count* 0)
1279 ;; FIXME: Do we need this rebinding here? It's a literal
1280 ;; translation of the old CMU CL rebinding to
1281 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1282 ;; and it's not obvious whether the rebinding to itself is
1283 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1284 (*info-environment* *info-environment*)
1285 (*gensym-counter* 0))
1287 (with-compilation-values
1288 (sb!xc:with-compilation-unit ()
1291 (sub-sub-compile-file info)
1293 (finish-block-compilation)
1294 (compile-top-level-lambdas () t)
1295 (let ((object *compile-object*))
1297 (fasl-output (fasl-dump-source-info info object))
1298 (core-object (fix-core-source-info info object))
1301 ;; Some errors are sufficiently bewildering that we just fail
1302 ;; immediately, without trying to recover and compile more of
1304 (input-error-in-compile-file (condition)
1305 (format *error-output*
1306 "~@<compilation aborted because of input error: ~2I~_~A~:>"
1308 (values nil t t)))))
1310 ;;; Return a pathname for the named file. The file must exist.
1311 (defun verify-source-file (pathname-designator)
1312 (let* ((pathname (pathname pathname-designator))
1313 (default-host (make-pathname :host (pathname-host pathname))))
1314 (flet ((try-with-type (path type error-p)
1315 (let ((new (merge-pathnames
1316 path (make-pathname :type type
1317 :defaults default-host))))
1318 (if (probe-file new)
1320 (and error-p (truename new))))))
1321 (cond ((typep pathname 'logical-pathname)
1322 (try-with-type pathname "LISP" t))
1323 ((probe-file pathname) pathname)
1324 ((try-with-type pathname "lisp" nil))
1325 ((try-with-type pathname "lisp" t))))))
1327 (defun elapsed-time-to-string (tsec)
1328 (multiple-value-bind (tmin sec) (truncate tsec 60)
1329 (multiple-value-bind (thr min) (truncate tmin 60)
1330 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1332 ;;; Print some junk at the beginning and end of compilation.
1333 (defun start-error-output (source-info)
1334 (declare (type source-info source-info))
1335 (let ((file-info (source-info-file-info source-info)))
1336 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1337 (namestring (file-info-name file-info))
1338 (sb!int:format-universal-time nil
1339 (file-info-write-date
1343 :print-timezone nil)))
1345 (defun finish-error-output (source-info won)
1346 (declare (type source-info source-info))
1347 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1349 (elapsed-time-to-string
1350 (- (get-universal-time)
1351 (source-info-start-time source-info))))
1354 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1355 ;;; out of the compile, then abort the writing of the output file, so
1356 ;;; that we don't overwrite it with known garbage.
1357 (defun sb!xc:compile-file
1362 (output-file (cfp-output-file-default input-file))
1363 ;; FIXME: ANSI doesn't seem to say anything about
1364 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1366 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1367 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1368 (external-format :default)
1372 ((:block-compile *block-compile-argument*) nil)
1373 ((:byte-compile *byte-compile*) *byte-compile-default*))
1376 "Compile INPUT-FILE, producing a corresponding fasl file and returning
1377 its filename. Besides the ANSI &KEY arguments :OUTPUT-FILE, :VERBOSE,
1378 :PRINT, and :EXTERNAL-FORMAT,the following extensions are supported:
1380 If given, internal data structures are dumped to the specified
1381 file, or if a value of T is given, to a file of *.trace type
1382 derived from the input file name.
1383 :BYTE-COMPILE {T | NIL | :MAYBE}
1384 Determines whether to compile into interpreted byte code instead of
1385 machine instructions. Byte code is several times smaller, but much
1386 slower. If :MAYBE, then only byte-compile when SPEED is 0 and
1387 DEBUG <= 1. The default is the value of SB-EXT:*BYTE-COMPILE-DEFAULT*,
1388 which is initially :MAYBE. (This option will probably become
1389 formally deprecated starting around sbcl-0.7.0, when various
1390 cleanups related to the byte interpreter are planned.)
1391 Also, as a workaround for vaguely-non-ANSI behavior, the :BLOCK-COMPILE
1392 argument is quasi-supported, to determine whether multiple
1393 functions are compiled together as a unit, resolving function
1394 references at compile time. NIL means that global function names
1395 are never resolved at compilation time. Currently NIL is the
1396 default behavior, because although section 3.2.2.3, \"Semantic
1397 Constraints\", of the ANSI spec allows this behavior under all
1398 circumstances, the compiler's runtime scales badly when it
1399 tries to do this for large files. If/when this performance
1400 problem is fixed, the block compilation default behavior will
1401 probably be made dependent on the SPEED and COMPILATION-SPEED
1402 optimization values, and the :BLOCK-COMPILE argument will probably
1405 (unless (eq external-format :default)
1406 (error "Non-:DEFAULT EXTERNAL-FORMAT values are not supported."))
1407 (let* ((fasl-output nil)
1408 (output-file-name nil)
1411 (failure-p t) ; T in case error keeps this from being set later
1412 (input-pathname (verify-source-file input-file))
1413 (source-info (make-file-source-info input-pathname))
1414 (*compiler-trace-output* nil)) ; might be modified below
1419 (setq output-file-name
1420 (sb!xc:compile-file-pathname input-file
1421 :output-file output-file))
1423 (open-fasl-output output-file-name
1424 (namestring input-pathname)
1425 (eq *byte-compile* t))))
1427 (let* ((default-trace-file-pathname
1428 (make-pathname :type "trace" :defaults input-pathname))
1429 (trace-file-pathname
1430 (if (eql trace-file t)
1431 default-trace-file-pathname
1432 (merge-pathnames trace-file
1433 default-trace-file-pathname))))
1434 (setf *compiler-trace-output*
1435 (open trace-file-pathname
1436 :if-exists :supersede
1437 :direction :output))))
1439 (when sb!xc:*compile-verbose*
1440 (start-error-output source-info))
1441 (let ((*compile-object* fasl-output)
1443 (multiple-value-setq (dummy warnings-p failure-p)
1444 (sub-compile-file source-info)))
1445 (setq compile-won t))
1447 (close-source-info source-info)
1450 (close-fasl-output fasl-output (not compile-won))
1451 (setq output-file-name
1452 (pathname (fasl-output-stream fasl-output)))
1453 (when (and compile-won sb!xc:*compile-verbose*)
1454 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1456 (when sb!xc:*compile-verbose*
1457 (finish-error-output source-info compile-won))
1459 (when *compiler-trace-output*
1460 (close *compiler-trace-output*)))
1462 (values (if output-file
1463 ;; Hack around filesystem race condition...
1464 (or (probe-file output-file-name) output-file-name)
1469 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1470 ;;; the OUTPUT-FILE argument
1472 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1473 ;;; that results from merging the INPUT-FILE with the value of
1474 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1475 ;;; default to the appropriate implementation-defined default type for
1477 (defun cfp-output-file-default (input-file)
1478 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1479 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1482 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1483 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1484 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1485 ;;; pathname, it is translated into a physical pathname as if by
1486 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1487 ;;; So I haven't really tried to make this precisely ANSI-compatible
1488 ;;; at the level of e.g. whether it returns logical pathname or a
1489 ;;; physical pathname. Patches to make it more correct are welcome.
1490 ;;; -- WHN 2000-12-09
1491 (defun sb!xc:compile-file-pathname (input-file
1493 (output-file (cfp-output-file-default
1497 "Return a pathname describing what file COMPILE-FILE would write to given
1499 (pathname output-file))
1501 ;;;; MAKE-LOAD-FORM stuff
1503 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1504 ;;; finds a constant structure, it invokes this to arrange for proper
1505 ;;; dumping. If it turns out that the constant has already been
1506 ;;; dumped, then we don't need to do anything.
1508 ;;; If the constant hasn't been dumped, then we check to see whether
1509 ;;; we are in the process of creating it. We detect this by
1510 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1511 ;;; the constants we are in the process of creating. Actually, each
1512 ;;; entry is a list of the constant and any init forms that need to be
1513 ;;; processed on behalf of that constant.
1515 ;;; It's not necessarily an error for this to happen. If we are
1516 ;;; processing the init form for some object that showed up *after*
1517 ;;; the original reference to this constant, then we just need to
1518 ;;; defer the processing of that init form. To detect this, we
1519 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1520 ;;; constants created since the last time we started processing an
1521 ;;; init form. If the constant passed to emit-make-load-form shows up
1522 ;;; in this list, then there is a circular chain through creation
1523 ;;; forms, which is an error.
1525 ;;; If there is some intervening init form, then we blow out of
1526 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1527 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1528 ;;; offending init form can be tacked onto the init forms for the
1529 ;;; circular object.
1531 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1532 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1533 ;;; whether the creation form is the magic value
1534 ;;; :JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1535 ;;; dumper will eventually get its hands on the object and use the
1536 ;;; normal structure dumping noise on it.
1538 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1539 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1540 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1541 ;;; dumper to use that result instead whenever it sees this constant.
1543 ;;; Now we try to compile the init form. We bind
1544 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1545 ;;; form (and any init forms that were added because of circularity
1546 ;;; detection). If this works, great. If not, we add the init forms to
1547 ;;; the init forms for the object that caused the problems and let it
1549 (defvar *constants-being-created* nil)
1550 (defvar *constants-created-since-last-init* nil)
1551 ;;; FIXME: Shouldn't these^ variables be bound in LET forms?
1552 (defun emit-make-load-form (constant)
1553 (aver (fasl-output-p *compile-object*))
1554 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1555 ;; KLUDGE: This special hack is because I was too lazy
1556 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1557 ;; function of LAYOUT returns nontrivial forms when
1558 ;; building the cross-compiler but :IGNORE-IT when
1559 ;; cross-compiling or running under the target Lisp. --
1561 #+sb-xc-host (typep constant 'layout))
1562 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1564 (when (find constant *constants-created-since-last-init* :test #'eq)
1566 (throw 'pending-init circular-ref)))
1567 (multiple-value-bind (creation-form init-form)
1569 (sb!xc:make-load-form constant (make-null-lexenv))
1571 (compiler-error "(while making load form for ~S)~%~A"
1575 (:just-dump-it-normally
1576 (fasl-validate-structure constant *compile-object*)
1581 (compile-top-level-lambdas () t)
1582 (when (fasl-constant-already-dumped-p constant *compile-object*)
1583 (return-from emit-make-load-form nil))
1584 (let* ((name (let ((*print-level* 1) (*print-length* 2))
1585 (with-output-to-string (stream)
1586 (write constant :stream stream))))
1588 (list constant name init-form)
1590 (let ((*constants-being-created*
1591 (cons info *constants-being-created*))
1592 (*constants-created-since-last-init*
1593 (cons constant *constants-created-since-last-init*)))
1596 (fasl-note-handle-for-constant
1598 (compile-load-time-value
1600 (format nil "creation form for ~A" name))
1603 (compiler-error "circular references in creation form for ~S"
1606 (let* ((*constants-created-since-last-init* nil)
1608 (catch 'pending-init
1609 (loop for (name form) on (cdr info) by #'cddr
1610 collect name into names
1611 collect form into forms
1613 (compile-make-load-form-init-forms
1615 (format nil "init form~:[~;s~] for ~{~A~^, ~}"
1616 (cdr forms) names)))
1619 (setf (cdr circular-ref)
1620 (append (cdr circular-ref) (cdr info))))))))))))