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 ;;; This lock is siezed in the same situation: the compiler is not
91 ;;; presently thread-safe
92 (defvar *big-compiler-lock*
93 (sb!thread:make-mutex :name "big compiler lock"))
95 ;;; Count of the number of compilation units dynamically enclosed by
96 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
97 (defvar *aborted-compilation-unit-count*)
99 ;;; Mumble conditional on *COMPILE-PROGRESS*.
100 (defun maybe-mumble (&rest foo)
101 (when *compile-progress*
102 (compiler-mumble "~&")
103 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
104 (apply #'compiler-mumble foo))))
106 (deftype object () '(or fasl-output core-object null))
108 (defvar *compile-object* nil)
109 (declaim (type object *compile-object*))
111 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
113 (defmacro sb!xc:with-compilation-unit (options &body body)
115 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
116 This form affects compilations that take place within its dynamic extent. It
117 is intended to be wrapped around the compilation of all files in the same
118 system. These keywords are defined:
119 :OVERRIDE Boolean-Form
120 One of the effects of this form is to delay undefined warnings
121 until the end of the form, instead of giving them at the end of each
122 compilation. If OVERRIDE is NIL (the default), then the outermost
123 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
124 OVERRIDE true causes that form to grab any enclosed warnings, even if
125 it is enclosed by another WITH-COMPILATION-UNIT."
126 `(%with-compilation-unit (lambda () ,@body) ,@options))
128 (defun %with-compilation-unit (fn &key override)
129 (declare (type function fn))
130 (let ((succeeded-p nil))
131 (if (and *in-compilation-unit* (not override))
132 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
133 ;; ordinarily (unless OVERRIDE) basically a no-op.
135 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
137 (incf *aborted-compilation-unit-count*)))
138 ;; FIXME: Now *COMPILER-FOO-COUNT* stuff is bound in more than
139 ;; one place. If we can get rid of the IR1 interpreter, this
140 ;; should be easier to clean up.
141 (let ((*aborted-compilation-unit-count* 0)
142 (*compiler-error-count* 0)
143 (*compiler-warning-count* 0)
144 (*compiler-style-warning-count* 0)
145 (*compiler-note-count* 0)
146 (*undefined-warnings* nil)
147 (*in-compilation-unit* t))
148 (sb!thread:with-recursive-lock (*big-compiler-lock*)
149 (handler-bind ((parse-unknown-type
151 (note-undefined-reference
152 (parse-unknown-type-specifier c)
155 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
157 (incf *aborted-compilation-unit-count*))
158 (summarize-compilation-unit (not succeeded-p)))))))))
160 ;;; This is to be called at the end of a compilation unit. It signals
161 ;;; any residual warnings about unknown stuff, then prints the total
162 ;;; error counts. ABORT-P should be true when the compilation unit was
163 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
164 ;;; enclosed nested compilation units that were aborted.
165 (defun summarize-compilation-unit (abort-p)
167 (handler-bind ((style-warning #'compiler-style-warning-handler)
168 (warning #'compiler-warning-handler))
170 (let ((undefs (sort *undefined-warnings* #'string<
172 (let ((x (undefined-warning-name x)))
175 (prin1-to-string x)))))))
176 (dolist (undef undefs)
177 (let ((name (undefined-warning-name undef))
178 (kind (undefined-warning-kind undef))
179 (warnings (undefined-warning-warnings undef))
180 (undefined-warning-count (undefined-warning-count undef)))
181 (dolist (*compiler-error-context* warnings)
182 (compiler-style-warn "undefined ~(~A~): ~S" kind name))
183 (let ((warn-count (length warnings)))
184 (when (and warnings (> undefined-warning-count warn-count))
185 (let ((more (- undefined-warning-count warn-count)))
187 "~W more use~:P of undefined ~(~A~) ~S"
190 (dolist (kind '(:variable :function :type))
191 (let ((summary (mapcar #'undefined-warning-name
192 (remove kind undefs :test-not #'eq
193 :key #'undefined-warning-kind))))
196 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
197 ~% ~{~<~% ~1:;~S~>~^ ~}"
198 (cdr summary) kind summary)))))))
200 (unless (and (not abort-p)
201 (zerop *aborted-compilation-unit-count*)
202 (zerop *compiler-error-count*)
203 (zerop *compiler-warning-count*)
204 (zerop *compiler-style-warning-count*)
205 (zerop *compiler-note-count*))
206 (format *error-output* "~&")
207 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
208 (compiler-mumble "compilation unit ~:[finished~;aborted~]~
209 ~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
210 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
211 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
212 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
213 ~[~:;~:*~& printed ~W note~:P~]"
215 *aborted-compilation-unit-count*
216 *compiler-error-count*
217 *compiler-warning-count*
218 *compiler-style-warning-count*
219 *compiler-note-count*)))
220 (format *error-output* "~&"))
222 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
223 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
224 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
225 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
226 (defmacro with-compilation-values (&body body)
228 (let ((*warnings-p* nil)
230 (values (progn ,@body)
234 ;;;; component compilation
236 (defparameter *max-optimize-iterations* 3 ; ARB
238 "The upper limit on the number of times that we will consecutively do IR1
239 optimization that doesn't introduce any new code. A finite limit is
240 necessary, since type inference may take arbitrarily long to converge.")
242 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
243 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
245 ;;; Repeatedly optimize COMPONENT until no further optimizations can
246 ;;; be found or we hit our iteration limit. When we hit the limit, we
247 ;;; clear the component and block REOPTIMIZE flags to discourage the
248 ;;; next optimization attempt from pounding on the same code.
249 (defun ir1-optimize-until-done (component)
250 (declare (type component component))
252 (event ir1-optimize-until-done)
254 (cleared-reanalyze nil))
256 (when (component-reanalyze component)
258 (setq cleared-reanalyze t)
259 (setf (component-reanalyze component) nil))
260 (setf (component-reoptimize component) nil)
261 (ir1-optimize component)
262 (cond ((component-reoptimize component)
264 (when (= count *max-optimize-iterations*)
266 (cond ((retry-delayed-ir1-transforms :optimize)
270 (event ir1-optimize-maxed-out)
271 (setf (component-reoptimize component) nil)
272 (do-blocks (block component)
273 (setf (block-reoptimize block) nil))
275 ((retry-delayed-ir1-transforms :optimize)
282 (when cleared-reanalyze
283 (setf (component-reanalyze component) t)))
286 (defparameter *constraint-propagate* t)
288 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
289 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
290 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
291 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
293 ;;; FIXME: It would be good to document why it's important to have a
294 ;;; large value here, and what the drawbacks of an excessively large
295 ;;; value are; and it might also be good to make it depend on
296 ;;; optimization policy.
297 (defparameter *reoptimize-after-type-check-max* 10)
299 (defevent reoptimize-maxed-out
300 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
302 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
303 (defun dfo-as-needed (component)
304 (declare (type component component))
305 (when (component-reanalyze component)
309 (unless (component-reanalyze component)
315 ;;; Do all the IR1 phases for a non-top-level component.
316 (defun ir1-phases (component)
317 (declare (type component component))
318 (aver-live-component component)
319 (let ((*constraint-number* 0)
321 (*delayed-ir1-transforms* nil))
322 (declare (special *constraint-number* *delayed-ir1-transforms*))
324 (ir1-optimize-until-done component)
325 (when (or (component-new-functionals component)
326 (component-reanalyze-functionals component))
327 (maybe-mumble "locall ")
328 (locall-analyze-component component))
329 (dfo-as-needed component)
330 (when *constraint-propagate*
331 (maybe-mumble "constraint ")
332 (constraint-propagate component))
333 (when (retry-delayed-ir1-transforms :constraint)
334 (maybe-mumble "Rtran "))
335 (flet ((want-reoptimization-p ()
336 (or (component-reoptimize component)
337 (component-reanalyze component)
338 (component-new-functionals component)
339 (component-reanalyze-functionals component))))
340 (unless (and (want-reoptimization-p)
341 ;; We delay the generation of type checks until
342 ;; the type constraints have had time to
343 ;; propagate, else the compiler can confuse itself.
344 (< loop-count (- *reoptimize-after-type-check-max* 4)))
345 (maybe-mumble "type ")
346 (generate-type-checks component)
347 (unless (want-reoptimization-p)
349 (when (>= loop-count *reoptimize-after-type-check-max*)
350 (maybe-mumble "[reoptimize limit]")
351 (event reoptimize-maxed-out)
355 (ir1-finalize component)
358 (defun %compile-component (component)
359 (let ((*code-segment* nil)
361 (maybe-mumble "GTN ")
362 (gtn-analyze component)
363 (maybe-mumble "LTN ")
364 (ltn-analyze component)
365 (dfo-as-needed component)
366 (maybe-mumble "control ")
367 (control-analyze component #'make-ir2-block)
369 (when (ir2-component-values-receivers (component-info component))
370 (maybe-mumble "stack ")
371 (stack-analyze component)
372 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
373 ;; stack analysis. There shouldn't be any unreachable code after
374 ;; control, so this won't delete anything.
375 (dfo-as-needed component))
379 (maybe-mumble "IR2tran ")
381 (entry-analyze component)
382 (ir2-convert component)
384 (when (policy *lexenv* (>= speed compilation-speed))
385 (maybe-mumble "copy ")
386 (copy-propagate component))
388 (select-representations component)
390 (when *check-consistency*
391 (maybe-mumble "check2 ")
392 (check-ir2-consistency component))
394 (delete-unreferenced-tns component)
396 (maybe-mumble "life ")
397 (lifetime-analyze component)
399 (when *compile-progress*
400 (compiler-mumble "") ; Sync before doing more output.
401 (pre-pack-tn-stats component *error-output*))
403 (when *check-consistency*
404 (maybe-mumble "check-life ")
405 (check-life-consistency component))
407 (maybe-mumble "pack ")
410 (when *check-consistency*
411 (maybe-mumble "check-pack ")
412 (check-pack-consistency component))
414 (when *compiler-trace-output*
415 (describe-component component *compiler-trace-output*)
416 (describe-ir2-component component *compiler-trace-output*))
418 (maybe-mumble "code ")
419 (multiple-value-bind (code-length trace-table fixups)
420 (generate-code component)
422 (when *compiler-trace-output*
423 (format *compiler-trace-output*
424 "~|~%disassembly of code for ~S~2%" component)
425 (sb!disassem:disassemble-assem-segment *code-segment*
426 *compiler-trace-output*))
428 (etypecase *compile-object*
430 (maybe-mumble "fasl")
431 (fasl-dump-component component
438 (maybe-mumble "core")
439 (make-core-component component
447 ;; We're done, so don't bother keeping anything around.
448 (setf (component-info component) :dead)
452 ;;; Delete components with no external entry points before we try to
453 ;;; generate code. Unreachable closures can cause IR2 conversion to
454 ;;; puke on itself, since it is the reference to the closure which
455 ;;; normally causes the components to be combined.
456 (defun delete-if-no-entries (component)
457 (dolist (fun (component-lambdas component) (delete-component component))
458 (when (functional-has-external-references-p fun)
460 (case (functional-kind fun)
463 (unless (every (lambda (ref)
464 (eq (node-component ref) component))
468 (defun compile-component (component)
470 ;; miscellaneous sanity checks
472 ;; FIXME: These are basically pretty wimpy compared to the checks done
473 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
474 ;; make those internal consistency checks work again and use them.
475 (aver-live-component component)
476 (do-blocks (block component)
477 (aver (eql (block-component block) component)))
478 (dolist (lambda (component-lambdas component))
479 ;; sanity check to prevent weirdness from propagating insidiously as
480 ;; far from its root cause as it did in bug 138: Make sure that
481 ;; thing-to-COMPONENT links are consistent.
482 (aver (eql (lambda-component lambda) component))
483 (aver (eql (node-component (lambda-bind lambda)) component)))
485 (let* ((*component-being-compiled* component))
486 (when sb!xc:*compile-print*
487 (compiler-mumble "~&; compiling ~A: " (component-name component)))
489 (ir1-phases component)
491 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
492 (maybe-mumble "env ")
493 (physenv-analyze component)
494 (dfo-as-needed component)
496 (delete-if-no-entries component)
498 (unless (eq (block-next (component-head component))
499 (component-tail component))
500 (%compile-component component)))
502 (clear-constant-info)
504 (when sb!xc:*compile-print*
505 (compiler-mumble "~&"))
509 ;;;; clearing global data structures
511 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
512 ;;;; global data structures entirely when possible and consing up the
513 ;;;; others from scratch instead of clearing and reusing them?
515 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
516 ;;; addition to allowing stuff to be reclaimed, this is required for
517 ;;; correct assignment of constant offsets, since we need to assign a
518 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
519 ;;; slots, since they are used to keep track of functions across
520 ;;; component boundaries.
521 (defun clear-constant-info ()
522 (maphash (lambda (k v)
524 (setf (leaf-info v) nil))
526 (maphash (lambda (k v)
529 (setf (leaf-info v) nil)))
533 ;;; Blow away the REFS for all global variables, and let COMPONENT
535 (defun clear-ir1-info (component)
536 (declare (type component component))
538 (maphash (lambda (k v)
542 (delete-if #'here-p (leaf-refs v)))
543 (when (basic-var-p v)
544 (setf (basic-var-sets v)
545 (delete-if #'here-p (basic-var-sets v))))))
548 (eq (node-component x) component)))
554 ;;; Clear global variables used by the compiler.
556 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
557 ;;; and it adds overhead even when people aren't using the compiler.
558 ;;; Perhaps we could make these global vars unbound except when
559 ;;; actually in use, so that this function could go away.
560 (defun clear-stuff (&optional (debug-too t))
562 ;; Clear global tables.
563 (when (boundp '*free-funs*)
564 (clrhash *free-funs*)
565 (clrhash *free-vars*)
566 (clrhash *constants*))
568 ;; Clear debug counters and tables.
569 (clrhash *seen-blocks*)
570 (clrhash *seen-funs*)
571 (clrhash *list-conflicts-table*)
574 (clrhash *continuation-numbers*)
575 (clrhash *number-continuations*)
576 (setq *continuation-number* 0)
580 (clrhash *label-ids*)
581 (clrhash *id-labels*)
584 ;; Clear some PACK data structures (for GC purposes only).
585 (aver (not *in-pack*))
586 (dolist (sb *backend-sb-list*)
587 (when (finite-sb-p sb)
588 (fill (finite-sb-live-tns sb) nil))))
590 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
591 ;; Superficially, this seemed harmful -- the user could reasonably be
592 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
593 ;; compiled. A closer inspection showed that this actually turned out to be
594 ;; harmless in practice, because CLEAR-STUFF was only called from within
595 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
596 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
597 ;; practice, it was also useless in practice. So we don't do it any more.)
603 ;;; Print out some useful info about COMPONENT to STREAM.
604 (defun describe-component (component *standard-output*)
605 (declare (type component component))
606 (format t "~|~%;;;; component: ~S~2%" (component-name component))
607 (print-blocks component)
610 (defun describe-ir2-component (component *standard-output*)
611 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
612 (format t "entries:~%")
613 (dolist (entry (ir2-component-entries (component-info component)))
614 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
615 (label-id (entry-info-offset entry))
616 (entry-info-name entry)
617 (entry-info-closure-p entry)))
619 (pre-pack-tn-stats component *standard-output*)
621 (print-ir2-blocks component)
627 ;;;; When reading from a file, we have to keep track of some source
628 ;;;; information. We also exploit our ability to back up for printing
629 ;;;; the error context and for recovering from errors.
631 ;;;; The interface we provide to this stuff is the stream-oid
632 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
633 ;;;; of getting the next source form.
635 ;;; A FILE-INFO structure holds all the source information for a
637 (defstruct (file-info (:copier nil))
638 ;; If a file, the truename of the corresponding source file. If from
639 ;; a Lisp form, :LISP. If from a stream, :STREAM.
640 (name (missing-arg) :type (or pathname (member :lisp :stream)))
641 ;; the defaulted, but not necessarily absolute file name (i.e. prior
642 ;; to TRUENAME call.) Null if not a file. This is used to set
643 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
645 (untruename nil :type (or pathname null))
646 ;; the file's write date (if relevant)
647 (write-date nil :type (or unsigned-byte null))
648 ;; the source path root number of the first form in this file (i.e.
649 ;; the total number of forms converted previously in this
651 (source-root 0 :type unsigned-byte)
652 ;; parallel vectors containing the forms read out of the file and
653 ;; the file positions that reading of each form started at (i.e. the
654 ;; end of the previous form)
655 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
656 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
658 ;;; The SOURCE-INFO structure provides a handle on all the source
659 ;;; information for an entire compilation.
660 (defstruct (source-info
661 #-no-ansi-print-object
662 (:print-object (lambda (s stream)
663 (print-unreadable-object (s stream :type t))))
665 ;; the UT that compilation started at
666 (start-time (get-universal-time) :type unsigned-byte)
667 ;; the FILE-INFO structure for this compilation
668 (file-info nil :type (or file-info null))
669 ;; the stream that we are using to read the FILE-INFO, or NIL if
670 ;; no stream has been opened yet
671 (stream nil :type (or stream null)))
673 ;;; Given a pathname, return a SOURCE-INFO structure.
674 (defun make-file-source-info (file)
675 (let ((file-info (make-file-info :name (truename file)
677 :write-date (file-write-date file))))
679 (make-source-info :file-info file-info)))
681 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
682 (defun make-lisp-source-info (form)
683 (make-source-info :start-time (get-universal-time)
684 :file-info (make-file-info :name :lisp
688 ;;; Return a SOURCE-INFO which will read from STREAM.
689 (defun make-stream-source-info (stream)
690 (let ((file-info (make-file-info :name :stream)))
691 (make-source-info :file-info file-info
694 ;;; Return a form read from STREAM; or for EOF use the trick,
695 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
696 ;;; error happens, then convert it to standard abort-the-compilation
697 ;;; error condition (possibly recording some extra location
699 (defun read-for-compile-file (stream position)
700 (handler-case (read stream nil stream)
701 (reader-error (condition)
702 (error 'input-error-in-compile-file
704 ;; We don't need to supply :POSITION here because
705 ;; READER-ERRORs already know their position in the file.
707 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
708 ;; (and that this is not a READER-ERROR) when it encounters end of
709 ;; file in the middle of something it's trying to read.
710 (end-of-file (condition)
711 (error 'input-error-in-compile-file
713 ;; We need to supply :POSITION here because the END-OF-FILE
714 ;; condition doesn't carry the position that the user
715 ;; probably cares about, where the failed READ began.
716 :position position))))
718 ;;; If STREAM is present, return it, otherwise open a stream to the
719 ;;; current file. There must be a current file.
721 ;;; FIXME: This is probably an unnecessarily roundabout way to do
722 ;;; things now that we process a single file in COMPILE-FILE (unlike
723 ;;; the old CMU CL code, which accepted multiple files). Also, the old
725 ;;; When we open a new file, we also reset *PACKAGE* and policy.
726 ;;; This gives the effect of rebinding around each file.
727 ;;; which doesn't seem to be true now. Check to make sure that if
728 ;;; such rebinding is necessary, it's still done somewhere.
729 (defun get-source-stream (info)
730 (declare (type source-info info))
731 (or (source-info-stream info)
732 (let* ((file-info (source-info-file-info info))
733 (name (file-info-name file-info)))
734 (setf sb!xc:*compile-file-truename* name
735 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
736 (source-info-stream info) (open name :direction :input)))))
738 ;;; Close the stream in INFO if it is open.
739 (defun close-source-info (info)
740 (declare (type source-info info))
741 (let ((stream (source-info-stream info)))
742 (when stream (close stream)))
743 (setf (source-info-stream info) nil)
746 ;;; Read and compile the source file.
747 (defun sub-sub-compile-file (info)
748 (let* ((file-info (source-info-file-info info))
749 (stream (get-source-stream info)))
751 (let* ((pos (file-position stream))
752 (form (read-for-compile-file stream pos)))
753 (if (eq form stream) ; i.e., if EOF
755 (let* ((forms (file-info-forms file-info))
756 (current-idx (+ (fill-pointer forms)
757 (file-info-source-root file-info))))
758 (vector-push-extend form forms)
759 (vector-push-extend pos (file-info-positions file-info))
760 (find-source-paths form current-idx)
761 (process-toplevel-form form
762 `(original-source-start 0 ,current-idx)
765 ;;; Return the INDEX'th source form read from INFO and the position
766 ;;; where it was read.
767 (defun find-source-root (index info)
768 (declare (type index index) (type source-info info))
769 (let ((file-info (source-info-file-info info)))
770 (values (aref (file-info-forms file-info) index)
771 (aref (file-info-positions file-info) index))))
773 ;;;; processing of top level forms
775 ;;; This is called by top level form processing when we are ready to
776 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
777 ;;; convert the form, but delay compilation, pushing the result on
778 ;;; *TOPLEVEL-LAMBDAS* instead.
779 (defun convert-and-maybe-compile (form path)
780 (declare (list path))
781 (let* ((*lexenv* (make-lexenv :policy *policy*))
782 (tll (ir1-toplevel form path nil)))
783 (cond ((eq *block-compile* t) (push tll *toplevel-lambdas*))
784 (t (compile-toplevel (list tll) nil)))))
786 ;;; Macroexpand FORM in the current environment with an error handler.
787 ;;; We only expand one level, so that we retain all the intervening
788 ;;; forms in the source path.
789 (defun preprocessor-macroexpand-1 (form)
790 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
792 (compiler-error "(during macroexpansion of ~A)~%~A"
793 (let ((*print-level* 1)
795 (format nil "~S" form))
798 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
799 ;;; the forms, and PATH is the source path of the FORM they came out of.
800 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
801 (defun process-toplevel-progn (forms path compile-time-too)
802 (declare (list forms) (list path))
804 (process-toplevel-form form path compile-time-too)))
806 ;;; Process a top level use of LOCALLY, or anything else (e.g.
807 ;;; MACROLET) at top level which has declarations and ordinary forms.
808 ;;; We parse declarations and then recursively process the body.
809 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
810 (declare (list path))
811 (multiple-value-bind (forms decls) (parse-body body nil)
813 (process-decls decls vars funs (make-continuation)))
814 ;; Binding *POLICY* is pretty much of a hack, since it
815 ;; causes LOCALLY to "capture" enclosed proclamations. It
816 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
817 ;; value of *POLICY* as the policy. The need for this hack
818 ;; is due to the quirk that there is no way to represent in
819 ;; a POLICY that an optimize quality came from the default.
821 ;; FIXME: Ideally, something should be done so that DECLAIM
822 ;; inside LOCALLY works OK. Failing that, at least we could
823 ;; issue a warning instead of silently screwing up.
824 (*policy* (lexenv-policy *lexenv*)))
825 (process-toplevel-progn forms path compile-time-too))))
827 ;;; Parse an EVAL-WHEN situations list, returning three flags,
828 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
829 ;;; the types of situations present in the list.
830 (defun parse-eval-when-situations (situations)
831 (when (or (not (listp situations))
832 (set-difference situations
839 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
840 (let ((deprecated-names (intersection situations '(compile load eval))))
841 (when deprecated-names
842 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
844 (values (intersection '(:compile-toplevel compile)
846 (intersection '(:load-toplevel load) situations)
847 (intersection '(:execute eval) situations)))
850 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
851 (defun functional-components (f)
852 (declare (type functional f))
854 (clambda (list (lambda-component f)))
855 (optional-dispatch (let ((result nil))
856 (labels ((frob (clambda)
857 (pushnew (lambda-component clambda)
859 (maybe-frob (maybe-clambda)
861 (frob maybe-clambda))))
862 (mapc #'frob (optional-dispatch-entry-points f))
863 (maybe-frob (optional-dispatch-more-entry f))
864 (maybe-frob (optional-dispatch-main-entry f)))))))
866 (defun make-functional-from-toplevel-lambda (definition
870 ;; I'd thought NIL should
871 ;; work, but it doesn't.
874 (let* ((*current-path* path)
875 (component (make-empty-component))
876 (*current-component* component))
877 (setf (component-name component)
878 (debug-namify "~S initial component" name))
879 (setf (component-kind component) :initial)
880 (let* ((locall-fun (ir1-convert-lambdalike
882 :debug-name (debug-namify "top level local call ~S"
884 ;; KLUDGE: we do this so that we get to have
885 ;; nice debug returnness in functions defined
887 :allow-debug-catch-tag t))
888 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
889 :source-name (or name '.anonymous.)
890 :debug-name (unless name
893 (assert-global-function-definition-type name locall-fun))
894 (setf (functional-entry-fun fun) locall-fun
895 (functional-kind fun) :external
896 (functional-has-external-references-p fun) t)
899 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
900 ;;; description of the result.
901 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
902 ;;; into core and return the compiled FUNCTION value.
903 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
904 ;;; into the fasl file and return a dump handle.
906 ;;; If NAME is provided, then we try to use it as the name of the
907 ;;; function for debugging/diagnostic information.
908 (defun %compile (lambda-expression
913 ;; This magical idiom seems to be the appropriate
914 ;; path for compiling standalone LAMBDAs, judging
915 ;; from the CMU CL code and experiment, so it's a
916 ;; nice default for things where we don't have a
917 ;; real source path (as in e.g. inside CL:COMPILE).
918 '(original-source-start 0 0)))
920 (legal-fun-name-or-type-error name))
921 (let* ((*lexenv* (make-lexenv :policy *policy*))
922 (fun (make-functional-from-toplevel-lambda lambda-expression
926 ;; FIXME: The compile-it code from here on is sort of a
927 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
928 ;; better to find a way to share the code there; or
929 ;; alternatively, to use this code to replace the code there.
930 ;; (The second alternative might be pretty easy if we used
931 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
932 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
934 (locall-analyze-clambdas-until-done (list fun))
936 (multiple-value-bind (components-from-dfo top-components hairy-top)
937 (find-initial-dfo (list fun))
939 (let ((*all-components* (append components-from-dfo top-components)))
940 ;; FIXME: This is more monkey see monkey do based on CMU CL
941 ;; code. If anyone figures out why to only prescan HAIRY-TOP
942 ;; and TOP-COMPONENTS here, instead of *ALL-COMPONENTS* or
943 ;; some other combination of results from FIND-INITIAL-VALUES,
944 ;; it'd be good to explain it.
945 (mapc #'preallocate-physenvs-for-toplevelish-lambdas hairy-top)
946 (mapc #'preallocate-physenvs-for-toplevelish-lambdas top-components)
947 (dolist (component-from-dfo components-from-dfo)
948 (compile-component component-from-dfo)
949 (replace-toplevel-xeps component-from-dfo)))
951 (let ((entry-table (etypecase *compile-object*
952 (fasl-output (fasl-output-entry-table
954 (core-object (core-object-entry-table
955 *compile-object*)))))
956 (multiple-value-bind (result found-p)
957 (gethash (leaf-info fun) entry-table)
961 ;; KLUDGE: This code duplicates some other code in this
962 ;; file. In the great reorganzation, the flow of program
963 ;; logic changed from the original CMUCL model, and that
964 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
965 ;; longer followed for CORE-OBJECTS, leading to BUG
966 ;; 156. This place is transparently not the right one for
967 ;; this code, but I don't have a clear enough overview of
968 ;; the compiler to know how to rearrange it all so that
969 ;; this operation fits in nicely, and it was blocking
970 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
971 ;; ((..)) (DEFUN FOO ...))
973 ;; FIXME: This KLUDGE doesn't solve all the problem in an
974 ;; ideal way, as (1) definitions typed in at the REPL
975 ;; without an INLINE declaration will give a NULL
976 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
977 ;; and (2) INLINE declarations will yield a
978 ;; FUNCTION-LAMBDA-EXPRESSION headed by
979 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
982 ;; (2) is probably fairly easy to fix -- it is, after all,
983 ;; a matter of list manipulation (or possibly of teaching
984 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
985 ;; significantly harder, as the association between
986 ;; function object and source is a tricky one.
988 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
989 ;; non-NULL list) when the function in question has been
990 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
991 ;; work when it has been compiled as part of the top-level
992 ;; EVAL strategy of compiling everything inside (LAMBDA ()
993 ;; ...). -- CSR, 2002-11-02
994 (when (core-object-p *compile-object*)
995 (fix-core-source-info *source-info* *compile-object* result))
997 (mapc #'clear-ir1-info components-from-dfo)
1000 (defun process-toplevel-cold-fset (name lambda-expression path)
1001 (unless (producing-fasl-file)
1002 (error "can't COLD-FSET except in a fasl file"))
1003 (legal-fun-name-or-type-error name)
1004 (fasl-dump-cold-fset name
1005 (%compile lambda-expression
1012 ;;; Process a top level FORM with the specified source PATH.
1013 ;;; * If this is a magic top level form, then do stuff.
1014 ;;; * If this is a macro, then expand it.
1015 ;;; * Otherwise, just compile it.
1017 ;;; COMPILE-TIME-TOO is as defined in ANSI
1018 ;;; "3.2.3.1 Processing of Top Level Forms".
1019 (defun process-toplevel-form (form path compile-time-too)
1021 (declare (list path))
1023 (catch 'process-toplevel-form-error-abort
1024 (let* ((path (or (gethash form *source-paths*) (cons form path)))
1025 (*compiler-error-bailout*
1027 (convert-and-maybe-compile
1028 `(error 'simple-program-error
1029 :format-control "execution of a form compiled with errors:~% ~S"
1030 :format-arguments (list ',form))
1032 (throw 'process-toplevel-form-error-abort nil))))
1034 (flet ((default-processor (form)
1035 ;; When we're cross-compiling, consider: what should we
1036 ;; do when we hit e.g.
1037 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1038 ;; (DEFUN FOO (X) (+ 7 X)))?
1039 ;; DEFUN has a macro definition in the cross-compiler,
1040 ;; and a different macro definition in the target
1041 ;; compiler. The only sensible thing is to use the
1042 ;; target compiler's macro definition, since the
1043 ;; cross-compiler's macro is in general into target
1044 ;; functions which can't meaningfully be executed at
1045 ;; cross-compilation time. So make sure we do the EVAL
1046 ;; here, before we macroexpand.
1048 ;; Then things get even dicier with something like
1049 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1050 ;; where we have to make sure that we don't uncross
1051 ;; the SB!XC: prefix before we do EVAL, because otherwise
1052 ;; we'd be trying to redefine the cross-compilation host's
1055 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1058 (when compile-time-too
1059 (eval form)) ; letting xc host EVAL do its own macroexpansion
1060 (let* (;; (We uncross the operator name because things
1061 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1062 ;; should be equivalent to their CL: counterparts
1063 ;; when being compiled as target code. We leave
1064 ;; the rest of the form uncrossed because macros
1065 ;; might yet expand into EVAL-WHEN stuff, and
1066 ;; things inside EVAL-WHEN can't be uncrossed
1067 ;; until after we've EVALed them in the
1068 ;; cross-compilation host.)
1069 (slightly-uncrossed (cons (uncross (first form))
1071 (expanded (preprocessor-macroexpand-1
1072 slightly-uncrossed)))
1073 (if (eq expanded slightly-uncrossed)
1074 ;; (Now that we're no longer processing toplevel
1075 ;; forms, and hence no longer need to worry about
1076 ;; EVAL-WHEN, we can uncross everything.)
1077 (convert-and-maybe-compile expanded path)
1078 ;; (We have to demote COMPILE-TIME-TOO to NIL
1079 ;; here, no matter what it was before, since
1080 ;; otherwise we'd tend to EVAL subforms more than
1081 ;; once, because of WHEN COMPILE-TIME-TOO form
1083 (process-toplevel-form expanded path nil))))
1084 ;; When we're not cross-compiling, we only need to
1085 ;; macroexpand once, so we can follow the 1-thru-6
1086 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1087 ;; Top Level Forms".
1089 (let ((expanded (preprocessor-macroexpand-1 form)))
1090 (cond ((eq expanded form)
1091 (when compile-time-too
1092 (eval-in-lexenv form *lexenv*))
1093 (convert-and-maybe-compile form path))
1095 (process-toplevel-form expanded
1097 compile-time-too))))))
1100 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1101 ;; (1) SBCL gets smart enough to handle global
1102 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1103 ;; implementors start using symbol macros in a way which
1104 ;; interacts with SB-XC/CL distinction.)
1105 (convert-and-maybe-compile form path)
1107 (default-processor form)
1108 (flet ((need-at-least-one-arg (form)
1110 (compiler-error "~S form is too short: ~S"
1114 ;; In the cross-compiler, top level COLD-FSET arranges
1115 ;; for static linking at cold init time.
1118 (aver (not compile-time-too))
1119 (destructuring-bind (cold-fset fun-name lambda-expression) form
1120 (declare (ignore cold-fset))
1121 (process-toplevel-cold-fset fun-name
1124 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1125 (need-at-least-one-arg form)
1126 (destructuring-bind (special-operator magic &rest body) form
1127 (ecase special-operator
1129 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1130 ;; "3.2.3.1 Processing of Top Level Forms".
1131 (multiple-value-bind (ct lt e)
1132 (parse-eval-when-situations magic)
1133 (let ((new-compile-time-too (or ct
1134 (and compile-time-too
1136 (cond (lt (process-toplevel-progn
1137 body path new-compile-time-too))
1138 (new-compile-time-too (eval-in-lexenv
1142 (funcall-in-macrolet-lexenv
1145 (declare (ignore funs))
1146 (process-toplevel-locally body
1148 compile-time-too))))
1150 (funcall-in-symbol-macrolet-lexenv
1153 (process-toplevel-locally body
1158 (process-toplevel-locally (rest form) path compile-time-too))
1160 (process-toplevel-progn (rest form) path compile-time-too))
1161 (t (default-processor form))))))))
1165 ;;;; load time value support
1167 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1169 ;;; Return T if we are currently producing a fasl file and hence
1170 ;;; constants need to be dumped carefully.
1171 (defun producing-fasl-file ()
1172 (fasl-output-p *compile-object*))
1174 ;;; Compile FORM and arrange for it to be called at load-time. Return
1175 ;;; the dumper handle and our best guess at the type of the object.
1176 (defun compile-load-time-value (form)
1177 (let ((lambda (compile-load-time-stuff form t)))
1179 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1180 (let ((type (leaf-type lambda)))
1181 (if (fun-type-p type)
1182 (single-value-type (fun-type-returns type))
1185 ;;; Compile the FORMS and arrange for them to be called (for effect,
1186 ;;; not value) at load time.
1187 (defun compile-make-load-form-init-forms (forms)
1188 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1189 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1191 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1192 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1193 (defun compile-load-time-stuff (form for-value)
1195 (let* ((*lexenv* (make-null-lexenv))
1196 (lambda (ir1-toplevel form *current-path* for-value)))
1197 (compile-toplevel (list lambda) t)
1200 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1201 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1202 ;;; don't try to combine this component with anything else and frob
1203 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1204 ;;; compiling, because it was merged with a run-time component.
1205 (defun compile-load-time-value-lambda (lambdas)
1206 (aver (null (cdr lambdas)))
1207 (let* ((lambda (car lambdas))
1208 (component (lambda-component lambda)))
1209 (when (eql (component-kind component) :toplevel)
1210 (setf (component-name component) (leaf-debug-name lambda))
1211 (compile-component component)
1212 (clear-ir1-info component))))
1216 (defun object-call-toplevel-lambda (tll)
1217 (declare (type functional tll))
1218 (let ((object *compile-object*))
1220 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1221 (core-object (core-call-toplevel-lambda tll object))
1224 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1225 ;;; the resulting function to be called.
1226 (defun sub-compile-toplevel-lambdas (lambdas)
1227 (declare (list lambdas))
1229 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1230 (compile-component component)
1231 (clear-ir1-info component)
1232 (object-call-toplevel-lambda tll)))
1235 ;;; Compile top level code and call the top level lambdas. We pick off
1236 ;;; top level lambdas in non-top-level components here, calling
1237 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1238 (defun compile-toplevel-lambdas (lambdas)
1239 (declare (list lambdas))
1240 (let ((len (length lambdas)))
1241 (flet ((loser (start)
1242 (or (position-if (lambda (x)
1243 (not (eq (component-kind
1244 (node-component (lambda-bind x)))
1247 ;; this used to read ":start start", but
1248 ;; start can be greater than len, which
1249 ;; is an error according to ANSI - CSR,
1251 :start (min start len))
1253 (do* ((start 0 (1+ loser))
1254 (loser (loser start) (loser start)))
1256 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1257 (unless (= loser len)
1258 (object-call-toplevel-lambda (elt lambdas loser))))))
1261 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1264 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1265 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1266 (defun compile-toplevel (lambdas load-time-value-p)
1267 (declare (list lambdas))
1269 (maybe-mumble "locall ")
1270 (locall-analyze-clambdas-until-done lambdas)
1272 (maybe-mumble "IDFO ")
1273 (multiple-value-bind (components top-components hairy-top)
1274 (find-initial-dfo lambdas)
1275 (let ((*all-components* (append components top-components)))
1276 (when *check-consistency*
1277 (maybe-mumble "[check]~%")
1278 (check-ir1-consistency *all-components*))
1280 (dolist (component (append hairy-top top-components))
1281 (pre-physenv-analyze-toplevel component))
1283 (dolist (component components)
1284 (compile-component component)
1285 (replace-toplevel-xeps component))
1287 (when *check-consistency*
1288 (maybe-mumble "[check]~%")
1289 (check-ir1-consistency *all-components*))
1291 (if load-time-value-p
1292 (compile-load-time-value-lambda lambdas)
1293 (compile-toplevel-lambdas lambdas))
1295 (mapc #'clear-ir1-info components)
1299 ;;; Actually compile any stuff that has been queued up for block
1301 (defun finish-block-compilation ()
1302 (when *block-compile*
1303 (when *toplevel-lambdas*
1304 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1305 (setq *toplevel-lambdas* ()))
1306 (setq *block-compile* nil)
1307 (setq *entry-points* nil)))
1309 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1310 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1311 (defun sub-compile-file (info)
1312 (declare (type source-info info))
1313 (let* ((*block-compile* *block-compile-arg*)
1314 (*package* (sane-package))
1316 (*lexenv* (make-null-lexenv))
1317 (*source-info* info)
1318 (sb!xc:*compile-file-pathname* nil)
1319 (sb!xc:*compile-file-truename* nil)
1320 (*toplevel-lambdas* ())
1321 (*compiler-error-bailout*
1323 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1324 (return-from sub-compile-file (values nil t t))))
1325 (*current-path* nil)
1326 (*last-source-context* nil)
1327 (*last-original-source* nil)
1328 (*last-source-form* nil)
1329 (*last-format-string* nil)
1330 (*last-format-args* nil)
1331 (*last-message-count* 0)
1332 ;; FIXME: Do we need this rebinding here? It's a literal
1333 ;; translation of the old CMU CL rebinding to
1334 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1335 ;; and it's not obvious whether the rebinding to itself is
1336 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1337 (*info-environment* *info-environment*)
1338 (*gensym-counter* 0))
1340 (with-compilation-values
1341 (sb!xc:with-compilation-unit ()
1344 (sub-sub-compile-file info)
1346 (finish-block-compilation)
1347 (let ((object *compile-object*))
1349 (fasl-output (fasl-dump-source-info info object))
1350 (core-object (fix-core-source-info info object))
1353 ;; Some errors are sufficiently bewildering that we just fail
1354 ;; immediately, without trying to recover and compile more of
1356 (input-error-in-compile-file (condition)
1357 (format *error-output*
1358 "~@<compilation aborted because of input error: ~2I~_~A~:>"
1360 (values nil t t)))))
1362 ;;; Return a pathname for the named file. The file must exist.
1363 (defun verify-source-file (pathname-designator)
1364 (let* ((pathname (pathname pathname-designator))
1365 (default-host (make-pathname :host (pathname-host pathname))))
1366 (flet ((try-with-type (path type error-p)
1367 (let ((new (merge-pathnames
1368 path (make-pathname :type type
1369 :defaults default-host))))
1370 (if (probe-file new)
1372 (and error-p (truename new))))))
1373 (cond ((typep pathname 'logical-pathname)
1374 (try-with-type pathname "LISP" t))
1375 ((probe-file pathname) pathname)
1376 ((try-with-type pathname "lisp" nil))
1377 ((try-with-type pathname "lisp" t))))))
1379 (defun elapsed-time-to-string (tsec)
1380 (multiple-value-bind (tmin sec) (truncate tsec 60)
1381 (multiple-value-bind (thr min) (truncate tmin 60)
1382 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1384 ;;; Print some junk at the beginning and end of compilation.
1385 (defun start-error-output (source-info)
1386 (declare (type source-info source-info))
1387 (let ((file-info (source-info-file-info source-info)))
1388 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1389 (namestring (file-info-name file-info))
1390 (sb!int:format-universal-time nil
1391 (file-info-write-date
1395 :print-timezone nil)))
1397 (defun finish-error-output (source-info won)
1398 (declare (type source-info source-info))
1399 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1401 (elapsed-time-to-string
1402 (- (get-universal-time)
1403 (source-info-start-time source-info))))
1406 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1407 ;;; out of the compile, then abort the writing of the output file, so
1408 ;;; that we don't overwrite it with known garbage.
1409 (defun sb!xc:compile-file
1414 (output-file (cfp-output-file-default input-file))
1415 ;; FIXME: ANSI doesn't seem to say anything about
1416 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1418 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1419 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1420 (external-format :default)
1424 ((:block-compile *block-compile-arg*) nil))
1427 "Compile INPUT-FILE, producing a corresponding fasl file and returning
1428 its filename. Besides the ANSI &KEY arguments :OUTPUT-FILE, :VERBOSE,
1429 :PRINT, and :EXTERNAL-FORMAT,the following extensions are supported:
1431 If given, internal data structures are dumped to the specified
1432 file, or if a value of T is given, to a file of *.trace type
1433 derived from the input file name.
1434 Also, as a workaround for vaguely-non-ANSI behavior, the :BLOCK-COMPILE
1435 argument is quasi-supported, to determine whether multiple
1436 functions are compiled together as a unit, resolving function
1437 references at compile time. NIL means that global function names
1438 are never resolved at compilation time. Currently NIL is the
1439 default behavior, because although section 3.2.2.3, \"Semantic
1440 Constraints\", of the ANSI spec allows this behavior under all
1441 circumstances, the compiler's runtime scales badly when it
1442 tries to do this for large files. If/when this performance
1443 problem is fixed, the block compilation default behavior will
1444 probably be made dependent on the SPEED and COMPILATION-SPEED
1445 optimization values, and the :BLOCK-COMPILE argument will probably
1448 (unless (eq external-format :default)
1449 (error "Non-:DEFAULT EXTERNAL-FORMAT values are not supported."))
1450 (let* ((fasl-output nil)
1451 (output-file-name nil)
1454 (failure-p t) ; T in case error keeps this from being set later
1455 (input-pathname (verify-source-file input-file))
1456 (source-info (make-file-source-info input-pathname))
1457 (*compiler-trace-output* nil)) ; might be modified below
1462 (setq output-file-name
1463 (sb!xc:compile-file-pathname input-file
1464 :output-file output-file))
1466 (open-fasl-output output-file-name
1467 (namestring input-pathname))))
1469 (let* ((default-trace-file-pathname
1470 (make-pathname :type "trace" :defaults input-pathname))
1471 (trace-file-pathname
1472 (if (eql trace-file t)
1473 default-trace-file-pathname
1474 (merge-pathnames trace-file
1475 default-trace-file-pathname))))
1476 (setf *compiler-trace-output*
1477 (open trace-file-pathname
1478 :if-exists :supersede
1479 :direction :output))))
1481 (when sb!xc:*compile-verbose*
1482 (start-error-output source-info))
1483 (let ((*compile-object* fasl-output)
1485 (multiple-value-setq (dummy warnings-p failure-p)
1486 (sub-compile-file source-info)))
1487 (setq compile-won t))
1489 (close-source-info source-info)
1492 (close-fasl-output fasl-output (not compile-won))
1493 (setq output-file-name
1494 (pathname (fasl-output-stream fasl-output)))
1495 (when (and compile-won sb!xc:*compile-verbose*)
1496 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1498 (when sb!xc:*compile-verbose*
1499 (finish-error-output source-info compile-won))
1501 (when *compiler-trace-output*
1502 (close *compiler-trace-output*)))
1504 (values (if output-file
1505 ;; Hack around filesystem race condition...
1506 (or (probe-file output-file-name) output-file-name)
1511 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1512 ;;; the OUTPUT-FILE argument
1514 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1515 ;;; that results from merging the INPUT-FILE with the value of
1516 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1517 ;;; default to the appropriate implementation-defined default type for
1519 (defun cfp-output-file-default (input-file)
1520 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1521 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1524 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1525 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1526 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1527 ;;; pathname, it is translated into a physical pathname as if by
1528 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1529 ;;; So I haven't really tried to make this precisely ANSI-compatible
1530 ;;; at the level of e.g. whether it returns logical pathname or a
1531 ;;; physical pathname. Patches to make it more correct are welcome.
1532 ;;; -- WHN 2000-12-09
1533 (defun sb!xc:compile-file-pathname (input-file
1535 (output-file (cfp-output-file-default
1539 "Return a pathname describing what file COMPILE-FILE would write to given
1541 (merge-pathnames output-file (merge-pathnames input-file)))
1543 ;;;; MAKE-LOAD-FORM stuff
1545 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1546 ;;; finds a constant structure, it invokes this to arrange for proper
1547 ;;; dumping. If it turns out that the constant has already been
1548 ;;; dumped, then we don't need to do anything.
1550 ;;; If the constant hasn't been dumped, then we check to see whether
1551 ;;; we are in the process of creating it. We detect this by
1552 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1553 ;;; the constants we are in the process of creating. Actually, each
1554 ;;; entry is a list of the constant and any init forms that need to be
1555 ;;; processed on behalf of that constant.
1557 ;;; It's not necessarily an error for this to happen. If we are
1558 ;;; processing the init form for some object that showed up *after*
1559 ;;; the original reference to this constant, then we just need to
1560 ;;; defer the processing of that init form. To detect this, we
1561 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1562 ;;; constants created since the last time we started processing an
1563 ;;; init form. If the constant passed to emit-make-load-form shows up
1564 ;;; in this list, then there is a circular chain through creation
1565 ;;; forms, which is an error.
1567 ;;; If there is some intervening init form, then we blow out of
1568 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1569 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1570 ;;; offending init form can be tacked onto the init forms for the
1571 ;;; circular object.
1573 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1574 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1575 ;;; whether the creation form is the magic value
1576 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1577 ;;; dumper will eventually get its hands on the object and use the
1578 ;;; normal structure dumping noise on it.
1580 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1581 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1582 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1583 ;;; dumper to use that result instead whenever it sees this constant.
1585 ;;; Now we try to compile the init form. We bind
1586 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1587 ;;; form (and any init forms that were added because of circularity
1588 ;;; detection). If this works, great. If not, we add the init forms to
1589 ;;; the init forms for the object that caused the problems and let it
1591 (defvar *constants-being-created* nil)
1592 (defvar *constants-created-since-last-init* nil)
1593 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1594 (defun emit-make-load-form (constant)
1595 (aver (fasl-output-p *compile-object*))
1596 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1597 ;; KLUDGE: This special hack is because I was too lazy
1598 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1599 ;; function of LAYOUT returns nontrivial forms when
1600 ;; building the cross-compiler but :IGNORE-IT when
1601 ;; cross-compiling or running under the target Lisp. --
1603 #+sb-xc-host (typep constant 'layout))
1604 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1606 (when (find constant *constants-created-since-last-init* :test #'eq)
1608 (throw 'pending-init circular-ref)))
1609 (multiple-value-bind (creation-form init-form)
1611 (sb!xc:make-load-form constant (make-null-lexenv))
1613 (compiler-error "(while making load form for ~S)~%~A"
1617 (:sb-just-dump-it-normally
1618 (fasl-validate-structure constant *compile-object*)
1623 (when (fasl-constant-already-dumped-p constant *compile-object*)
1624 (return-from emit-make-load-form nil))
1625 (let* ((name (let ((*print-level* 1) (*print-length* 2))
1626 (with-output-to-string (stream)
1627 (write constant :stream stream))))
1629 (list constant name init-form)
1631 (let ((*constants-being-created*
1632 (cons info *constants-being-created*))
1633 (*constants-created-since-last-init*
1634 (cons constant *constants-created-since-last-init*)))
1637 (fasl-note-handle-for-constant
1639 (compile-load-time-value
1643 (compiler-error "circular references in creation form for ~S"
1646 (let* ((*constants-created-since-last-init* nil)
1648 (catch 'pending-init
1649 (loop for (name form) on (cdr info) by #'cddr
1650 collect name into names
1651 collect form into forms
1652 finally (compile-make-load-form-init-forms forms))
1655 (setf (cdr circular-ref)
1656 (append (cdr circular-ref) (cdr info))))))))))))
1659 ;;;; Host compile time definitions
1661 (defun compile-in-lexenv (name lambda lexenv)
1662 (declare (ignore lexenv))
1663 (compile name lambda))
1666 (defun eval-in-lexenv (form lexenv)
1667 (declare (ignore lexenv))