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* *fun-names-in-this-file*
32 *allow-instrumenting*))
34 ;;; Whether call of a function which cannot be defined causes a full
36 (defvar *flame-on-necessarily-undefined-function* nil)
38 (defvar *check-consistency* nil)
39 (defvar *all-components*)
41 ;;; Set to NIL to disable loop analysis for register allocation.
42 (defvar *loop-analyze* t)
44 ;;; Bind this to a stream to capture various internal debugging output.
45 (defvar *compiler-trace-output* nil)
47 ;;; The current block compilation state. These are initialized to the
48 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
51 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
52 ;;; argument, which overrides any internal declarations.
53 (defvar *block-compile*)
54 (defvar *block-compile-arg*)
55 (declaim (type (member nil t :specified) *block-compile* *block-compile-arg*))
56 (defvar *entry-points*)
57 (declaim (list *entry-points*))
59 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
60 ;;; lambdas resulting from compiling subforms. (In reverse order.)
61 (defvar *toplevel-lambdas*)
62 (declaim (list *toplevel-lambdas*))
64 ;;; The current non-macroexpanded toplevel form as printed when
65 ;;; *compile-print* is true.
66 (defvar *top-level-form-noted* nil)
68 (defvar sb!xc:*compile-verbose* t
70 "The default for the :VERBOSE argument to COMPILE-FILE.")
71 (defvar sb!xc:*compile-print* t
73 "The default for the :PRINT argument to COMPILE-FILE.")
74 (defvar *compile-progress* nil
76 "When this is true, the compiler prints to *STANDARD-OUTPUT* progress
77 information about the phases of compilation of each function. (This
78 is useful mainly in large block compilations.)")
80 (defvar sb!xc:*compile-file-pathname* nil
82 "The defaulted pathname of the file currently being compiled, or NIL if not
84 (defvar sb!xc:*compile-file-truename* nil
86 "The TRUENAME of the file currently being compiled, or NIL if not
89 (declaim (type (or pathname null)
90 sb!xc:*compile-file-pathname*
91 sb!xc:*compile-file-truename*))
93 ;;; the SOURCE-INFO structure for the current compilation. This is
94 ;;; null globally to indicate that we aren't currently in any
95 ;;; identifiable compilation.
96 (defvar *source-info* nil)
98 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
99 ;;; normally causes nested uses to be no-ops).
100 (defvar *in-compilation-unit* nil)
102 ;;; Count of the number of compilation units dynamically enclosed by
103 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
104 (defvar *aborted-compilation-unit-count*)
106 ;;; Mumble conditional on *COMPILE-PROGRESS*.
107 (defun maybe-mumble (&rest foo)
108 (when *compile-progress*
109 (compiler-mumble "~&")
110 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
111 (apply #'compiler-mumble foo))))
113 (deftype object () '(or fasl-output core-object null))
115 (defvar *compile-object* nil)
116 (declaim (type object *compile-object*))
118 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
120 (defmacro sb!xc:with-compilation-unit (options &body body)
122 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
123 This form affects compilations that take place within its dynamic extent. It
124 is intended to be wrapped around the compilation of all files in the same
125 system. These keywords are defined:
126 :OVERRIDE Boolean-Form
127 One of the effects of this form is to delay undefined warnings
128 until the end of the form, instead of giving them at the end of each
129 compilation. If OVERRIDE is NIL (the default), then the outermost
130 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
131 OVERRIDE true causes that form to grab any enclosed warnings, even if
132 it is enclosed by another WITH-COMPILATION-UNIT."
133 `(%with-compilation-unit (lambda () ,@body) ,@options))
135 (defun %with-compilation-unit (fn &key override)
136 (declare (type function fn))
137 (let ((succeeded-p nil))
138 (if (and *in-compilation-unit* (not override))
139 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
140 ;; ordinarily (unless OVERRIDE) basically a no-op.
142 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
144 (incf *aborted-compilation-unit-count*)))
145 (let ((*aborted-compilation-unit-count* 0)
146 (*compiler-error-count* 0)
147 (*compiler-warning-count* 0)
148 (*compiler-style-warning-count* 0)
149 (*compiler-note-count* 0)
150 (*undefined-warnings* nil)
151 (*in-compilation-unit* t))
152 (sb!thread:with-recursive-lock (*big-compiler-lock*)
153 (handler-bind ((parse-unknown-type
155 (note-undefined-reference
156 (parse-unknown-type-specifier c)
159 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
161 (incf *aborted-compilation-unit-count*))
162 (summarize-compilation-unit (not succeeded-p)))))))))
164 ;;; Is FUN-NAME something that no conforming program can rely on
165 ;;; defining as a function?
166 (defun fun-name-reserved-by-ansi-p (fun-name)
167 (eq (symbol-package (fun-name-block-name fun-name))
170 ;;; This is to be called at the end of a compilation unit. It signals
171 ;;; any residual warnings about unknown stuff, then prints the total
172 ;;; error counts. ABORT-P should be true when the compilation unit was
173 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
174 ;;; enclosed nested compilation units that were aborted.
175 (defun summarize-compilation-unit (abort-p)
177 (handler-bind ((style-warning #'compiler-style-warning-handler)
178 (warning #'compiler-warning-handler))
180 (let ((undefs (sort *undefined-warnings* #'string<
182 (let ((x (undefined-warning-name x)))
185 (prin1-to-string x)))))))
186 (dolist (undef undefs)
187 (let ((name (undefined-warning-name undef))
188 (kind (undefined-warning-kind undef))
189 (warnings (undefined-warning-warnings undef))
190 (undefined-warning-count (undefined-warning-count undef)))
191 (dolist (*compiler-error-context* warnings)
192 (if #-sb-xc-host (and (eq kind :function)
193 (fun-name-reserved-by-ansi-p name)
194 *flame-on-necessarily-undefined-function*)
199 "~@<There is no function named ~S. References to ~S in ~
200 some contexts (like starts of blocks) have special ~
201 meaning, but here it would have to be a function, ~
202 and that shouldn't be right.~:@>"
206 "~@<The ~(~A~) ~S is undefined, and its name is ~
207 reserved by ANSI CL so that even if it were ~
208 defined later, the code doing so would not be ~
211 (if (eq kind :variable)
212 (compiler-warn "undefined ~(~A~): ~S" kind name)
213 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
214 (let ((warn-count (length warnings)))
215 (when (and warnings (> undefined-warning-count warn-count))
216 (let ((more (- undefined-warning-count warn-count)))
217 (if (eq kind :variable)
219 "~W more use~:P of undefined ~(~A~) ~S"
222 "~W more use~:P of undefined ~(~A~) ~S"
223 more kind name)))))))
225 (dolist (kind '(:variable :function :type))
226 (let ((summary (mapcar #'undefined-warning-name
227 (remove kind undefs :test #'neq
228 :key #'undefined-warning-kind))))
230 (if (eq kind :variable)
232 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
233 ~% ~{~<~% ~1:;~S~>~^ ~}"
234 (cdr summary) kind summary)
236 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
237 ~% ~{~<~% ~1:;~S~>~^ ~}"
238 (cdr summary) kind summary))))))))
240 (unless (and (not abort-p)
241 (zerop *aborted-compilation-unit-count*)
242 (zerop *compiler-error-count*)
243 (zerop *compiler-warning-count*)
244 (zerop *compiler-style-warning-count*)
245 (zerop *compiler-note-count*))
246 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
247 (format *error-output* "~&compilation unit ~:[finished~;aborted~]~
248 ~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
249 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
250 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
251 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
252 ~[~:;~:*~& printed ~W note~:P~]"
254 *aborted-compilation-unit-count*
255 *compiler-error-count*
256 *compiler-warning-count*
257 *compiler-style-warning-count*
258 *compiler-note-count*))
259 (terpri *error-output*)
260 (force-output *error-output*)))
262 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
263 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
264 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
265 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
266 (defmacro with-compilation-values (&body body)
268 (let ((*warnings-p* nil)
270 (values (progn ,@body)
274 ;;;; component compilation
276 (defparameter *max-optimize-iterations* 3 ; ARB
278 "The upper limit on the number of times that we will consecutively do IR1
279 optimization that doesn't introduce any new code. A finite limit is
280 necessary, since type inference may take arbitrarily long to converge.")
282 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
283 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
285 ;;; Repeatedly optimize COMPONENT until no further optimizations can
286 ;;; be found or we hit our iteration limit. When we hit the limit, we
287 ;;; clear the component and block REOPTIMIZE flags to discourage the
288 ;;; next optimization attempt from pounding on the same code.
289 (defun ir1-optimize-until-done (component)
290 (declare (type component component))
292 (event ir1-optimize-until-done)
294 (cleared-reanalyze nil)
297 (when (component-reanalyze component)
299 (setq cleared-reanalyze t)
300 (setf (component-reanalyze component) nil))
301 (setf (component-reoptimize component) nil)
302 (ir1-optimize component fastp)
303 (cond ((component-reoptimize component)
305 (when (and (>= count *max-optimize-iterations*)
306 (not (component-reanalyze component))
307 (eq (component-reoptimize component) :maybe))
309 (cond ((retry-delayed-ir1-transforms :optimize)
313 (event ir1-optimize-maxed-out)
314 (setf (component-reoptimize component) nil)
315 (do-blocks (block component)
316 (setf (block-reoptimize block) nil))
318 ((retry-delayed-ir1-transforms :optimize)
324 (setq fastp (>= count *max-optimize-iterations*))
325 (maybe-mumble (if fastp "-" ".")))
326 (when cleared-reanalyze
327 (setf (component-reanalyze component) t)))
330 (defparameter *constraint-propagate* t)
332 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
333 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
334 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
335 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
337 ;;; FIXME: It would be good to document why it's important to have a
338 ;;; large value here, and what the drawbacks of an excessively large
339 ;;; value are; and it might also be good to make it depend on
340 ;;; optimization policy.
341 (defparameter *reoptimize-after-type-check-max* 10)
343 (defevent reoptimize-maxed-out
344 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
346 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
347 (defun dfo-as-needed (component)
348 (declare (type component component))
349 (when (component-reanalyze component)
353 (unless (component-reanalyze component)
359 ;;; Do all the IR1 phases for a non-top-level component.
360 (defun ir1-phases (component)
361 (declare (type component component))
362 (aver-live-component component)
363 (let ((*constraint-number* 0)
365 (*delayed-ir1-transforms* nil))
366 (declare (special *constraint-number* *delayed-ir1-transforms*))
368 (ir1-optimize-until-done component)
369 (when (or (component-new-functionals component)
370 (component-reanalyze-functionals component))
371 (maybe-mumble "locall ")
372 (locall-analyze-component component))
373 (dfo-as-needed component)
374 (when *constraint-propagate*
375 (maybe-mumble "constraint ")
376 (constraint-propagate component))
377 (when (retry-delayed-ir1-transforms :constraint)
378 (maybe-mumble "Rtran "))
379 (flet ((want-reoptimization-p ()
380 (or (component-reoptimize component)
381 (component-reanalyze component)
382 (component-new-functionals component)
383 (component-reanalyze-functionals component))))
384 (unless (and (want-reoptimization-p)
385 ;; We delay the generation of type checks until
386 ;; the type constraints have had time to
387 ;; propagate, else the compiler can confuse itself.
388 (< loop-count (- *reoptimize-after-type-check-max* 4)))
389 (maybe-mumble "type ")
390 (generate-type-checks component)
391 (unless (want-reoptimization-p)
393 (when (>= loop-count *reoptimize-after-type-check-max*)
394 (maybe-mumble "[reoptimize limit]")
395 (event reoptimize-maxed-out)
399 (ir1-finalize component)
402 (defun %compile-component (component)
403 (let ((*code-segment* nil)
405 (maybe-mumble "GTN ")
406 (gtn-analyze component)
407 (maybe-mumble "LTN ")
408 (ltn-analyze component)
409 (dfo-as-needed component)
410 (maybe-mumble "control ")
411 (control-analyze component #'make-ir2-block)
413 (when (or (ir2-component-values-receivers (component-info component))
414 (component-dx-lvars component))
415 (maybe-mumble "stack ")
416 (stack-analyze component)
417 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
418 ;; stack analysis. There shouldn't be any unreachable code after
419 ;; control, so this won't delete anything.
420 (dfo-as-needed component))
424 (maybe-mumble "IR2tran ")
426 (entry-analyze component)
427 (ir2-convert component)
429 (when (policy *lexenv* (>= speed compilation-speed))
430 (maybe-mumble "copy ")
431 (copy-propagate component))
433 (select-representations component)
435 (when *check-consistency*
436 (maybe-mumble "check2 ")
437 (check-ir2-consistency component))
439 (delete-unreferenced-tns component)
441 (maybe-mumble "life ")
442 (lifetime-analyze component)
444 (when *compile-progress*
445 (compiler-mumble "") ; Sync before doing more output.
446 (pre-pack-tn-stats component *standard-output*))
448 (when *check-consistency*
449 (maybe-mumble "check-life ")
450 (check-life-consistency component))
452 (maybe-mumble "pack ")
455 (when *check-consistency*
456 (maybe-mumble "check-pack ")
457 (check-pack-consistency component))
459 (when *compiler-trace-output*
460 (describe-component component *compiler-trace-output*)
461 (describe-ir2-component component *compiler-trace-output*))
463 (maybe-mumble "code ")
464 (multiple-value-bind (code-length trace-table fixup-notes)
465 (generate-code component)
468 (when *compiler-trace-output*
469 (format *compiler-trace-output*
470 "~|~%disassembly of code for ~S~2%" component)
471 (sb!disassem:disassemble-assem-segment *code-segment*
472 *compiler-trace-output*))
474 (etypecase *compile-object*
476 (maybe-mumble "fasl")
477 (fasl-dump-component component
484 (maybe-mumble "core")
485 (make-core-component component
493 ;; We're done, so don't bother keeping anything around.
494 (setf (component-info component) :dead)
498 ;;; Delete components with no external entry points before we try to
499 ;;; generate code. Unreachable closures can cause IR2 conversion to
500 ;;; puke on itself, since it is the reference to the closure which
501 ;;; normally causes the components to be combined.
502 (defun delete-if-no-entries (component)
503 (dolist (fun (component-lambdas component) (delete-component component))
504 (when (functional-has-external-references-p fun)
506 (case (functional-kind fun)
509 (unless (every (lambda (ref)
510 (eq (node-component ref) component))
514 (defun compile-component (component)
516 ;; miscellaneous sanity checks
518 ;; FIXME: These are basically pretty wimpy compared to the checks done
519 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
520 ;; make those internal consistency checks work again and use them.
521 (aver-live-component component)
522 (do-blocks (block component)
523 (aver (eql (block-component block) component)))
524 (dolist (lambda (component-lambdas component))
525 ;; sanity check to prevent weirdness from propagating insidiously as
526 ;; far from its root cause as it did in bug 138: Make sure that
527 ;; thing-to-COMPONENT links are consistent.
528 (aver (eql (lambda-component lambda) component))
529 (aver (eql (node-component (lambda-bind lambda)) component)))
531 (let* ((*component-being-compiled* component))
533 (ir1-phases component)
536 (dfo-as-needed component)
537 (find-dominators component)
538 (loop-analyze component))
541 (when (and *loop-analyze* *compiler-trace-output*)
542 (labels ((print-blocks (block)
543 (format *compiler-trace-output* " ~A~%" block)
544 (when (block-loop-next block)
545 (print-blocks (block-loop-next block))))
547 (format *compiler-trace-output* "loop=~A~%" loop)
548 (print-blocks (loop-blocks loop))
549 (dolist (l (loop-inferiors loop))
551 (print-loop (component-outer-loop component))))
554 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
555 (maybe-mumble "env ")
556 (physenv-analyze component)
557 (dfo-as-needed component)
559 (delete-if-no-entries component)
561 (unless (eq (block-next (component-head component))
562 (component-tail component))
563 (%compile-component component)))
565 (clear-constant-info)
569 ;;;; clearing global data structures
571 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
572 ;;;; global data structures entirely when possible and consing up the
573 ;;;; others from scratch instead of clearing and reusing them?
575 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
576 ;;; addition to allowing stuff to be reclaimed, this is required for
577 ;;; correct assignment of constant offsets, since we need to assign a
578 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
579 ;;; slots, since they are used to keep track of functions across
580 ;;; component boundaries.
581 (defun clear-constant-info ()
582 (maphash (lambda (k v)
584 (setf (leaf-info v) nil))
586 (maphash (lambda (k v)
589 (setf (leaf-info v) nil)))
593 ;;; Blow away the REFS for all global variables, and let COMPONENT
595 (defun clear-ir1-info (component)
596 (declare (type component component))
598 (maphash (lambda (k v)
602 (delete-if #'here-p (leaf-refs v)))
603 (when (basic-var-p v)
604 (setf (basic-var-sets v)
605 (delete-if #'here-p (basic-var-sets v))))))
608 (eq (node-component x) component)))
614 ;;; Clear global variables used by the compiler.
616 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
617 ;;; and it adds overhead even when people aren't using the compiler.
618 ;;; Perhaps we could make these global vars unbound except when
619 ;;; actually in use, so that this function could go away.
620 (defun clear-stuff (&optional (debug-too t))
622 ;; Clear global tables.
623 (when (boundp '*free-funs*)
624 (clrhash *free-funs*)
625 (clrhash *free-vars*)
626 (clrhash *constants*))
628 ;; Clear debug counters and tables.
629 (clrhash *seen-blocks*)
630 (clrhash *seen-funs*)
631 (clrhash *list-conflicts-table*)
634 (clrhash *continuation-numbers*)
635 (clrhash *number-continuations*)
636 (setq *continuation-number* 0)
640 (clrhash *label-ids*)
641 (clrhash *id-labels*)
644 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
645 ;; Superficially, this seemed harmful -- the user could reasonably be
646 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
647 ;; compiled. A closer inspection showed that this actually turned out to be
648 ;; harmless in practice, because CLEAR-STUFF was only called from within
649 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
650 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
651 ;; practice, it was also useless in practice. So we don't do it any more.)
657 ;;; Print out some useful info about COMPONENT to STREAM.
658 (defun describe-component (component *standard-output*)
659 (declare (type component component))
660 (format t "~|~%;;;; component: ~S~2%" (component-name component))
661 (print-all-blocks component)
664 (defun describe-ir2-component (component *standard-output*)
665 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
666 (format t "entries:~%")
667 (dolist (entry (ir2-component-entries (component-info component)))
668 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
669 (label-id (entry-info-offset entry))
670 (entry-info-name entry)
671 (entry-info-closure-tn entry)))
673 (pre-pack-tn-stats component *standard-output*)
675 (print-ir2-blocks component)
681 ;;;; When reading from a file, we have to keep track of some source
682 ;;;; information. We also exploit our ability to back up for printing
683 ;;;; the error context and for recovering from errors.
685 ;;;; The interface we provide to this stuff is the stream-oid
686 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
687 ;;;; of getting the next source form.
689 ;;; A FILE-INFO structure holds all the source information for a
691 (def!struct (file-info (:copier nil))
692 ;; If a file, the truename of the corresponding source file. If from
693 ;; a Lisp form, :LISP. If from a stream, :STREAM.
694 (name (missing-arg) :type (or pathname (member :lisp :stream)))
695 ;; the external format that we'll call OPEN with, if NAME is a file.
696 (external-format nil)
697 ;; the defaulted, but not necessarily absolute file name (i.e. prior
698 ;; to TRUENAME call.) Null if not a file. This is used to set
699 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
701 (untruename nil :type (or pathname null))
702 ;; the file's write date (if relevant)
703 (write-date nil :type (or unsigned-byte null))
704 ;; the source path root number of the first form in this file (i.e.
705 ;; the total number of forms converted previously in this
707 (source-root 0 :type unsigned-byte)
708 ;; parallel vectors containing the forms read out of the file and
709 ;; the file positions that reading of each form started at (i.e. the
710 ;; end of the previous form)
711 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
712 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
714 ;;; The SOURCE-INFO structure provides a handle on all the source
715 ;;; information for an entire compilation.
716 (def!struct (source-info
717 #-no-ansi-print-object
718 (:print-object (lambda (s stream)
719 (print-unreadable-object (s stream :type t))))
721 ;; the UT that compilation started at
722 (start-time (get-universal-time) :type unsigned-byte)
723 ;; the FILE-INFO structure for this compilation
724 (file-info nil :type (or file-info null))
725 ;; the stream that we are using to read the FILE-INFO, or NIL if
726 ;; no stream has been opened yet
727 (stream nil :type (or stream null)))
729 ;;; Given a pathname, return a SOURCE-INFO structure.
730 (defun make-file-source-info (file external-format)
731 (let ((file-info (make-file-info :name (truename file)
733 :external-format external-format
734 :write-date (file-write-date file))))
736 (make-source-info :file-info file-info)))
738 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
739 (defun make-lisp-source-info (form)
740 (make-source-info :start-time (get-universal-time)
741 :file-info (make-file-info :name :lisp
745 ;;; Return a SOURCE-INFO which will read from STREAM.
746 (defun make-stream-source-info (stream)
747 (let ((file-info (make-file-info :name :stream)))
748 (make-source-info :file-info file-info
751 ;;; Return a form read from STREAM; or for EOF use the trick,
752 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
753 ;;; error happens, then convert it to standard abort-the-compilation
754 ;;; error condition (possibly recording some extra location
756 (defun read-for-compile-file (stream position)
757 (handler-case (read stream nil stream)
758 (reader-error (condition)
759 (error 'input-error-in-compile-file
761 ;; We don't need to supply :POSITION here because
762 ;; READER-ERRORs already know their position in the file.
764 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
765 ;; (and that this is not a READER-ERROR) when it encounters end of
766 ;; file in the middle of something it's trying to read.
767 (end-of-file (condition)
768 (error 'input-error-in-compile-file
770 ;; We need to supply :POSITION here because the END-OF-FILE
771 ;; condition doesn't carry the position that the user
772 ;; probably cares about, where the failed READ began.
773 :position position))))
775 ;;; If STREAM is present, return it, otherwise open a stream to the
776 ;;; current file. There must be a current file.
778 ;;; FIXME: This is probably an unnecessarily roundabout way to do
779 ;;; things now that we process a single file in COMPILE-FILE (unlike
780 ;;; the old CMU CL code, which accepted multiple files). Also, the old
782 ;;; When we open a new file, we also reset *PACKAGE* and policy.
783 ;;; This gives the effect of rebinding around each file.
784 ;;; which doesn't seem to be true now. Check to make sure that if
785 ;;; such rebinding is necessary, it's still done somewhere.
786 (defun get-source-stream (info)
787 (declare (type source-info info))
788 (or (source-info-stream info)
789 (let* ((file-info (source-info-file-info info))
790 (name (file-info-name file-info))
791 (external-format (file-info-external-format file-info)))
792 (setf sb!xc:*compile-file-truename* name
793 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
794 (source-info-stream info)
795 (open name :direction :input
796 :external-format external-format)))))
798 ;;; Close the stream in INFO if it is open.
799 (defun close-source-info (info)
800 (declare (type source-info info))
801 (let ((stream (source-info-stream info)))
802 (when stream (close stream)))
803 (setf (source-info-stream info) nil)
806 ;;; Read and compile the source file.
807 (defun sub-sub-compile-file (info)
808 (let* ((file-info (source-info-file-info info))
809 (stream (get-source-stream info)))
811 (let* ((pos (file-position stream))
812 (form (read-for-compile-file stream pos)))
813 (if (eq form stream) ; i.e., if EOF
815 (let* ((forms (file-info-forms file-info))
816 (current-idx (+ (fill-pointer forms)
817 (file-info-source-root file-info))))
818 (vector-push-extend form forms)
819 (vector-push-extend pos (file-info-positions file-info))
820 (find-source-paths form current-idx)
821 (process-toplevel-form form
822 `(original-source-start 0 ,current-idx)
825 ;;; Return the INDEX'th source form read from INFO and the position
826 ;;; where it was read.
827 (defun find-source-root (index info)
828 (declare (type index index) (type source-info info))
829 (let ((file-info (source-info-file-info info)))
830 (values (aref (file-info-forms file-info) index)
831 (aref (file-info-positions file-info) index))))
833 ;;;; processing of top level forms
835 ;;; This is called by top level form processing when we are ready to
836 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
837 ;;; convert the form, but delay compilation, pushing the result on
838 ;;; *TOPLEVEL-LAMBDAS* instead.
839 (defun convert-and-maybe-compile (form path)
840 (declare (list path))
841 (let* ((*top-level-form-noted* (note-top-level-form form t))
842 (*lexenv* (make-lexenv
844 :handled-conditions *handled-conditions*
845 :disabled-package-locks *disabled-package-locks*))
846 (tll (ir1-toplevel form path nil)))
847 (if (eq *block-compile* t)
848 (push tll *toplevel-lambdas*)
849 (compile-toplevel (list tll) nil))
852 ;;; Macroexpand FORM in the current environment with an error handler.
853 ;;; We only expand one level, so that we retain all the intervening
854 ;;; forms in the source path.
855 (defun preprocessor-macroexpand-1 (form)
856 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
858 (compiler-error "(during macroexpansion of ~A)~%~A"
859 (let ((*print-level* 2)
861 (format nil "~S" form))
864 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
865 ;;; the forms, and PATH is the source path of the FORM they came out of.
866 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
867 (defun process-toplevel-progn (forms path compile-time-too)
868 (declare (list forms) (list path))
870 (process-toplevel-form form path compile-time-too)))
872 ;;; Process a top level use of LOCALLY, or anything else (e.g.
873 ;;; MACROLET) at top level which has declarations and ordinary forms.
874 ;;; We parse declarations and then recursively process the body.
875 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
876 (declare (list path))
877 (multiple-value-bind (forms decls)
878 (parse-body body :doc-string-allowed nil :toplevel t)
879 (let* ((*lexenv* (process-decls decls vars funs))
880 ;; FIXME: VALUES declaration
882 ;; Binding *POLICY* is pretty much of a hack, since it
883 ;; causes LOCALLY to "capture" enclosed proclamations. It
884 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
885 ;; value of *POLICY* as the policy. The need for this hack
886 ;; is due to the quirk that there is no way to represent in
887 ;; a POLICY that an optimize quality came from the default.
889 ;; FIXME: Ideally, something should be done so that DECLAIM
890 ;; inside LOCALLY works OK. Failing that, at least we could
891 ;; issue a warning instead of silently screwing up.
892 (*policy* (lexenv-policy *lexenv*))
893 ;; This is probably also a hack
894 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
896 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*)))
897 (process-toplevel-progn forms path compile-time-too))))
899 ;;; Parse an EVAL-WHEN situations list, returning three flags,
900 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
901 ;;; the types of situations present in the list.
902 (defun parse-eval-when-situations (situations)
903 (when (or (not (listp situations))
904 (set-difference situations
911 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
912 (let ((deprecated-names (intersection situations '(compile load eval))))
913 (when deprecated-names
914 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
916 (values (intersection '(:compile-toplevel compile)
918 (intersection '(:load-toplevel load) situations)
919 (intersection '(:execute eval) situations)))
922 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
923 (defun functional-components (f)
924 (declare (type functional f))
926 (clambda (list (lambda-component f)))
927 (optional-dispatch (let ((result nil))
928 (flet ((maybe-frob (maybe-clambda)
929 (when (and maybe-clambda
930 (promise-ready-p maybe-clambda))
931 (pushnew (lambda-component
932 (force maybe-clambda))
934 (map nil #'maybe-frob (optional-dispatch-entry-points f))
935 (maybe-frob (optional-dispatch-more-entry f))
936 (maybe-frob (optional-dispatch-main-entry f)))
939 (defun make-functional-from-toplevel-lambda (definition
943 ;; I'd thought NIL should
944 ;; work, but it doesn't.
947 (let* ((*current-path* path)
948 (component (make-empty-component))
949 (*current-component* component))
950 (setf (component-name component)
951 (debug-name 'initial-component name))
952 (setf (component-kind component) :initial)
953 (let* ((locall-fun (let ((*allow-instrumenting* t))
954 (apply #'ir1-convert-lambdalike
956 (list :source-name name))))
957 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
958 :source-name (or name '.anonymous.)
959 :debug-name (debug-name 'tl-xep name))))
961 (assert-global-function-definition-type name locall-fun))
962 (setf (functional-entry-fun fun) locall-fun
963 (functional-kind fun) :external
964 (functional-has-external-references-p fun) t)
967 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
968 ;;; description of the result.
969 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
970 ;;; into core and return the compiled FUNCTION value.
971 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
972 ;;; into the fasl file and return a dump handle.
974 ;;; If NAME is provided, then we try to use it as the name of the
975 ;;; function for debugging/diagnostic information.
976 (defun %compile (lambda-expression
981 ;; This magical idiom seems to be the appropriate
982 ;; path for compiling standalone LAMBDAs, judging
983 ;; from the CMU CL code and experiment, so it's a
984 ;; nice default for things where we don't have a
985 ;; real source path (as in e.g. inside CL:COMPILE).
986 '(original-source-start 0 0)))
988 (legal-fun-name-or-type-error name))
989 (let* ((*lexenv* (make-lexenv
991 :handled-conditions *handled-conditions*
992 :disabled-package-locks *disabled-package-locks*))
993 (fun (make-functional-from-toplevel-lambda lambda-expression
997 ;; FIXME: The compile-it code from here on is sort of a
998 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
999 ;; better to find a way to share the code there; or
1000 ;; alternatively, to use this code to replace the code there.
1001 ;; (The second alternative might be pretty easy if we used
1002 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1003 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1005 (locall-analyze-clambdas-until-done (list fun))
1007 (multiple-value-bind (components-from-dfo top-components hairy-top)
1008 (find-initial-dfo (list fun))
1009 (declare (ignore hairy-top))
1011 (let ((*all-components* (append components-from-dfo top-components)))
1012 (dolist (component-from-dfo components-from-dfo)
1013 (compile-component component-from-dfo)
1014 (replace-toplevel-xeps component-from-dfo)))
1016 (let ((entry-table (etypecase *compile-object*
1017 (fasl-output (fasl-output-entry-table
1019 (core-object (core-object-entry-table
1020 *compile-object*)))))
1021 (multiple-value-bind (result found-p)
1022 (gethash (leaf-info fun) entry-table)
1026 ;; KLUDGE: This code duplicates some other code in this
1027 ;; file. In the great reorganzation, the flow of program
1028 ;; logic changed from the original CMUCL model, and that
1029 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1030 ;; longer followed for CORE-OBJECTS, leading to BUG
1031 ;; 156. This place is transparently not the right one for
1032 ;; this code, but I don't have a clear enough overview of
1033 ;; the compiler to know how to rearrange it all so that
1034 ;; this operation fits in nicely, and it was blocking
1035 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1036 ;; ((..)) (DEFUN FOO ...))
1038 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1039 ;; ideal way, as (1) definitions typed in at the REPL
1040 ;; without an INLINE declaration will give a NULL
1041 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1042 ;; and (2) INLINE declarations will yield a
1043 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1044 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1047 ;; (2) is probably fairly easy to fix -- it is, after all,
1048 ;; a matter of list manipulation (or possibly of teaching
1049 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1050 ;; significantly harder, as the association between
1051 ;; function object and source is a tricky one.
1053 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1054 ;; non-NULL list) when the function in question has been
1055 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1056 ;; work when it has been compiled as part of the top-level
1057 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1058 ;; ...). -- CSR, 2002-11-02
1059 (when (core-object-p *compile-object*)
1060 (fix-core-source-info *source-info* *compile-object* result))
1062 (mapc #'clear-ir1-info components-from-dfo)
1065 (defun process-toplevel-cold-fset (name lambda-expression path)
1066 (unless (producing-fasl-file)
1067 (error "can't COLD-FSET except in a fasl file"))
1068 (legal-fun-name-or-type-error name)
1069 (fasl-dump-cold-fset name
1070 (%compile lambda-expression
1077 (defun note-top-level-form (form &optional finalp)
1078 (when *compile-print*
1079 (cond ((not *top-level-form-noted*)
1080 (let ((*print-length* 2)
1082 (*print-pretty* nil))
1083 (with-compiler-io-syntax
1084 (compiler-mumble "~&; ~:[compiling~;converting~] ~S"
1085 *block-compile* form)))
1088 (eq :top-level-forms *compile-print*)
1089 (neq form *top-level-form-noted*))
1090 (let ((*print-length* 1)
1092 (*print-pretty* nil))
1093 (with-compiler-io-syntax
1094 (compiler-mumble "~&; ... top level ~S" form)))
1097 *top-level-form-noted*))))
1099 ;;; Process a top level FORM with the specified source PATH.
1100 ;;; * If this is a magic top level form, then do stuff.
1101 ;;; * If this is a macro, then expand it.
1102 ;;; * Otherwise, just compile it.
1104 ;;; COMPILE-TIME-TOO is as defined in ANSI
1105 ;;; "3.2.3.1 Processing of Top Level Forms".
1106 (defun process-toplevel-form (form path compile-time-too)
1107 (declare (list path))
1109 (catch 'process-toplevel-form-error-abort
1110 (let* ((path (or (gethash form *source-paths*) (cons form path)))
1111 (*compiler-error-bailout*
1112 (lambda (&optional condition)
1113 (convert-and-maybe-compile
1114 (make-compiler-error-form condition form)
1116 (throw 'process-toplevel-form-error-abort nil))))
1118 (flet ((default-processor (form)
1119 (let ((*top-level-form-noted* (note-top-level-form form)))
1120 ;; When we're cross-compiling, consider: what should we
1121 ;; do when we hit e.g.
1122 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1123 ;; (DEFUN FOO (X) (+ 7 X)))?
1124 ;; DEFUN has a macro definition in the cross-compiler,
1125 ;; and a different macro definition in the target
1126 ;; compiler. The only sensible thing is to use the
1127 ;; target compiler's macro definition, since the
1128 ;; cross-compiler's macro is in general into target
1129 ;; functions which can't meaningfully be executed at
1130 ;; cross-compilation time. So make sure we do the EVAL
1131 ;; here, before we macroexpand.
1133 ;; Then things get even dicier with something like
1134 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1135 ;; where we have to make sure that we don't uncross
1136 ;; the SB!XC: prefix before we do EVAL, because otherwise
1137 ;; we'd be trying to redefine the cross-compilation host's
1140 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1143 (when compile-time-too
1144 (eval form)) ; letting xc host EVAL do its own macroexpansion
1145 (let* (;; (We uncross the operator name because things
1146 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1147 ;; should be equivalent to their CL: counterparts
1148 ;; when being compiled as target code. We leave
1149 ;; the rest of the form uncrossed because macros
1150 ;; might yet expand into EVAL-WHEN stuff, and
1151 ;; things inside EVAL-WHEN can't be uncrossed
1152 ;; until after we've EVALed them in the
1153 ;; cross-compilation host.)
1154 (slightly-uncrossed (cons (uncross (first form))
1156 (expanded (preprocessor-macroexpand-1
1157 slightly-uncrossed)))
1158 (if (eq expanded slightly-uncrossed)
1159 ;; (Now that we're no longer processing toplevel
1160 ;; forms, and hence no longer need to worry about
1161 ;; EVAL-WHEN, we can uncross everything.)
1162 (convert-and-maybe-compile expanded path)
1163 ;; (We have to demote COMPILE-TIME-TOO to NIL
1164 ;; here, no matter what it was before, since
1165 ;; otherwise we'd tend to EVAL subforms more than
1166 ;; once, because of WHEN COMPILE-TIME-TOO form
1168 (process-toplevel-form expanded path nil))))
1169 ;; When we're not cross-compiling, we only need to
1170 ;; macroexpand once, so we can follow the 1-thru-6
1171 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1172 ;; Top Level Forms".
1174 (let ((expanded (preprocessor-macroexpand-1 form)))
1175 (cond ((eq expanded form)
1176 (when compile-time-too
1177 (eval-in-lexenv form *lexenv*))
1178 (convert-and-maybe-compile form path))
1180 (process-toplevel-form expanded
1182 compile-time-too)))))))
1185 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1186 ;; (1) SBCL gets smart enough to handle global
1187 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1188 ;; implementors start using symbol macros in a way which
1189 ;; interacts with SB-XC/CL distinction.)
1190 (convert-and-maybe-compile form path)
1192 (default-processor form)
1193 (flet ((need-at-least-one-arg (form)
1195 (compiler-error "~S form is too short: ~S"
1199 ;; In the cross-compiler, top level COLD-FSET arranges
1200 ;; for static linking at cold init time.
1203 (aver (not compile-time-too))
1204 (destructuring-bind (cold-fset fun-name lambda-expression) form
1205 (declare (ignore cold-fset))
1206 (process-toplevel-cold-fset fun-name
1209 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1210 (need-at-least-one-arg form)
1211 (destructuring-bind (special-operator magic &rest body) form
1212 (ecase special-operator
1214 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1215 ;; "3.2.3.1 Processing of Top Level Forms".
1216 (multiple-value-bind (ct lt e)
1217 (parse-eval-when-situations magic)
1218 (let ((new-compile-time-too (or ct
1219 (and compile-time-too
1221 (cond (lt (process-toplevel-progn
1222 body path new-compile-time-too))
1223 (new-compile-time-too (eval-in-lexenv
1227 (funcall-in-macrolet-lexenv
1229 (lambda (&key funs prepend)
1230 (declare (ignore funs))
1231 (aver (null prepend))
1232 (process-toplevel-locally body
1237 (funcall-in-symbol-macrolet-lexenv
1239 (lambda (&key vars prepend)
1240 (aver (null prepend))
1241 (process-toplevel-locally body
1247 (process-toplevel-locally (rest form) path compile-time-too))
1249 (process-toplevel-progn (rest form) path compile-time-too))
1250 (t (default-processor form))))))))
1254 ;;;; load time value support
1256 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1258 ;;; Return T if we are currently producing a fasl file and hence
1259 ;;; constants need to be dumped carefully.
1260 (defun producing-fasl-file ()
1261 (fasl-output-p *compile-object*))
1263 ;;; Compile FORM and arrange for it to be called at load-time. Return
1264 ;;; the dumper handle and our best guess at the type of the object.
1265 (defun compile-load-time-value (form)
1266 (let ((lambda (compile-load-time-stuff form t)))
1268 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1269 (let ((type (leaf-type lambda)))
1270 (if (fun-type-p type)
1271 (single-value-type (fun-type-returns type))
1274 ;;; Compile the FORMS and arrange for them to be called (for effect,
1275 ;;; not value) at load time.
1276 (defun compile-make-load-form-init-forms (forms)
1277 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1278 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1280 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1281 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1282 (defun compile-load-time-stuff (form for-value)
1284 (let* ((*lexenv* (make-null-lexenv))
1285 (lambda (ir1-toplevel form *current-path* for-value)))
1286 (compile-toplevel (list lambda) t)
1289 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1290 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1291 ;;; don't try to combine this component with anything else and frob
1292 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1293 ;;; compiling, because it was merged with a run-time component.
1294 (defun compile-load-time-value-lambda (lambdas)
1295 (aver (null (cdr lambdas)))
1296 (let* ((lambda (car lambdas))
1297 (component (lambda-component lambda)))
1298 (when (eql (component-kind component) :toplevel)
1299 (setf (component-name component) (leaf-debug-name lambda))
1300 (compile-component component)
1301 (clear-ir1-info component))))
1305 (defun object-call-toplevel-lambda (tll)
1306 (declare (type functional tll))
1307 (let ((object *compile-object*))
1309 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1310 (core-object (core-call-toplevel-lambda tll object))
1313 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1314 ;;; the resulting function to be called.
1315 (defun sub-compile-toplevel-lambdas (lambdas)
1316 (declare (list lambdas))
1318 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1319 (compile-component component)
1320 (clear-ir1-info component)
1321 (object-call-toplevel-lambda tll)))
1324 ;;; Compile top level code and call the top level lambdas. We pick off
1325 ;;; top level lambdas in non-top-level components here, calling
1326 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1327 (defun compile-toplevel-lambdas (lambdas)
1328 (declare (list lambdas))
1329 (let ((len (length lambdas)))
1330 (flet ((loser (start)
1331 (or (position-if (lambda (x)
1332 (not (eq (component-kind
1333 (node-component (lambda-bind x)))
1336 ;; this used to read ":start start", but
1337 ;; start can be greater than len, which
1338 ;; is an error according to ANSI - CSR,
1340 :start (min start len))
1342 (do* ((start 0 (1+ loser))
1343 (loser (loser start) (loser start)))
1345 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1346 (unless (= loser len)
1347 (object-call-toplevel-lambda (elt lambdas loser))))))
1350 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1353 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1354 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1355 (defun compile-toplevel (lambdas load-time-value-p)
1356 (declare (list lambdas))
1358 (maybe-mumble "locall ")
1359 (locall-analyze-clambdas-until-done lambdas)
1361 (maybe-mumble "IDFO ")
1362 (multiple-value-bind (components top-components hairy-top)
1363 (find-initial-dfo lambdas)
1364 (let ((*all-components* (append components top-components)))
1365 (when *check-consistency*
1366 (maybe-mumble "[check]~%")
1367 (check-ir1-consistency *all-components*))
1369 (dolist (component (append hairy-top top-components))
1370 (pre-physenv-analyze-toplevel component))
1372 (dolist (component components)
1373 (compile-component component)
1374 (replace-toplevel-xeps component))
1376 (when *check-consistency*
1377 (maybe-mumble "[check]~%")
1378 (check-ir1-consistency *all-components*))
1380 (if load-time-value-p
1381 (compile-load-time-value-lambda lambdas)
1382 (compile-toplevel-lambdas lambdas))
1384 (mapc #'clear-ir1-info components)
1388 ;;; Actually compile any stuff that has been queued up for block
1390 (defun finish-block-compilation ()
1391 (when *block-compile*
1392 (when *compile-print*
1393 (compiler-mumble "~&; block compiling converted top level forms..."))
1394 (when *toplevel-lambdas*
1395 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1396 (setq *toplevel-lambdas* ()))
1397 (setq *block-compile* nil)
1398 (setq *entry-points* nil)))
1400 (defun handle-condition-p (condition)
1402 (etypecase *compiler-error-context*
1404 (node-lexenv *compiler-error-context*))
1405 (compiler-error-context
1406 (let ((lexenv (compiler-error-context-lexenv
1407 *compiler-error-context*)))
1411 (let ((muffles (lexenv-handled-conditions lexenv)))
1412 (if (null muffles) ; common case
1414 (dolist (muffle muffles nil)
1415 (destructuring-bind (typespec . restart-name) muffle
1416 (when (and (typep condition typespec)
1417 (find-restart restart-name condition))
1420 (defun handle-condition-handler (condition)
1422 (etypecase *compiler-error-context*
1424 (node-lexenv *compiler-error-context*))
1425 (compiler-error-context
1426 (let ((lexenv (compiler-error-context-lexenv
1427 *compiler-error-context*)))
1431 (let ((muffles (lexenv-handled-conditions lexenv)))
1433 (dolist (muffle muffles (bug "fell through"))
1434 (destructuring-bind (typespec . restart-name) muffle
1435 (when (typep condition typespec)
1436 (awhen (find-restart restart-name condition)
1437 (invoke-restart it))))))))
1439 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1440 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1441 (defun sub-compile-file (info)
1442 (declare (type source-info info))
1443 (let ((*package* (sane-package))
1444 (*readtable* *readtable*)
1445 (sb!xc:*compile-file-pathname* nil) ; really bound in
1446 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1448 (*handled-conditions* *handled-conditions*)
1449 (*disabled-package-locks* *disabled-package-locks*)
1450 (*lexenv* (make-null-lexenv))
1451 (*block-compile* *block-compile-arg*)
1452 (*source-info* info)
1453 (*toplevel-lambdas* ())
1454 (*fun-names-in-this-file* ())
1455 (*allow-instrumenting* nil)
1456 (*compiler-error-bailout*
1458 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1459 (return-from sub-compile-file (values nil t t))))
1460 (*current-path* nil)
1461 (*last-source-context* nil)
1462 (*last-original-source* nil)
1463 (*last-source-form* nil)
1464 (*last-format-string* nil)
1465 (*last-format-args* nil)
1466 (*last-message-count* 0)
1467 ;; FIXME: Do we need this rebinding here? It's a literal
1468 ;; translation of the old CMU CL rebinding to
1469 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1470 ;; and it's not obvious whether the rebinding to itself is
1471 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1472 (*info-environment* *info-environment*)
1473 (*gensym-counter* 0))
1475 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1476 (with-compilation-values
1477 (sb!xc:with-compilation-unit ()
1480 (sub-sub-compile-file info)
1482 (finish-block-compilation)
1483 (let ((object *compile-object*))
1485 (fasl-output (fasl-dump-source-info info object))
1486 (core-object (fix-core-source-info info object))
1489 ;; Some errors are sufficiently bewildering that we just fail
1490 ;; immediately, without trying to recover and compile more of
1492 (fatal-compiler-error (condition)
1494 (when *compile-verbose*
1495 (format *standard-output*
1496 "~@<compilation aborted because of fatal error: ~2I~_~A~:>"
1498 (values nil t t)))))
1500 ;;; Return a pathname for the named file. The file must exist.
1501 (defun verify-source-file (pathname-designator)
1502 (let* ((pathname (pathname pathname-designator))
1503 (default-host (make-pathname :host (pathname-host pathname))))
1504 (flet ((try-with-type (path type error-p)
1505 (let ((new (merge-pathnames
1506 path (make-pathname :type type
1507 :defaults default-host))))
1508 (if (probe-file new)
1510 (and error-p (truename new))))))
1511 (cond ((typep pathname 'logical-pathname)
1512 (try-with-type pathname "LISP" t))
1513 ((probe-file pathname) pathname)
1514 ((try-with-type pathname "lisp" nil))
1515 ((try-with-type pathname "lisp" t))))))
1517 (defun elapsed-time-to-string (tsec)
1518 (multiple-value-bind (tmin sec) (truncate tsec 60)
1519 (multiple-value-bind (thr min) (truncate tmin 60)
1520 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1522 ;;; Print some junk at the beginning and end of compilation.
1523 (defun print-compile-start-note (source-info)
1524 (declare (type source-info source-info))
1525 (let ((file-info (source-info-file-info source-info)))
1526 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1527 (namestring (file-info-name file-info))
1528 (sb!int:format-universal-time nil
1529 (file-info-write-date
1533 :print-timezone nil)))
1536 (defun print-compile-end-note (source-info won)
1537 (declare (type source-info source-info))
1538 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1540 (elapsed-time-to-string
1541 (- (get-universal-time)
1542 (source-info-start-time source-info))))
1545 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1546 ;;; out of the compile, then abort the writing of the output file, so
1547 ;;; that we don't overwrite it with known garbage.
1548 (defun sb!xc:compile-file
1553 (output-file (cfp-output-file-default input-file))
1554 ;; FIXME: ANSI doesn't seem to say anything about
1555 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1557 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1558 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1559 (external-format :default)
1563 ((:block-compile *block-compile-arg*) nil))
1565 "Compile INPUT-FILE, producing a corresponding fasl file and
1566 returning its filename.
1569 If true, a message per non-macroexpanded top level form is printed
1570 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1571 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1572 no such message, but their subforms do.
1574 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1575 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1576 For example, compiling an IN-PACKAGE form will result in a message about
1577 a top level SETQ in addition to the message about the IN-PACKAGE form'
1580 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1583 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1584 argument, it is not currently supported. (non-standard)
1587 If given, internal data structures are dumped to the specified
1588 file, or if a value of T is given, to a file of *.trace type
1589 derived from the input file name. (non-standard)"
1590 ;;; Block compilation is currently broken.
1592 "Also, as a workaround for vaguely-non-ANSI behavior, the
1593 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1594 multiple functions are compiled together as a unit, resolving function
1595 references at compile time. NIL means that global function names are
1596 never resolved at compilation time. Currently NIL is the default
1597 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1598 of the ANSI spec allows this behavior under all circumstances, the
1599 compiler's runtime scales badly when it tries to do this for large
1600 files. If/when this performance problem is fixed, the block
1601 compilation default behavior will probably be made dependent on the
1602 SPEED and COMPILATION-SPEED optimization values, and the
1603 :BLOCK-COMPILE argument will probably become deprecated."
1605 (let* ((fasl-output nil)
1606 (output-file-name nil)
1609 (failure-p t) ; T in case error keeps this from being set later
1610 (input-pathname (verify-source-file input-file))
1611 (source-info (make-file-source-info input-pathname external-format))
1612 (*compiler-trace-output* nil)) ; might be modified below
1617 (setq output-file-name
1618 (sb!xc:compile-file-pathname input-file
1619 :output-file output-file))
1621 (open-fasl-output output-file-name
1622 (namestring input-pathname))))
1624 (let* ((default-trace-file-pathname
1625 (make-pathname :type "trace" :defaults input-pathname))
1626 (trace-file-pathname
1627 (if (eql trace-file t)
1628 default-trace-file-pathname
1629 (merge-pathnames trace-file
1630 default-trace-file-pathname))))
1631 (setf *compiler-trace-output*
1632 (open trace-file-pathname
1633 :if-exists :supersede
1634 :direction :output))))
1636 (when sb!xc:*compile-verbose*
1637 (print-compile-start-note source-info))
1638 (let ((*compile-object* fasl-output)
1640 (multiple-value-setq (dummy warnings-p failure-p)
1641 (sub-compile-file source-info)))
1642 (setq compile-won t))
1644 (close-source-info source-info)
1647 (close-fasl-output fasl-output (not compile-won))
1648 (setq output-file-name
1649 (pathname (fasl-output-stream fasl-output)))
1650 (when (and compile-won sb!xc:*compile-verbose*)
1651 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1653 (when sb!xc:*compile-verbose*
1654 (print-compile-end-note source-info compile-won))
1656 (when *compiler-trace-output*
1657 (close *compiler-trace-output*)))
1659 (values (if output-file
1660 ;; Hack around filesystem race condition...
1661 (or (probe-file output-file-name) output-file-name)
1666 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1667 ;;; the OUTPUT-FILE argument
1669 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1670 ;;; that results from merging the INPUT-FILE with the value of
1671 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1672 ;;; default to the appropriate implementation-defined default type for
1674 (defun cfp-output-file-default (input-file)
1675 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1676 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1679 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1680 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1681 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1682 ;;; pathname, it is translated into a physical pathname as if by
1683 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1684 ;;; So I haven't really tried to make this precisely ANSI-compatible
1685 ;;; at the level of e.g. whether it returns logical pathname or a
1686 ;;; physical pathname. Patches to make it more correct are welcome.
1687 ;;; -- WHN 2000-12-09
1688 (defun sb!xc:compile-file-pathname (input-file
1690 (output-file (cfp-output-file-default
1694 "Return a pathname describing what file COMPILE-FILE would write to given
1696 (merge-pathnames output-file (merge-pathnames input-file)))
1698 ;;;; MAKE-LOAD-FORM stuff
1700 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1701 ;;; finds a constant structure, it invokes this to arrange for proper
1702 ;;; dumping. If it turns out that the constant has already been
1703 ;;; dumped, then we don't need to do anything.
1705 ;;; If the constant hasn't been dumped, then we check to see whether
1706 ;;; we are in the process of creating it. We detect this by
1707 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1708 ;;; the constants we are in the process of creating. Actually, each
1709 ;;; entry is a list of the constant and any init forms that need to be
1710 ;;; processed on behalf of that constant.
1712 ;;; It's not necessarily an error for this to happen. If we are
1713 ;;; processing the init form for some object that showed up *after*
1714 ;;; the original reference to this constant, then we just need to
1715 ;;; defer the processing of that init form. To detect this, we
1716 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1717 ;;; constants created since the last time we started processing an
1718 ;;; init form. If the constant passed to emit-make-load-form shows up
1719 ;;; in this list, then there is a circular chain through creation
1720 ;;; forms, which is an error.
1722 ;;; If there is some intervening init form, then we blow out of
1723 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1724 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1725 ;;; offending init form can be tacked onto the init forms for the
1726 ;;; circular object.
1728 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1729 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1730 ;;; whether the creation form is the magic value
1731 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1732 ;;; dumper will eventually get its hands on the object and use the
1733 ;;; normal structure dumping noise on it.
1735 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1736 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1737 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1738 ;;; dumper to use that result instead whenever it sees this constant.
1740 ;;; Now we try to compile the init form. We bind
1741 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1742 ;;; form (and any init forms that were added because of circularity
1743 ;;; detection). If this works, great. If not, we add the init forms to
1744 ;;; the init forms for the object that caused the problems and let it
1746 (defvar *constants-being-created* nil)
1747 (defvar *constants-created-since-last-init* nil)
1748 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1749 (defun emit-make-load-form (constant)
1750 (aver (fasl-output-p *compile-object*))
1751 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1752 ;; KLUDGE: This special hack is because I was too lazy
1753 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1754 ;; function of LAYOUT returns nontrivial forms when
1755 ;; building the cross-compiler but :IGNORE-IT when
1756 ;; cross-compiling or running under the target Lisp. --
1758 #+sb-xc-host (typep constant 'layout))
1759 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1761 (when (find constant *constants-created-since-last-init* :test #'eq)
1763 (throw 'pending-init circular-ref)))
1764 (multiple-value-bind (creation-form init-form)
1766 (sb!xc:make-load-form constant (make-null-lexenv))
1768 (compiler-error condition)))
1770 (:sb-just-dump-it-normally
1771 (fasl-validate-structure constant *compile-object*)
1776 (when (fasl-constant-already-dumped-p constant *compile-object*)
1777 (return-from emit-make-load-form nil))
1778 (let* ((name (write-to-string constant :level 1 :length 2))
1780 (list constant name init-form)
1782 (let ((*constants-being-created*
1783 (cons info *constants-being-created*))
1784 (*constants-created-since-last-init*
1785 (cons constant *constants-created-since-last-init*)))
1788 (fasl-note-handle-for-constant
1790 (compile-load-time-value
1794 (compiler-error "circular references in creation form for ~S"
1797 (let* ((*constants-created-since-last-init* nil)
1799 (catch 'pending-init
1800 (loop for (name form) on (cdr info) by #'cddr
1801 collect name into names
1802 collect form into forms
1803 finally (compile-make-load-form-init-forms forms))
1806 (setf (cdr circular-ref)
1807 (append (cdr circular-ref) (cdr info))))))))))))
1810 ;;;; Host compile time definitions
1812 (defun compile-in-lexenv (name lambda lexenv)
1813 (declare (ignore lexenv))
1814 (compile name lambda))
1817 (defun eval-in-lexenv (form lexenv)
1818 (declare (ignore lexenv))