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-vars* *component-being-compiled*
18 *code-vector* *next-location* *result-fixups*
19 *free-funs* *source-paths*
20 *seen-blocks* *seen-funs* *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 *check-consistency* nil)
34 (defvar *all-components*)
36 ;;; Bind this to a stream to capture various internal debugging output.
37 (defvar *compiler-trace-output* nil)
39 ;;; The current block compilation state. These are initialized to the
40 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
43 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
44 ;;; argument, which overrides any internal declarations.
45 (defvar *block-compile*)
46 (defvar *block-compile-arg*)
47 (declaim (type (member nil t :specified) *block-compile* *block-compile-arg*))
48 (defvar *entry-points*)
49 (declaim (list *entry-points*))
51 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
52 ;;; lambdas resulting from compiling subforms. (In reverse order.)
53 (defvar *toplevel-lambdas*)
54 (declaim (list *toplevel-lambdas*))
56 (defvar sb!xc:*compile-verbose* t
58 "The default for the :VERBOSE argument to COMPILE-FILE.")
59 (defvar sb!xc:*compile-print* t
61 "The default for the :PRINT argument to COMPILE-FILE.")
62 (defvar *compile-progress* nil
64 "When this is true, the compiler prints to *ERROR-OUTPUT* progress
65 information about the phases of compilation of each function. (This
66 is useful mainly in large block compilations.)")
68 (defvar sb!xc:*compile-file-pathname* nil
70 "The defaulted pathname of the file currently being compiled, or NIL if not
72 (defvar sb!xc:*compile-file-truename* nil
74 "The TRUENAME of the file currently being compiled, or NIL if not
77 (declaim (type (or pathname null)
78 sb!xc:*compile-file-pathname*
79 sb!xc:*compile-file-truename*))
81 ;;; the SOURCE-INFO structure for the current compilation. This is
82 ;;; null globally to indicate that we aren't currently in any
83 ;;; identifiable compilation.
84 (defvar *source-info* nil)
86 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
87 ;;; normally causes nested uses to be no-ops).
88 (defvar *in-compilation-unit* nil)
90 ;;; Count of the number of compilation units dynamically enclosed by
91 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
92 (defvar *aborted-compilation-unit-count*)
94 ;;; Mumble conditional on *COMPILE-PROGRESS*.
95 (defun maybe-mumble (&rest foo)
96 (when *compile-progress*
97 (compiler-mumble "~&")
98 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
99 (apply #'compiler-mumble foo))))
101 (deftype object () '(or fasl-output core-object null))
103 (defvar *compile-object* nil)
104 (declaim (type object *compile-object*))
106 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
108 (defmacro sb!xc:with-compilation-unit (options &body body)
110 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
111 This form affects compilations that take place within its dynamic extent. It
112 is intended to be wrapped around the compilation of all files in the same
113 system. These keywords are defined:
114 :OVERRIDE Boolean-Form
115 One of the effects of this form is to delay undefined warnings
116 until the end of the form, instead of giving them at the end of each
117 compilation. If OVERRIDE is NIL (the default), then the outermost
118 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
119 OVERRIDE true causes that form to grab any enclosed warnings, even if
120 it is enclosed by another WITH-COMPILATION-UNIT."
121 `(%with-compilation-unit (lambda () ,@body) ,@options))
123 (defun %with-compilation-unit (fn &key override)
124 (declare (type function fn))
125 (let ((succeeded-p nil))
126 (if (and *in-compilation-unit* (not override))
127 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
128 ;; ordinarily (unless OVERRIDE) basically a no-op.
130 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
132 (incf *aborted-compilation-unit-count*)))
133 ;; FIXME: Now *COMPILER-FOO-COUNT* stuff is bound in more than
134 ;; one place. If we can get rid of the IR1 interpreter, this
135 ;; should be easier to clean up.
136 (let ((*aborted-compilation-unit-count* 0)
137 (*compiler-error-count* 0)
138 (*compiler-warning-count* 0)
139 (*compiler-style-warning-count* 0)
140 (*compiler-note-count* 0)
141 (*undefined-warnings* nil)
142 (*in-compilation-unit* t))
143 (handler-bind ((parse-unknown-type
145 (note-undefined-reference
146 (parse-unknown-type-specifier c)
149 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
151 (incf *aborted-compilation-unit-count*))
152 (summarize-compilation-unit (not succeeded-p))))))))
154 ;;; This is to be called at the end of a compilation unit. It signals
155 ;;; any residual warnings about unknown stuff, then prints the total
156 ;;; error counts. ABORT-P should be true when the compilation unit was
157 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
158 ;;; enclosed nested compilation units that were aborted.
159 (defun summarize-compilation-unit (abort-p)
161 (handler-bind ((style-warning #'compiler-style-warning-handler)
162 (warning #'compiler-warning-handler))
164 (let ((undefs (sort *undefined-warnings* #'string<
166 (let ((x (undefined-warning-name x)))
169 (prin1-to-string x)))))))
170 (dolist (undef undefs)
171 (let ((name (undefined-warning-name undef))
172 (kind (undefined-warning-kind undef))
173 (warnings (undefined-warning-warnings undef))
174 (undefined-warning-count (undefined-warning-count undef)))
175 (dolist (*compiler-error-context* warnings)
176 (compiler-style-warn "undefined ~(~A~): ~S" kind name))
177 (let ((warn-count (length warnings)))
178 (when (and warnings (> undefined-warning-count warn-count))
179 (let ((more (- undefined-warning-count warn-count)))
181 "~W more use~:P of undefined ~(~A~) ~S"
184 (dolist (kind '(:variable :function :type))
185 (let ((summary (mapcar #'undefined-warning-name
186 (remove kind undefs :test-not #'eq
187 :key #'undefined-warning-kind))))
190 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
191 ~% ~{~<~% ~1:;~S~>~^ ~}"
192 (cdr summary) kind summary)))))))
194 (unless (and (not abort-p)
195 (zerop *aborted-compilation-unit-count*)
196 (zerop *compiler-error-count*)
197 (zerop *compiler-warning-count*)
198 (zerop *compiler-style-warning-count*)
199 (zerop *compiler-note-count*))
200 (format *error-output* "~&")
201 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
202 (compiler-mumble "compilation unit ~:[finished~;aborted~]~
203 ~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
204 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
205 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
206 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
207 ~[~:;~:*~& printed ~W note~:P~]"
209 *aborted-compilation-unit-count*
210 *compiler-error-count*
211 *compiler-warning-count*
212 *compiler-style-warning-count*
213 *compiler-note-count*)))
214 (format *error-output* "~&"))
216 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
217 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
218 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
219 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
220 (defmacro with-compilation-values (&body body)
222 (let ((*warnings-p* nil)
224 (values (progn ,@body)
228 ;;;; component compilation
230 (defparameter *max-optimize-iterations* 3 ; ARB
232 "The upper limit on the number of times that we will consecutively do IR1
233 optimization that doesn't introduce any new code. A finite limit is
234 necessary, since type inference may take arbitrarily long to converge.")
236 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
237 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
239 ;;; Repeatedly optimize COMPONENT until no further optimizations can
240 ;;; be found or we hit our iteration limit. When we hit the limit, we
241 ;;; clear the component and block REOPTIMIZE flags to discourage the
242 ;;; next optimization attempt from pounding on the same code.
243 (defun ir1-optimize-until-done (component)
244 (declare (type component component))
246 (event ir1-optimize-until-done)
248 (cleared-reanalyze nil))
250 (when (component-reanalyze component)
252 (setq cleared-reanalyze t)
253 (setf (component-reanalyze component) nil))
254 (setf (component-reoptimize component) nil)
255 (ir1-optimize component)
256 (cond ((component-reoptimize component)
258 (when (= count *max-optimize-iterations*)
260 (cond ((retry-delayed-ir1-transforms :optimize)
264 (event ir1-optimize-maxed-out)
265 (setf (component-reoptimize component) nil)
266 (do-blocks (block component)
267 (setf (block-reoptimize block) nil))
269 ((retry-delayed-ir1-transforms :optimize)
276 (when cleared-reanalyze
277 (setf (component-reanalyze component) t)))
280 (defparameter *constraint-propagate* t)
282 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
283 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
284 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
285 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
287 ;;; FIXME: It would be good to document why it's important to have a
288 ;;; large value here, and what the drawbacks of an excessively large
289 ;;; value are; and it might also be good to make it depend on
290 ;;; optimization policy.
291 (defparameter *reoptimize-after-type-check-max* 10)
293 (defevent reoptimize-maxed-out
294 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
296 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
297 (defun dfo-as-needed (component)
298 (declare (type component component))
299 (when (component-reanalyze component)
303 (unless (component-reanalyze component)
309 ;;; Do all the IR1 phases for a non-top-level component.
310 (defun ir1-phases (component)
311 (declare (type component component))
312 (aver-live-component component)
313 (let ((*constraint-number* 0)
315 (*delayed-ir1-transforms* nil))
316 (declare (special *constraint-number* *delayed-ir1-transforms*))
318 (ir1-optimize-until-done component)
319 (when (or (component-new-functionals component)
320 (component-reanalyze-functionals component))
321 (maybe-mumble "locall ")
322 (locall-analyze-component component))
323 (dfo-as-needed component)
324 (when *constraint-propagate*
325 (maybe-mumble "constraint ")
326 (constraint-propagate component))
327 (when (retry-delayed-ir1-transforms :constraint)
328 (maybe-mumble "Rtran "))
329 (flet ((want-reoptimization-p ()
330 (or (component-reoptimize component)
331 (component-reanalyze component)
332 (component-new-functionals component)
333 (component-reanalyze-functionals component))))
334 (unless (and (want-reoptimization-p)
335 ;; We delay the generation of type checks until
336 ;; the type constraints have had time to
337 ;; propagate, else the compiler can confuse itself.
338 (< loop-count (- *reoptimize-after-type-check-max* 4)))
339 (maybe-mumble "type ")
340 (generate-type-checks component)
341 (unless (want-reoptimization-p)
343 (when (>= loop-count *reoptimize-after-type-check-max*)
344 (maybe-mumble "[reoptimize limit]")
345 (event reoptimize-maxed-out)
349 (ir1-finalize component)
352 (defun %compile-component (component)
353 (let ((*code-segment* nil)
355 (maybe-mumble "GTN ")
356 (gtn-analyze component)
357 (maybe-mumble "LTN ")
358 (ltn-analyze component)
359 (dfo-as-needed component)
360 (maybe-mumble "control ")
361 (control-analyze component #'make-ir2-block)
363 (when (ir2-component-values-receivers (component-info component))
364 (maybe-mumble "stack ")
365 (stack-analyze component)
366 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
367 ;; stack analysis. There shouldn't be any unreachable code after
368 ;; control, so this won't delete anything.
369 (dfo-as-needed component))
373 (maybe-mumble "IR2tran ")
375 (entry-analyze component)
376 (ir2-convert component)
378 (when (policy *lexenv* (>= speed compilation-speed))
379 (maybe-mumble "copy ")
380 (copy-propagate component))
382 (select-representations component)
384 (when *check-consistency*
385 (maybe-mumble "check2 ")
386 (check-ir2-consistency component))
388 (delete-unreferenced-tns component)
390 (maybe-mumble "life ")
391 (lifetime-analyze component)
393 (when *compile-progress*
394 (compiler-mumble "") ; Sync before doing more output.
395 (pre-pack-tn-stats component *error-output*))
397 (when *check-consistency*
398 (maybe-mumble "check-life ")
399 (check-life-consistency component))
401 (maybe-mumble "pack ")
404 (when *check-consistency*
405 (maybe-mumble "check-pack ")
406 (check-pack-consistency component))
408 (when *compiler-trace-output*
409 (describe-component component *compiler-trace-output*)
410 (describe-ir2-component component *compiler-trace-output*))
412 (maybe-mumble "code ")
413 (multiple-value-bind (code-length trace-table fixups)
414 (generate-code component)
416 (when *compiler-trace-output*
417 (format *compiler-trace-output*
418 "~|~%disassembly of code for ~S~2%" component)
419 (sb!disassem:disassemble-assem-segment *code-segment*
420 *compiler-trace-output*))
422 (etypecase *compile-object*
424 (maybe-mumble "fasl")
425 (fasl-dump-component component
432 (maybe-mumble "core")
433 (make-core-component component
441 ;; We're done, so don't bother keeping anything around.
442 (setf (component-info component) :dead)
446 ;;; Delete components with no external entry points before we try to
447 ;;; generate code. Unreachable closures can cause IR2 conversion to
448 ;;; puke on itself, since it is the reference to the closure which
449 ;;; normally causes the components to be combined.
450 (defun delete-if-no-entries (component)
451 (dolist (fun (component-lambdas component) (delete-component component))
452 (when (functional-has-external-references-p fun)
454 (case (functional-kind fun)
457 (unless (every (lambda (ref)
458 (eq (node-component ref) component))
462 (defun compile-component (component)
464 ;; miscellaneous sanity checks
466 ;; FIXME: These are basically pretty wimpy compared to the checks done
467 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
468 ;; make those internal consistency checks work again and use them.
469 (aver-live-component component)
470 (do-blocks (block component)
471 (aver (eql (block-component block) component)))
472 (dolist (lambda (component-lambdas component))
473 ;; sanity check to prevent weirdness from propagating insidiously as
474 ;; far from its root cause as it did in bug 138: Make sure that
475 ;; thing-to-COMPONENT links are consistent.
476 (aver (eql (lambda-component lambda) component))
477 (aver (eql (node-component (lambda-bind lambda)) component)))
479 (let* ((*component-being-compiled* component))
480 (when sb!xc:*compile-print*
481 (compiler-mumble "~&; compiling ~A: " (component-name component)))
483 (ir1-phases component)
485 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
486 (maybe-mumble "env ")
487 (physenv-analyze component)
488 (dfo-as-needed component)
490 (delete-if-no-entries component)
492 (unless (eq (block-next (component-head component))
493 (component-tail component))
494 (%compile-component component)))
496 (clear-constant-info)
498 (when sb!xc:*compile-print*
499 (compiler-mumble "~&"))
503 ;;;; clearing global data structures
505 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
506 ;;;; global data structures entirely when possible and consing up the
507 ;;;; others from scratch instead of clearing and reusing them?
509 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
510 ;;; addition to allowing stuff to be reclaimed, this is required for
511 ;;; correct assignment of constant offsets, since we need to assign a
512 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
513 ;;; slots, since they are used to keep track of functions across
514 ;;; component boundaries.
515 (defun clear-constant-info ()
516 (maphash (lambda (k v)
518 (setf (leaf-info v) nil))
520 (maphash (lambda (k v)
523 (setf (leaf-info v) nil)))
527 ;;; Blow away the REFS for all global variables, and let COMPONENT
529 (defun clear-ir1-info (component)
530 (declare (type component component))
532 (maphash (lambda (k v)
536 (delete-if #'here-p (leaf-refs v)))
537 (when (basic-var-p v)
538 (setf (basic-var-sets v)
539 (delete-if #'here-p (basic-var-sets v))))))
542 (eq (node-component x) component)))
548 ;;; Clear global variables used by the compiler.
550 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
551 ;;; and it adds overhead even when people aren't using the compiler.
552 ;;; Perhaps we could make these global vars unbound except when
553 ;;; actually in use, so that this function could go away.
554 (defun clear-stuff (&optional (debug-too t))
556 ;; Clear global tables.
557 (when (boundp '*free-funs*)
558 (clrhash *free-funs*)
559 (clrhash *free-vars*)
560 (clrhash *constants*))
562 ;; Clear debug counters and tables.
563 (clrhash *seen-blocks*)
564 (clrhash *seen-funs*)
565 (clrhash *list-conflicts-table*)
568 (clrhash *continuation-numbers*)
569 (clrhash *number-continuations*)
570 (setq *continuation-number* 0)
574 (clrhash *label-ids*)
575 (clrhash *id-labels*)
578 ;; Clear some PACK data structures (for GC purposes only).
579 (aver (not *in-pack*))
580 (dolist (sb *backend-sb-list*)
581 (when (finite-sb-p sb)
582 (fill (finite-sb-live-tns sb) nil))))
584 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
585 ;; Superficially, this seemed harmful -- the user could reasonably be
586 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
587 ;; compiled. A closer inspection showed that this actually turned out to be
588 ;; harmless in practice, because CLEAR-STUFF was only called from within
589 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
590 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
591 ;; practice, it was also useless in practice. So we don't do it any more.)
597 ;;; Print out some useful info about COMPONENT to STREAM.
598 (defun describe-component (component *standard-output*)
599 (declare (type component component))
600 (format t "~|~%;;;; component: ~S~2%" (component-name component))
601 (print-blocks component)
604 (defun describe-ir2-component (component *standard-output*)
605 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
606 (format t "entries:~%")
607 (dolist (entry (ir2-component-entries (component-info component)))
608 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
609 (label-id (entry-info-offset entry))
610 (entry-info-name entry)
611 (entry-info-closure-p entry)))
613 (pre-pack-tn-stats component *standard-output*)
615 (print-ir2-blocks component)
621 ;;;; When reading from a file, we have to keep track of some source
622 ;;;; information. We also exploit our ability to back up for printing
623 ;;;; the error context and for recovering from errors.
625 ;;;; The interface we provide to this stuff is the stream-oid
626 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
627 ;;;; of getting the next source form.
629 ;;; A FILE-INFO structure holds all the source information for a
631 (defstruct (file-info (:copier nil))
632 ;; If a file, the truename of the corresponding source file. If from
633 ;; a Lisp form, :LISP. If from a stream, :STREAM.
634 (name (missing-arg) :type (or pathname (member :lisp :stream)))
635 ;; the defaulted, but not necessarily absolute file name (i.e. prior
636 ;; to TRUENAME call.) Null if not a file. This is used to set
637 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
639 (untruename nil :type (or pathname null))
640 ;; the file's write date (if relevant)
641 (write-date nil :type (or unsigned-byte null))
642 ;; the source path root number of the first form in this file (i.e.
643 ;; the total number of forms converted previously in this
645 (source-root 0 :type unsigned-byte)
646 ;; parallel vectors containing the forms read out of the file and
647 ;; the file positions that reading of each form started at (i.e. the
648 ;; end of the previous form)
649 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
650 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
652 ;;; The SOURCE-INFO structure provides a handle on all the source
653 ;;; information for an entire compilation.
654 (defstruct (source-info
655 #-no-ansi-print-object
656 (:print-object (lambda (s stream)
657 (print-unreadable-object (s stream :type t))))
659 ;; the UT that compilation started at
660 (start-time (get-universal-time) :type unsigned-byte)
661 ;; the FILE-INFO structure for this compilation
662 (file-info nil :type (or file-info null))
663 ;; the stream that we are using to read the FILE-INFO, or NIL if
664 ;; no stream has been opened yet
665 (stream nil :type (or stream null)))
667 ;;; Given a pathname, return a SOURCE-INFO structure.
668 (defun make-file-source-info (file)
669 (let ((file-info (make-file-info :name (truename file)
671 :write-date (file-write-date file))))
673 (make-source-info :file-info file-info)))
675 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
676 (defun make-lisp-source-info (form)
677 (make-source-info :start-time (get-universal-time)
678 :file-info (make-file-info :name :lisp
682 ;;; Return a SOURCE-INFO which will read from STREAM.
683 (defun make-stream-source-info (stream)
684 (let ((file-info (make-file-info :name :stream)))
685 (make-source-info :file-info file-info
688 ;;; Return a form read from STREAM; or for EOF use the trick,
689 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
690 ;;; error happens, then convert it to standard abort-the-compilation
691 ;;; error condition (possibly recording some extra location
693 (defun read-for-compile-file (stream position)
694 (handler-case (read stream nil stream)
695 (reader-error (condition)
696 (error 'input-error-in-compile-file
698 ;; We don't need to supply :POSITION here because
699 ;; READER-ERRORs already know their position in the file.
701 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
702 ;; (and that this is not a READER-ERROR) when it encounters end of
703 ;; file in the middle of something it's trying to read.
704 (end-of-file (condition)
705 (error 'input-error-in-compile-file
707 ;; We need to supply :POSITION here because the END-OF-FILE
708 ;; condition doesn't carry the position that the user
709 ;; probably cares about, where the failed READ began.
710 :position position))))
712 ;;; If STREAM is present, return it, otherwise open a stream to the
713 ;;; current file. There must be a current file.
715 ;;; FIXME: This is probably an unnecessarily roundabout way to do
716 ;;; things now that we process a single file in COMPILE-FILE (unlike
717 ;;; the old CMU CL code, which accepted multiple files). Also, the old
719 ;;; When we open a new file, we also reset *PACKAGE* and policy.
720 ;;; This gives the effect of rebinding around each file.
721 ;;; which doesn't seem to be true now. Check to make sure that if
722 ;;; such rebinding is necessary, it's still done somewhere.
723 (defun get-source-stream (info)
724 (declare (type source-info info))
725 (or (source-info-stream info)
726 (let* ((file-info (source-info-file-info info))
727 (name (file-info-name file-info)))
728 (setf sb!xc:*compile-file-truename* name
729 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
730 (source-info-stream info) (open name :direction :input)))))
732 ;;; Close the stream in INFO if it is open.
733 (defun close-source-info (info)
734 (declare (type source-info info))
735 (let ((stream (source-info-stream info)))
736 (when stream (close stream)))
737 (setf (source-info-stream info) nil)
740 ;;; Read and compile the source file.
741 (defun sub-sub-compile-file (info)
742 (let* ((file-info (source-info-file-info info))
743 (stream (get-source-stream info)))
745 (let* ((pos (file-position stream))
746 (form (read-for-compile-file stream pos)))
747 (if (eq form stream) ; i.e., if EOF
749 (let* ((forms (file-info-forms file-info))
750 (current-idx (+ (fill-pointer forms)
751 (file-info-source-root file-info))))
752 (vector-push-extend form forms)
753 (vector-push-extend pos (file-info-positions file-info))
754 (find-source-paths form current-idx)
755 (process-toplevel-form form
756 `(original-source-start 0 ,current-idx)
759 ;;; Return the INDEX'th source form read from INFO and the position
760 ;;; where it was read.
761 (defun find-source-root (index info)
762 (declare (type index index) (type source-info info))
763 (let ((file-info (source-info-file-info info)))
764 (values (aref (file-info-forms file-info) index)
765 (aref (file-info-positions file-info) index))))
767 ;;;; processing of top level forms
769 ;;; This is called by top level form processing when we are ready to
770 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
771 ;;; convert the form, but delay compilation, pushing the result on
772 ;;; *TOPLEVEL-LAMBDAS* instead.
773 (defun convert-and-maybe-compile (form path)
774 (declare (list path))
775 (let* ((*lexenv* (make-lexenv :policy *policy*))
776 (tll (ir1-toplevel form path nil)))
777 (cond ((eq *block-compile* t) (push tll *toplevel-lambdas*))
778 (t (compile-toplevel (list tll) nil)))))
780 ;;; Macroexpand FORM in the current environment with an error handler.
781 ;;; We only expand one level, so that we retain all the intervening
782 ;;; forms in the source path.
783 (defun preprocessor-macroexpand-1 (form)
784 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
786 (compiler-error "(during macroexpansion of ~A)~%~A"
787 (let ((*print-level* 1)
789 (format nil "~S" form))
792 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
793 ;;; the forms, and PATH is the source path of the FORM they came out of.
794 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
795 (defun process-toplevel-progn (forms path compile-time-too)
796 (declare (list forms) (list path))
798 (process-toplevel-form form path compile-time-too)))
800 ;;; Process a top level use of LOCALLY, or anything else (e.g.
801 ;;; MACROLET) at top level which has declarations and ordinary forms.
802 ;;; We parse declarations and then recursively process the body.
803 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
804 (declare (list path))
805 (multiple-value-bind (forms decls) (parse-body body nil)
807 (process-decls decls vars funs (make-continuation)))
808 ;; Binding *POLICY* is pretty much of a hack, since it
809 ;; causes LOCALLY to "capture" enclosed proclamations. It
810 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
811 ;; value of *POLICY* as the policy. The need for this hack
812 ;; is due to the quirk that there is no way to represent in
813 ;; a POLICY that an optimize quality came from the default.
815 ;; FIXME: Ideally, something should be done so that DECLAIM
816 ;; inside LOCALLY works OK. Failing that, at least we could
817 ;; issue a warning instead of silently screwing up.
818 (*policy* (lexenv-policy *lexenv*)))
819 (process-toplevel-progn forms path compile-time-too))))
821 ;;; Parse an EVAL-WHEN situations list, returning three flags,
822 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
823 ;;; the types of situations present in the list.
824 (defun parse-eval-when-situations (situations)
825 (when (or (not (listp situations))
826 (set-difference situations
833 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
834 (let ((deprecated-names (intersection situations '(compile load eval))))
835 (when deprecated-names
836 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
838 (values (intersection '(:compile-toplevel compile)
840 (intersection '(:load-toplevel load) situations)
841 (intersection '(:execute eval) situations)))
844 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
845 (defun functional-components (f)
846 (declare (type functional f))
848 (clambda (list (lambda-component f)))
849 (optional-dispatch (let ((result nil))
850 (labels ((frob (clambda)
851 (pushnew (lambda-component clambda)
853 (maybe-frob (maybe-clambda)
855 (frob maybe-clambda))))
856 (mapc #'frob (optional-dispatch-entry-points f))
857 (maybe-frob (optional-dispatch-more-entry f))
858 (maybe-frob (optional-dispatch-main-entry f)))))))
860 (defun make-functional-from-toplevel-lambda (definition
864 ;; I'd thought NIL should
865 ;; work, but it doesn't.
868 (let* ((*current-path* path)
869 (component (make-empty-component))
870 (*current-component* component))
871 (setf (component-name component)
872 (debug-namify "~S initial component" name))
873 (setf (component-kind component) :initial)
874 (let* ((locall-fun (ir1-convert-lambda
876 :debug-name (debug-namify "top level local call ~S"
878 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
879 :source-name (or name '.anonymous.)
880 :debug-name (unless name
883 (assert-global-function-definition-type name locall-fun))
884 (setf (functional-entry-fun fun) locall-fun
885 (functional-kind fun) :external
886 (functional-has-external-references-p fun) t)
889 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
890 ;;; description of the result.
891 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
892 ;;; into core and return the compiled FUNCTION value.
893 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
894 ;;; into the fasl file and return a dump handle.
896 ;;; If NAME is provided, then we try to use it as the name of the
897 ;;; function for debugging/diagnostic information.
898 (defun %compile (lambda-expression
903 ;; This magical idiom seems to be the appropriate
904 ;; path for compiling standalone LAMBDAs, judging
905 ;; from the CMU CL code and experiment, so it's a
906 ;; nice default for things where we don't have a
907 ;; real source path (as in e.g. inside CL:COMPILE).
908 '(original-source-start 0 0)))
910 (legal-fun-name-or-type-error name))
911 (let* ((*lexenv* (make-lexenv :policy *policy*))
912 (fun (make-functional-from-toplevel-lambda lambda-expression
916 ;; FIXME: The compile-it code from here on is sort of a
917 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
918 ;; better to find a way to share the code there; or
919 ;; alternatively, to use this code to replace the code there.
920 ;; (The second alternative might be pretty easy if we used
921 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
922 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
924 (locall-analyze-clambdas-until-done (list fun))
926 (multiple-value-bind (components-from-dfo top-components hairy-top)
927 (find-initial-dfo (list fun))
929 (let ((*all-components* (append components-from-dfo top-components)))
930 ;; FIXME: This is more monkey see monkey do based on CMU CL
931 ;; code. If anyone figures out why to only prescan HAIRY-TOP
932 ;; and TOP-COMPONENTS here, instead of *ALL-COMPONENTS* or
933 ;; some other combination of results from FIND-INITIAL-VALUES,
934 ;; it'd be good to explain it.
935 (mapc #'preallocate-physenvs-for-toplevelish-lambdas hairy-top)
936 (mapc #'preallocate-physenvs-for-toplevelish-lambdas top-components)
937 (dolist (component-from-dfo components-from-dfo)
938 (compile-component component-from-dfo)
939 (replace-toplevel-xeps component-from-dfo)))
941 (let ((entry-table (etypecase *compile-object*
942 (fasl-output (fasl-output-entry-table
944 (core-object (core-object-entry-table
945 *compile-object*)))))
946 (multiple-value-bind (result found-p)
947 (gethash (leaf-info fun) entry-table)
951 ;; KLUDGE: This code duplicates some other code in this
952 ;; file. In the great reorganzation, the flow of program
953 ;; logic changed from the original CMUCL model, and that
954 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
955 ;; longer followed for CORE-OBJECTS, leading to BUG
956 ;; 156. This place is transparently not the right one for
957 ;; this code, but I don't have a clear enough overview of
958 ;; the compiler to know how to rearrange it all so that
959 ;; this operation fits in nicely, and it was blocking
960 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
961 ;; ((..)) (DEFUN FOO ...))
963 ;; FIXME: This KLUDGE doesn't solve all the problem in an
964 ;; ideal way, as (1) definitions typed in at the REPL
965 ;; without an INLINE declaration will give a NULL
966 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
967 ;; and (2) INLINE declarations will yield a
968 ;; FUNCTION-LAMBDA-EXPRESSION headed by
969 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
972 ;; (2) is probably fairly easy to fix -- it is, after all,
973 ;; a matter of list manipulation (or possibly of teaching
974 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
975 ;; significantly harder, as the association between
976 ;; function object and source is a tricky one.
978 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
979 ;; non-NULL list) when the function in question has been
980 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
981 ;; work when it has been compiled as part of the top-level
982 ;; EVAL strategy of compiling everything inside (LAMBDA ()
983 ;; ...). -- CSR, 2002-11-02
984 (when (core-object-p *compile-object*)
985 (fix-core-source-info *source-info* *compile-object* result))
987 (mapc #'clear-ir1-info components-from-dfo)
990 (defun process-toplevel-cold-fset (name lambda-expression path)
991 (unless (producing-fasl-file)
992 (error "can't COLD-FSET except in a fasl file"))
993 (legal-fun-name-or-type-error name)
994 (fasl-dump-cold-fset name
995 (%compile lambda-expression
1002 ;;; Process a top level FORM with the specified source PATH.
1003 ;;; * If this is a magic top level form, then do stuff.
1004 ;;; * If this is a macro, then expand it.
1005 ;;; * Otherwise, just compile it.
1007 ;;; COMPILE-TIME-TOO is as defined in ANSI
1008 ;;; "3.2.3.1 Processing of Top Level Forms".
1009 (defun process-toplevel-form (form path compile-time-too)
1011 (declare (list path))
1013 (catch 'process-toplevel-form-error-abort
1014 (let* ((path (or (gethash form *source-paths*) (cons form path)))
1015 (*compiler-error-bailout*
1017 (convert-and-maybe-compile
1018 `(error 'simple-program-error
1019 :format-control "execution of a form compiled with errors:~% ~S"
1020 :format-arguments (list ',form))
1022 (throw 'process-toplevel-form-error-abort nil))))
1024 (flet ((default-processor (form)
1025 ;; When we're cross-compiling, consider: what should we
1026 ;; do when we hit e.g.
1027 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1028 ;; (DEFUN FOO (X) (+ 7 X)))?
1029 ;; DEFUN has a macro definition in the cross-compiler,
1030 ;; and a different macro definition in the target
1031 ;; compiler. The only sensible thing is to use the
1032 ;; target compiler's macro definition, since the
1033 ;; cross-compiler's macro is in general into target
1034 ;; functions which can't meaningfully be executed at
1035 ;; cross-compilation time. So make sure we do the EVAL
1036 ;; here, before we macroexpand.
1038 ;; Then things get even dicier with something like
1039 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1040 ;; where we have to make sure that we don't uncross
1041 ;; the SB!XC: prefix before we do EVAL, because otherwise
1042 ;; we'd be trying to redefine the cross-compilation host's
1045 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1048 (when compile-time-too
1049 (eval form)) ; letting xc host EVAL do its own macroexpansion
1050 (let* (;; (We uncross the operator name because things
1051 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1052 ;; should be equivalent to their CL: counterparts
1053 ;; when being compiled as target code. We leave
1054 ;; the rest of the form uncrossed because macros
1055 ;; might yet expand into EVAL-WHEN stuff, and
1056 ;; things inside EVAL-WHEN can't be uncrossed
1057 ;; until after we've EVALed them in the
1058 ;; cross-compilation host.)
1059 (slightly-uncrossed (cons (uncross (first form))
1061 (expanded (preprocessor-macroexpand-1
1062 slightly-uncrossed)))
1063 (if (eq expanded slightly-uncrossed)
1064 ;; (Now that we're no longer processing toplevel
1065 ;; forms, and hence no longer need to worry about
1066 ;; EVAL-WHEN, we can uncross everything.)
1067 (convert-and-maybe-compile expanded path)
1068 ;; (We have to demote COMPILE-TIME-TOO to NIL
1069 ;; here, no matter what it was before, since
1070 ;; otherwise we'd tend to EVAL subforms more than
1071 ;; once, because of WHEN COMPILE-TIME-TOO form
1073 (process-toplevel-form expanded path nil))))
1074 ;; When we're not cross-compiling, we only need to
1075 ;; macroexpand once, so we can follow the 1-thru-6
1076 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1077 ;; Top Level Forms".
1079 (let ((expanded (preprocessor-macroexpand-1 form)))
1080 (cond ((eq expanded form)
1081 (when compile-time-too
1082 (eval-in-lexenv form *lexenv*))
1083 (convert-and-maybe-compile form path))
1085 (process-toplevel-form expanded
1087 compile-time-too))))))
1090 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1091 ;; (1) SBCL gets smart enough to handle global
1092 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1093 ;; implementors start using symbol macros in a way which
1094 ;; interacts with SB-XC/CL distinction.)
1095 (convert-and-maybe-compile form path)
1097 (default-processor form)
1098 (flet ((need-at-least-one-arg (form)
1100 (compiler-error "~S form is too short: ~S"
1104 ;; In the cross-compiler, top level COLD-FSET arranges
1105 ;; for static linking at cold init time.
1108 (aver (not compile-time-too))
1109 (destructuring-bind (cold-fset fun-name lambda-expression) form
1110 (declare (ignore cold-fset))
1111 (process-toplevel-cold-fset fun-name
1114 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1115 (need-at-least-one-arg form)
1116 (destructuring-bind (special-operator magic &rest body) form
1117 (ecase special-operator
1119 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1120 ;; "3.2.3.1 Processing of Top Level Forms".
1121 (multiple-value-bind (ct lt e)
1122 (parse-eval-when-situations magic)
1123 (let ((new-compile-time-too (or ct
1124 (and compile-time-too
1126 (cond (lt (process-toplevel-progn
1127 body path new-compile-time-too))
1128 (new-compile-time-too (eval-in-lexenv
1132 (funcall-in-macrolet-lexenv
1135 (declare (ignore funs))
1136 (process-toplevel-locally body
1138 compile-time-too))))
1140 (funcall-in-symbol-macrolet-lexenv
1143 (process-toplevel-locally body
1148 (process-toplevel-locally (rest form) path compile-time-too))
1150 (process-toplevel-progn (rest form) path compile-time-too))
1151 (t (default-processor form))))))))
1155 ;;;; load time value support
1157 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1159 ;;; Return T if we are currently producing a fasl file and hence
1160 ;;; constants need to be dumped carefully.
1161 (defun producing-fasl-file ()
1162 (fasl-output-p *compile-object*))
1164 ;;; Compile FORM and arrange for it to be called at load-time. Return
1165 ;;; the dumper handle and our best guess at the type of the object.
1166 (defun compile-load-time-value (form)
1167 (let ((lambda (compile-load-time-stuff form t)))
1169 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1170 (let ((type (leaf-type lambda)))
1171 (if (fun-type-p type)
1172 (single-value-type (fun-type-returns type))
1175 ;;; Compile the FORMS and arrange for them to be called (for effect,
1176 ;;; not value) at load time.
1177 (defun compile-make-load-form-init-forms (forms)
1178 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1179 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1181 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1182 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1183 (defun compile-load-time-stuff (form for-value)
1185 (let* ((*lexenv* (make-null-lexenv))
1186 (lambda (ir1-toplevel form *current-path* for-value)))
1187 (compile-toplevel (list lambda) t)
1190 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1191 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1192 ;;; don't try to combine this component with anything else and frob
1193 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1194 ;;; compiling, because it was merged with a run-time component.
1195 (defun compile-load-time-value-lambda (lambdas)
1196 (aver (null (cdr lambdas)))
1197 (let* ((lambda (car lambdas))
1198 (component (lambda-component lambda)))
1199 (when (eql (component-kind component) :toplevel)
1200 (setf (component-name component) (leaf-debug-name lambda))
1201 (compile-component component)
1202 (clear-ir1-info component))))
1206 (defun object-call-toplevel-lambda (tll)
1207 (declare (type functional tll))
1208 (let ((object *compile-object*))
1210 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1211 (core-object (core-call-toplevel-lambda tll object))
1214 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1215 ;;; the resulting function to be called.
1216 (defun sub-compile-toplevel-lambdas (lambdas)
1217 (declare (list lambdas))
1219 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1220 (compile-component component)
1221 (clear-ir1-info component)
1222 (object-call-toplevel-lambda tll)))
1225 ;;; Compile top level code and call the top level lambdas. We pick off
1226 ;;; top level lambdas in non-top-level components here, calling
1227 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1228 (defun compile-toplevel-lambdas (lambdas)
1229 (declare (list lambdas))
1230 (let ((len (length lambdas)))
1231 (flet ((loser (start)
1232 (or (position-if (lambda (x)
1233 (not (eq (component-kind
1234 (node-component (lambda-bind x)))
1237 ;; this used to read ":start start", but
1238 ;; start can be greater than len, which
1239 ;; is an error according to ANSI - CSR,
1241 :start (min start len))
1243 (do* ((start 0 (1+ loser))
1244 (loser (loser start) (loser start)))
1246 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1247 (unless (= loser len)
1248 (object-call-toplevel-lambda (elt lambdas loser))))))
1251 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1254 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1255 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1256 (defun compile-toplevel (lambdas load-time-value-p)
1257 (declare (list lambdas))
1259 (maybe-mumble "locall ")
1260 (locall-analyze-clambdas-until-done lambdas)
1262 (maybe-mumble "IDFO ")
1263 (multiple-value-bind (components top-components hairy-top)
1264 (find-initial-dfo lambdas)
1265 (let ((*all-components* (append components top-components)))
1266 (when *check-consistency*
1267 (maybe-mumble "[check]~%")
1268 (check-ir1-consistency *all-components*))
1270 (dolist (component (append hairy-top top-components))
1271 (pre-physenv-analyze-toplevel component))
1273 (dolist (component components)
1274 (compile-component component)
1275 (replace-toplevel-xeps component))
1277 (when *check-consistency*
1278 (maybe-mumble "[check]~%")
1279 (check-ir1-consistency *all-components*))
1281 (if load-time-value-p
1282 (compile-load-time-value-lambda lambdas)
1283 (compile-toplevel-lambdas lambdas))
1285 (mapc #'clear-ir1-info components)
1289 ;;; Actually compile any stuff that has been queued up for block
1291 (defun finish-block-compilation ()
1292 (when *block-compile*
1293 (when *toplevel-lambdas*
1294 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1295 (setq *toplevel-lambdas* ()))
1296 (setq *block-compile* nil)
1297 (setq *entry-points* nil)))
1299 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1300 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1301 (defun sub-compile-file (info)
1302 (declare (type source-info info))
1303 (let* ((*block-compile* *block-compile-arg*)
1304 (*package* (sane-package))
1306 (*lexenv* (make-null-lexenv))
1307 (*source-info* info)
1308 (sb!xc:*compile-file-pathname* nil)
1309 (sb!xc:*compile-file-truename* nil)
1310 (*toplevel-lambdas* ())
1311 (*compiler-error-bailout*
1313 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1314 (return-from sub-compile-file (values nil t t))))
1315 (*current-path* nil)
1316 (*last-source-context* nil)
1317 (*last-original-source* nil)
1318 (*last-source-form* nil)
1319 (*last-format-string* nil)
1320 (*last-format-args* nil)
1321 (*last-message-count* 0)
1322 ;; FIXME: Do we need this rebinding here? It's a literal
1323 ;; translation of the old CMU CL rebinding to
1324 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1325 ;; and it's not obvious whether the rebinding to itself is
1326 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1327 (*info-environment* *info-environment*)
1328 (*gensym-counter* 0))
1330 (with-compilation-values
1331 (sb!xc:with-compilation-unit ()
1334 (sub-sub-compile-file info)
1336 (finish-block-compilation)
1337 (let ((object *compile-object*))
1339 (fasl-output (fasl-dump-source-info info object))
1340 (core-object (fix-core-source-info info object))
1343 ;; Some errors are sufficiently bewildering that we just fail
1344 ;; immediately, without trying to recover and compile more of
1346 (input-error-in-compile-file (condition)
1347 (format *error-output*
1348 "~@<compilation aborted because of input error: ~2I~_~A~:>"
1350 (values nil t t)))))
1352 ;;; Return a pathname for the named file. The file must exist.
1353 (defun verify-source-file (pathname-designator)
1354 (let* ((pathname (pathname pathname-designator))
1355 (default-host (make-pathname :host (pathname-host pathname))))
1356 (flet ((try-with-type (path type error-p)
1357 (let ((new (merge-pathnames
1358 path (make-pathname :type type
1359 :defaults default-host))))
1360 (if (probe-file new)
1362 (and error-p (truename new))))))
1363 (cond ((typep pathname 'logical-pathname)
1364 (try-with-type pathname "LISP" t))
1365 ((probe-file pathname) pathname)
1366 ((try-with-type pathname "lisp" nil))
1367 ((try-with-type pathname "lisp" t))))))
1369 (defun elapsed-time-to-string (tsec)
1370 (multiple-value-bind (tmin sec) (truncate tsec 60)
1371 (multiple-value-bind (thr min) (truncate tmin 60)
1372 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1374 ;;; Print some junk at the beginning and end of compilation.
1375 (defun start-error-output (source-info)
1376 (declare (type source-info source-info))
1377 (let ((file-info (source-info-file-info source-info)))
1378 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1379 (namestring (file-info-name file-info))
1380 (sb!int:format-universal-time nil
1381 (file-info-write-date
1385 :print-timezone nil)))
1387 (defun finish-error-output (source-info won)
1388 (declare (type source-info source-info))
1389 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1391 (elapsed-time-to-string
1392 (- (get-universal-time)
1393 (source-info-start-time source-info))))
1396 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1397 ;;; out of the compile, then abort the writing of the output file, so
1398 ;;; that we don't overwrite it with known garbage.
1399 (defun sb!xc:compile-file
1404 (output-file (cfp-output-file-default input-file))
1405 ;; FIXME: ANSI doesn't seem to say anything about
1406 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1408 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1409 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1410 (external-format :default)
1414 ((:block-compile *block-compile-arg*) nil))
1417 "Compile INPUT-FILE, producing a corresponding fasl file and returning
1418 its filename. Besides the ANSI &KEY arguments :OUTPUT-FILE, :VERBOSE,
1419 :PRINT, and :EXTERNAL-FORMAT,the following extensions are supported:
1421 If given, internal data structures are dumped to the specified
1422 file, or if a value of T is given, to a file of *.trace type
1423 derived from the input file name.
1424 Also, as a workaround for vaguely-non-ANSI behavior, the :BLOCK-COMPILE
1425 argument is quasi-supported, to determine whether multiple
1426 functions are compiled together as a unit, resolving function
1427 references at compile time. NIL means that global function names
1428 are never resolved at compilation time. Currently NIL is the
1429 default behavior, because although section 3.2.2.3, \"Semantic
1430 Constraints\", of the ANSI spec allows this behavior under all
1431 circumstances, the compiler's runtime scales badly when it
1432 tries to do this for large files. If/when this performance
1433 problem is fixed, the block compilation default behavior will
1434 probably be made dependent on the SPEED and COMPILATION-SPEED
1435 optimization values, and the :BLOCK-COMPILE argument will probably
1438 (unless (eq external-format :default)
1439 (error "Non-:DEFAULT EXTERNAL-FORMAT values are not supported."))
1440 (let* ((fasl-output nil)
1441 (output-file-name nil)
1444 (failure-p t) ; T in case error keeps this from being set later
1445 (input-pathname (verify-source-file input-file))
1446 (source-info (make-file-source-info input-pathname))
1447 (*compiler-trace-output* nil)) ; might be modified below
1452 (setq output-file-name
1453 (sb!xc:compile-file-pathname input-file
1454 :output-file output-file))
1456 (open-fasl-output output-file-name
1457 (namestring input-pathname))))
1459 (let* ((default-trace-file-pathname
1460 (make-pathname :type "trace" :defaults input-pathname))
1461 (trace-file-pathname
1462 (if (eql trace-file t)
1463 default-trace-file-pathname
1464 (merge-pathnames trace-file
1465 default-trace-file-pathname))))
1466 (setf *compiler-trace-output*
1467 (open trace-file-pathname
1468 :if-exists :supersede
1469 :direction :output))))
1471 (when sb!xc:*compile-verbose*
1472 (start-error-output source-info))
1473 (let ((*compile-object* fasl-output)
1475 (multiple-value-setq (dummy warnings-p failure-p)
1476 (sub-compile-file source-info)))
1477 (setq compile-won t))
1479 (close-source-info source-info)
1482 (close-fasl-output fasl-output (not compile-won))
1483 (setq output-file-name
1484 (pathname (fasl-output-stream fasl-output)))
1485 (when (and compile-won sb!xc:*compile-verbose*)
1486 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1488 (when sb!xc:*compile-verbose*
1489 (finish-error-output source-info compile-won))
1491 (when *compiler-trace-output*
1492 (close *compiler-trace-output*)))
1494 (values (if output-file
1495 ;; Hack around filesystem race condition...
1496 (or (probe-file output-file-name) output-file-name)
1501 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1502 ;;; the OUTPUT-FILE argument
1504 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1505 ;;; that results from merging the INPUT-FILE with the value of
1506 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1507 ;;; default to the appropriate implementation-defined default type for
1509 (defun cfp-output-file-default (input-file)
1510 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1511 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1514 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1515 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1516 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1517 ;;; pathname, it is translated into a physical pathname as if by
1518 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1519 ;;; So I haven't really tried to make this precisely ANSI-compatible
1520 ;;; at the level of e.g. whether it returns logical pathname or a
1521 ;;; physical pathname. Patches to make it more correct are welcome.
1522 ;;; -- WHN 2000-12-09
1523 (defun sb!xc:compile-file-pathname (input-file
1525 (output-file (cfp-output-file-default
1529 "Return a pathname describing what file COMPILE-FILE would write to given
1531 (merge-pathnames output-file (merge-pathnames input-file)))
1533 ;;;; MAKE-LOAD-FORM stuff
1535 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1536 ;;; finds a constant structure, it invokes this to arrange for proper
1537 ;;; dumping. If it turns out that the constant has already been
1538 ;;; dumped, then we don't need to do anything.
1540 ;;; If the constant hasn't been dumped, then we check to see whether
1541 ;;; we are in the process of creating it. We detect this by
1542 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1543 ;;; the constants we are in the process of creating. Actually, each
1544 ;;; entry is a list of the constant and any init forms that need to be
1545 ;;; processed on behalf of that constant.
1547 ;;; It's not necessarily an error for this to happen. If we are
1548 ;;; processing the init form for some object that showed up *after*
1549 ;;; the original reference to this constant, then we just need to
1550 ;;; defer the processing of that init form. To detect this, we
1551 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1552 ;;; constants created since the last time we started processing an
1553 ;;; init form. If the constant passed to emit-make-load-form shows up
1554 ;;; in this list, then there is a circular chain through creation
1555 ;;; forms, which is an error.
1557 ;;; If there is some intervening init form, then we blow out of
1558 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1559 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1560 ;;; offending init form can be tacked onto the init forms for the
1561 ;;; circular object.
1563 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1564 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1565 ;;; whether the creation form is the magic value
1566 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1567 ;;; dumper will eventually get its hands on the object and use the
1568 ;;; normal structure dumping noise on it.
1570 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1571 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1572 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1573 ;;; dumper to use that result instead whenever it sees this constant.
1575 ;;; Now we try to compile the init form. We bind
1576 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1577 ;;; form (and any init forms that were added because of circularity
1578 ;;; detection). If this works, great. If not, we add the init forms to
1579 ;;; the init forms for the object that caused the problems and let it
1581 (defvar *constants-being-created* nil)
1582 (defvar *constants-created-since-last-init* nil)
1583 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1584 (defun emit-make-load-form (constant)
1585 (aver (fasl-output-p *compile-object*))
1586 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1587 ;; KLUDGE: This special hack is because I was too lazy
1588 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1589 ;; function of LAYOUT returns nontrivial forms when
1590 ;; building the cross-compiler but :IGNORE-IT when
1591 ;; cross-compiling or running under the target Lisp. --
1593 #+sb-xc-host (typep constant 'layout))
1594 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1596 (when (find constant *constants-created-since-last-init* :test #'eq)
1598 (throw 'pending-init circular-ref)))
1599 (multiple-value-bind (creation-form init-form)
1601 (sb!xc:make-load-form constant (make-null-lexenv))
1603 (compiler-error "(while making load form for ~S)~%~A"
1607 (:sb-just-dump-it-normally
1608 (fasl-validate-structure constant *compile-object*)
1613 (when (fasl-constant-already-dumped-p constant *compile-object*)
1614 (return-from emit-make-load-form nil))
1615 (let* ((name (let ((*print-level* 1) (*print-length* 2))
1616 (with-output-to-string (stream)
1617 (write constant :stream stream))))
1619 (list constant name init-form)
1621 (let ((*constants-being-created*
1622 (cons info *constants-being-created*))
1623 (*constants-created-since-last-init*
1624 (cons constant *constants-created-since-last-init*)))
1627 (fasl-note-handle-for-constant
1629 (compile-load-time-value
1633 (compiler-error "circular references in creation form for ~S"
1636 (let* ((*constants-created-since-last-init* nil)
1638 (catch 'pending-init
1639 (loop for (name form) on (cdr info) by #'cddr
1640 collect name into names
1641 collect form into forms
1642 finally (compile-make-load-form-init-forms forms))
1645 (setf (cdr circular-ref)
1646 (append (cdr circular-ref) (cdr info))))))))))))
1649 ;;;; Host compile time definitions
1651 (defun compile-in-lexenv (name lambda lexenv)
1652 (declare (ignore lexenv))
1653 (compile name lambda))
1656 (defun eval-in-lexenv (form lexenv)
1657 (declare (ignore lexenv))