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* *last-error-context*
32 *lexenv* *fun-names-in-this-file*
33 *allow-instrumenting*))
35 ;;; Whether call of a function which cannot be defined causes a full
37 (defvar *flame-on-necessarily-undefined-function* nil)
39 (defvar *check-consistency* nil)
40 (defvar *all-components*)
42 ;;; Set to NIL to disable loop analysis for register allocation.
43 (defvar *loop-analyze* t)
45 ;;; Bind this to a stream to capture various internal debugging output.
46 (defvar *compiler-trace-output* nil)
48 ;;; The current block compilation state. These are initialized to the
49 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
52 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
53 ;;; argument, which overrides any internal declarations.
54 (defvar *block-compile*)
55 (defvar *block-compile-arg*)
56 (declaim (type (member nil t :specified) *block-compile* *block-compile-arg*))
57 (defvar *entry-points*)
58 (declaim (list *entry-points*))
60 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
61 ;;; lambdas resulting from compiling subforms. (In reverse order.)
62 (defvar *toplevel-lambdas*)
63 (declaim (list *toplevel-lambdas*))
65 ;;; The current non-macroexpanded toplevel form as printed when
66 ;;; *compile-print* is true.
67 (defvar *top-level-form-noted* nil)
69 (defvar sb!xc:*compile-verbose* t
71 "The default for the :VERBOSE argument to COMPILE-FILE.")
72 (defvar sb!xc:*compile-print* t
74 "The default for the :PRINT argument to COMPILE-FILE.")
75 (defvar *compile-progress* nil
77 "When this is true, the compiler prints to *STANDARD-OUTPUT* progress
78 information about the phases of compilation of each function. (This
79 is useful mainly in large block compilations.)")
81 (defvar sb!xc:*compile-file-pathname* nil
83 "The defaulted pathname of the file currently being compiled, or NIL if not
85 (defvar sb!xc:*compile-file-truename* nil
87 "The TRUENAME of the file currently being compiled, or NIL if not
90 (declaim (type (or pathname null)
91 sb!xc:*compile-file-pathname*
92 sb!xc:*compile-file-truename*))
94 ;;; the SOURCE-INFO structure for the current compilation. This is
95 ;;; null globally to indicate that we aren't currently in any
96 ;;; identifiable compilation.
97 (defvar *source-info* nil)
99 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
100 ;;; normally causes nested uses to be no-ops).
101 (defvar *in-compilation-unit* nil)
103 ;;; Count of the number of compilation units dynamically enclosed by
104 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
105 (defvar *aborted-compilation-unit-count*)
107 ;;; Mumble conditional on *COMPILE-PROGRESS*.
108 (defun maybe-mumble (&rest foo)
109 (when *compile-progress*
110 (compiler-mumble "~&")
111 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
112 (apply #'compiler-mumble foo))))
114 (deftype object () '(or fasl-output core-object null))
116 (defvar *compile-object* nil)
117 (declaim (type object *compile-object*))
119 (defvar *fopcompile-label-counter*)
121 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
123 (defmacro sb!xc:with-compilation-unit (options &body body)
125 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
126 This form affects compilations that take place within its dynamic extent. It
127 is intended to be wrapped around the compilation of all files in the same
128 system. These keywords are defined:
130 :OVERRIDE Boolean-Form
131 One of the effects of this form is to delay undefined warnings
132 until the end of the form, instead of giving them at the end of each
133 compilation. If OVERRIDE is NIL (the default), then the outermost
134 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
135 OVERRIDE true causes that form to grab any enclosed warnings, even if
136 it is enclosed by another WITH-COMPILATION-UNIT.
138 :SOURCE-PLIST Plist-Form
139 Attaches the value returned by the Plist-Form to internal debug-source
140 information of functions compiled in within the dynamic contour.
141 Primarily for use by development environments, in order to eg. associate
142 function definitions with editor-buffers. Can be accessed as
143 SB-INTROSPECT:DEFINITION-SOURCE-PLIST. If multiple, nested
144 WITH-COMPILATION-UNITs provide :SOURCE-PLISTs, they are appended
145 togather, innermost left. If Unaffected by :OVERRIDE."
146 `(%with-compilation-unit (lambda () ,@body) ,@options))
148 (defvar *source-plist* nil)
150 (defun %with-compilation-unit (fn &key override source-plist)
151 (declare (type function fn))
152 (let ((succeeded-p nil)
153 (*source-plist* (append source-plist *source-plist*)))
154 (if (and *in-compilation-unit* (not override))
155 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
156 ;; ordinarily (unless OVERRIDE) basically a no-op.
158 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
160 (incf *aborted-compilation-unit-count*)))
161 (let ((*aborted-compilation-unit-count* 0)
162 (*compiler-error-count* 0)
163 (*compiler-warning-count* 0)
164 (*compiler-style-warning-count* 0)
165 (*compiler-note-count* 0)
166 (*undefined-warnings* nil)
167 (*in-compilation-unit* t))
168 (sb!thread:with-recursive-lock (*big-compiler-lock*)
169 (handler-bind ((parse-unknown-type
171 (note-undefined-reference
172 (parse-unknown-type-specifier c)
175 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
177 (incf *aborted-compilation-unit-count*))
178 (summarize-compilation-unit (not succeeded-p)))))))))
180 ;;; Is FUN-NAME something that no conforming program can rely on
181 ;;; defining as a function?
182 (defun fun-name-reserved-by-ansi-p (fun-name)
183 (eq (symbol-package (fun-name-block-name fun-name))
186 ;;; This is to be called at the end of a compilation unit. It signals
187 ;;; any residual warnings about unknown stuff, then prints the total
188 ;;; error counts. ABORT-P should be true when the compilation unit was
189 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
190 ;;; enclosed nested compilation units that were aborted.
191 (defun summarize-compilation-unit (abort-p)
193 (handler-bind ((style-warning #'compiler-style-warning-handler)
194 (warning #'compiler-warning-handler))
196 (let ((undefs (sort *undefined-warnings* #'string<
198 (let ((x (undefined-warning-name x)))
201 (prin1-to-string x)))))))
202 (dolist (undef undefs)
203 (let ((name (undefined-warning-name undef))
204 (kind (undefined-warning-kind undef))
205 (warnings (undefined-warning-warnings undef))
206 (undefined-warning-count (undefined-warning-count undef)))
207 (dolist (*compiler-error-context* warnings)
208 (if #-sb-xc-host (and (eq kind :function)
209 (fun-name-reserved-by-ansi-p name)
210 *flame-on-necessarily-undefined-function*)
215 "~@<There is no function named ~S. References to ~S in ~
216 some contexts (like starts of blocks) have special ~
217 meaning, but here it would have to be a function, ~
218 and that shouldn't be right.~:@>"
222 "~@<The ~(~A~) ~S is undefined, and its name is ~
223 reserved by ANSI CL so that even if it were ~
224 defined later, the code doing so would not be ~
227 (if (eq kind :variable)
228 (compiler-warn "undefined ~(~A~): ~S" kind name)
229 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
230 (let ((warn-count (length warnings)))
231 (when (and warnings (> undefined-warning-count warn-count))
232 (let ((more (- undefined-warning-count warn-count)))
233 (if (eq kind :variable)
235 "~W more use~:P of undefined ~(~A~) ~S"
238 "~W more use~:P of undefined ~(~A~) ~S"
239 more kind name)))))))
241 (dolist (kind '(:variable :function :type))
242 (let ((summary (mapcar #'undefined-warning-name
243 (remove kind undefs :test #'neq
244 :key #'undefined-warning-kind))))
246 (if (eq kind :variable)
248 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
249 ~% ~{~<~% ~1:;~S~>~^ ~}"
250 (cdr summary) kind summary)
252 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
253 ~% ~{~<~% ~1:;~S~>~^ ~}"
254 (cdr summary) kind summary))))))))
256 (unless (and (not abort-p)
257 (zerop *aborted-compilation-unit-count*)
258 (zerop *compiler-error-count*)
259 (zerop *compiler-warning-count*)
260 (zerop *compiler-style-warning-count*)
261 (zerop *compiler-note-count*))
262 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
263 (format *error-output* "~&compilation unit ~:[finished~;aborted~]~
264 ~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
265 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
266 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
267 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
268 ~[~:;~:*~& printed ~W note~:P~]"
270 *aborted-compilation-unit-count*
271 *compiler-error-count*
272 *compiler-warning-count*
273 *compiler-style-warning-count*
274 *compiler-note-count*))
275 (terpri *error-output*)
276 (force-output *error-output*)))
278 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
279 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
280 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
281 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
282 (defmacro with-compilation-values (&body body)
284 (let ((*warnings-p* nil)
286 (values (progn ,@body)
290 ;;;; component compilation
292 (defparameter *max-optimize-iterations* 3 ; ARB
294 "The upper limit on the number of times that we will consecutively do IR1
295 optimization that doesn't introduce any new code. A finite limit is
296 necessary, since type inference may take arbitrarily long to converge.")
298 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
299 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
301 ;;; Repeatedly optimize COMPONENT until no further optimizations can
302 ;;; be found or we hit our iteration limit. When we hit the limit, we
303 ;;; clear the component and block REOPTIMIZE flags to discourage the
304 ;;; next optimization attempt from pounding on the same code.
305 (defun ir1-optimize-until-done (component)
306 (declare (type component component))
308 (event ir1-optimize-until-done)
310 (cleared-reanalyze nil)
313 (when (component-reanalyze component)
315 (setq cleared-reanalyze t)
316 (setf (component-reanalyze component) nil))
317 (setf (component-reoptimize component) nil)
318 (ir1-optimize component fastp)
319 (cond ((component-reoptimize component)
321 (when (and (>= count *max-optimize-iterations*)
322 (not (component-reanalyze component))
323 (eq (component-reoptimize component) :maybe))
325 (cond ((retry-delayed-ir1-transforms :optimize)
329 (event ir1-optimize-maxed-out)
330 (setf (component-reoptimize component) nil)
331 (do-blocks (block component)
332 (setf (block-reoptimize block) nil))
334 ((retry-delayed-ir1-transforms :optimize)
340 (setq fastp (>= count *max-optimize-iterations*))
341 (maybe-mumble (if fastp "-" ".")))
342 (when cleared-reanalyze
343 (setf (component-reanalyze component) t)))
346 (defparameter *constraint-propagate* t)
348 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
349 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
350 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
351 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
353 ;;; FIXME: It would be good to document why it's important to have a
354 ;;; large value here, and what the drawbacks of an excessively large
355 ;;; value are; and it might also be good to make it depend on
356 ;;; optimization policy.
357 (defparameter *reoptimize-after-type-check-max* 10)
359 (defevent reoptimize-maxed-out
360 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
362 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
363 (defun dfo-as-needed (component)
364 (declare (type component component))
365 (when (component-reanalyze component)
369 (unless (component-reanalyze component)
375 ;;; Do all the IR1 phases for a non-top-level component.
376 (defun ir1-phases (component)
377 (declare (type component component))
378 (aver-live-component component)
379 (let ((*constraint-number* 0)
381 (*delayed-ir1-transforms* nil))
382 (declare (special *constraint-number* *delayed-ir1-transforms*))
384 (ir1-optimize-until-done component)
385 (when (or (component-new-functionals component)
386 (component-reanalyze-functionals component))
387 (maybe-mumble "locall ")
388 (locall-analyze-component component))
389 (dfo-as-needed component)
390 (when *constraint-propagate*
391 (maybe-mumble "constraint ")
392 (constraint-propagate component))
393 (when (retry-delayed-ir1-transforms :constraint)
394 (maybe-mumble "Rtran "))
395 (flet ((want-reoptimization-p ()
396 (or (component-reoptimize component)
397 (component-reanalyze component)
398 (component-new-functionals component)
399 (component-reanalyze-functionals component))))
400 (unless (and (want-reoptimization-p)
401 ;; We delay the generation of type checks until
402 ;; the type constraints have had time to
403 ;; propagate, else the compiler can confuse itself.
404 (< loop-count (- *reoptimize-after-type-check-max* 4)))
405 (maybe-mumble "type ")
406 (generate-type-checks component)
407 (unless (want-reoptimization-p)
409 (when (>= loop-count *reoptimize-after-type-check-max*)
410 (maybe-mumble "[reoptimize limit]")
411 (event reoptimize-maxed-out)
415 (ir1-finalize component)
418 (defun %compile-component (component)
419 (let ((*code-segment* nil)
421 (maybe-mumble "GTN ")
422 (gtn-analyze component)
423 (maybe-mumble "LTN ")
424 (ltn-analyze component)
425 (dfo-as-needed component)
426 (maybe-mumble "control ")
427 (control-analyze component #'make-ir2-block)
429 (when (or (ir2-component-values-receivers (component-info component))
430 (component-dx-lvars component))
431 (maybe-mumble "stack ")
432 (stack-analyze component)
433 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
434 ;; stack analysis. There shouldn't be any unreachable code after
435 ;; control, so this won't delete anything.
436 (dfo-as-needed component))
440 (maybe-mumble "IR2tran ")
442 (entry-analyze component)
443 (ir2-convert component)
445 (when (policy *lexenv* (>= speed compilation-speed))
446 (maybe-mumble "copy ")
447 (copy-propagate component))
449 (select-representations component)
451 (when *check-consistency*
452 (maybe-mumble "check2 ")
453 (check-ir2-consistency component))
455 (delete-unreferenced-tns component)
457 (maybe-mumble "life ")
458 (lifetime-analyze component)
460 (when *compile-progress*
461 (compiler-mumble "") ; Sync before doing more output.
462 (pre-pack-tn-stats component *standard-output*))
464 (when *check-consistency*
465 (maybe-mumble "check-life ")
466 (check-life-consistency component))
468 (maybe-mumble "pack ")
471 (when *check-consistency*
472 (maybe-mumble "check-pack ")
473 (check-pack-consistency component))
475 (when *compiler-trace-output*
476 (describe-component component *compiler-trace-output*)
477 (describe-ir2-component component *compiler-trace-output*))
479 (maybe-mumble "code ")
480 (multiple-value-bind (code-length trace-table fixup-notes)
481 (generate-code component)
484 (when *compiler-trace-output*
485 (format *compiler-trace-output*
486 "~|~%disassembly of code for ~S~2%" component)
487 (sb!disassem:disassemble-assem-segment *code-segment*
488 *compiler-trace-output*))
490 (etypecase *compile-object*
492 (maybe-mumble "fasl")
493 (fasl-dump-component component
500 (maybe-mumble "core")
501 (make-core-component component
509 ;; We're done, so don't bother keeping anything around.
510 (setf (component-info component) :dead)
514 ;;; Delete components with no external entry points before we try to
515 ;;; generate code. Unreachable closures can cause IR2 conversion to
516 ;;; puke on itself, since it is the reference to the closure which
517 ;;; normally causes the components to be combined.
518 (defun delete-if-no-entries (component)
519 (dolist (fun (component-lambdas component) (delete-component component))
520 (when (functional-has-external-references-p fun)
522 (case (functional-kind fun)
525 (unless (every (lambda (ref)
526 (eq (node-component ref) component))
530 (defun compile-component (component)
532 ;; miscellaneous sanity checks
534 ;; FIXME: These are basically pretty wimpy compared to the checks done
535 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
536 ;; make those internal consistency checks work again and use them.
537 (aver-live-component component)
538 (do-blocks (block component)
539 (aver (eql (block-component block) component)))
540 (dolist (lambda (component-lambdas component))
541 ;; sanity check to prevent weirdness from propagating insidiously as
542 ;; far from its root cause as it did in bug 138: Make sure that
543 ;; thing-to-COMPONENT links are consistent.
544 (aver (eql (lambda-component lambda) component))
545 (aver (eql (node-component (lambda-bind lambda)) component)))
547 (let* ((*component-being-compiled* component))
549 ;; Record xref information before optimization. This way the
550 ;; stored xref data reflects the real source as closely as
552 (record-component-xrefs component)
554 (ir1-phases component)
557 (dfo-as-needed component)
558 (find-dominators component)
559 (loop-analyze component))
562 (when (and *loop-analyze* *compiler-trace-output*)
563 (labels ((print-blocks (block)
564 (format *compiler-trace-output* " ~A~%" block)
565 (when (block-loop-next block)
566 (print-blocks (block-loop-next block))))
568 (format *compiler-trace-output* "loop=~A~%" loop)
569 (print-blocks (loop-blocks loop))
570 (dolist (l (loop-inferiors loop))
572 (print-loop (component-outer-loop component))))
575 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
576 (maybe-mumble "env ")
577 (physenv-analyze component)
578 (dfo-as-needed component)
580 (delete-if-no-entries component)
582 (unless (eq (block-next (component-head component))
583 (component-tail component))
584 (%compile-component component)))
586 (clear-constant-info)
590 ;;;; clearing global data structures
592 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
593 ;;;; global data structures entirely when possible and consing up the
594 ;;;; others from scratch instead of clearing and reusing them?
596 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
597 ;;; addition to allowing stuff to be reclaimed, this is required for
598 ;;; correct assignment of constant offsets, since we need to assign a
599 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
600 ;;; slots, since they are used to keep track of functions across
601 ;;; component boundaries.
602 (defun clear-constant-info ()
603 (maphash (lambda (k v)
605 (setf (leaf-info v) nil))
607 (maphash (lambda (k v)
610 (setf (leaf-info v) nil)))
614 ;;; Blow away the REFS for all global variables, and let COMPONENT
616 (defun clear-ir1-info (component)
617 (declare (type component component))
619 (maphash (lambda (k v)
623 (delete-if #'here-p (leaf-refs v)))
624 (when (basic-var-p v)
625 (setf (basic-var-sets v)
626 (delete-if #'here-p (basic-var-sets v))))))
629 (eq (node-component x) component)))
635 ;;; Clear global variables used by the compiler.
637 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
638 ;;; and it adds overhead even when people aren't using the compiler.
639 ;;; Perhaps we could make these global vars unbound except when
640 ;;; actually in use, so that this function could go away.
641 (defun clear-stuff (&optional (debug-too t))
643 ;; Clear global tables.
644 (when (boundp '*free-funs*)
645 (clrhash *free-funs*)
646 (clrhash *free-vars*)
647 (clrhash *constants*))
649 ;; Clear debug counters and tables.
650 (clrhash *seen-blocks*)
651 (clrhash *seen-funs*)
652 (clrhash *list-conflicts-table*)
655 (clrhash *continuation-numbers*)
656 (clrhash *number-continuations*)
657 (setq *continuation-number* 0)
661 (clrhash *label-ids*)
662 (clrhash *id-labels*)
665 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
666 ;; Superficially, this seemed harmful -- the user could reasonably be
667 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
668 ;; compiled. A closer inspection showed that this actually turned out to be
669 ;; harmless in practice, because CLEAR-STUFF was only called from within
670 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
671 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
672 ;; practice, it was also useless in practice. So we don't do it any more.)
678 ;;; Print out some useful info about COMPONENT to STREAM.
679 (defun describe-component (component *standard-output*)
680 (declare (type component component))
681 (format t "~|~%;;;; component: ~S~2%" (component-name component))
682 (print-all-blocks component)
685 (defun describe-ir2-component (component *standard-output*)
686 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
687 (format t "entries:~%")
688 (dolist (entry (ir2-component-entries (component-info component)))
689 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
690 (label-id (entry-info-offset entry))
691 (entry-info-name entry)
692 (entry-info-closure-tn entry)))
694 (pre-pack-tn-stats component *standard-output*)
696 (print-ir2-blocks component)
702 ;;;; When reading from a file, we have to keep track of some source
703 ;;;; information. We also exploit our ability to back up for printing
704 ;;;; the error context and for recovering from errors.
706 ;;;; The interface we provide to this stuff is the stream-oid
707 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
708 ;;;; of getting the next source form.
710 ;;; A FILE-INFO structure holds all the source information for a
712 (def!struct (file-info (:copier nil))
713 ;; If a file, the truename of the corresponding source file. If from
714 ;; a Lisp form, :LISP. If from a stream, :STREAM.
715 (name (missing-arg) :type (or pathname (member :lisp :stream)))
716 ;; the external format that we'll call OPEN with, if NAME is a file.
717 (external-format nil)
718 ;; the defaulted, but not necessarily absolute file name (i.e. prior
719 ;; to TRUENAME call.) Null if not a file. This is used to set
720 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
722 (untruename nil :type (or pathname null))
723 ;; the file's write date (if relevant)
724 (write-date nil :type (or unsigned-byte null))
725 ;; the source path root number of the first form in this file (i.e.
726 ;; the total number of forms converted previously in this
728 (source-root 0 :type unsigned-byte)
729 ;; parallel vectors containing the forms read out of the file and
730 ;; the file positions that reading of each form started at (i.e. the
731 ;; end of the previous form)
732 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
733 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
735 ;;; The SOURCE-INFO structure provides a handle on all the source
736 ;;; information for an entire compilation.
737 (def!struct (source-info
738 #-no-ansi-print-object
739 (:print-object (lambda (s stream)
740 (print-unreadable-object (s stream :type t))))
742 ;; the UT that compilation started at
743 (start-time (get-universal-time) :type unsigned-byte)
744 ;; the FILE-INFO structure for this compilation
745 (file-info nil :type (or file-info null))
746 ;; the stream that we are using to read the FILE-INFO, or NIL if
747 ;; no stream has been opened yet
748 (stream nil :type (or stream null)))
750 ;;; Given a pathname, return a SOURCE-INFO structure.
751 (defun make-file-source-info (file external-format)
752 (let ((file-info (make-file-info :name (truename file)
753 :untruename (merge-pathnames file)
754 :external-format external-format
755 :write-date (file-write-date file))))
757 (make-source-info :file-info file-info)))
759 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
760 (defun make-lisp-source-info (form)
761 (make-source-info :start-time (get-universal-time)
762 :file-info (make-file-info :name :lisp
766 ;;; Return a SOURCE-INFO which will read from STREAM.
767 (defun make-stream-source-info (stream)
768 (let ((file-info (make-file-info :name :stream)))
769 (make-source-info :file-info file-info
772 ;;; Return a form read from STREAM; or for EOF use the trick,
773 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
774 ;;; error happens, then convert it to standard abort-the-compilation
775 ;;; error condition (possibly recording some extra location
777 (defun read-for-compile-file (stream position)
778 (handler-case (read stream nil stream)
779 (reader-error (condition)
780 (error 'input-error-in-compile-file
782 ;; We don't need to supply :POSITION here because
783 ;; READER-ERRORs already know their position in the file.
785 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
786 ;; (and that this is not a READER-ERROR) when it encounters end of
787 ;; file in the middle of something it's trying to read.
788 (end-of-file (condition)
789 (error 'input-error-in-compile-file
791 ;; We need to supply :POSITION here because the END-OF-FILE
792 ;; condition doesn't carry the position that the user
793 ;; probably cares about, where the failed READ began.
794 :position position))))
796 ;;; If STREAM is present, return it, otherwise open a stream to the
797 ;;; current file. There must be a current file.
799 ;;; FIXME: This is probably an unnecessarily roundabout way to do
800 ;;; things now that we process a single file in COMPILE-FILE (unlike
801 ;;; the old CMU CL code, which accepted multiple files). Also, the old
803 ;;; When we open a new file, we also reset *PACKAGE* and policy.
804 ;;; This gives the effect of rebinding around each file.
805 ;;; which doesn't seem to be true now. Check to make sure that if
806 ;;; such rebinding is necessary, it's still done somewhere.
807 (defun get-source-stream (info)
808 (declare (type source-info info))
809 (or (source-info-stream info)
810 (let* ((file-info (source-info-file-info info))
811 (name (file-info-name file-info))
812 (external-format (file-info-external-format file-info)))
813 (setf sb!xc:*compile-file-truename* name
814 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
815 (source-info-stream info)
816 (open name :direction :input
817 :external-format external-format)))))
819 ;;; Close the stream in INFO if it is open.
820 (defun close-source-info (info)
821 (declare (type source-info info))
822 (let ((stream (source-info-stream info)))
823 (when stream (close stream)))
824 (setf (source-info-stream info) nil)
827 ;;; Read and compile the source file.
828 (defun sub-sub-compile-file (info)
829 (let* ((file-info (source-info-file-info info))
830 (stream (get-source-stream info)))
832 (let* ((pos (file-position stream))
833 (form (read-for-compile-file stream pos)))
834 (if (eq form stream) ; i.e., if EOF
836 (let* ((forms (file-info-forms file-info))
837 (current-idx (+ (fill-pointer forms)
838 (file-info-source-root file-info))))
839 (vector-push-extend form forms)
840 (vector-push-extend pos (file-info-positions file-info))
841 (find-source-paths form current-idx)
842 (process-toplevel-form form
843 `(original-source-start 0 ,current-idx)
846 ;;; Return the INDEX'th source form read from INFO and the position
847 ;;; where it was read.
848 (defun find-source-root (index info)
849 (declare (type index index) (type source-info info))
850 (let ((file-info (source-info-file-info info)))
851 (values (aref (file-info-forms file-info) index)
852 (aref (file-info-positions file-info) index))))
854 ;;;; processing of top level forms
856 ;;; This is called by top level form processing when we are ready to
857 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
858 ;;; convert the form, but delay compilation, pushing the result on
859 ;;; *TOPLEVEL-LAMBDAS* instead.
860 (defun convert-and-maybe-compile (form path)
861 (declare (list path))
862 (if (fopcompilable-p form)
863 (let ((*fopcompile-label-counter* 0))
864 (fopcompile form path nil))
865 (let* ((*top-level-form-noted* (note-top-level-form form t))
866 (*lexenv* (make-lexenv
868 :handled-conditions *handled-conditions*
869 :disabled-package-locks *disabled-package-locks*))
870 (tll (ir1-toplevel form path nil)))
871 (if (eq *block-compile* t)
872 (push tll *toplevel-lambdas*)
873 (compile-toplevel (list tll) nil))
876 ;;; Macroexpand FORM in the current environment with an error handler.
877 ;;; We only expand one level, so that we retain all the intervening
878 ;;; forms in the source path.
879 (defun preprocessor-macroexpand-1 (form)
880 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
882 (compiler-error "(during macroexpansion of ~A)~%~A"
883 (let ((*print-level* 2)
885 (format nil "~S" form))
888 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
889 ;;; the forms, and PATH is the source path of the FORM they came out of.
890 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
891 (defun process-toplevel-progn (forms path compile-time-too)
892 (declare (list forms) (list path))
894 (process-toplevel-form form path compile-time-too)))
896 ;;; Process a top level use of LOCALLY, or anything else (e.g.
897 ;;; MACROLET) at top level which has declarations and ordinary forms.
898 ;;; We parse declarations and then recursively process the body.
899 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
900 (declare (list path))
901 (multiple-value-bind (forms decls)
902 (parse-body body :doc-string-allowed nil :toplevel t)
903 (let* ((*lexenv* (process-decls decls vars funs))
904 ;; FIXME: VALUES declaration
906 ;; Binding *POLICY* is pretty much of a hack, since it
907 ;; causes LOCALLY to "capture" enclosed proclamations. It
908 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
909 ;; value of *POLICY* as the policy. The need for this hack
910 ;; is due to the quirk that there is no way to represent in
911 ;; a POLICY that an optimize quality came from the default.
913 ;; FIXME: Ideally, something should be done so that DECLAIM
914 ;; inside LOCALLY works OK. Failing that, at least we could
915 ;; issue a warning instead of silently screwing up.
916 (*policy* (lexenv-policy *lexenv*))
917 ;; This is probably also a hack
918 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
920 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*)))
921 (process-toplevel-progn forms path compile-time-too))))
923 ;;; Parse an EVAL-WHEN situations list, returning three flags,
924 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
925 ;;; the types of situations present in the list.
926 (defun parse-eval-when-situations (situations)
927 (when (or (not (listp situations))
928 (set-difference situations
935 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
936 (let ((deprecated-names (intersection situations '(compile load eval))))
937 (when deprecated-names
938 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
940 (values (intersection '(:compile-toplevel compile)
942 (intersection '(:load-toplevel load) situations)
943 (intersection '(:execute eval) situations)))
946 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
947 (defun functional-components (f)
948 (declare (type functional f))
950 (clambda (list (lambda-component f)))
951 (optional-dispatch (let ((result nil))
952 (flet ((maybe-frob (maybe-clambda)
953 (when (and maybe-clambda
954 (promise-ready-p maybe-clambda))
955 (pushnew (lambda-component
956 (force maybe-clambda))
958 (map nil #'maybe-frob (optional-dispatch-entry-points f))
959 (maybe-frob (optional-dispatch-more-entry f))
960 (maybe-frob (optional-dispatch-main-entry f)))
963 (defun make-functional-from-toplevel-lambda (definition
967 ;; I'd thought NIL should
968 ;; work, but it doesn't.
971 (let* ((*current-path* path)
972 (component (make-empty-component))
973 (*current-component* component))
974 (setf (component-name component)
975 (debug-name 'initial-component name))
976 (setf (component-kind component) :initial)
977 (let* ((locall-fun (let ((*allow-instrumenting* t))
978 (funcall #'ir1-convert-lambdalike
981 (debug-name (debug-name 'tl-xep name))
982 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
983 :source-name (or name '.anonymous.)
984 :debug-name debug-name)))
986 (assert-global-function-definition-type name locall-fun))
987 (setf (functional-entry-fun fun) locall-fun
988 (functional-kind fun) :external
989 (functional-has-external-references-p fun) t)
992 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
993 ;;; description of the result.
994 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
995 ;;; into core and return the compiled FUNCTION value.
996 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
997 ;;; into the fasl file and return a dump handle.
999 ;;; If NAME is provided, then we try to use it as the name of the
1000 ;;; function for debugging/diagnostic information.
1001 (defun %compile (lambda-expression
1006 ;; This magical idiom seems to be the appropriate
1007 ;; path for compiling standalone LAMBDAs, judging
1008 ;; from the CMU CL code and experiment, so it's a
1009 ;; nice default for things where we don't have a
1010 ;; real source path (as in e.g. inside CL:COMPILE).
1011 '(original-source-start 0 0)))
1013 (legal-fun-name-or-type-error name))
1014 (let* ((*lexenv* (make-lexenv
1016 :handled-conditions *handled-conditions*
1017 :disabled-package-locks *disabled-package-locks*))
1018 (fun (make-functional-from-toplevel-lambda lambda-expression
1022 ;; FIXME: The compile-it code from here on is sort of a
1023 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1024 ;; better to find a way to share the code there; or
1025 ;; alternatively, to use this code to replace the code there.
1026 ;; (The second alternative might be pretty easy if we used
1027 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1028 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1030 (locall-analyze-clambdas-until-done (list fun))
1032 (multiple-value-bind (components-from-dfo top-components hairy-top)
1033 (find-initial-dfo (list fun))
1034 (declare (ignore hairy-top))
1036 (let ((*all-components* (append components-from-dfo top-components)))
1037 (dolist (component-from-dfo components-from-dfo)
1038 (compile-component component-from-dfo)
1039 (replace-toplevel-xeps component-from-dfo)))
1041 (let ((entry-table (etypecase *compile-object*
1042 (fasl-output (fasl-output-entry-table
1044 (core-object (core-object-entry-table
1045 *compile-object*)))))
1046 (multiple-value-bind (result found-p)
1047 (gethash (leaf-info fun) entry-table)
1051 ;; KLUDGE: This code duplicates some other code in this
1052 ;; file. In the great reorganzation, the flow of program
1053 ;; logic changed from the original CMUCL model, and that
1054 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1055 ;; longer followed for CORE-OBJECTS, leading to BUG
1056 ;; 156. This place is transparently not the right one for
1057 ;; this code, but I don't have a clear enough overview of
1058 ;; the compiler to know how to rearrange it all so that
1059 ;; this operation fits in nicely, and it was blocking
1060 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1061 ;; ((..)) (DEFUN FOO ...))
1063 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1064 ;; ideal way, as (1) definitions typed in at the REPL
1065 ;; without an INLINE declaration will give a NULL
1066 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1067 ;; and (2) INLINE declarations will yield a
1068 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1069 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1072 ;; (2) is probably fairly easy to fix -- it is, after all,
1073 ;; a matter of list manipulation (or possibly of teaching
1074 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1075 ;; significantly harder, as the association between
1076 ;; function object and source is a tricky one.
1078 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1079 ;; non-NULL list) when the function in question has been
1080 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1081 ;; work when it has been compiled as part of the top-level
1082 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1083 ;; ...). -- CSR, 2002-11-02
1084 (when (core-object-p *compile-object*)
1085 (fix-core-source-info *source-info* *compile-object* result))
1087 (mapc #'clear-ir1-info components-from-dfo)
1090 (defun process-toplevel-cold-fset (name lambda-expression path)
1091 (unless (producing-fasl-file)
1092 (error "can't COLD-FSET except in a fasl file"))
1093 (legal-fun-name-or-type-error name)
1094 (fasl-dump-cold-fset name
1095 (%compile lambda-expression
1102 (defun note-top-level-form (form &optional finalp)
1103 (when *compile-print*
1104 (cond ((not *top-level-form-noted*)
1105 (let ((*print-length* 2)
1107 (*print-pretty* nil))
1108 (with-compiler-io-syntax
1109 (compiler-mumble "~&; ~:[compiling~;converting~] ~S"
1110 *block-compile* form)))
1113 (eq :top-level-forms *compile-print*)
1114 (neq form *top-level-form-noted*))
1115 (let ((*print-length* 1)
1117 (*print-pretty* nil))
1118 (with-compiler-io-syntax
1119 (compiler-mumble "~&; ... top level ~S" form)))
1122 *top-level-form-noted*))))
1124 ;;; Process a top level FORM with the specified source PATH.
1125 ;;; * If this is a magic top level form, then do stuff.
1126 ;;; * If this is a macro, then expand it.
1127 ;;; * Otherwise, just compile it.
1129 ;;; COMPILE-TIME-TOO is as defined in ANSI
1130 ;;; "3.2.3.1 Processing of Top Level Forms".
1131 (defun process-toplevel-form (form path compile-time-too)
1132 (declare (list path))
1134 (catch 'process-toplevel-form-error-abort
1135 (let* ((path (or (gethash form *source-paths*) (cons form path)))
1136 (*compiler-error-bailout*
1137 (lambda (&optional condition)
1138 (convert-and-maybe-compile
1139 (make-compiler-error-form condition form)
1141 (throw 'process-toplevel-form-error-abort nil))))
1143 (flet ((default-processor (form)
1144 (let ((*top-level-form-noted* (note-top-level-form form)))
1145 ;; When we're cross-compiling, consider: what should we
1146 ;; do when we hit e.g.
1147 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1148 ;; (DEFUN FOO (X) (+ 7 X)))?
1149 ;; DEFUN has a macro definition in the cross-compiler,
1150 ;; and a different macro definition in the target
1151 ;; compiler. The only sensible thing is to use the
1152 ;; target compiler's macro definition, since the
1153 ;; cross-compiler's macro is in general into target
1154 ;; functions which can't meaningfully be executed at
1155 ;; cross-compilation time. So make sure we do the EVAL
1156 ;; here, before we macroexpand.
1158 ;; Then things get even dicier with something like
1159 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1160 ;; where we have to make sure that we don't uncross
1161 ;; the SB!XC: prefix before we do EVAL, because otherwise
1162 ;; we'd be trying to redefine the cross-compilation host's
1165 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1168 (when compile-time-too
1169 (eval form)) ; letting xc host EVAL do its own macroexpansion
1170 (let* (;; (We uncross the operator name because things
1171 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1172 ;; should be equivalent to their CL: counterparts
1173 ;; when being compiled as target code. We leave
1174 ;; the rest of the form uncrossed because macros
1175 ;; might yet expand into EVAL-WHEN stuff, and
1176 ;; things inside EVAL-WHEN can't be uncrossed
1177 ;; until after we've EVALed them in the
1178 ;; cross-compilation host.)
1179 (slightly-uncrossed (cons (uncross (first form))
1181 (expanded (preprocessor-macroexpand-1
1182 slightly-uncrossed)))
1183 (if (eq expanded slightly-uncrossed)
1184 ;; (Now that we're no longer processing toplevel
1185 ;; forms, and hence no longer need to worry about
1186 ;; EVAL-WHEN, we can uncross everything.)
1187 (convert-and-maybe-compile expanded path)
1188 ;; (We have to demote COMPILE-TIME-TOO to NIL
1189 ;; here, no matter what it was before, since
1190 ;; otherwise we'd tend to EVAL subforms more than
1191 ;; once, because of WHEN COMPILE-TIME-TOO form
1193 (process-toplevel-form expanded path nil))))
1194 ;; When we're not cross-compiling, we only need to
1195 ;; macroexpand once, so we can follow the 1-thru-6
1196 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1197 ;; Top Level Forms".
1200 (let ((*current-path* path))
1201 (preprocessor-macroexpand-1 form))))
1202 (cond ((eq expanded form)
1203 (when compile-time-too
1204 (eval-in-lexenv form *lexenv*))
1205 (convert-and-maybe-compile form path))
1207 (process-toplevel-form expanded
1209 compile-time-too)))))))
1212 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1213 ;; (1) SBCL gets smart enough to handle global
1214 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1215 ;; implementors start using symbol macros in a way which
1216 ;; interacts with SB-XC/CL distinction.)
1217 (convert-and-maybe-compile form path)
1219 (default-processor form)
1220 (flet ((need-at-least-one-arg (form)
1222 (compiler-error "~S form is too short: ~S"
1226 ;; In the cross-compiler, top level COLD-FSET arranges
1227 ;; for static linking at cold init time.
1230 (aver (not compile-time-too))
1231 (destructuring-bind (cold-fset fun-name lambda-expression) form
1232 (declare (ignore cold-fset))
1233 (process-toplevel-cold-fset fun-name
1236 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1237 (need-at-least-one-arg form)
1238 (destructuring-bind (special-operator magic &rest body) form
1239 (ecase special-operator
1241 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1242 ;; "3.2.3.1 Processing of Top Level Forms".
1243 (multiple-value-bind (ct lt e)
1244 (parse-eval-when-situations magic)
1245 (let ((new-compile-time-too (or ct
1246 (and compile-time-too
1248 (cond (lt (process-toplevel-progn
1249 body path new-compile-time-too))
1250 (new-compile-time-too (eval-in-lexenv
1254 (funcall-in-macrolet-lexenv
1256 (lambda (&key funs prepend)
1257 (declare (ignore funs))
1258 (aver (null prepend))
1259 (process-toplevel-locally body
1264 (funcall-in-symbol-macrolet-lexenv
1266 (lambda (&key vars prepend)
1267 (aver (null prepend))
1268 (process-toplevel-locally body
1274 (process-toplevel-locally (rest form) path compile-time-too))
1276 (process-toplevel-progn (rest form) path compile-time-too))
1277 (t (default-processor form))))))))
1281 ;;;; load time value support
1283 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1285 ;;; Return T if we are currently producing a fasl file and hence
1286 ;;; constants need to be dumped carefully.
1287 (defun producing-fasl-file ()
1288 (fasl-output-p *compile-object*))
1290 ;;; Compile FORM and arrange for it to be called at load-time. Return
1291 ;;; the dumper handle and our best guess at the type of the object.
1292 (defun compile-load-time-value (form)
1293 (let ((lambda (compile-load-time-stuff form t)))
1295 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1296 (let ((type (leaf-type lambda)))
1297 (if (fun-type-p type)
1298 (single-value-type (fun-type-returns type))
1301 ;;; Compile the FORMS and arrange for them to be called (for effect,
1302 ;;; not value) at load time.
1303 (defun compile-make-load-form-init-forms (forms)
1304 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1305 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1307 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1308 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1309 (defun compile-load-time-stuff (form for-value)
1311 (let* ((*lexenv* (make-null-lexenv))
1312 (lambda (ir1-toplevel form *current-path* for-value)))
1313 (compile-toplevel (list lambda) t)
1316 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1317 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1318 ;;; don't try to combine this component with anything else and frob
1319 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1320 ;;; compiling, because it was merged with a run-time component.
1321 (defun compile-load-time-value-lambda (lambdas)
1322 (aver (null (cdr lambdas)))
1323 (let* ((lambda (car lambdas))
1324 (component (lambda-component lambda)))
1325 (when (eql (component-kind component) :toplevel)
1326 (setf (component-name component) (leaf-debug-name lambda))
1327 (compile-component component)
1328 (clear-ir1-info component))))
1332 (defun object-call-toplevel-lambda (tll)
1333 (declare (type functional tll))
1334 (let ((object *compile-object*))
1336 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1337 (core-object (core-call-toplevel-lambda tll object))
1340 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1341 ;;; the resulting function to be called.
1342 (defun sub-compile-toplevel-lambdas (lambdas)
1343 (declare (list lambdas))
1345 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1346 (compile-component component)
1347 (clear-ir1-info component)
1348 (object-call-toplevel-lambda tll)))
1351 ;;; Compile top level code and call the top level lambdas. We pick off
1352 ;;; top level lambdas in non-top-level components here, calling
1353 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1354 (defun compile-toplevel-lambdas (lambdas)
1355 (declare (list lambdas))
1356 (let ((len (length lambdas)))
1357 (flet ((loser (start)
1358 (or (position-if (lambda (x)
1359 (not (eq (component-kind
1360 (node-component (lambda-bind x)))
1363 ;; this used to read ":start start", but
1364 ;; start can be greater than len, which
1365 ;; is an error according to ANSI - CSR,
1367 :start (min start len))
1369 (do* ((start 0 (1+ loser))
1370 (loser (loser start) (loser start)))
1372 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1373 (unless (= loser len)
1374 (object-call-toplevel-lambda (elt lambdas loser))))))
1377 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1380 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1381 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1382 (defun compile-toplevel (lambdas load-time-value-p)
1383 (declare (list lambdas))
1385 (maybe-mumble "locall ")
1386 (locall-analyze-clambdas-until-done lambdas)
1388 (maybe-mumble "IDFO ")
1389 (multiple-value-bind (components top-components hairy-top)
1390 (find-initial-dfo lambdas)
1391 (let ((*all-components* (append components top-components)))
1392 (when *check-consistency*
1393 (maybe-mumble "[check]~%")
1394 (check-ir1-consistency *all-components*))
1396 (dolist (component (append hairy-top top-components))
1397 (pre-physenv-analyze-toplevel component))
1399 (dolist (component components)
1400 (compile-component component)
1401 (replace-toplevel-xeps component))
1403 (when *check-consistency*
1404 (maybe-mumble "[check]~%")
1405 (check-ir1-consistency *all-components*))
1407 (if load-time-value-p
1408 (compile-load-time-value-lambda lambdas)
1409 (compile-toplevel-lambdas lambdas))
1411 (mapc #'clear-ir1-info components)
1415 ;;; Actually compile any stuff that has been queued up for block
1417 (defun finish-block-compilation ()
1418 (when *block-compile*
1419 (when *compile-print*
1420 (compiler-mumble "~&; block compiling converted top level forms..."))
1421 (when *toplevel-lambdas*
1422 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1423 (setq *toplevel-lambdas* ()))
1424 (setq *block-compile* nil)
1425 (setq *entry-points* nil)))
1427 (defun handle-condition-p (condition)
1429 (etypecase *compiler-error-context*
1431 (node-lexenv *compiler-error-context*))
1432 (compiler-error-context
1433 (let ((lexenv (compiler-error-context-lexenv
1434 *compiler-error-context*)))
1438 (let ((muffles (lexenv-handled-conditions lexenv)))
1439 (if (null muffles) ; common case
1441 (dolist (muffle muffles nil)
1442 (destructuring-bind (typespec . restart-name) muffle
1443 (when (and (typep condition typespec)
1444 (find-restart restart-name condition))
1447 (defun handle-condition-handler (condition)
1449 (etypecase *compiler-error-context*
1451 (node-lexenv *compiler-error-context*))
1452 (compiler-error-context
1453 (let ((lexenv (compiler-error-context-lexenv
1454 *compiler-error-context*)))
1458 (let ((muffles (lexenv-handled-conditions lexenv)))
1460 (dolist (muffle muffles (bug "fell through"))
1461 (destructuring-bind (typespec . restart-name) muffle
1462 (when (typep condition typespec)
1463 (awhen (find-restart restart-name condition)
1464 (invoke-restart it))))))))
1466 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1467 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1468 (defun sub-compile-file (info)
1469 (declare (type source-info info))
1470 (let ((*package* (sane-package))
1471 (*readtable* *readtable*)
1472 (sb!xc:*compile-file-pathname* nil) ; really bound in
1473 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1475 (*handled-conditions* *handled-conditions*)
1476 (*disabled-package-locks* *disabled-package-locks*)
1477 (*lexenv* (make-null-lexenv))
1478 (*block-compile* *block-compile-arg*)
1479 (*source-info* info)
1480 (*toplevel-lambdas* ())
1481 (*fun-names-in-this-file* ())
1482 (*allow-instrumenting* nil)
1483 (*compiler-error-bailout*
1485 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1486 (return-from sub-compile-file (values nil t t))))
1487 (*current-path* nil)
1488 (*last-source-context* nil)
1489 (*last-original-source* nil)
1490 (*last-source-form* nil)
1491 (*last-format-string* nil)
1492 (*last-format-args* nil)
1493 (*last-message-count* 0)
1494 ;; FIXME: Do we need this rebinding here? It's a literal
1495 ;; translation of the old CMU CL rebinding to
1496 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1497 ;; and it's not obvious whether the rebinding to itself is
1498 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1499 (*info-environment* *info-environment*)
1500 (*gensym-counter* 0))
1502 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1503 (with-compilation-values
1504 (sb!xc:with-compilation-unit ()
1507 (sub-sub-compile-file info)
1509 (finish-block-compilation)
1510 (let ((object *compile-object*))
1512 (fasl-output (fasl-dump-source-info info object))
1513 (core-object (fix-core-source-info info object))
1516 ;; Some errors are sufficiently bewildering that we just fail
1517 ;; immediately, without trying to recover and compile more of
1519 (fatal-compiler-error (condition)
1521 (when *compile-verbose*
1522 (format *standard-output*
1523 "~@<compilation aborted because of fatal error: ~2I~_~A~:>"
1525 (values nil t t)))))
1527 ;;; Return a pathname for the named file. The file must exist.
1528 (defun verify-source-file (pathname-designator)
1529 (let* ((pathname (pathname pathname-designator))
1530 (default-host (make-pathname :host (pathname-host pathname))))
1531 (flet ((try-with-type (path type error-p)
1532 (let ((new (merge-pathnames
1533 path (make-pathname :type type
1534 :defaults default-host))))
1535 (if (probe-file new)
1537 (and error-p (truename new))))))
1538 (cond ((typep pathname 'logical-pathname)
1539 (try-with-type pathname "LISP" t))
1540 ((probe-file pathname) pathname)
1541 ((try-with-type pathname "lisp" nil))
1542 ((try-with-type pathname "lisp" t))))))
1544 (defun elapsed-time-to-string (tsec)
1545 (multiple-value-bind (tmin sec) (truncate tsec 60)
1546 (multiple-value-bind (thr min) (truncate tmin 60)
1547 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1549 ;;; Print some junk at the beginning and end of compilation.
1550 (defun print-compile-start-note (source-info)
1551 (declare (type source-info source-info))
1552 (let ((file-info (source-info-file-info source-info)))
1553 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1554 (namestring (file-info-name file-info))
1555 (sb!int:format-universal-time nil
1556 (file-info-write-date
1560 :print-timezone nil)))
1563 (defun print-compile-end-note (source-info won)
1564 (declare (type source-info source-info))
1565 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1567 (elapsed-time-to-string
1568 (- (get-universal-time)
1569 (source-info-start-time source-info))))
1572 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1573 ;;; out of the compile, then abort the writing of the output file, so
1574 ;;; that we don't overwrite it with known garbage.
1575 (defun sb!xc:compile-file
1580 (output-file (cfp-output-file-default input-file))
1581 ;; FIXME: ANSI doesn't seem to say anything about
1582 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1584 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1585 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1586 (external-format :default)
1590 ((:block-compile *block-compile-arg*) nil))
1592 "Compile INPUT-FILE, producing a corresponding fasl file and
1593 returning its filename.
1596 If true, a message per non-macroexpanded top level form is printed
1597 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1598 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1599 no such message, but their subforms do.
1601 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1602 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1603 For example, compiling an IN-PACKAGE form will result in a message about
1604 a top level SETQ in addition to the message about the IN-PACKAGE form'
1607 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1610 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1611 argument, it is not currently supported. (non-standard)
1614 If given, internal data structures are dumped to the specified
1615 file, or if a value of T is given, to a file of *.trace type
1616 derived from the input file name. (non-standard)"
1617 ;;; Block compilation is currently broken.
1619 "Also, as a workaround for vaguely-non-ANSI behavior, the
1620 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1621 multiple functions are compiled together as a unit, resolving function
1622 references at compile time. NIL means that global function names are
1623 never resolved at compilation time. Currently NIL is the default
1624 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1625 of the ANSI spec allows this behavior under all circumstances, the
1626 compiler's runtime scales badly when it tries to do this for large
1627 files. If/when this performance problem is fixed, the block
1628 compilation default behavior will probably be made dependent on the
1629 SPEED and COMPILATION-SPEED optimization values, and the
1630 :BLOCK-COMPILE argument will probably become deprecated."
1632 (let* ((fasl-output nil)
1633 (output-file-name nil)
1636 (failure-p t) ; T in case error keeps this from being set later
1637 (input-pathname (verify-source-file input-file))
1638 (source-info (make-file-source-info input-pathname external-format))
1639 (*compiler-trace-output* nil)) ; might be modified below
1644 (setq output-file-name
1645 (sb!xc:compile-file-pathname input-file
1646 :output-file output-file))
1648 (open-fasl-output output-file-name
1649 (namestring input-pathname))))
1651 (let* ((default-trace-file-pathname
1652 (make-pathname :type "trace" :defaults input-pathname))
1653 (trace-file-pathname
1654 (if (eql trace-file t)
1655 default-trace-file-pathname
1656 (merge-pathnames trace-file
1657 default-trace-file-pathname))))
1658 (setf *compiler-trace-output*
1659 (open trace-file-pathname
1660 :if-exists :supersede
1661 :direction :output))))
1663 (when sb!xc:*compile-verbose*
1664 (print-compile-start-note source-info))
1665 (let ((*compile-object* fasl-output)
1667 (multiple-value-setq (dummy warnings-p failure-p)
1668 (sub-compile-file source-info)))
1669 (setq compile-won t))
1671 (close-source-info source-info)
1674 (close-fasl-output fasl-output (not compile-won))
1675 (setq output-file-name
1676 (pathname (fasl-output-stream fasl-output)))
1677 (when (and compile-won sb!xc:*compile-verbose*)
1678 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1680 (when sb!xc:*compile-verbose*
1681 (print-compile-end-note source-info compile-won))
1683 (when *compiler-trace-output*
1684 (close *compiler-trace-output*)))
1686 (values (if output-file
1687 ;; Hack around filesystem race condition...
1688 (or (probe-file output-file-name) output-file-name)
1693 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1694 ;;; the OUTPUT-FILE argument
1696 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1697 ;;; that results from merging the INPUT-FILE with the value of
1698 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1699 ;;; default to the appropriate implementation-defined default type for
1701 (defun cfp-output-file-default (input-file)
1702 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1703 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1706 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1707 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1708 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1709 ;;; pathname, it is translated into a physical pathname as if by
1710 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1711 ;;; So I haven't really tried to make this precisely ANSI-compatible
1712 ;;; at the level of e.g. whether it returns logical pathname or a
1713 ;;; physical pathname. Patches to make it more correct are welcome.
1714 ;;; -- WHN 2000-12-09
1715 (defun sb!xc:compile-file-pathname (input-file
1717 (output-file nil output-file-p)
1720 "Return a pathname describing what file COMPILE-FILE would write to given
1723 (merge-pathnames output-file (cfp-output-file-default input-file))
1724 (cfp-output-file-default input-file)))
1726 ;;;; MAKE-LOAD-FORM stuff
1728 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1729 ;;; finds a constant structure, it invokes this to arrange for proper
1730 ;;; dumping. If it turns out that the constant has already been
1731 ;;; dumped, then we don't need to do anything.
1733 ;;; If the constant hasn't been dumped, then we check to see whether
1734 ;;; we are in the process of creating it. We detect this by
1735 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1736 ;;; the constants we are in the process of creating. Actually, each
1737 ;;; entry is a list of the constant and any init forms that need to be
1738 ;;; processed on behalf of that constant.
1740 ;;; It's not necessarily an error for this to happen. If we are
1741 ;;; processing the init form for some object that showed up *after*
1742 ;;; the original reference to this constant, then we just need to
1743 ;;; defer the processing of that init form. To detect this, we
1744 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1745 ;;; constants created since the last time we started processing an
1746 ;;; init form. If the constant passed to emit-make-load-form shows up
1747 ;;; in this list, then there is a circular chain through creation
1748 ;;; forms, which is an error.
1750 ;;; If there is some intervening init form, then we blow out of
1751 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1752 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1753 ;;; offending init form can be tacked onto the init forms for the
1754 ;;; circular object.
1756 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1757 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1758 ;;; whether the creation form is the magic value
1759 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1760 ;;; dumper will eventually get its hands on the object and use the
1761 ;;; normal structure dumping noise on it.
1763 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1764 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1765 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1766 ;;; dumper to use that result instead whenever it sees this constant.
1768 ;;; Now we try to compile the init form. We bind
1769 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1770 ;;; form (and any init forms that were added because of circularity
1771 ;;; detection). If this works, great. If not, we add the init forms to
1772 ;;; the init forms for the object that caused the problems and let it
1774 (defvar *constants-being-created* nil)
1775 (defvar *constants-created-since-last-init* nil)
1776 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1777 (defun emit-make-load-form (constant)
1778 (aver (fasl-output-p *compile-object*))
1779 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1780 ;; KLUDGE: This special hack is because I was too lazy
1781 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1782 ;; function of LAYOUT returns nontrivial forms when
1783 ;; building the cross-compiler but :IGNORE-IT when
1784 ;; cross-compiling or running under the target Lisp. --
1786 #+sb-xc-host (typep constant 'layout))
1787 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1789 (when (find constant *constants-created-since-last-init* :test #'eq)
1791 (throw 'pending-init circular-ref)))
1792 (multiple-value-bind (creation-form init-form)
1794 (sb!xc:make-load-form constant (make-null-lexenv))
1796 (compiler-error condition)))
1798 (:sb-just-dump-it-normally
1799 (fasl-validate-structure constant *compile-object*)
1804 (let* ((name (write-to-string constant :level 1 :length 2))
1806 (list constant name init-form)
1808 (let ((*constants-being-created*
1809 (cons info *constants-being-created*))
1810 (*constants-created-since-last-init*
1811 (cons constant *constants-created-since-last-init*)))
1814 (fasl-note-handle-for-constant
1816 (compile-load-time-value
1820 (compiler-error "circular references in creation form for ~S"
1823 (let* ((*constants-created-since-last-init* nil)
1825 (catch 'pending-init
1826 (loop for (name form) on (cdr info) by #'cddr
1827 collect name into names
1828 collect form into forms
1829 finally (compile-make-load-form-init-forms forms))
1832 (setf (cdr circular-ref)
1833 (append (cdr circular-ref) (cdr info))))))))))))
1836 ;;;; Host compile time definitions
1838 (defun compile-in-lexenv (name lambda lexenv)
1839 (declare (ignore lexenv))
1840 (compile name lambda))
1843 (defun eval-in-lexenv (form lexenv)
1844 (declare (ignore lexenv))