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 (fresh-line *standard-output*)
247 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
248 (compiler-mumble "compilation unit ~:[finished~;aborted~]~
249 ~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
250 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
251 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
252 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
253 ~[~:;~:*~& printed ~W note~:P~]"
255 *aborted-compilation-unit-count*
256 *compiler-error-count*
257 *compiler-warning-count*
258 *compiler-style-warning-count*
259 *compiler-note-count*)))
260 (fresh-line *standard-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-namify "~S initial component" name))
952 (setf (component-kind component) :initial)
953 (let* ((locall-fun (let ((*allow-instrumenting* t))
954 (ir1-convert-lambdalike
956 :debug-name (debug-namify "top level local call "
958 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
959 :source-name (or name '.anonymous.)
960 :debug-name (unless name
963 (assert-global-function-definition-type name locall-fun))
964 (setf (functional-entry-fun fun) locall-fun
965 (functional-kind fun) :external
966 (functional-has-external-references-p fun) t)
969 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
970 ;;; description of the result.
971 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
972 ;;; into core and return the compiled FUNCTION value.
973 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
974 ;;; into the fasl file and return a dump handle.
976 ;;; If NAME is provided, then we try to use it as the name of the
977 ;;; function for debugging/diagnostic information.
978 (defun %compile (lambda-expression
983 ;; This magical idiom seems to be the appropriate
984 ;; path for compiling standalone LAMBDAs, judging
985 ;; from the CMU CL code and experiment, so it's a
986 ;; nice default for things where we don't have a
987 ;; real source path (as in e.g. inside CL:COMPILE).
988 '(original-source-start 0 0)))
990 (legal-fun-name-or-type-error name))
992 (*lexenv* (make-lexenv :policy *policy*
993 :handled-conditions *handled-conditions*
994 :disabled-package-locks *disabled-package-locks*))
995 (fun (make-functional-from-toplevel-lambda lambda-expression
999 ;; FIXME: The compile-it code from here on is sort of a
1000 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1001 ;; better to find a way to share the code there; or
1002 ;; alternatively, to use this code to replace the code there.
1003 ;; (The second alternative might be pretty easy if we used
1004 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1005 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1007 (locall-analyze-clambdas-until-done (list fun))
1009 (multiple-value-bind (components-from-dfo top-components hairy-top)
1010 (find-initial-dfo (list fun))
1011 (declare (ignore hairy-top))
1013 (let ((*all-components* (append components-from-dfo top-components)))
1014 (dolist (component-from-dfo components-from-dfo)
1015 (compile-component component-from-dfo)
1016 (replace-toplevel-xeps component-from-dfo)))
1018 (let ((entry-table (etypecase *compile-object*
1019 (fasl-output (fasl-output-entry-table
1021 (core-object (core-object-entry-table
1022 *compile-object*)))))
1023 (multiple-value-bind (result found-p)
1024 (gethash (leaf-info fun) entry-table)
1028 ;; KLUDGE: This code duplicates some other code in this
1029 ;; file. In the great reorganzation, the flow of program
1030 ;; logic changed from the original CMUCL model, and that
1031 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1032 ;; longer followed for CORE-OBJECTS, leading to BUG
1033 ;; 156. This place is transparently not the right one for
1034 ;; this code, but I don't have a clear enough overview of
1035 ;; the compiler to know how to rearrange it all so that
1036 ;; this operation fits in nicely, and it was blocking
1037 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1038 ;; ((..)) (DEFUN FOO ...))
1040 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1041 ;; ideal way, as (1) definitions typed in at the REPL
1042 ;; without an INLINE declaration will give a NULL
1043 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1044 ;; and (2) INLINE declarations will yield a
1045 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1046 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1049 ;; (2) is probably fairly easy to fix -- it is, after all,
1050 ;; a matter of list manipulation (or possibly of teaching
1051 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1052 ;; significantly harder, as the association between
1053 ;; function object and source is a tricky one.
1055 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1056 ;; non-NULL list) when the function in question has been
1057 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1058 ;; work when it has been compiled as part of the top-level
1059 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1060 ;; ...). -- CSR, 2002-11-02
1061 (when (core-object-p *compile-object*)
1062 (fix-core-source-info *source-info* *compile-object* result))
1064 (mapc #'clear-ir1-info components-from-dfo)
1067 (defun process-toplevel-cold-fset (name lambda-expression path)
1068 (unless (producing-fasl-file)
1069 (error "can't COLD-FSET except in a fasl file"))
1070 (legal-fun-name-or-type-error name)
1071 (fasl-dump-cold-fset name
1072 (%compile lambda-expression
1079 (defun note-top-level-form (form &optional finalp)
1080 (when *compile-print*
1081 (cond ((not *top-level-form-noted*)
1082 (let ((*print-length* 2)
1084 (*print-pretty* nil))
1085 (with-compiler-io-syntax
1086 (compiler-mumble "~&; ~:[compiling~;converting~] ~S"
1087 *block-compile* form)))
1090 (eq :top-level-forms *compile-print*)
1091 (neq form *top-level-form-noted*))
1092 (let ((*print-length* 1)
1094 (*print-pretty* nil))
1095 (with-compiler-io-syntax
1096 (compiler-mumble "~&; ... top level ~S" form)))
1099 *top-level-form-noted*))))
1101 ;;; Process a top level FORM with the specified source PATH.
1102 ;;; * If this is a magic top level form, then do stuff.
1103 ;;; * If this is a macro, then expand it.
1104 ;;; * Otherwise, just compile it.
1106 ;;; COMPILE-TIME-TOO is as defined in ANSI
1107 ;;; "3.2.3.1 Processing of Top Level Forms".
1108 (defun process-toplevel-form (form path compile-time-too)
1109 (declare (list path))
1111 (catch 'process-toplevel-form-error-abort
1112 (let* ((path (or (gethash form *source-paths*) (cons form path)))
1113 (*compiler-error-bailout*
1114 (lambda (&optional condition)
1115 (convert-and-maybe-compile
1116 (make-compiler-error-form condition form)
1118 (throw 'process-toplevel-form-error-abort nil))))
1120 (flet ((default-processor (form)
1121 (let ((*top-level-form-noted* (note-top-level-form form)))
1122 ;; When we're cross-compiling, consider: what should we
1123 ;; do when we hit e.g.
1124 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1125 ;; (DEFUN FOO (X) (+ 7 X)))?
1126 ;; DEFUN has a macro definition in the cross-compiler,
1127 ;; and a different macro definition in the target
1128 ;; compiler. The only sensible thing is to use the
1129 ;; target compiler's macro definition, since the
1130 ;; cross-compiler's macro is in general into target
1131 ;; functions which can't meaningfully be executed at
1132 ;; cross-compilation time. So make sure we do the EVAL
1133 ;; here, before we macroexpand.
1135 ;; Then things get even dicier with something like
1136 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1137 ;; where we have to make sure that we don't uncross
1138 ;; the SB!XC: prefix before we do EVAL, because otherwise
1139 ;; we'd be trying to redefine the cross-compilation host's
1142 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1145 (when compile-time-too
1146 (eval form)) ; letting xc host EVAL do its own macroexpansion
1147 (let* (;; (We uncross the operator name because things
1148 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1149 ;; should be equivalent to their CL: counterparts
1150 ;; when being compiled as target code. We leave
1151 ;; the rest of the form uncrossed because macros
1152 ;; might yet expand into EVAL-WHEN stuff, and
1153 ;; things inside EVAL-WHEN can't be uncrossed
1154 ;; until after we've EVALed them in the
1155 ;; cross-compilation host.)
1156 (slightly-uncrossed (cons (uncross (first form))
1158 (expanded (preprocessor-macroexpand-1
1159 slightly-uncrossed)))
1160 (if (eq expanded slightly-uncrossed)
1161 ;; (Now that we're no longer processing toplevel
1162 ;; forms, and hence no longer need to worry about
1163 ;; EVAL-WHEN, we can uncross everything.)
1164 (convert-and-maybe-compile expanded path)
1165 ;; (We have to demote COMPILE-TIME-TOO to NIL
1166 ;; here, no matter what it was before, since
1167 ;; otherwise we'd tend to EVAL subforms more than
1168 ;; once, because of WHEN COMPILE-TIME-TOO form
1170 (process-toplevel-form expanded path nil))))
1171 ;; When we're not cross-compiling, we only need to
1172 ;; macroexpand once, so we can follow the 1-thru-6
1173 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1174 ;; Top Level Forms".
1176 (let ((expanded (preprocessor-macroexpand-1 form)))
1177 (cond ((eq expanded form)
1178 (when compile-time-too
1179 (eval-in-lexenv form *lexenv*))
1180 (convert-and-maybe-compile form path))
1182 (process-toplevel-form expanded
1184 compile-time-too)))))))
1187 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1188 ;; (1) SBCL gets smart enough to handle global
1189 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1190 ;; implementors start using symbol macros in a way which
1191 ;; interacts with SB-XC/CL distinction.)
1192 (convert-and-maybe-compile form path)
1194 (default-processor form)
1195 (flet ((need-at-least-one-arg (form)
1197 (compiler-error "~S form is too short: ~S"
1201 ;; In the cross-compiler, top level COLD-FSET arranges
1202 ;; for static linking at cold init time.
1205 (aver (not compile-time-too))
1206 (destructuring-bind (cold-fset fun-name lambda-expression) form
1207 (declare (ignore cold-fset))
1208 (process-toplevel-cold-fset fun-name
1211 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1212 (need-at-least-one-arg form)
1213 (destructuring-bind (special-operator magic &rest body) form
1214 (ecase special-operator
1216 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1217 ;; "3.2.3.1 Processing of Top Level Forms".
1218 (multiple-value-bind (ct lt e)
1219 (parse-eval-when-situations magic)
1220 (let ((new-compile-time-too (or ct
1221 (and compile-time-too
1223 (cond (lt (process-toplevel-progn
1224 body path new-compile-time-too))
1225 (new-compile-time-too (eval-in-lexenv
1229 (funcall-in-macrolet-lexenv
1231 (lambda (&key funs prepend)
1232 (declare (ignore funs))
1233 (aver (null prepend))
1234 (process-toplevel-locally body
1239 (funcall-in-symbol-macrolet-lexenv
1241 (lambda (&key vars prepend)
1242 (aver (null prepend))
1243 (process-toplevel-locally body
1249 (process-toplevel-locally (rest form) path compile-time-too))
1251 (process-toplevel-progn (rest form) path compile-time-too))
1252 (t (default-processor form))))))))
1256 ;;;; load time value support
1258 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1260 ;;; Return T if we are currently producing a fasl file and hence
1261 ;;; constants need to be dumped carefully.
1262 (defun producing-fasl-file ()
1263 (fasl-output-p *compile-object*))
1265 ;;; Compile FORM and arrange for it to be called at load-time. Return
1266 ;;; the dumper handle and our best guess at the type of the object.
1267 (defun compile-load-time-value (form)
1268 (let ((lambda (compile-load-time-stuff form t)))
1270 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1271 (let ((type (leaf-type lambda)))
1272 (if (fun-type-p type)
1273 (single-value-type (fun-type-returns type))
1276 ;;; Compile the FORMS and arrange for them to be called (for effect,
1277 ;;; not value) at load time.
1278 (defun compile-make-load-form-init-forms (forms)
1279 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1280 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1282 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1283 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1284 (defun compile-load-time-stuff (form for-value)
1286 (let* ((*lexenv* (make-null-lexenv))
1287 (lambda (ir1-toplevel form *current-path* for-value)))
1288 (compile-toplevel (list lambda) t)
1291 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1292 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1293 ;;; don't try to combine this component with anything else and frob
1294 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1295 ;;; compiling, because it was merged with a run-time component.
1296 (defun compile-load-time-value-lambda (lambdas)
1297 (aver (null (cdr lambdas)))
1298 (let* ((lambda (car lambdas))
1299 (component (lambda-component lambda)))
1300 (when (eql (component-kind component) :toplevel)
1301 (setf (component-name component) (leaf-debug-name lambda))
1302 (compile-component component)
1303 (clear-ir1-info component))))
1307 (defun object-call-toplevel-lambda (tll)
1308 (declare (type functional tll))
1309 (let ((object *compile-object*))
1311 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1312 (core-object (core-call-toplevel-lambda tll object))
1315 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1316 ;;; the resulting function to be called.
1317 (defun sub-compile-toplevel-lambdas (lambdas)
1318 (declare (list lambdas))
1320 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1321 (compile-component component)
1322 (clear-ir1-info component)
1323 (object-call-toplevel-lambda tll)))
1326 ;;; Compile top level code and call the top level lambdas. We pick off
1327 ;;; top level lambdas in non-top-level components here, calling
1328 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1329 (defun compile-toplevel-lambdas (lambdas)
1330 (declare (list lambdas))
1331 (let ((len (length lambdas)))
1332 (flet ((loser (start)
1333 (or (position-if (lambda (x)
1334 (not (eq (component-kind
1335 (node-component (lambda-bind x)))
1338 ;; this used to read ":start start", but
1339 ;; start can be greater than len, which
1340 ;; is an error according to ANSI - CSR,
1342 :start (min start len))
1344 (do* ((start 0 (1+ loser))
1345 (loser (loser start) (loser start)))
1347 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1348 (unless (= loser len)
1349 (object-call-toplevel-lambda (elt lambdas loser))))))
1352 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1355 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1356 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1357 (defun compile-toplevel (lambdas load-time-value-p)
1358 (declare (list lambdas))
1360 (maybe-mumble "locall ")
1361 (locall-analyze-clambdas-until-done lambdas)
1363 (maybe-mumble "IDFO ")
1364 (multiple-value-bind (components top-components hairy-top)
1365 (find-initial-dfo lambdas)
1366 (let ((*all-components* (append components top-components)))
1367 (when *check-consistency*
1368 (maybe-mumble "[check]~%")
1369 (check-ir1-consistency *all-components*))
1371 (dolist (component (append hairy-top top-components))
1372 (pre-physenv-analyze-toplevel component))
1374 (dolist (component components)
1375 (compile-component component)
1376 (replace-toplevel-xeps component))
1378 (when *check-consistency*
1379 (maybe-mumble "[check]~%")
1380 (check-ir1-consistency *all-components*))
1382 (if load-time-value-p
1383 (compile-load-time-value-lambda lambdas)
1384 (compile-toplevel-lambdas lambdas))
1386 (mapc #'clear-ir1-info components)
1390 ;;; Actually compile any stuff that has been queued up for block
1392 (defun finish-block-compilation ()
1393 (when *block-compile*
1394 (when *compile-print*
1395 (compiler-mumble "~&; block compiling converted top level forms..."))
1396 (when *toplevel-lambdas*
1397 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1398 (setq *toplevel-lambdas* ()))
1399 (setq *block-compile* nil)
1400 (setq *entry-points* nil)))
1402 (defun handle-condition-p (condition)
1404 (etypecase *compiler-error-context*
1406 (node-lexenv *compiler-error-context*))
1407 (compiler-error-context
1408 (let ((lexenv (compiler-error-context-lexenv
1409 *compiler-error-context*)))
1413 (let ((muffles (lexenv-handled-conditions lexenv)))
1414 (if (null muffles) ; common case
1416 (dolist (muffle muffles nil)
1417 (destructuring-bind (typespec . restart-name) muffle
1418 (when (and (typep condition typespec)
1419 (find-restart restart-name condition))
1422 (defun handle-condition-handler (condition)
1424 (etypecase *compiler-error-context*
1426 (node-lexenv *compiler-error-context*))
1427 (compiler-error-context
1428 (let ((lexenv (compiler-error-context-lexenv
1429 *compiler-error-context*)))
1433 (let ((muffles (lexenv-handled-conditions lexenv)))
1435 (dolist (muffle muffles (bug "fell through"))
1436 (destructuring-bind (typespec . restart-name) muffle
1437 (when (typep condition typespec)
1438 (awhen (find-restart restart-name condition)
1439 (invoke-restart it))))))))
1441 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1442 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1443 (defun sub-compile-file (info)
1444 (declare (type source-info info))
1445 (let ((*package* (sane-package))
1446 (*readtable* *readtable*)
1447 (sb!xc:*compile-file-pathname* nil) ; really bound in
1448 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1450 (*handled-conditions* *handled-conditions*)
1451 (*disabled-package-locks* *disabled-package-locks*)
1452 (*lexenv* (make-null-lexenv))
1453 (*block-compile* *block-compile-arg*)
1454 (*source-info* info)
1455 (*toplevel-lambdas* ())
1456 (*fun-names-in-this-file* ())
1457 (*allow-instrumenting* nil)
1458 (*compiler-error-bailout*
1460 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1461 (return-from sub-compile-file (values nil t t))))
1462 (*current-path* nil)
1463 (*last-source-context* nil)
1464 (*last-original-source* nil)
1465 (*last-source-form* nil)
1466 (*last-format-string* nil)
1467 (*last-format-args* nil)
1468 (*last-message-count* 0)
1469 ;; FIXME: Do we need this rebinding here? It's a literal
1470 ;; translation of the old CMU CL rebinding to
1471 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1472 ;; and it's not obvious whether the rebinding to itself is
1473 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1474 (*info-environment* *info-environment*)
1475 (*gensym-counter* 0))
1477 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1478 (with-compilation-values
1479 (sb!xc:with-compilation-unit ()
1482 (sub-sub-compile-file info)
1484 (finish-block-compilation)
1485 (let ((object *compile-object*))
1487 (fasl-output (fasl-dump-source-info info object))
1488 (core-object (fix-core-source-info info object))
1491 ;; Some errors are sufficiently bewildering that we just fail
1492 ;; immediately, without trying to recover and compile more of
1494 (fatal-compiler-error (condition)
1496 (when *compile-verbose*
1497 (format *standard-output*
1498 "~@<compilation aborted because of fatal error: ~2I~_~A~:>"
1500 (values nil t t)))))
1502 ;;; Return a pathname for the named file. The file must exist.
1503 (defun verify-source-file (pathname-designator)
1504 (let* ((pathname (pathname pathname-designator))
1505 (default-host (make-pathname :host (pathname-host pathname))))
1506 (flet ((try-with-type (path type error-p)
1507 (let ((new (merge-pathnames
1508 path (make-pathname :type type
1509 :defaults default-host))))
1510 (if (probe-file new)
1512 (and error-p (truename new))))))
1513 (cond ((typep pathname 'logical-pathname)
1514 (try-with-type pathname "LISP" t))
1515 ((probe-file pathname) pathname)
1516 ((try-with-type pathname "lisp" nil))
1517 ((try-with-type pathname "lisp" t))))))
1519 (defun elapsed-time-to-string (tsec)
1520 (multiple-value-bind (tmin sec) (truncate tsec 60)
1521 (multiple-value-bind (thr min) (truncate tmin 60)
1522 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1524 ;;; Print some junk at the beginning and end of compilation.
1525 (defun print-compile-start-note (source-info)
1526 (declare (type source-info source-info))
1527 (let ((file-info (source-info-file-info source-info)))
1528 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1529 (namestring (file-info-name file-info))
1530 (sb!int:format-universal-time nil
1531 (file-info-write-date
1535 :print-timezone nil)))
1538 (defun print-compile-end-note (source-info won)
1539 (declare (type source-info source-info))
1540 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1542 (elapsed-time-to-string
1543 (- (get-universal-time)
1544 (source-info-start-time source-info))))
1547 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1548 ;;; out of the compile, then abort the writing of the output file, so
1549 ;;; that we don't overwrite it with known garbage.
1550 (defun sb!xc:compile-file
1555 (output-file (cfp-output-file-default input-file))
1556 ;; FIXME: ANSI doesn't seem to say anything about
1557 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1559 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1560 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1561 (external-format :default)
1565 ((:block-compile *block-compile-arg*) nil))
1567 "Compile INPUT-FILE, producing a corresponding fasl file and
1568 returning its filename.
1571 If true, a message per non-macroexpanded top level form is printed
1572 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1573 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1574 no such message, but their subforms do.
1576 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1577 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1578 For example, compiling an IN-PACKAGE form will result in a message about
1579 a top level SETQ in addition to the message about the IN-PACKAGE form'
1582 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1585 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1586 argument, it is not currently supported. (non-standard)
1589 If given, internal data structures are dumped to the specified
1590 file, or if a value of T is given, to a file of *.trace type
1591 derived from the input file name. (non-standard)"
1592 ;;; Block compilation is currently broken.
1594 "Also, as a workaround for vaguely-non-ANSI behavior, the
1595 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1596 multiple functions are compiled together as a unit, resolving function
1597 references at compile time. NIL means that global function names are
1598 never resolved at compilation time. Currently NIL is the default
1599 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1600 of the ANSI spec allows this behavior under all circumstances, the
1601 compiler's runtime scales badly when it tries to do this for large
1602 files. If/when this performance problem is fixed, the block
1603 compilation default behavior will probably be made dependent on the
1604 SPEED and COMPILATION-SPEED optimization values, and the
1605 :BLOCK-COMPILE argument will probably become deprecated."
1607 (let* ((fasl-output nil)
1608 (output-file-name nil)
1611 (failure-p t) ; T in case error keeps this from being set later
1612 (input-pathname (verify-source-file input-file))
1613 (source-info (make-file-source-info input-pathname external-format))
1614 (*compiler-trace-output* nil)) ; might be modified below
1619 (setq output-file-name
1620 (sb!xc:compile-file-pathname input-file
1621 :output-file output-file))
1623 (open-fasl-output output-file-name
1624 (namestring input-pathname))))
1626 (let* ((default-trace-file-pathname
1627 (make-pathname :type "trace" :defaults input-pathname))
1628 (trace-file-pathname
1629 (if (eql trace-file t)
1630 default-trace-file-pathname
1631 (merge-pathnames trace-file
1632 default-trace-file-pathname))))
1633 (setf *compiler-trace-output*
1634 (open trace-file-pathname
1635 :if-exists :supersede
1636 :direction :output))))
1638 (when sb!xc:*compile-verbose*
1639 (print-compile-start-note source-info))
1640 (let ((*compile-object* fasl-output)
1642 (multiple-value-setq (dummy warnings-p failure-p)
1643 (sub-compile-file source-info)))
1644 (setq compile-won t))
1646 (close-source-info source-info)
1649 (close-fasl-output fasl-output (not compile-won))
1650 (setq output-file-name
1651 (pathname (fasl-output-stream fasl-output)))
1652 (when (and compile-won sb!xc:*compile-verbose*)
1653 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1655 (when sb!xc:*compile-verbose*
1656 (print-compile-end-note source-info compile-won))
1658 (when *compiler-trace-output*
1659 (close *compiler-trace-output*)))
1661 (values (if output-file
1662 ;; Hack around filesystem race condition...
1663 (or (probe-file output-file-name) output-file-name)
1668 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1669 ;;; the OUTPUT-FILE argument
1671 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1672 ;;; that results from merging the INPUT-FILE with the value of
1673 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1674 ;;; default to the appropriate implementation-defined default type for
1676 (defun cfp-output-file-default (input-file)
1677 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1678 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1681 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1682 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1683 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1684 ;;; pathname, it is translated into a physical pathname as if by
1685 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1686 ;;; So I haven't really tried to make this precisely ANSI-compatible
1687 ;;; at the level of e.g. whether it returns logical pathname or a
1688 ;;; physical pathname. Patches to make it more correct are welcome.
1689 ;;; -- WHN 2000-12-09
1690 (defun sb!xc:compile-file-pathname (input-file
1692 (output-file (cfp-output-file-default
1696 "Return a pathname describing what file COMPILE-FILE would write to given
1698 (merge-pathnames output-file (merge-pathnames input-file)))
1700 ;;;; MAKE-LOAD-FORM stuff
1702 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1703 ;;; finds a constant structure, it invokes this to arrange for proper
1704 ;;; dumping. If it turns out that the constant has already been
1705 ;;; dumped, then we don't need to do anything.
1707 ;;; If the constant hasn't been dumped, then we check to see whether
1708 ;;; we are in the process of creating it. We detect this by
1709 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1710 ;;; the constants we are in the process of creating. Actually, each
1711 ;;; entry is a list of the constant and any init forms that need to be
1712 ;;; processed on behalf of that constant.
1714 ;;; It's not necessarily an error for this to happen. If we are
1715 ;;; processing the init form for some object that showed up *after*
1716 ;;; the original reference to this constant, then we just need to
1717 ;;; defer the processing of that init form. To detect this, we
1718 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1719 ;;; constants created since the last time we started processing an
1720 ;;; init form. If the constant passed to emit-make-load-form shows up
1721 ;;; in this list, then there is a circular chain through creation
1722 ;;; forms, which is an error.
1724 ;;; If there is some intervening init form, then we blow out of
1725 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1726 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1727 ;;; offending init form can be tacked onto the init forms for the
1728 ;;; circular object.
1730 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1731 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1732 ;;; whether the creation form is the magic value
1733 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1734 ;;; dumper will eventually get its hands on the object and use the
1735 ;;; normal structure dumping noise on it.
1737 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1738 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1739 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1740 ;;; dumper to use that result instead whenever it sees this constant.
1742 ;;; Now we try to compile the init form. We bind
1743 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1744 ;;; form (and any init forms that were added because of circularity
1745 ;;; detection). If this works, great. If not, we add the init forms to
1746 ;;; the init forms for the object that caused the problems and let it
1748 (defvar *constants-being-created* nil)
1749 (defvar *constants-created-since-last-init* nil)
1750 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1751 (defun emit-make-load-form (constant)
1752 (aver (fasl-output-p *compile-object*))
1753 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1754 ;; KLUDGE: This special hack is because I was too lazy
1755 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1756 ;; function of LAYOUT returns nontrivial forms when
1757 ;; building the cross-compiler but :IGNORE-IT when
1758 ;; cross-compiling or running under the target Lisp. --
1760 #+sb-xc-host (typep constant 'layout))
1761 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1763 (when (find constant *constants-created-since-last-init* :test #'eq)
1765 (throw 'pending-init circular-ref)))
1766 (multiple-value-bind (creation-form init-form)
1768 (sb!xc:make-load-form constant (make-null-lexenv))
1770 (compiler-error condition)))
1772 (:sb-just-dump-it-normally
1773 (fasl-validate-structure constant *compile-object*)
1778 (when (fasl-constant-already-dumped-p constant *compile-object*)
1779 (return-from emit-make-load-form nil))
1780 (let* ((name (write-to-string constant :level 1 :length 2))
1782 (list constant name init-form)
1784 (let ((*constants-being-created*
1785 (cons info *constants-being-created*))
1786 (*constants-created-since-last-init*
1787 (cons constant *constants-created-since-last-init*)))
1790 (fasl-note-handle-for-constant
1792 (compile-load-time-value
1796 (compiler-error "circular references in creation form for ~S"
1799 (let* ((*constants-created-since-last-init* nil)
1801 (catch 'pending-init
1802 (loop for (name form) on (cdr info) by #'cddr
1803 collect name into names
1804 collect form into forms
1805 finally (compile-make-load-form-init-forms forms))
1808 (setf (cdr circular-ref)
1809 (append (cdr circular-ref) (cdr info))))))))))))
1812 ;;;; Host compile time definitions
1814 (defun compile-in-lexenv (name lambda lexenv)
1815 (declare (ignore lexenv))
1816 (compile name lambda))
1819 (defun eval-in-lexenv (form lexenv)
1820 (declare (ignore lexenv))