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-lambdalike
876 :debug-name (debug-namify "top level local call ~S"
878 ;; KLUDGE: we do this so that we get to have
879 ;; nice debug returnness in functions defined
881 :allow-debug-catch-tag t))
882 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
883 :source-name (or name '.anonymous.)
884 :debug-name (unless name
887 (assert-global-function-definition-type name locall-fun))
888 (setf (functional-entry-fun fun) locall-fun
889 (functional-kind fun) :external
890 (functional-has-external-references-p fun) t)
893 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
894 ;;; description of the result.
895 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
896 ;;; into core and return the compiled FUNCTION value.
897 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
898 ;;; into the fasl file and return a dump handle.
900 ;;; If NAME is provided, then we try to use it as the name of the
901 ;;; function for debugging/diagnostic information.
902 (defun %compile (lambda-expression
907 ;; This magical idiom seems to be the appropriate
908 ;; path for compiling standalone LAMBDAs, judging
909 ;; from the CMU CL code and experiment, so it's a
910 ;; nice default for things where we don't have a
911 ;; real source path (as in e.g. inside CL:COMPILE).
912 '(original-source-start 0 0)))
914 (legal-fun-name-or-type-error name))
915 (let* ((*lexenv* (make-lexenv :policy *policy*))
916 (fun (make-functional-from-toplevel-lambda lambda-expression
920 ;; FIXME: The compile-it code from here on is sort of a
921 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
922 ;; better to find a way to share the code there; or
923 ;; alternatively, to use this code to replace the code there.
924 ;; (The second alternative might be pretty easy if we used
925 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
926 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
928 (locall-analyze-clambdas-until-done (list fun))
930 (multiple-value-bind (components-from-dfo top-components hairy-top)
931 (find-initial-dfo (list fun))
933 (let ((*all-components* (append components-from-dfo top-components)))
934 ;; FIXME: This is more monkey see monkey do based on CMU CL
935 ;; code. If anyone figures out why to only prescan HAIRY-TOP
936 ;; and TOP-COMPONENTS here, instead of *ALL-COMPONENTS* or
937 ;; some other combination of results from FIND-INITIAL-VALUES,
938 ;; it'd be good to explain it.
939 (mapc #'preallocate-physenvs-for-toplevelish-lambdas hairy-top)
940 (mapc #'preallocate-physenvs-for-toplevelish-lambdas top-components)
941 (dolist (component-from-dfo components-from-dfo)
942 (compile-component component-from-dfo)
943 (replace-toplevel-xeps component-from-dfo)))
945 (let ((entry-table (etypecase *compile-object*
946 (fasl-output (fasl-output-entry-table
948 (core-object (core-object-entry-table
949 *compile-object*)))))
950 (multiple-value-bind (result found-p)
951 (gethash (leaf-info fun) entry-table)
955 ;; KLUDGE: This code duplicates some other code in this
956 ;; file. In the great reorganzation, the flow of program
957 ;; logic changed from the original CMUCL model, and that
958 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
959 ;; longer followed for CORE-OBJECTS, leading to BUG
960 ;; 156. This place is transparently not the right one for
961 ;; this code, but I don't have a clear enough overview of
962 ;; the compiler to know how to rearrange it all so that
963 ;; this operation fits in nicely, and it was blocking
964 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
965 ;; ((..)) (DEFUN FOO ...))
967 ;; FIXME: This KLUDGE doesn't solve all the problem in an
968 ;; ideal way, as (1) definitions typed in at the REPL
969 ;; without an INLINE declaration will give a NULL
970 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
971 ;; and (2) INLINE declarations will yield a
972 ;; FUNCTION-LAMBDA-EXPRESSION headed by
973 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
976 ;; (2) is probably fairly easy to fix -- it is, after all,
977 ;; a matter of list manipulation (or possibly of teaching
978 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
979 ;; significantly harder, as the association between
980 ;; function object and source is a tricky one.
982 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
983 ;; non-NULL list) when the function in question has been
984 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
985 ;; work when it has been compiled as part of the top-level
986 ;; EVAL strategy of compiling everything inside (LAMBDA ()
987 ;; ...). -- CSR, 2002-11-02
988 (when (core-object-p *compile-object*)
989 (fix-core-source-info *source-info* *compile-object* result))
991 (mapc #'clear-ir1-info components-from-dfo)
994 (defun process-toplevel-cold-fset (name lambda-expression path)
995 (unless (producing-fasl-file)
996 (error "can't COLD-FSET except in a fasl file"))
997 (legal-fun-name-or-type-error name)
998 (fasl-dump-cold-fset name
999 (%compile lambda-expression
1006 ;;; Process a top level FORM with the specified source PATH.
1007 ;;; * If this is a magic top level form, then do stuff.
1008 ;;; * If this is a macro, then expand it.
1009 ;;; * Otherwise, just compile it.
1011 ;;; COMPILE-TIME-TOO is as defined in ANSI
1012 ;;; "3.2.3.1 Processing of Top Level Forms".
1013 (defun process-toplevel-form (form path compile-time-too)
1015 (declare (list path))
1017 (catch 'process-toplevel-form-error-abort
1018 (let* ((path (or (gethash form *source-paths*) (cons form path)))
1019 (*compiler-error-bailout*
1021 (convert-and-maybe-compile
1022 `(error 'simple-program-error
1023 :format-control "execution of a form compiled with errors:~% ~S"
1024 :format-arguments (list ',form))
1026 (throw 'process-toplevel-form-error-abort nil))))
1028 (flet ((default-processor (form)
1029 ;; When we're cross-compiling, consider: what should we
1030 ;; do when we hit e.g.
1031 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1032 ;; (DEFUN FOO (X) (+ 7 X)))?
1033 ;; DEFUN has a macro definition in the cross-compiler,
1034 ;; and a different macro definition in the target
1035 ;; compiler. The only sensible thing is to use the
1036 ;; target compiler's macro definition, since the
1037 ;; cross-compiler's macro is in general into target
1038 ;; functions which can't meaningfully be executed at
1039 ;; cross-compilation time. So make sure we do the EVAL
1040 ;; here, before we macroexpand.
1042 ;; Then things get even dicier with something like
1043 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1044 ;; where we have to make sure that we don't uncross
1045 ;; the SB!XC: prefix before we do EVAL, because otherwise
1046 ;; we'd be trying to redefine the cross-compilation host's
1049 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1052 (when compile-time-too
1053 (eval form)) ; letting xc host EVAL do its own macroexpansion
1054 (let* (;; (We uncross the operator name because things
1055 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1056 ;; should be equivalent to their CL: counterparts
1057 ;; when being compiled as target code. We leave
1058 ;; the rest of the form uncrossed because macros
1059 ;; might yet expand into EVAL-WHEN stuff, and
1060 ;; things inside EVAL-WHEN can't be uncrossed
1061 ;; until after we've EVALed them in the
1062 ;; cross-compilation host.)
1063 (slightly-uncrossed (cons (uncross (first form))
1065 (expanded (preprocessor-macroexpand-1
1066 slightly-uncrossed)))
1067 (if (eq expanded slightly-uncrossed)
1068 ;; (Now that we're no longer processing toplevel
1069 ;; forms, and hence no longer need to worry about
1070 ;; EVAL-WHEN, we can uncross everything.)
1071 (convert-and-maybe-compile expanded path)
1072 ;; (We have to demote COMPILE-TIME-TOO to NIL
1073 ;; here, no matter what it was before, since
1074 ;; otherwise we'd tend to EVAL subforms more than
1075 ;; once, because of WHEN COMPILE-TIME-TOO form
1077 (process-toplevel-form expanded path nil))))
1078 ;; When we're not cross-compiling, we only need to
1079 ;; macroexpand once, so we can follow the 1-thru-6
1080 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1081 ;; Top Level Forms".
1083 (let ((expanded (preprocessor-macroexpand-1 form)))
1084 (cond ((eq expanded form)
1085 (when compile-time-too
1086 (eval-in-lexenv form *lexenv*))
1087 (convert-and-maybe-compile form path))
1089 (process-toplevel-form expanded
1091 compile-time-too))))))
1094 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1095 ;; (1) SBCL gets smart enough to handle global
1096 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1097 ;; implementors start using symbol macros in a way which
1098 ;; interacts with SB-XC/CL distinction.)
1099 (convert-and-maybe-compile form path)
1101 (default-processor form)
1102 (flet ((need-at-least-one-arg (form)
1104 (compiler-error "~S form is too short: ~S"
1108 ;; In the cross-compiler, top level COLD-FSET arranges
1109 ;; for static linking at cold init time.
1112 (aver (not compile-time-too))
1113 (destructuring-bind (cold-fset fun-name lambda-expression) form
1114 (declare (ignore cold-fset))
1115 (process-toplevel-cold-fset fun-name
1118 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1119 (need-at-least-one-arg form)
1120 (destructuring-bind (special-operator magic &rest body) form
1121 (ecase special-operator
1123 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1124 ;; "3.2.3.1 Processing of Top Level Forms".
1125 (multiple-value-bind (ct lt e)
1126 (parse-eval-when-situations magic)
1127 (let ((new-compile-time-too (or ct
1128 (and compile-time-too
1130 (cond (lt (process-toplevel-progn
1131 body path new-compile-time-too))
1132 (new-compile-time-too (eval-in-lexenv
1136 (funcall-in-macrolet-lexenv
1139 (declare (ignore funs))
1140 (process-toplevel-locally body
1142 compile-time-too))))
1144 (funcall-in-symbol-macrolet-lexenv
1147 (process-toplevel-locally body
1152 (process-toplevel-locally (rest form) path compile-time-too))
1154 (process-toplevel-progn (rest form) path compile-time-too))
1155 (t (default-processor form))))))))
1159 ;;;; load time value support
1161 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1163 ;;; Return T if we are currently producing a fasl file and hence
1164 ;;; constants need to be dumped carefully.
1165 (defun producing-fasl-file ()
1166 (fasl-output-p *compile-object*))
1168 ;;; Compile FORM and arrange for it to be called at load-time. Return
1169 ;;; the dumper handle and our best guess at the type of the object.
1170 (defun compile-load-time-value (form)
1171 (let ((lambda (compile-load-time-stuff form t)))
1173 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1174 (let ((type (leaf-type lambda)))
1175 (if (fun-type-p type)
1176 (single-value-type (fun-type-returns type))
1179 ;;; Compile the FORMS and arrange for them to be called (for effect,
1180 ;;; not value) at load time.
1181 (defun compile-make-load-form-init-forms (forms)
1182 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1183 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1185 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1186 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1187 (defun compile-load-time-stuff (form for-value)
1189 (let* ((*lexenv* (make-null-lexenv))
1190 (lambda (ir1-toplevel form *current-path* for-value)))
1191 (compile-toplevel (list lambda) t)
1194 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1195 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1196 ;;; don't try to combine this component with anything else and frob
1197 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1198 ;;; compiling, because it was merged with a run-time component.
1199 (defun compile-load-time-value-lambda (lambdas)
1200 (aver (null (cdr lambdas)))
1201 (let* ((lambda (car lambdas))
1202 (component (lambda-component lambda)))
1203 (when (eql (component-kind component) :toplevel)
1204 (setf (component-name component) (leaf-debug-name lambda))
1205 (compile-component component)
1206 (clear-ir1-info component))))
1210 (defun object-call-toplevel-lambda (tll)
1211 (declare (type functional tll))
1212 (let ((object *compile-object*))
1214 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1215 (core-object (core-call-toplevel-lambda tll object))
1218 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1219 ;;; the resulting function to be called.
1220 (defun sub-compile-toplevel-lambdas (lambdas)
1221 (declare (list lambdas))
1223 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1224 (compile-component component)
1225 (clear-ir1-info component)
1226 (object-call-toplevel-lambda tll)))
1229 ;;; Compile top level code and call the top level lambdas. We pick off
1230 ;;; top level lambdas in non-top-level components here, calling
1231 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1232 (defun compile-toplevel-lambdas (lambdas)
1233 (declare (list lambdas))
1234 (let ((len (length lambdas)))
1235 (flet ((loser (start)
1236 (or (position-if (lambda (x)
1237 (not (eq (component-kind
1238 (node-component (lambda-bind x)))
1241 ;; this used to read ":start start", but
1242 ;; start can be greater than len, which
1243 ;; is an error according to ANSI - CSR,
1245 :start (min start len))
1247 (do* ((start 0 (1+ loser))
1248 (loser (loser start) (loser start)))
1250 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1251 (unless (= loser len)
1252 (object-call-toplevel-lambda (elt lambdas loser))))))
1255 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1258 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1259 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1260 (defun compile-toplevel (lambdas load-time-value-p)
1261 (declare (list lambdas))
1263 (maybe-mumble "locall ")
1264 (locall-analyze-clambdas-until-done lambdas)
1266 (maybe-mumble "IDFO ")
1267 (multiple-value-bind (components top-components hairy-top)
1268 (find-initial-dfo lambdas)
1269 (let ((*all-components* (append components top-components)))
1270 (when *check-consistency*
1271 (maybe-mumble "[check]~%")
1272 (check-ir1-consistency *all-components*))
1274 (dolist (component (append hairy-top top-components))
1275 (pre-physenv-analyze-toplevel component))
1277 (dolist (component components)
1278 (compile-component component)
1279 (replace-toplevel-xeps component))
1281 (when *check-consistency*
1282 (maybe-mumble "[check]~%")
1283 (check-ir1-consistency *all-components*))
1285 (if load-time-value-p
1286 (compile-load-time-value-lambda lambdas)
1287 (compile-toplevel-lambdas lambdas))
1289 (mapc #'clear-ir1-info components)
1293 ;;; Actually compile any stuff that has been queued up for block
1295 (defun finish-block-compilation ()
1296 (when *block-compile*
1297 (when *toplevel-lambdas*
1298 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1299 (setq *toplevel-lambdas* ()))
1300 (setq *block-compile* nil)
1301 (setq *entry-points* nil)))
1303 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1304 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1305 (defun sub-compile-file (info)
1306 (declare (type source-info info))
1307 (let* ((*block-compile* *block-compile-arg*)
1308 (*package* (sane-package))
1310 (*lexenv* (make-null-lexenv))
1311 (*source-info* info)
1312 (sb!xc:*compile-file-pathname* nil)
1313 (sb!xc:*compile-file-truename* nil)
1314 (*toplevel-lambdas* ())
1315 (*compiler-error-bailout*
1317 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1318 (return-from sub-compile-file (values nil t t))))
1319 (*current-path* nil)
1320 (*last-source-context* nil)
1321 (*last-original-source* nil)
1322 (*last-source-form* nil)
1323 (*last-format-string* nil)
1324 (*last-format-args* nil)
1325 (*last-message-count* 0)
1326 ;; FIXME: Do we need this rebinding here? It's a literal
1327 ;; translation of the old CMU CL rebinding to
1328 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1329 ;; and it's not obvious whether the rebinding to itself is
1330 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1331 (*info-environment* *info-environment*)
1332 (*gensym-counter* 0))
1334 (with-compilation-values
1335 (sb!xc:with-compilation-unit ()
1338 (sub-sub-compile-file info)
1340 (finish-block-compilation)
1341 (let ((object *compile-object*))
1343 (fasl-output (fasl-dump-source-info info object))
1344 (core-object (fix-core-source-info info object))
1347 ;; Some errors are sufficiently bewildering that we just fail
1348 ;; immediately, without trying to recover and compile more of
1350 (input-error-in-compile-file (condition)
1351 (format *error-output*
1352 "~@<compilation aborted because of input error: ~2I~_~A~:>"
1354 (values nil t t)))))
1356 ;;; Return a pathname for the named file. The file must exist.
1357 (defun verify-source-file (pathname-designator)
1358 (let* ((pathname (pathname pathname-designator))
1359 (default-host (make-pathname :host (pathname-host pathname))))
1360 (flet ((try-with-type (path type error-p)
1361 (let ((new (merge-pathnames
1362 path (make-pathname :type type
1363 :defaults default-host))))
1364 (if (probe-file new)
1366 (and error-p (truename new))))))
1367 (cond ((typep pathname 'logical-pathname)
1368 (try-with-type pathname "LISP" t))
1369 ((probe-file pathname) pathname)
1370 ((try-with-type pathname "lisp" nil))
1371 ((try-with-type pathname "lisp" t))))))
1373 (defun elapsed-time-to-string (tsec)
1374 (multiple-value-bind (tmin sec) (truncate tsec 60)
1375 (multiple-value-bind (thr min) (truncate tmin 60)
1376 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1378 ;;; Print some junk at the beginning and end of compilation.
1379 (defun start-error-output (source-info)
1380 (declare (type source-info source-info))
1381 (let ((file-info (source-info-file-info source-info)))
1382 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1383 (namestring (file-info-name file-info))
1384 (sb!int:format-universal-time nil
1385 (file-info-write-date
1389 :print-timezone nil)))
1391 (defun finish-error-output (source-info won)
1392 (declare (type source-info source-info))
1393 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1395 (elapsed-time-to-string
1396 (- (get-universal-time)
1397 (source-info-start-time source-info))))
1400 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1401 ;;; out of the compile, then abort the writing of the output file, so
1402 ;;; that we don't overwrite it with known garbage.
1403 (defun sb!xc:compile-file
1408 (output-file (cfp-output-file-default input-file))
1409 ;; FIXME: ANSI doesn't seem to say anything about
1410 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1412 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1413 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1414 (external-format :default)
1418 ((:block-compile *block-compile-arg*) nil))
1421 "Compile INPUT-FILE, producing a corresponding fasl file and returning
1422 its filename. Besides the ANSI &KEY arguments :OUTPUT-FILE, :VERBOSE,
1423 :PRINT, and :EXTERNAL-FORMAT,the following extensions are supported:
1425 If given, internal data structures are dumped to the specified
1426 file, or if a value of T is given, to a file of *.trace type
1427 derived from the input file name.
1428 Also, as a workaround for vaguely-non-ANSI behavior, the :BLOCK-COMPILE
1429 argument is quasi-supported, to determine whether multiple
1430 functions are compiled together as a unit, resolving function
1431 references at compile time. NIL means that global function names
1432 are never resolved at compilation time. Currently NIL is the
1433 default behavior, because although section 3.2.2.3, \"Semantic
1434 Constraints\", of the ANSI spec allows this behavior under all
1435 circumstances, the compiler's runtime scales badly when it
1436 tries to do this for large files. If/when this performance
1437 problem is fixed, the block compilation default behavior will
1438 probably be made dependent on the SPEED and COMPILATION-SPEED
1439 optimization values, and the :BLOCK-COMPILE argument will probably
1442 (unless (eq external-format :default)
1443 (error "Non-:DEFAULT EXTERNAL-FORMAT values are not supported."))
1444 (let* ((fasl-output nil)
1445 (output-file-name nil)
1448 (failure-p t) ; T in case error keeps this from being set later
1449 (input-pathname (verify-source-file input-file))
1450 (source-info (make-file-source-info input-pathname))
1451 (*compiler-trace-output* nil)) ; might be modified below
1456 (setq output-file-name
1457 (sb!xc:compile-file-pathname input-file
1458 :output-file output-file))
1460 (open-fasl-output output-file-name
1461 (namestring input-pathname))))
1463 (let* ((default-trace-file-pathname
1464 (make-pathname :type "trace" :defaults input-pathname))
1465 (trace-file-pathname
1466 (if (eql trace-file t)
1467 default-trace-file-pathname
1468 (merge-pathnames trace-file
1469 default-trace-file-pathname))))
1470 (setf *compiler-trace-output*
1471 (open trace-file-pathname
1472 :if-exists :supersede
1473 :direction :output))))
1475 (when sb!xc:*compile-verbose*
1476 (start-error-output source-info))
1477 (let ((*compile-object* fasl-output)
1479 (multiple-value-setq (dummy warnings-p failure-p)
1480 (sub-compile-file source-info)))
1481 (setq compile-won t))
1483 (close-source-info source-info)
1486 (close-fasl-output fasl-output (not compile-won))
1487 (setq output-file-name
1488 (pathname (fasl-output-stream fasl-output)))
1489 (when (and compile-won sb!xc:*compile-verbose*)
1490 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1492 (when sb!xc:*compile-verbose*
1493 (finish-error-output source-info compile-won))
1495 (when *compiler-trace-output*
1496 (close *compiler-trace-output*)))
1498 (values (if output-file
1499 ;; Hack around filesystem race condition...
1500 (or (probe-file output-file-name) output-file-name)
1505 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1506 ;;; the OUTPUT-FILE argument
1508 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1509 ;;; that results from merging the INPUT-FILE with the value of
1510 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1511 ;;; default to the appropriate implementation-defined default type for
1513 (defun cfp-output-file-default (input-file)
1514 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1515 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1518 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1519 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1520 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1521 ;;; pathname, it is translated into a physical pathname as if by
1522 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1523 ;;; So I haven't really tried to make this precisely ANSI-compatible
1524 ;;; at the level of e.g. whether it returns logical pathname or a
1525 ;;; physical pathname. Patches to make it more correct are welcome.
1526 ;;; -- WHN 2000-12-09
1527 (defun sb!xc:compile-file-pathname (input-file
1529 (output-file (cfp-output-file-default
1533 "Return a pathname describing what file COMPILE-FILE would write to given
1535 (merge-pathnames output-file (merge-pathnames input-file)))
1537 ;;;; MAKE-LOAD-FORM stuff
1539 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1540 ;;; finds a constant structure, it invokes this to arrange for proper
1541 ;;; dumping. If it turns out that the constant has already been
1542 ;;; dumped, then we don't need to do anything.
1544 ;;; If the constant hasn't been dumped, then we check to see whether
1545 ;;; we are in the process of creating it. We detect this by
1546 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1547 ;;; the constants we are in the process of creating. Actually, each
1548 ;;; entry is a list of the constant and any init forms that need to be
1549 ;;; processed on behalf of that constant.
1551 ;;; It's not necessarily an error for this to happen. If we are
1552 ;;; processing the init form for some object that showed up *after*
1553 ;;; the original reference to this constant, then we just need to
1554 ;;; defer the processing of that init form. To detect this, we
1555 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1556 ;;; constants created since the last time we started processing an
1557 ;;; init form. If the constant passed to emit-make-load-form shows up
1558 ;;; in this list, then there is a circular chain through creation
1559 ;;; forms, which is an error.
1561 ;;; If there is some intervening init form, then we blow out of
1562 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1563 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1564 ;;; offending init form can be tacked onto the init forms for the
1565 ;;; circular object.
1567 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1568 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1569 ;;; whether the creation form is the magic value
1570 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1571 ;;; dumper will eventually get its hands on the object and use the
1572 ;;; normal structure dumping noise on it.
1574 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1575 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1576 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1577 ;;; dumper to use that result instead whenever it sees this constant.
1579 ;;; Now we try to compile the init form. We bind
1580 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1581 ;;; form (and any init forms that were added because of circularity
1582 ;;; detection). If this works, great. If not, we add the init forms to
1583 ;;; the init forms for the object that caused the problems and let it
1585 (defvar *constants-being-created* nil)
1586 (defvar *constants-created-since-last-init* nil)
1587 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1588 (defun emit-make-load-form (constant)
1589 (aver (fasl-output-p *compile-object*))
1590 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1591 ;; KLUDGE: This special hack is because I was too lazy
1592 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1593 ;; function of LAYOUT returns nontrivial forms when
1594 ;; building the cross-compiler but :IGNORE-IT when
1595 ;; cross-compiling or running under the target Lisp. --
1597 #+sb-xc-host (typep constant 'layout))
1598 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1600 (when (find constant *constants-created-since-last-init* :test #'eq)
1602 (throw 'pending-init circular-ref)))
1603 (multiple-value-bind (creation-form init-form)
1605 (sb!xc:make-load-form constant (make-null-lexenv))
1607 (compiler-error "(while making load form for ~S)~%~A"
1611 (:sb-just-dump-it-normally
1612 (fasl-validate-structure constant *compile-object*)
1617 (when (fasl-constant-already-dumped-p constant *compile-object*)
1618 (return-from emit-make-load-form nil))
1619 (let* ((name (let ((*print-level* 1) (*print-length* 2))
1620 (with-output-to-string (stream)
1621 (write constant :stream stream))))
1623 (list constant name init-form)
1625 (let ((*constants-being-created*
1626 (cons info *constants-being-created*))
1627 (*constants-created-since-last-init*
1628 (cons constant *constants-created-since-last-init*)))
1631 (fasl-note-handle-for-constant
1633 (compile-load-time-value
1637 (compiler-error "circular references in creation form for ~S"
1640 (let* ((*constants-created-since-last-init* nil)
1642 (catch 'pending-init
1643 (loop for (name form) on (cdr info) by #'cddr
1644 collect name into names
1645 collect form into forms
1646 finally (compile-make-load-form-init-forms forms))
1649 (setf (cdr circular-ref)
1650 (append (cdr circular-ref) (cdr info))))))))))))
1653 ;;;; Host compile time definitions
1655 (defun compile-in-lexenv (name lambda lexenv)
1656 (declare (ignore lexenv))
1657 (compile name lambda))
1660 (defun eval-in-lexenv (form lexenv)
1661 (declare (ignore lexenv))