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 (defconstant sb!xc:call-arguments-limit most-positive-fixnum
18 "The exclusive upper bound on the number of arguments which may be passed
19 to a function, including &REST args.")
20 (defconstant sb!xc:lambda-parameters-limit most-positive-fixnum
22 "The exclusive upper bound on the number of parameters which may be specifed
23 in a given lambda list. This is actually the limit on required and &OPTIONAL
24 parameters. With &KEY and &AUX you can get more.")
25 (defconstant sb!xc:multiple-values-limit most-positive-fixnum
27 "The exclusive upper bound on the number of multiple VALUES that you can
30 ;;; FIXME: Doesn't this belong somewhere else, like early-c.lisp?
31 (declaim (special *constants* *free-variables* *component-being-compiled*
32 *code-vector* *next-location* *result-fixups*
33 *free-functions* *source-paths*
34 *seen-blocks* *seen-functions* *list-conflicts-table*
35 *continuation-number* *continuation-numbers*
36 *number-continuations* *tn-id* *tn-ids* *id-tns*
37 *label-ids* *label-id* *id-labels*
38 *undefined-warnings* *compiler-error-count*
39 *compiler-warning-count* *compiler-style-warning-count*
41 *compiler-error-bailout*
42 #!+sb-show *compiler-trace-output*
43 *last-source-context* *last-original-source*
44 *last-source-form* *last-format-string* *last-format-args*
45 *last-message-count* *lexenv*))
47 (defvar *byte-compile-default* :maybe
49 "the default value for the :BYTE-COMPILE argument to COMPILE-FILE")
51 (defvar *byte-compile-top-level*
53 #+sb-xc-host nil ; since the byte compiler isn't supported in cross-compiler
55 "Similar to *BYTE-COMPILE-DEFAULT*, but controls the compilation of top-level
56 forms (evaluated at load-time) when the :BYTE-COMPILE argument is :MAYBE
57 (the default.) When true, we decide to byte-compile.")
59 ;;; the value of the :BYTE-COMPILE argument which was passed to the
61 (defvar *byte-compile* :maybe)
63 ;;; Bound by COMPILE-COMPONENT to T when byte-compiling, and NIL when
64 ;;; native compiling. During IR1 conversion this can also be :MAYBE,
65 ;;; in which case we must look at the policy, see (byte-compiling).
66 (defvar *byte-compiling* :maybe)
67 (declaim (type (member t nil :maybe) *byte-compile* *byte-compiling*
68 *byte-compile-default*))
70 (defvar *check-consistency* nil)
71 (defvar *all-components*)
73 ;;; Bind this to a stream to capture various internal debugging output.
74 (defvar *compiler-trace-output* nil)
76 ;;; The current block compilation state. These are initialized to the
77 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
80 ;;; *BLOCK-COMPILE-ARGUMENT* holds the original value of the
81 ;;; :BLOCK-COMPILE argument, which overrides any internal
83 (defvar *block-compile*)
84 (defvar *block-compile-argument*)
85 (declaim (type (member nil t :specified)
86 *block-compile* *block-compile-argument*))
87 (defvar *entry-points*)
88 (declaim (list *entry-points*))
90 ;;; When block compiling, used by PROCESS-FORM to accumulate top-level
91 ;;; lambdas resulting from compiling subforms. (In reverse order.)
92 (defvar *top-level-lambdas*)
93 (declaim (list *top-level-lambdas*))
95 (defvar sb!xc:*compile-verbose* t
97 "The default for the :VERBOSE argument to COMPILE-FILE.")
98 (defvar sb!xc:*compile-print* t
100 "The default for the :PRINT argument to COMPILE-FILE.")
101 (defvar *compile-progress* nil
103 "When this is true, the compiler prints to *ERROR-OUTPUT* progress
104 information about the phases of compilation of each function. (This
105 is useful mainly in large block compilations.)")
107 (defvar sb!xc:*compile-file-pathname* nil
109 "The defaulted pathname of the file currently being compiled, or NIL if not
111 (defvar sb!xc:*compile-file-truename* nil
113 "The TRUENAME of the file currently being compiled, or NIL if not
116 (declaim (type (or pathname null)
117 sb!xc:*compile-file-pathname*
118 sb!xc:*compile-file-truename*))
120 ;;; the SOURCE-INFO structure for the current compilation. This is
121 ;;; null globally to indicate that we aren't currently in any
122 ;;; identifiable compilation.
123 (defvar *source-info* nil)
125 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
126 ;;; normally causes nested uses to be no-ops).
127 (defvar *in-compilation-unit* nil)
129 ;;; Count of the number of compilation units dynamically enclosed by
130 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
131 (defvar *aborted-compilation-unit-count*)
133 ;;; Mumble conditional on *COMPILE-PROGRESS*.
134 (defun maybe-mumble (&rest foo)
135 (when *compile-progress*
136 (compiler-mumble "~&")
137 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
138 (apply #'compiler-mumble foo))))
140 (deftype object () '(or fasl-output core-object null))
142 (defvar *compile-object* nil)
143 (declaim (type object *compile-object*))
145 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
147 (defmacro sb!xc:with-compilation-unit (options &body body)
149 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
150 This form affects compilations that take place within its dynamic extent. It
151 is intended to be wrapped around the compilation of all files in the same
152 system. These keywords are defined:
153 :OVERRIDE Boolean-Form
154 One of the effects of this form is to delay undefined warnings
155 until the end of the form, instead of giving them at the end of each
156 compilation. If OVERRIDE is NIL (the default), then the outermost
157 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
158 OVERRIDE true causes that form to grab any enclosed warnings, even if
159 it is enclosed by another WITH-COMPILATION-UNIT."
160 `(%with-compilation-unit (lambda () ,@body) ,@options))
162 (defun %with-compilation-unit (fn &key override)
163 (let ((succeeded-p nil))
164 (if (and *in-compilation-unit* (not override))
165 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
166 ;; ordinarily (unless OVERRIDE) basically a no-op.
168 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
170 (incf *aborted-compilation-unit-count*)))
171 ;; FIXME: Now *COMPILER-FOO-COUNT* stuff is bound in more than
172 ;; one place. If we can get rid of the IR1 interpreter, this
173 ;; should be easier to clean up.
174 (let ((*aborted-compilation-unit-count* 0)
175 (*compiler-error-count* 0)
176 (*compiler-warning-count* 0)
177 (*compiler-style-warning-count* 0)
178 (*compiler-note-count* 0)
179 (*undefined-warnings* nil)
180 (*in-compilation-unit* t))
181 (handler-bind ((parse-unknown-type
183 (note-undefined-reference
184 (parse-unknown-type-specifier c)
187 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
189 (incf *aborted-compilation-unit-count*))
190 (summarize-compilation-unit (not succeeded-p))))))))
192 ;;; This is to be called at the end of a compilation unit. It signals
193 ;;; any residual warnings about unknown stuff, then prints the total
194 ;;; error counts. ABORT-P should be true when the compilation unit was
195 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
196 ;;; enclosed nested compilation units that were aborted.
197 (defun summarize-compilation-unit (abort-p)
199 (handler-bind ((style-warning #'compiler-style-warning-handler)
200 (warning #'compiler-warning-handler))
202 (let ((undefs (sort *undefined-warnings* #'string<
204 (let ((x (undefined-warning-name x)))
207 (prin1-to-string x)))))))
208 (unless *converting-for-interpreter*
209 (dolist (undef undefs)
210 (let ((name (undefined-warning-name undef))
211 (kind (undefined-warning-kind undef))
212 (warnings (undefined-warning-warnings undef))
213 (undefined-warning-count (undefined-warning-count undef)))
214 (dolist (*compiler-error-context* warnings)
215 (compiler-style-warning "undefined ~(~A~): ~S" kind name))
217 (let ((warn-count (length warnings)))
218 (when (and warnings (> undefined-warning-count warn-count))
219 (let ((more (- undefined-warning-count warn-count)))
220 (compiler-style-warning
221 "~D more use~:P of undefined ~(~A~) ~S"
222 more kind name)))))))
224 (dolist (kind '(:variable :function :type))
225 (let ((summary (mapcar #'undefined-warning-name
226 (remove kind undefs :test-not #'eq
227 :key #'undefined-warning-kind))))
229 (compiler-style-warning
230 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
231 ~% ~{~<~% ~1:;~S~>~^ ~}"
232 (cdr summary) kind summary)))))))
234 (unless (or *converting-for-interpreter*
236 (zerop *aborted-compilation-unit-count*)
237 (zerop *compiler-error-count*)
238 (zerop *compiler-warning-count*)
239 (zerop *compiler-style-warning-count*)
240 (zerop *compiler-note-count*)))
241 (format *error-output* "~&")
242 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
243 (compiler-mumble "compilation unit ~:[finished~;aborted~]~
244 ~[~:;~:*~& caught ~D fatal ERROR condition~:P~]~
245 ~[~:;~:*~& caught ~D ERROR condition~:P~]~
246 ~[~:;~:*~& caught ~D WARNING condition~:P~]~
247 ~[~:;~:*~& caught ~D STYLE-WARNING condition~:P~]~
248 ~[~:;~:*~& printed ~D note~:P~]"
250 *aborted-compilation-unit-count*
251 *compiler-error-count*
252 *compiler-warning-count*
253 *compiler-style-warning-count*
254 *compiler-note-count*)))
255 (format *error-output* "~&"))
257 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
258 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
259 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
260 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
261 (defmacro with-compilation-values (&body body)
263 (let ((*warnings-p* nil)
265 (values (progn ,@body)
269 ;;;; component compilation
271 (defparameter *max-optimize-iterations* 3 ; ARB
273 "The upper limit on the number of times that we will consecutively do IR1
274 optimization that doesn't introduce any new code. A finite limit is
275 necessary, since type inference may take arbitrarily long to converge.")
277 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
278 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
280 ;;; Repeatedly optimize COMPONENT until no further optimizations can
281 ;;; be found or we hit our iteration limit. When we hit the limit, we
282 ;;; clear the component and block REOPTIMIZE flags to discourage the
283 ;;; next optimization attempt from pounding on the same code.
284 (defun ir1-optimize-until-done (component)
285 (declare (type component component))
287 (event ir1-optimize-until-done)
289 (cleared-reanalyze nil))
291 (when (component-reanalyze component)
293 (setq cleared-reanalyze t)
294 (setf (component-reanalyze component) nil))
295 (setf (component-reoptimize component) nil)
296 (ir1-optimize component)
297 (cond ((component-reoptimize component)
299 (when (= count *max-optimize-iterations*)
301 (cond ((retry-delayed-ir1-transforms :optimize)
305 (event ir1-optimize-maxed-out)
306 (setf (component-reoptimize component) nil)
307 (do-blocks (block component)
308 (setf (block-reoptimize block) nil))
310 ((retry-delayed-ir1-transforms :optimize)
317 (when cleared-reanalyze
318 (setf (component-reanalyze component) t)))
321 (defparameter *constraint-propagate* t)
323 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
324 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
325 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
326 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
328 ;;; FIXME: It would be good to document why it's important to have a
329 ;;; large value here, and what the drawbacks of an excessively large
330 ;;; value are; and it might also be good to make it depend on
331 ;;; optimization policy.
332 (defparameter *reoptimize-after-type-check-max* 10)
334 (defevent reoptimize-maxed-out
335 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
337 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
338 (defun dfo-as-needed (component)
339 (declare (type component component))
340 (when (component-reanalyze component)
344 (unless (component-reanalyze component)
350 ;;; Do all the IR1 phases for a non-top-level component.
351 (defun ir1-phases (component)
352 (declare (type component component))
353 (let ((*constraint-number* 0)
355 (*delayed-ir1-transforms* nil))
356 (declare (special *constraint-number* *delayed-ir1-transforms*))
358 (ir1-optimize-until-done component)
359 (when (or (component-new-functions component)
360 (component-reanalyze-functions component))
361 (maybe-mumble "locall ")
362 (local-call-analyze component))
363 (dfo-as-needed component)
364 (when *constraint-propagate*
365 (maybe-mumble "constraint ")
366 (constraint-propagate component))
367 (when (retry-delayed-ir1-transforms :constraint)
368 (maybe-mumble "Rtran "))
369 ;; Delay the generation of type checks until the type
370 ;; constraints have had time to propagate, else the compiler can
372 (unless (and (or (component-reoptimize component)
373 (component-reanalyze component)
374 (component-new-functions component)
375 (component-reanalyze-functions component))
376 (< loop-count (- *reoptimize-after-type-check-max* 4)))
377 (maybe-mumble "type ")
378 (generate-type-checks component)
379 (unless (or (component-reoptimize component)
380 (component-reanalyze component)
381 (component-new-functions component)
382 (component-reanalyze-functions component))
384 (when (>= loop-count *reoptimize-after-type-check-max*)
385 (maybe-mumble "[reoptimize limit]")
386 (event reoptimize-maxed-out)
390 (ir1-finalize component)
393 (defun native-compile-component (component)
394 (let ((*code-segment* nil)
396 (maybe-mumble "GTN ")
397 (gtn-analyze component)
398 (maybe-mumble "LTN ")
399 (ltn-analyze component)
400 (dfo-as-needed component)
401 (maybe-mumble "control ")
402 (control-analyze component #'make-ir2-block)
404 (when (ir2-component-values-receivers (component-info component))
405 (maybe-mumble "stack ")
406 (stack-analyze component)
407 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
408 ;; stack analysis. There shouldn't be any unreachable code after
409 ;; control, so this won't delete anything.
410 (dfo-as-needed component))
414 (maybe-mumble "IR2tran ")
416 (entry-analyze component)
417 (ir2-convert component)
419 (when (policy *lexenv* (>= speed compilation-speed))
420 (maybe-mumble "copy ")
421 (copy-propagate component))
423 (select-representations component)
425 (when *check-consistency*
426 (maybe-mumble "check2 ")
427 (check-ir2-consistency component))
429 (delete-unreferenced-tns component)
431 (maybe-mumble "life ")
432 (lifetime-analyze component)
434 (when *compile-progress*
435 (compiler-mumble "") ; Sync before doing more output.
436 (pre-pack-tn-stats component *error-output*))
438 (when *check-consistency*
439 (maybe-mumble "check-life ")
440 (check-life-consistency component))
442 (maybe-mumble "pack ")
445 (when *check-consistency*
446 (maybe-mumble "check-pack ")
447 (check-pack-consistency component))
449 (when *compiler-trace-output*
450 (describe-component component *compiler-trace-output*)
451 (describe-ir2-component component *compiler-trace-output*))
453 (maybe-mumble "code ")
454 (multiple-value-bind (code-length trace-table fixups)
455 (generate-code component)
457 (when *compiler-trace-output*
458 (format *compiler-trace-output*
459 "~|~%disassembly of code for ~S~2%" component)
460 (sb!disassem:disassemble-assem-segment *code-segment*
461 *compiler-trace-output*))
463 (etypecase *compile-object*
465 (maybe-mumble "fasl")
466 (fasl-dump-component component
473 (maybe-mumble "core")
474 (make-core-component component
482 ;; We're done, so don't bother keeping anything around.
483 (setf (component-info component) nil)
487 (defun policy-byte-compile-p (thing)
492 ;;; Return our best guess for whether we will byte compile code
493 ;;; currently being IR1 converted. This is only a guess because the
494 ;;; decision is made on a per-component basis.
496 ;;; FIXME: This should be called something more mnemonic, e.g.
497 ;;; PROBABLY-BYTE-COMPILING
498 (defun byte-compiling ()
499 (if (eq *byte-compiling* :maybe)
500 (or (eq *byte-compile* t)
501 (policy-byte-compile-p *lexenv*))
502 (and *byte-compile* *byte-compiling*)))
504 ;;; Delete components with no external entry points before we try to
505 ;;; generate code. Unreachable closures can cause IR2 conversion to
506 ;;; puke on itself, since it is the reference to the closure which
507 ;;; normally causes the components to be combined.
509 ;;; FIXME: The original CMU CL comment said "This doesn't really cover
510 ;;; all cases..." That's a little scary.
511 (defun delete-if-no-entries (component)
512 (dolist (fun (component-lambdas component)
513 (delete-component component))
514 (case (functional-kind fun)
515 (:top-level (return))
517 (unless (every (lambda (ref)
518 (eq (block-component (node-block ref))
523 (defun byte-compile-this-component-p (component)
524 (ecase *byte-compile*
528 (every #'policy-byte-compile-p (component-lambdas component)))))
530 (defun compile-component (component)
531 (let* ((*component-being-compiled* component)
532 (*byte-compiling* (byte-compile-this-component-p component)))
533 (when sb!xc:*compile-print*
534 (compiler-mumble "~&; ~:[~;byte ~]compiling ~A: "
536 (component-name component)))
538 (ir1-phases component)
540 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
541 (maybe-mumble "env ")
542 (environment-analyze component)
543 (dfo-as-needed component)
545 (delete-if-no-entries component)
547 (unless (eq (block-next (component-head component))
548 (component-tail component))
550 (byte-compile-component component)
551 (native-compile-component component))))
553 (clear-constant-info)
555 (when sb!xc:*compile-print*
556 (compiler-mumble "~&"))
560 ;;;; clearing global data structures
562 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
563 ;;;; global data structures entirely when possible and consing up the
564 ;;;; others from scratch instead of clearing and reusing them?
566 ;;; Clear the INFO in constants in the *FREE-VARIABLES*, etc. In
567 ;;; addition to allowing stuff to be reclaimed, this is required for
568 ;;; correct assignment of constant offsets, since we need to assign a
569 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
570 ;;; slots, since they are used to keep track of functions across
571 ;;; component boundaries.
572 (defun clear-constant-info ()
573 (maphash #'(lambda (k v)
575 (setf (leaf-info v) nil))
577 (maphash #'(lambda (k v)
580 (setf (leaf-info v) nil)))
584 ;;; Blow away the REFS for all global variables, and let COMPONENT
586 (defun clear-ir1-info (component)
587 (declare (type component component))
589 (maphash #'(lambda (k v)
593 (delete-if #'here-p (leaf-refs v)))
594 (when (basic-var-p v)
595 (setf (basic-var-sets v)
596 (delete-if #'here-p (basic-var-sets v))))))
599 (eq (block-component (node-block x)) component)))
600 (blast *free-variables*)
601 (blast *free-functions*)
605 ;;; Clear global variables used by the compiler.
607 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
608 ;;; and it adds overhead even when people aren't using the compiler.
609 ;;; Perhaps we could make these global vars unbound except when
610 ;;; actually in use, so that this function could go away.
611 (defun clear-stuff (&optional (debug-too t))
613 ;; Clear global tables.
614 (when (boundp '*free-functions*)
615 (clrhash *free-functions*)
616 (clrhash *free-variables*)
617 (clrhash *constants*))
619 ;; Clear debug counters and tables.
620 (clrhash *seen-blocks*)
621 (clrhash *seen-functions*)
622 (clrhash *list-conflicts-table*)
625 (clrhash *continuation-numbers*)
626 (clrhash *number-continuations*)
627 (setq *continuation-number* 0)
631 (clrhash *label-ids*)
632 (clrhash *id-labels*)
635 ;; Clear some PACK data structures (for GC purposes only).
636 (aver (not *in-pack*))
637 (dolist (sb *backend-sb-list*)
638 (when (finite-sb-p sb)
639 (fill (finite-sb-live-tns sb) nil))))
641 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
642 ;; Superficially, this seemed harmful -- the user could reasonably be
643 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
644 ;; compiled. A closer inspection showed that this actually turned out to be
645 ;; harmless in practice, because CLEAR-STUFF was only called from within
646 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
647 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
648 ;; practice, it was also useless in practice. So we don't do it any more.)
654 ;;; Print out some useful info about Component to Stream.
655 (defun describe-component (component *standard-output*)
656 (declare (type component component))
657 (format t "~|~%;;;; component: ~S~2%" (component-name component))
658 (print-blocks component)
661 (defun describe-ir2-component (component *standard-output*)
662 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
663 (format t "entries:~%")
664 (dolist (entry (ir2-component-entries (component-info component)))
665 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
666 (label-id (entry-info-offset entry))
667 (entry-info-name entry)
668 (entry-info-closure-p entry)))
670 (pre-pack-tn-stats component *standard-output*)
672 (print-ir2-blocks component)
678 ;;;; When reading from a file, we have to keep track of some source
679 ;;;; information. We also exploit our ability to back up for printing
680 ;;;; the error context and for recovering from errors.
682 ;;;; The interface we provide to this stuff is the stream-oid
683 ;;;; Source-Info structure. The bookkeeping is done as a side-effect
684 ;;;; of getting the next source form.
686 ;;; A FILE-INFO structure holds all the source information for a
688 (defstruct (file-info (:copier nil))
689 ;; If a file, the truename of the corresponding source file. If from
690 ;; a Lisp form, :LISP. If from a stream, :STREAM.
691 (name (required-argument) :type (or pathname (member :lisp :stream)))
692 ;; the defaulted, but not necessarily absolute file name (i.e. prior
693 ;; to TRUENAME call.) Null if not a file. This is used to set
694 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
696 (untruename nil :type (or pathname null))
697 ;; the file's write date (if relevant)
698 (write-date nil :type (or unsigned-byte null))
699 ;; the source path root number of the first form in this file (i.e.
700 ;; the total number of forms converted previously in this
702 (source-root 0 :type unsigned-byte)
703 ;; parallel vectors containing the forms read out of the file and
704 ;; the file positions that reading of each form started at (i.e. the
705 ;; end of the previous form)
706 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
707 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
709 ;;; The SOURCE-INFO structure provides a handle on all the source
710 ;;; information for an entire compilation.
711 (defstruct (source-info
712 #-no-ansi-print-object
713 (:print-object (lambda (s stream)
714 (print-unreadable-object (s stream :type t))))
716 ;; the UT that compilation started at
717 (start-time (get-universal-time) :type unsigned-byte)
718 ;; the FILE-INFO structure for this compilation
719 (file-info nil :type (or file-info null))
720 ;; the stream that we are using to read the FILE-INFO, or NIL if
721 ;; no stream has been opened yet
722 (stream nil :type (or stream null)))
724 ;;; Given a pathname, return a SOURCE-INFO structure.
725 (defun make-file-source-info (file)
726 (let ((file-info (make-file-info :name (truename file)
728 :write-date (file-write-date file))))
730 (make-source-info :file-info file-info)))
732 ;;; Return a SOURCE-INFO to describe the incremental compilation of
733 ;;; FORM. Also used by SB!EVAL:INTERNAL-EVAL.
734 (defun make-lisp-source-info (form)
735 (make-source-info :start-time (get-universal-time)
736 :file-info (make-file-info :name :lisp
740 ;;; Return a SOURCE-INFO which will read from STREAM.
741 (defun make-stream-source-info (stream)
742 (let ((file-info (make-file-info :name :stream)))
743 (make-source-info :file-info file-info
746 ;;; Return a form read from STREAM; or for EOF, use the trick
747 ;;; popularized by Kent Pitman of returning STREAM itself. If an error
748 ;;; happens, then convert it to standard abort-the-compilation error
749 ;;; condition (possibly recording some extra location information).
750 (defun read-for-compile-file (stream position)
751 (handler-case (read stream nil stream)
752 (reader-error (condition)
753 (error 'input-error-in-compile-file
755 ;; We don't need to supply :POSITION here because
756 ;; READER-ERRORs already know their position in the file.
758 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
759 ;; (and that this is not a READER-ERROR) when it encounters end of
760 ;; file in the middle of something it's trying to read.
761 (end-of-file (condition)
762 (error 'input-error-in-compile-file
764 ;; We need to supply :POSITION here because the END-OF-FILE
765 ;; condition doesn't carry the position that the user
766 ;; probably cares about, where the failed READ began.
767 :position position))))
769 ;;; If STREAM is present, return it, otherwise open a stream to the
770 ;;; current file. There must be a current file.
772 ;;; FIXME: This is probably an unnecessarily roundabout way to do
773 ;;; things now that we process a single file in COMPILE-FILE (unlike
774 ;;; the old CMU CL code, which accepted multiple files). Also, the old
776 ;;; When we open a new file, we also reset *PACKAGE* and policy.
777 ;;; This gives the effect of rebinding around each file.
778 ;;; which doesn't seem to be true now. Check to make sure that if
779 ;;; such rebinding is necessary, it's still done somewhere.
780 (defun get-source-stream (info)
781 (declare (type source-info info))
782 (or (source-info-stream info)
783 (let* ((file-info (source-info-file-info info))
784 (name (file-info-name file-info)))
785 (setf sb!xc:*compile-file-truename* name
786 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
787 (source-info-stream info) (open name :direction :input)))))
789 ;;; Close the stream in INFO if it is open.
790 (defun close-source-info (info)
791 (declare (type source-info info))
792 (let ((stream (source-info-stream info)))
793 (when stream (close stream)))
794 (setf (source-info-stream info) nil)
797 ;;; Read the source file.
798 (defun process-source (info)
799 (let* ((file-info (source-info-file-info info))
800 (stream (get-source-stream info)))
802 (let* ((pos (file-position stream))
803 (form (read-for-compile-file stream pos)))
804 (if (eq form stream) ; i.e., if EOF
806 (let* ((forms (file-info-forms file-info))
807 (current-idx (+ (fill-pointer forms)
808 (file-info-source-root file-info))))
809 (vector-push-extend form forms)
810 (vector-push-extend pos (file-info-positions file-info))
811 (clrhash *source-paths*)
812 (find-source-paths form current-idx)
813 (process-top-level-form form
814 `(original-source-start 0
817 ;;; Return the INDEX'th source form read from INFO and the position
818 ;;; where it was read.
819 (defun find-source-root (index info)
820 (declare (type index index) (type source-info info))
821 (let ((file-info (source-info-file-info info)))
822 (values (aref (file-info-forms file-info) index)
823 (aref (file-info-positions file-info) index))))
825 ;;;; top-level form processing
827 ;;; This is called by top-level form processing when we are ready to
828 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
829 ;;; convert the form, but delay compilation, pushing the result on
830 ;;; *TOP-LEVEL-LAMBDAS* instead.
831 (defun convert-and-maybe-compile (form path)
832 (declare (list path))
833 (let* ((*lexenv* (make-lexenv :policy *policy*))
834 (tll (ir1-top-level form path nil)))
835 (cond ((eq *block-compile* t) (push tll *top-level-lambdas*))
836 (t (compile-top-level (list tll) nil)))))
838 ;;; Process a PROGN-like portion of a top-level form. Forms is a list of
839 ;;; the forms, and Path is source path of the form they came out of.
840 (defun process-top-level-progn (forms path)
841 (declare (list forms) (list path))
843 (process-top-level-form form path)))
845 ;;; Macroexpand form in the current environment with an error handler.
846 ;;; We only expand one level, so that we retain all the intervening
847 ;;; forms in the source path.
848 (defun preprocessor-macroexpand (form)
849 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
851 (compiler-error "(during macroexpansion)~%~A" condition))))
853 ;;; Process a top-level use of LOCALLY. We parse declarations and then
854 ;;; recursively process the body.
855 (defun process-top-level-locally (form path)
856 (declare (list path))
857 (multiple-value-bind (forms decls) (sb!sys:parse-body (cdr form) nil)
859 (process-decls decls nil nil (make-continuation)))
860 ;; Binding *POLICY* is pretty much of a hack, since it
861 ;; causes LOCALLY to "capture" enclosed proclamations. It
862 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
863 ;; value of *POLICY* as the policy. The need for this hack
864 ;; is due to the quirk that there is no way to represent in
865 ;; a POLICY that an optimize quality came from the default.
866 ;; FIXME: Ideally, something should be done so that DECLAIM
867 ;; inside LOCALLY works OK. Failing that, at least we could
868 ;; issue a warning instead of silently screwing up.
869 (*policy* (lexenv-policy *lexenv*)))
870 (process-top-level-progn forms path))))
872 ;;; Force any pending top-level forms to be compiled and dumped so
873 ;;; that they will be evaluated in the correct package environment.
874 ;;; Dump the form to be evaled at (cold) load time, and if EVAL is
875 ;;; true, eval the form immediately.
876 (defun process-cold-load-form (form path eval)
877 (let ((object *compile-object*))
880 (compile-top-level-lambdas () t)
881 (fasl-dump-cold-load-form form object))
882 ((or null core-object)
883 (convert-and-maybe-compile form path)))
887 ;;; Process a top-level FORM with the specified source PATH.
888 ;;; * If this is a magic top-level form, then do stuff.
889 ;;; * If this is a macro, then expand it.
890 ;;; * Otherwise, just compile it.
891 (defun process-top-level-form (form path)
893 (declare (list path))
895 (catch 'process-top-level-form-error-abort
896 (let* ((path (or (gethash form *source-paths*) (cons form path)))
897 (*compiler-error-bailout*
899 (convert-and-maybe-compile
900 `(error "execution of a form compiled with errors:~% ~S"
903 (throw 'process-top-level-form-error-abort nil))))
905 (convert-and-maybe-compile form path)
907 ;; FIXME: It's not clear to me why we would want this
908 ;; special case; it might have been needed for some
909 ;; variation of the old GENESIS system, but it certainly
910 ;; doesn't seem to be needed for ours. Sometime after the
911 ;; system is running I'd like to remove it tentatively and
912 ;; see whether anything breaks, and if nothing does break,
913 ;; remove it permanently. (And if we *do* want special
914 ;; treatment of all these, we probably want to treat WARN
916 ((error cerror break signal)
917 (process-cold-load-form form path nil))
918 ;; FIXME: ANSI seems to encourage things like DEFSTRUCT to
919 ;; be done with EVAL-WHEN, without this kind of one-off
921 (sb!kernel:%compiler-defstruct
922 (convert-and-maybe-compile form path)
923 (compile-top-level-lambdas () t))
925 (unless (>= (length form) 2)
926 (compiler-error "EVAL-WHEN form is too short: ~S" form))
928 (cadr form) (cddr form)
930 (process-top-level-progn forms path))))
932 (unless (>= (length form) 2)
933 (compiler-error "MACROLET form is too short: ~S" form))
937 (process-top-level-progn (cddr form) path))))
938 (locally (process-top-level-locally form path))
939 (progn (process-top-level-progn (cdr form) path))
941 (let* ((uform (uncross form))
942 (exp (preprocessor-macroexpand uform)))
944 (convert-and-maybe-compile uform path)
945 (process-top-level-form exp path))))))))
949 ;;;; load time value support
951 ;;;; (See EMIT-MAKE-LOAD-FORM.)
953 ;;; Returns T iff we are currently producing a fasl file and hence
954 ;;; constants need to be dumped carefully.
955 (defun producing-fasl-file ()
956 (unless *converting-for-interpreter*
957 (fasl-output-p *compile-object*)))
959 ;;; Compile FORM and arrange for it to be called at load-time. Return
960 ;;; the dumper handle and our best guess at the type of the object.
961 (defun compile-load-time-value
963 (name (let ((*print-level* 2) (*print-length* 3))
964 (format nil "load time value of ~S"
965 (if (and (listp form)
966 (eq (car form) 'make-value-cell))
969 (let ((lambda (compile-load-time-stuff form name t)))
971 (fasl-dump-load-time-value-lambda lambda *compile-object*)
972 (let ((type (leaf-type lambda)))
973 (if (function-type-p type)
974 (single-value-type (function-type-returns type))
977 ;;; Compile the FORMS and arrange for them to be called (for effect,
978 ;;; not value) at load time.
979 (defun compile-make-load-form-init-forms (forms name)
980 (let ((lambda (compile-load-time-stuff `(progn ,@forms) name nil)))
981 (fasl-dump-top-level-lambda-call lambda *compile-object*)))
983 ;;; Does the actual work of COMPILE-LOAD-TIME-VALUE or
984 ;;; COMPILE-MAKE-LOAD-FORM- INIT-FORMS.
985 (defun compile-load-time-stuff (form name for-value)
987 (let* ((*lexenv* (make-null-lexenv))
988 (lambda (ir1-top-level form *current-path* for-value)))
989 (setf (leaf-name lambda) name)
990 (compile-top-level (list lambda) t)
993 ;;; Called by COMPILE-TOP-LEVEL when it was pased T for
994 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
995 ;;; don't try to combine this component with anything else and frob
996 ;;; the name. If not in a :TOP-LEVEL component, then don't bother
997 ;;; compiling, because it was merged with a run-time component.
998 (defun compile-load-time-value-lambda (lambdas)
999 (aver (null (cdr lambdas)))
1000 (let* ((lambda (car lambdas))
1001 (component (block-component (node-block (lambda-bind lambda)))))
1002 (when (eq (component-kind component) :top-level)
1003 (setf (component-name component) (leaf-name lambda))
1004 (compile-component component)
1005 (clear-ir1-info component))))
1009 ;;; We build a list of top-level lambdas, and then periodically smash
1010 ;;; them together into a single component and compile it.
1011 (defvar *pending-top-level-lambdas*)
1013 ;;; The maximum number of top-level lambdas we put in a single
1014 ;;; top-level component.
1016 ;;; CMU CL 18b used this nontrivially by default (setting it to 10)
1017 ;;; but consequently suffered from the inability to execute some
1018 ;;; troublesome constructs correctly, e.g. inability to load a fasl
1019 ;;; file compiled from the source file
1020 ;;; (defpackage "FOO" (:use "CL"))
1021 ;;; (print 'foo::bar)
1022 ;;; because it would dump data-setup fops (including a FOP-PACKAGE for
1023 ;;; "FOO") for the second form before dumping the the code in the
1024 ;;; first form, or the fop to execute the code in the first form. By
1025 ;;; setting this value to 0 by default, we avoid this badness. This
1026 ;;; increases the number of toplevel form functions, and so increases
1027 ;;; the size of object files.
1029 ;;; The variable is still supported because when we are compiling the
1030 ;;; SBCL system itself, which is known not contain any troublesome
1031 ;;; constructs, we can set it to a nonzero value, which reduces the
1032 ;;; number of toplevel form objects, reducing the peak memory usage in
1033 ;;; GENESIS, which is desirable, since at least for SBCL version
1034 ;;; 0.6.7, this is the high water mark for memory usage during system
1036 (defparameter *top-level-lambda-max* 0)
1038 (defun object-call-top-level-lambda (tll)
1039 (declare (type functional tll))
1040 (let ((object *compile-object*))
1043 (fasl-dump-top-level-lambda-call tll object))
1045 (core-call-top-level-lambda tll object))
1048 ;;; Add LAMBDAS to the pending lambdas. If this leaves more than
1049 ;;; *TOP-LEVEL-LAMBDA-MAX* lambdas in the list, or if FORCE-P is true,
1050 ;;; then smash the lambdas into a single component, compile it, and
1051 ;;; call the resulting function.
1052 (defun sub-compile-top-level-lambdas (lambdas force-p)
1053 (declare (list lambdas))
1054 (setq *pending-top-level-lambdas*
1055 (append *pending-top-level-lambdas* lambdas))
1056 (let ((pending *pending-top-level-lambdas*))
1058 (or (> (length pending) *top-level-lambda-max*)
1060 (multiple-value-bind (component tll) (merge-top-level-lambdas pending)
1061 (setq *pending-top-level-lambdas* ())
1062 (let ((*byte-compile* (if (eq *byte-compile* :maybe)
1063 *byte-compile-top-level*
1065 (compile-component component))
1066 (clear-ir1-info component)
1067 (object-call-top-level-lambda tll))))
1070 ;;; Compile top-level code and call the top-level lambdas. We pick off
1071 ;;; top-level lambdas in non-top-level components here, calling
1072 ;;; SUB-c-t-l-l on each subsequence of normal top-level lambdas.
1073 (defun compile-top-level-lambdas (lambdas force-p)
1074 (declare (list lambdas))
1075 (let ((len (length lambdas)))
1076 (flet ((loser (start)
1077 (or (position-if #'(lambda (x)
1078 (not (eq (component-kind
1086 (do* ((start 0 (1+ loser))
1087 (loser (loser start) (loser start)))
1090 (sub-compile-top-level-lambdas nil t)))
1091 (sub-compile-top-level-lambdas (subseq lambdas start loser)
1092 (or force-p (/= loser len)))
1093 (unless (= loser len)
1094 (object-call-top-level-lambda (elt lambdas loser))))))
1097 ;;; Compile LAMBDAS (a list of the lambdas for top-level forms) into
1098 ;;; the object file. We loop doing local call analysis until it
1099 ;;; converges, since a single pass might miss something due to
1100 ;;; components being joined by LET conversion.
1102 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1103 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1104 (defun compile-top-level (lambdas load-time-value-p)
1105 (declare (list lambdas))
1106 (maybe-mumble "locall ")
1108 (let ((did-something nil))
1109 (dolist (lambda lambdas)
1110 (let* ((component (block-component (node-block (lambda-bind lambda))))
1111 (*all-components* (list component)))
1112 (when (component-new-functions component)
1113 (setq did-something t)
1114 (local-call-analyze component))))
1115 (unless did-something (return))))
1117 (maybe-mumble "IDFO ")
1118 (multiple-value-bind (components top-components hairy-top)
1119 (find-initial-dfo lambdas)
1120 (let ((*all-components* (append components top-components))
1121 (top-level-closure nil))
1122 (when *check-consistency*
1123 (maybe-mumble "[check]~%")
1124 (check-ir1-consistency *all-components*))
1126 (dolist (component (append hairy-top top-components))
1127 (when (pre-environment-analyze-top-level component)
1128 (setq top-level-closure t)))
1130 (let ((*byte-compile*
1131 (if (and top-level-closure (eq *byte-compile* :maybe))
1134 (dolist (component components)
1135 (compile-component component)
1136 (when (replace-top-level-xeps component)
1137 (setq top-level-closure t)))
1139 (when *check-consistency*
1140 (maybe-mumble "[check]~%")
1141 (check-ir1-consistency *all-components*))
1143 (if load-time-value-p
1144 (compile-load-time-value-lambda lambdas)
1145 (compile-top-level-lambdas lambdas top-level-closure)))
1147 (dolist (component components)
1148 (clear-ir1-info component))
1152 ;;; Actually compile any stuff that has been queued up for block
1154 (defun finish-block-compilation ()
1155 (when *block-compile*
1156 (when *top-level-lambdas*
1157 (compile-top-level (nreverse *top-level-lambdas*) nil)
1158 (setq *top-level-lambdas* ()))
1159 (setq *block-compile* nil)
1160 (setq *entry-points* nil)))
1162 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1163 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1164 (defun sub-compile-file (info)
1165 (declare (type source-info info))
1166 (let* (;; These are bound in WITH-COMPILATION-UNIT now. -- WHN 20000308
1167 #+nil (*compiler-error-count* 0)
1168 #+nil (*compiler-warning-count* 0)
1169 #+nil (*compiler-style-warning-count* 0)
1170 #+nil (*compiler-note-count* 0)
1171 (*block-compile* *block-compile-argument*)
1172 (*package* (sane-package))
1174 (*lexenv* (make-null-lexenv))
1175 (*converting-for-interpreter* nil)
1176 (*source-info* info)
1177 (sb!xc:*compile-file-pathname* nil)
1178 (sb!xc:*compile-file-truename* nil)
1179 (*top-level-lambdas* ())
1180 (*pending-top-level-lambdas* ())
1181 (*compiler-error-bailout*
1183 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1184 (return-from sub-compile-file (values nil t t))))
1185 (*current-path* nil)
1186 (*last-source-context* nil)
1187 (*last-original-source* nil)
1188 (*last-source-form* nil)
1189 (*last-format-string* nil)
1190 (*last-format-args* nil)
1191 (*last-message-count* 0)
1192 (*info-environment* (or *backend-info-environment*
1193 *info-environment*))
1194 (*gensym-counter* 0))
1196 (with-compilation-values
1197 (sb!xc:with-compilation-unit ()
1200 (process-source info)
1202 (finish-block-compilation)
1203 (compile-top-level-lambdas () t)
1204 (let ((object *compile-object*))
1206 (fasl-output (fasl-dump-source-info info object))
1207 (core-object (fix-core-source-info info object))
1210 ;; Some errors are sufficiently bewildering that we just fail
1211 ;; immediately, without trying to recover and compile more of
1213 (input-error-in-compile-file (condition)
1214 (format *error-output*
1215 "~@<compilation aborted because of input error: ~2I~_~A~:>"
1217 (values nil t t)))))
1219 ;;; Return a pathname for the named file. The file must exist.
1220 (defun verify-source-file (pathname-designator)
1221 (let* ((pathname (pathname pathname-designator))
1222 (default-host (make-pathname :host (pathname-host pathname))))
1223 (flet ((try-with-type (path type error-p)
1224 (let ((new (merge-pathnames
1225 path (make-pathname :type type
1226 :defaults default-host))))
1227 (if (probe-file new)
1229 (and error-p (truename new))))))
1230 (cond ((typep pathname 'logical-pathname)
1231 (try-with-type pathname "LISP" t))
1232 ((probe-file pathname) pathname)
1233 ((try-with-type pathname "lisp" nil))
1234 ((try-with-type pathname "lisp" t))))))
1236 (defun elapsed-time-to-string (tsec)
1237 (multiple-value-bind (tmin sec) (truncate tsec 60)
1238 (multiple-value-bind (thr min) (truncate tmin 60)
1239 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1241 ;;; Print some junk at the beginning and end of compilation.
1242 (defun start-error-output (source-info)
1243 (declare (type source-info source-info))
1244 (let ((file-info (source-info-file-info source-info)))
1245 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1246 (namestring (file-info-name file-info))
1247 (sb!int:format-universal-time nil
1248 (file-info-write-date
1252 :print-timezone nil)))
1254 (defun finish-error-output (source-info won)
1255 (declare (type source-info source-info))
1256 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1258 (elapsed-time-to-string
1259 (- (get-universal-time)
1260 (source-info-start-time source-info))))
1263 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1264 ;;; out of the compile, then abort the writing of the output file, so
1265 ;;; that we don't overwrite it with known garbage.
1266 (defun sb!xc:compile-file
1271 (output-file (cfp-output-file-default input-file))
1272 ;; FIXME: ANSI doesn't seem to say anything about
1273 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1275 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1276 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1277 (external-format :default)
1281 ((:block-compile *block-compile-argument*) nil)
1282 ((:byte-compile *byte-compile*) *byte-compile-default*))
1285 "Compile INPUT-FILE, producing a corresponding fasl file and returning
1286 its filename. Besides the ANSI &KEY arguments :OUTPUT-FILE, :VERBOSE,
1287 :PRINT, and :EXTERNAL-FORMAT,the following extensions are supported:
1289 If given, internal data structures are dumped to the specified
1290 file, or if a value of T is given, to a file of *.trace type
1291 derived from the input file name.
1292 :BYTE-COMPILE {T | NIL | :MAYBE}
1293 Determines whether to compile into interpreted byte code instead of
1294 machine instructions. Byte code is several times smaller, but much
1295 slower. If :MAYBE, then only byte-compile when SPEED is 0 and
1296 DEBUG <= 1. The default is the value of SB-EXT:*BYTE-COMPILE-DEFAULT*,
1297 which is initially :MAYBE. (This option will probably become
1298 formally deprecated starting around sbcl-0.7.0, when various
1299 cleanups related to the byte interpreter are planned.)
1300 Also, as a workaround for vaguely-non-ANSI behavior, the :BLOCK-COMPILE
1301 argument is quasi-supported, to determine whether multiple
1302 functions are compiled together as a unit, resolving function
1303 references at compile time. NIL means that global function names
1304 are never resolved at compilation time. Currently NIL is the
1305 default behavior, because although section 3.2.2.3, \"Semantic
1306 Constraints\", of the ANSI spec allows this behavior under all
1307 circumstances, the compiler's runtime scales badly when it
1308 tries to do this for large files. If/when this performance
1309 problem is fixed, the block compilation default behavior will
1310 probably be made dependent on the SPEED and COMPILATION-SPEED
1311 optimization values, and the :BLOCK-COMPILE argument will probably
1314 (unless (eq external-format :default)
1315 (error "Non-:DEFAULT EXTERNAL-FORMAT values are not supported."))
1316 (let* ((fasl-output nil)
1317 (output-file-name nil)
1320 (failure-p t) ; T in case error keeps this from being set later
1321 (input-pathname (verify-source-file input-file))
1322 (source-info (make-file-source-info input-pathname))
1323 (*compiler-trace-output* nil)) ; might be modified below
1328 (setq output-file-name
1329 (sb!xc:compile-file-pathname input-file
1330 :output-file output-file))
1332 (open-fasl-output output-file-name
1333 (namestring input-pathname)
1334 (eq *byte-compile* t))))
1336 (let* ((default-trace-file-pathname
1337 (make-pathname :type "trace" :defaults input-pathname))
1338 (trace-file-pathname
1339 (if (eql trace-file t)
1340 default-trace-file-pathname
1341 (merge-pathnames trace-file
1342 default-trace-file-pathname))))
1343 (setf *compiler-trace-output*
1344 (open trace-file-pathname
1345 :if-exists :supersede
1346 :direction :output))))
1348 (when sb!xc:*compile-verbose*
1349 (start-error-output source-info))
1350 (let ((*compile-object* fasl-output)
1352 (multiple-value-setq (dummy warnings-p failure-p)
1353 (sub-compile-file source-info)))
1354 (setq compile-won t))
1356 (close-source-info source-info)
1359 (close-fasl-output fasl-output (not compile-won))
1360 (setq output-file-name
1361 (pathname (fasl-output-stream fasl-output)))
1362 (when (and compile-won sb!xc:*compile-verbose*)
1363 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1365 (when sb!xc:*compile-verbose*
1366 (finish-error-output source-info compile-won))
1368 (when *compiler-trace-output*
1369 (close *compiler-trace-output*)))
1371 (values (if output-file
1372 ;; Hack around filesystem race condition...
1373 (or (probe-file output-file-name) output-file-name)
1378 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1379 ;;; the OUTPUT-FILE argument
1381 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1382 ;;; that results from merging the INPUT-FILE with the value of
1383 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1384 ;;; default to the appropriate implementation-defined default type for
1386 (defun cfp-output-file-default (input-file)
1387 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1388 (retyped (make-pathname :type *backend-fasl-file-type*
1389 :defaults defaults)))
1392 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1393 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1394 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1395 ;;; pathname, it is translated into a physical pathname as if by
1396 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1397 ;;; So I haven't really tried to make this precisely ANSI-compatible
1398 ;;; at the level of e.g. whether it returns logical pathname or a
1399 ;;; physical pathname. Patches to make it more correct are welcome.
1400 ;;; -- WHN 2000-12-09
1401 (defun sb!xc:compile-file-pathname (input-file
1403 (output-file (cfp-output-file-default
1407 "Return a pathname describing what file COMPILE-FILE would write to given
1409 (pathname output-file))
1411 ;;;; MAKE-LOAD-FORM stuff
1413 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1414 ;;; finds a constant structure, it invokes this to arrange for proper
1415 ;;; dumping. If it turns out that the constant has already been
1416 ;;; dumped, then we don't need to do anything.
1418 ;;; If the constant hasn't been dumped, then we check to see whether
1419 ;;; we are in the process of creating it. We detect this by
1420 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1421 ;;; the constants we are in the process of creating. Actually, each
1422 ;;; entry is a list of the constant and any init forms that need to be
1423 ;;; processed on behalf of that constant.
1425 ;;; It's not necessarily an error for this to happen. If we are
1426 ;;; processing the init form for some object that showed up *after*
1427 ;;; the original reference to this constant, then we just need to
1428 ;;; defer the processing of that init form. To detect this, we
1429 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1430 ;;; constants created since the last time we started processing an
1431 ;;; init form. If the constant passed to emit-make-load-form shows up
1432 ;;; in this list, then there is a circular chain through creation
1433 ;;; forms, which is an error.
1435 ;;; If there is some intervening init form, then we blow out of
1436 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1437 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1438 ;;; offending init form can be tacked onto the init forms for the
1439 ;;; circular object.
1441 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1442 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1443 ;;; whether the creation form is the magic value
1444 ;;; :JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1445 ;;; dumper will eventually get its hands on the object and use the
1446 ;;; normal structure dumping noise on it.
1448 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1449 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1450 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1451 ;;; dumper to use that result instead whenever it sees this constant.
1453 ;;; Now we try to compile the init form. We bind
1454 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1455 ;;; form (and any init forms that were added because of circularity
1456 ;;; detection). If this works, great. If not, we add the init forms to
1457 ;;; the init forms for the object that caused the problems and let it
1459 (defvar *constants-being-created* nil)
1460 (defvar *constants-created-since-last-init* nil)
1461 ;;; FIXME: Shouldn't these^ variables be bound in LET forms?
1462 (defun emit-make-load-form (constant)
1463 (aver (fasl-output-p *compile-object*))
1464 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1465 ;; KLUDGE: This special hack is because I was too lazy
1466 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1467 ;; function of LAYOUT returns nontrivial forms when
1468 ;; building the cross-compiler but :IGNORE-IT when
1469 ;; cross-compiling or running under the target Lisp. --
1471 #+sb-xc-host (typep constant 'layout))
1472 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1474 (when (find constant *constants-created-since-last-init* :test #'eq)
1476 (throw 'pending-init circular-ref)))
1477 (multiple-value-bind (creation-form init-form)
1479 (sb!xc:make-load-form constant (make-null-lexenv))
1481 (compiler-error "(while making load form for ~S)~%~A"
1485 (:just-dump-it-normally
1486 (fasl-validate-structure constant *compile-object*)
1491 (compile-top-level-lambdas () t)
1492 (when (fasl-constant-already-dumped-p constant *compile-object*)
1493 (return-from emit-make-load-form nil))
1494 (let* ((name (let ((*print-level* 1) (*print-length* 2))
1495 (with-output-to-string (stream)
1496 (write constant :stream stream))))
1498 (list constant name init-form)
1500 (let ((*constants-being-created*
1501 (cons info *constants-being-created*))
1502 (*constants-created-since-last-init*
1503 (cons constant *constants-created-since-last-init*)))
1506 (fasl-note-handle-for-constant
1508 (compile-load-time-value
1510 (format nil "creation form for ~A" name))
1513 (compiler-error "circular references in creation form for ~S"
1516 (let* ((*constants-created-since-last-init* nil)
1518 (catch 'pending-init
1519 (loop for (name form) on (cdr info) by #'cddr
1520 collect name into names
1521 collect form into forms
1523 (compile-make-load-form-init-forms
1525 (format nil "init form~:[~;s~] for ~{~A~^, ~}"
1526 (cdr forms) names)))
1529 (setf (cdr circular-ref)
1530 (append (cdr circular-ref) (cdr info))))))))))))