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 reference to a thing which cannot be defined causes a full
37 (defvar *flame-on-necessarily-undefined-thing* nil)
39 (defvar *check-consistency* nil)
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 (defvar *fopcompile-label-counter*)
120 ;; Used during compilation to map code paths to the matching
121 ;; instrumentation conses.
122 (defvar *code-coverage-records* nil)
123 ;; Used during compilation to keep track of with source paths have been
124 ;; instrumented in which blocks.
125 (defvar *code-coverage-blocks* nil)
126 ;; Stores the code coverage instrumentation results. Keys are namestrings,
127 ;; the value is a list of (CONS PATH STATE), where STATE is NIL for
128 ;; a path that has not been visited, and T for one that has.
129 (defvar *code-coverage-info* (make-hash-table :test 'equal))
132 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
134 (defmacro sb!xc:with-compilation-unit (options &body body)
136 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
137 This form affects compilations that take place within its dynamic extent. It
138 is intended to be wrapped around the compilation of all files in the same
139 system. These keywords are defined:
141 :OVERRIDE Boolean-Form
142 One of the effects of this form is to delay undefined warnings
143 until the end of the form, instead of giving them at the end of each
144 compilation. If OVERRIDE is NIL (the default), then the outermost
145 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
146 OVERRIDE true causes that form to grab any enclosed warnings, even if
147 it is enclosed by another WITH-COMPILATION-UNIT.
149 :SOURCE-PLIST Plist-Form
150 Attaches the value returned by the Plist-Form to internal debug-source
151 information of functions compiled in within the dynamic contour.
152 Primarily for use by development environments, in order to eg. associate
153 function definitions with editor-buffers. Can be accessed as
154 SB-INTROSPECT:DEFINITION-SOURCE-PLIST. If multiple, nested
155 WITH-COMPILATION-UNITs provide :SOURCE-PLISTs, they are appended
156 togather, innermost left. If Unaffected by :OVERRIDE."
157 `(%with-compilation-unit (lambda () ,@body) ,@options))
159 (defvar *source-plist* nil)
161 (defun %with-compilation-unit (fn &key override source-plist)
162 (declare (type function fn))
163 (let ((succeeded-p nil)
164 (*source-plist* (append source-plist *source-plist*)))
165 (if (and *in-compilation-unit* (not override))
166 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
167 ;; ordinarily (unless OVERRIDE) basically a no-op.
169 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
171 (incf *aborted-compilation-unit-count*)))
172 (let ((*aborted-compilation-unit-count* 0)
173 (*compiler-error-count* 0)
174 (*compiler-warning-count* 0)
175 (*compiler-style-warning-count* 0)
176 (*compiler-note-count* 0)
177 (*undefined-warnings* nil)
178 (*in-compilation-unit* t))
180 (handler-bind ((parse-unknown-type
182 (note-undefined-reference
183 (parse-unknown-type-specifier c)
186 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
188 (incf *aborted-compilation-unit-count*))
189 (summarize-compilation-unit (not succeeded-p)))))))))
191 ;;; Is NAME something that no conforming program can rely on
193 (defun name-reserved-by-ansi-p (name kind)
196 (eq (symbol-package (fun-name-block-name name))
199 (let ((symbol (typecase name
201 ((cons symbol) (car name))
202 (t (return-from name-reserved-by-ansi-p nil)))))
203 (eq (symbol-package symbol) *cl-package*)))))
205 ;;; This is to be called at the end of a compilation unit. It signals
206 ;;; any residual warnings about unknown stuff, then prints the total
207 ;;; error counts. ABORT-P should be true when the compilation unit was
208 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
209 ;;; enclosed nested compilation units that were aborted.
210 (defun summarize-compilation-unit (abort-p)
213 (handler-bind ((style-warning #'compiler-style-warning-handler)
214 (warning #'compiler-warning-handler))
216 (let ((undefs (sort *undefined-warnings* #'string<
218 (let ((x (undefined-warning-name x)))
221 (prin1-to-string x)))))))
222 (dolist (kind '(:variable :function :type))
223 (let ((names (mapcar #'undefined-warning-name
224 (remove kind undefs :test #'neq
225 :key #'undefined-warning-kind))))
226 (when names (push (cons kind names) summary))))
227 (dolist (undef undefs)
228 (let ((name (undefined-warning-name undef))
229 (kind (undefined-warning-kind undef))
230 (warnings (undefined-warning-warnings undef))
231 (undefined-warning-count (undefined-warning-count undef)))
232 (dolist (*compiler-error-context* warnings)
233 (if #-sb-xc-host (and (member kind '(:function :type))
234 (name-reserved-by-ansi-p name kind)
235 *flame-on-necessarily-undefined-thing*)
242 "~@<There is no function named ~S. References to ~S ~
243 in some contexts (like starts of blocks) have ~
244 special meaning, but here it would have to be a ~
245 function, and that shouldn't be right.~:@>" name
249 "~@<The function ~S is undefined, and its name is ~
250 reserved by ANSI CL so that even if it were ~
251 defined later, the code doing so would not be ~
252 portable.~:@>" name))))
254 (if (and (consp name) (eq 'quote (car name)))
256 "~@<Undefined type ~S. The name starts with ~S: ~
257 probably use of a quoted type name in a context ~
258 where the name is not evaluated.~:@>"
261 "~@<Undefined type ~S. Note that name ~S is ~
262 reserved by ANSI CL, so code defining a type with ~
263 that name would not be portable.~:@>" name
265 (if (eq kind :variable)
266 (compiler-warn "undefined ~(~A~): ~S" kind name)
267 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
268 (let ((warn-count (length warnings)))
269 (when (and warnings (> undefined-warning-count warn-count))
270 (let ((more (- undefined-warning-count warn-count)))
271 (if (eq kind :variable)
273 "~W more use~:P of undefined ~(~A~) ~S"
276 "~W more use~:P of undefined ~(~A~) ~S"
277 more kind name))))))))))
279 (unless (and (not abort-p)
280 (zerop *aborted-compilation-unit-count*)
281 (zerop *compiler-error-count*)
282 (zerop *compiler-warning-count*)
283 (zerop *compiler-style-warning-count*)
284 (zerop *compiler-note-count*))
285 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
286 (format *error-output* "~&compilation unit ~:[finished~;aborted~]"
288 (dolist (cell summary)
289 (destructuring-bind (kind &rest names) cell
290 (format *error-output*
291 "~& Undefined ~(~A~)~p:~
292 ~% ~{~<~% ~1:;~S~>~^ ~}"
293 kind (length names) names)))
294 (format *error-output* "~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
295 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
296 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
297 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
298 ~[~:;~:*~& printed ~W note~:P~]"
299 *aborted-compilation-unit-count*
300 *compiler-error-count*
301 *compiler-warning-count*
302 *compiler-style-warning-count*
303 *compiler-note-count*))
304 (terpri *error-output*)
305 (force-output *error-output*))))
307 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
308 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
309 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
310 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
311 (defmacro with-compilation-values (&body body)
313 (let ((*warnings-p* nil)
315 (values (progn ,@body)
319 ;;;; component compilation
321 (defparameter *max-optimize-iterations* 3 ; ARB
323 "The upper limit on the number of times that we will consecutively do IR1
324 optimization that doesn't introduce any new code. A finite limit is
325 necessary, since type inference may take arbitrarily long to converge.")
327 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
328 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
330 ;;; Repeatedly optimize COMPONENT until no further optimizations can
331 ;;; be found or we hit our iteration limit. When we hit the limit, we
332 ;;; clear the component and block REOPTIMIZE flags to discourage the
333 ;;; next optimization attempt from pounding on the same code.
334 (defun ir1-optimize-until-done (component)
335 (declare (type component component))
337 (event ir1-optimize-until-done)
339 (cleared-reanalyze nil)
342 (when (component-reanalyze component)
344 (setq cleared-reanalyze t)
345 (setf (component-reanalyze component) nil))
346 (setf (component-reoptimize component) nil)
347 (ir1-optimize component fastp)
348 (cond ((component-reoptimize component)
350 (when (and (>= count *max-optimize-iterations*)
351 (not (component-reanalyze component))
352 (eq (component-reoptimize component) :maybe))
354 (cond ((retry-delayed-ir1-transforms :optimize)
358 (event ir1-optimize-maxed-out)
359 (setf (component-reoptimize component) nil)
360 (do-blocks (block component)
361 (setf (block-reoptimize block) nil))
363 ((retry-delayed-ir1-transforms :optimize)
369 (setq fastp (>= count *max-optimize-iterations*))
370 (maybe-mumble (if fastp "-" ".")))
371 (when cleared-reanalyze
372 (setf (component-reanalyze component) t)))
375 (defparameter *constraint-propagate* t)
377 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
378 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
379 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
380 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
382 ;;; FIXME: It would be good to document why it's important to have a
383 ;;; large value here, and what the drawbacks of an excessively large
384 ;;; value are; and it might also be good to make it depend on
385 ;;; optimization policy.
386 (defparameter *reoptimize-after-type-check-max* 10)
388 (defevent reoptimize-maxed-out
389 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
391 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
392 (defun dfo-as-needed (component)
393 (declare (type component component))
394 (when (component-reanalyze component)
398 (unless (component-reanalyze component)
404 ;;; Do all the IR1 phases for a non-top-level component.
405 (defun ir1-phases (component)
406 (declare (type component component))
407 (aver-live-component component)
408 (let ((*constraint-universe* (make-array 64 ; arbitrary, but don't
410 :fill-pointer 0 :adjustable t))
412 (*delayed-ir1-transforms* nil))
413 (declare (special *constraint-universe* *delayed-ir1-transforms*))
415 (ir1-optimize-until-done component)
416 (when (or (component-new-functionals component)
417 (component-reanalyze-functionals component))
418 (maybe-mumble "locall ")
419 (locall-analyze-component component))
420 (dfo-as-needed component)
421 (when *constraint-propagate*
422 (maybe-mumble "constraint ")
423 (constraint-propagate component))
424 (when (retry-delayed-ir1-transforms :constraint)
425 (maybe-mumble "Rtran "))
426 (flet ((want-reoptimization-p ()
427 (or (component-reoptimize component)
428 (component-reanalyze component)
429 (component-new-functionals component)
430 (component-reanalyze-functionals component))))
431 (unless (and (want-reoptimization-p)
432 ;; We delay the generation of type checks until
433 ;; the type constraints have had time to
434 ;; propagate, else the compiler can confuse itself.
435 (< loop-count (- *reoptimize-after-type-check-max* 4)))
436 (maybe-mumble "type ")
437 (generate-type-checks component)
438 (unless (want-reoptimization-p)
440 (when (>= loop-count *reoptimize-after-type-check-max*)
441 (maybe-mumble "[reoptimize limit]")
442 (event reoptimize-maxed-out)
446 (ir1-finalize component)
449 (defun %compile-component (component)
450 (let ((*code-segment* nil)
452 (maybe-mumble "GTN ")
453 (gtn-analyze component)
454 (maybe-mumble "LTN ")
455 (ltn-analyze component)
456 (dfo-as-needed component)
457 (maybe-mumble "control ")
458 (control-analyze component #'make-ir2-block)
460 (when (or (ir2-component-values-receivers (component-info component))
461 (component-dx-lvars component))
462 (maybe-mumble "stack ")
463 (stack-analyze component)
464 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
465 ;; stack analysis. There shouldn't be any unreachable code after
466 ;; control, so this won't delete anything.
467 (dfo-as-needed component))
471 (maybe-mumble "IR2tran ")
473 (entry-analyze component)
474 (ir2-convert component)
476 (when (policy *lexenv* (>= speed compilation-speed))
477 (maybe-mumble "copy ")
478 (copy-propagate component))
480 (ir2-optimize component)
482 (select-representations component)
484 (when *check-consistency*
485 (maybe-mumble "check2 ")
486 (check-ir2-consistency component))
488 (delete-unreferenced-tns component)
490 (maybe-mumble "life ")
491 (lifetime-analyze component)
493 (when *compile-progress*
494 (compiler-mumble "") ; Sync before doing more output.
495 (pre-pack-tn-stats component *standard-output*))
497 (when *check-consistency*
498 (maybe-mumble "check-life ")
499 (check-life-consistency component))
501 (maybe-mumble "pack ")
504 (when *check-consistency*
505 (maybe-mumble "check-pack ")
506 (check-pack-consistency component))
508 (when *compiler-trace-output*
509 (describe-component component *compiler-trace-output*)
510 (describe-ir2-component component *compiler-trace-output*))
512 (maybe-mumble "code ")
513 (multiple-value-bind (code-length trace-table fixup-notes)
514 (generate-code component)
517 (when *compiler-trace-output*
518 (format *compiler-trace-output*
519 "~|~%disassembly of code for ~S~2%" component)
520 (sb!disassem:disassemble-assem-segment *code-segment*
521 *compiler-trace-output*))
523 (etypecase *compile-object*
525 (maybe-mumble "fasl")
526 (fasl-dump-component component
533 (maybe-mumble "core")
534 (make-core-component component
542 ;; We're done, so don't bother keeping anything around.
543 (setf (component-info component) :dead)
547 ;;; Delete components with no external entry points before we try to
548 ;;; generate code. Unreachable closures can cause IR2 conversion to
549 ;;; puke on itself, since it is the reference to the closure which
550 ;;; normally causes the components to be combined.
551 (defun delete-if-no-entries (component)
552 (dolist (fun (component-lambdas component) (delete-component component))
553 (when (functional-has-external-references-p fun)
555 (case (functional-kind fun)
558 (unless (every (lambda (ref)
559 (eq (node-component ref) component))
563 (defun compile-component (component)
565 ;; miscellaneous sanity checks
567 ;; FIXME: These are basically pretty wimpy compared to the checks done
568 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
569 ;; make those internal consistency checks work again and use them.
570 (aver-live-component component)
571 (do-blocks (block component)
572 (aver (eql (block-component block) component)))
573 (dolist (lambda (component-lambdas component))
574 ;; sanity check to prevent weirdness from propagating insidiously as
575 ;; far from its root cause as it did in bug 138: Make sure that
576 ;; thing-to-COMPONENT links are consistent.
577 (aver (eql (lambda-component lambda) component))
578 (aver (eql (node-component (lambda-bind lambda)) component)))
580 (let* ((*component-being-compiled* component))
582 ;; Record xref information before optimization. This way the
583 ;; stored xref data reflects the real source as closely as
585 (record-component-xrefs component)
587 (ir1-phases component)
590 (dfo-as-needed component)
591 (find-dominators component)
592 (loop-analyze component))
595 (when (and *loop-analyze* *compiler-trace-output*)
596 (labels ((print-blocks (block)
597 (format *compiler-trace-output* " ~A~%" block)
598 (when (block-loop-next block)
599 (print-blocks (block-loop-next block))))
601 (format *compiler-trace-output* "loop=~A~%" loop)
602 (print-blocks (loop-blocks loop))
603 (dolist (l (loop-inferiors loop))
605 (print-loop (component-outer-loop component))))
608 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
609 (maybe-mumble "env ")
610 (physenv-analyze component)
611 (dfo-as-needed component)
613 (delete-if-no-entries component)
615 (unless (eq (block-next (component-head component))
616 (component-tail component))
617 (%compile-component component)))
619 (clear-constant-info)
623 ;;;; clearing global data structures
625 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
626 ;;;; global data structures entirely when possible and consing up the
627 ;;;; others from scratch instead of clearing and reusing them?
629 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
630 ;;; addition to allowing stuff to be reclaimed, this is required for
631 ;;; correct assignment of constant offsets, since we need to assign a
632 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
633 ;;; slots, since they are used to keep track of functions across
634 ;;; component boundaries.
635 (defun clear-constant-info ()
636 (maphash (lambda (k v)
638 (setf (leaf-info v) nil))
640 (maphash (lambda (k v)
643 (setf (leaf-info v) nil)))
647 ;;; Blow away the REFS for all global variables, and let COMPONENT
649 (defun clear-ir1-info (component)
650 (declare (type component component))
652 (maphash (lambda (k v)
656 (delete-if #'here-p (leaf-refs v)))
657 (when (basic-var-p v)
658 (setf (basic-var-sets v)
659 (delete-if #'here-p (basic-var-sets v))))))
662 (eq (node-component x) component)))
668 ;;; Clear global variables used by the compiler.
670 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
671 ;;; and it adds overhead even when people aren't using the compiler.
672 ;;; Perhaps we could make these global vars unbound except when
673 ;;; actually in use, so that this function could go away.
674 (defun clear-stuff (&optional (debug-too t))
676 ;; Clear global tables.
677 (when (boundp '*free-funs*)
678 (clrhash *free-funs*)
679 (clrhash *free-vars*)
680 (clrhash *constants*))
682 ;; Clear debug counters and tables.
683 (clrhash *seen-blocks*)
684 (clrhash *seen-funs*)
685 (clrhash *list-conflicts-table*)
688 (clrhash *continuation-numbers*)
689 (clrhash *number-continuations*)
690 (setq *continuation-number* 0)
694 (clrhash *label-ids*)
695 (clrhash *id-labels*)
698 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
699 ;; Superficially, this seemed harmful -- the user could reasonably be
700 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
701 ;; compiled. A closer inspection showed that this actually turned out to be
702 ;; harmless in practice, because CLEAR-STUFF was only called from within
703 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
704 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
705 ;; practice, it was also useless in practice. So we don't do it any more.)
711 ;;; Print out some useful info about COMPONENT to STREAM.
712 (defun describe-component (component *standard-output*)
713 (declare (type component component))
714 (format t "~|~%;;;; component: ~S~2%" (component-name component))
715 (print-all-blocks component)
718 (defun describe-ir2-component (component *standard-output*)
719 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
720 (format t "entries:~%")
721 (dolist (entry (ir2-component-entries (component-info component)))
722 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
723 (label-id (entry-info-offset entry))
724 (entry-info-name entry)
725 (entry-info-closure-tn entry)))
727 (pre-pack-tn-stats component *standard-output*)
729 (print-ir2-blocks component)
735 ;;;; When reading from a file, we have to keep track of some source
736 ;;;; information. We also exploit our ability to back up for printing
737 ;;;; the error context and for recovering from errors.
739 ;;;; The interface we provide to this stuff is the stream-oid
740 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
741 ;;;; of getting the next source form.
743 ;;; A FILE-INFO structure holds all the source information for a
745 (def!struct (file-info
747 #-no-ansi-print-object
748 (:print-object (lambda (s stream)
749 (print-unreadable-object (s stream :type t)
750 (princ (file-info-name s) stream)))))
751 ;; If a file, the truename of the corresponding source file. If from
752 ;; a Lisp form, :LISP. If from a stream, :STREAM.
753 (name (missing-arg) :type (or pathname (eql :lisp)))
754 ;; the external format that we'll call OPEN with, if NAME is a file.
755 (external-format nil)
756 ;; the defaulted, but not necessarily absolute file name (i.e. prior
757 ;; to TRUENAME call.) Null if not a file. This is used to set
758 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
760 (untruename nil :type (or pathname null))
761 ;; the file's write date (if relevant)
762 (write-date nil :type (or unsigned-byte null))
763 ;; the source path root number of the first form in this file (i.e.
764 ;; the total number of forms converted previously in this
766 (source-root 0 :type unsigned-byte)
767 ;; parallel vectors containing the forms read out of the file and
768 ;; the file positions that reading of each form started at (i.e. the
769 ;; end of the previous form)
770 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
771 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
773 ;;; The SOURCE-INFO structure provides a handle on all the source
774 ;;; information for an entire compilation.
775 (def!struct (source-info
776 #-no-ansi-print-object
777 (:print-object (lambda (s stream)
778 (print-unreadable-object (s stream :type t))))
780 ;; the UT that compilation started at
781 (start-time (get-internal-real-time) :type unsigned-byte)
782 ;; the FILE-INFO structure for this compilation
783 (file-info nil :type (or file-info null))
784 ;; the stream that we are using to read the FILE-INFO, or NIL if
785 ;; no stream has been opened yet
786 (stream nil :type (or stream null))
787 ;; if the current compilation is recursive (e.g., due to EVAL-WHEN
788 ;; processing at compile-time), the invoking compilation's
790 (parent nil :type (or source-info null)))
792 ;;; Given a pathname, return a SOURCE-INFO structure.
793 (defun make-file-source-info (file external-format)
795 :file-info (make-file-info :name (truename file)
796 :untruename (merge-pathnames file)
797 :external-format external-format
798 :write-date (file-write-date file))))
800 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
801 (defun make-lisp-source-info (form &key parent)
803 :file-info (make-file-info :name :lisp
808 ;;; Walk up the SOURCE-INFO list until we either reach a SOURCE-INFO
809 ;;; with no parent (e.g., from a REPL evaluation) or until we reach a
810 ;;; SOURCE-INFO whose FILE-INFO denotes a file.
811 (defun get-toplevelish-file-info (&optional (source-info *source-info*))
813 (do* ((sinfo source-info (source-info-parent sinfo))
814 (finfo (source-info-file-info sinfo)
815 (source-info-file-info sinfo)))
816 ((or (not (source-info-p (source-info-parent sinfo)))
817 (pathnamep (file-info-name finfo)))
820 ;;; Return a form read from STREAM; or for EOF use the trick,
821 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
822 ;;; error happens, then convert it to standard abort-the-compilation
823 ;;; error condition (possibly recording some extra location
825 (defun read-for-compile-file (stream position)
827 (read-preserving-whitespace stream nil stream)
828 (reader-error (condition)
829 (error 'input-error-in-compile-file
831 ;; We don't need to supply :POSITION here because
832 ;; READER-ERRORs already know their position in the file.
834 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
835 ;; (and that this is not a READER-ERROR) when it encounters end of
836 ;; file in the middle of something it's trying to read.
837 (end-of-file (condition)
838 (error 'input-error-in-compile-file
840 ;; We need to supply :POSITION here because the END-OF-FILE
841 ;; condition doesn't carry the position that the user
842 ;; probably cares about, where the failed READ began.
843 :position position))))
845 ;;; If STREAM is present, return it, otherwise open a stream to the
846 ;;; current file. There must be a current file.
848 ;;; FIXME: This is probably an unnecessarily roundabout way to do
849 ;;; things now that we process a single file in COMPILE-FILE (unlike
850 ;;; the old CMU CL code, which accepted multiple files). Also, the old
852 ;;; When we open a new file, we also reset *PACKAGE* and policy.
853 ;;; This gives the effect of rebinding around each file.
854 ;;; which doesn't seem to be true now. Check to make sure that if
855 ;;; such rebinding is necessary, it's still done somewhere.
856 (defun get-source-stream (info)
857 (declare (type source-info info))
858 (or (source-info-stream info)
859 (let* ((file-info (source-info-file-info info))
860 (name (file-info-name file-info))
861 (external-format (file-info-external-format file-info)))
862 (setf sb!xc:*compile-file-truename* name
863 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
864 (source-info-stream info)
865 (open name :direction :input
866 :external-format external-format)))))
868 ;;; Close the stream in INFO if it is open.
869 (defun close-source-info (info)
870 (declare (type source-info info))
871 (let ((stream (source-info-stream info)))
872 (when stream (close stream)))
873 (setf (source-info-stream info) nil)
876 ;;; Loop over FORMS retrieved from INFO. Used by COMPILE-FILE and
877 ;;; LOAD when loading from a FILE-STREAM associated with a source
879 (defmacro do-forms-from-info (((form &rest keys) info)
881 (aver (symbolp form))
882 (once-only ((info info))
883 `(let ((*source-info* ,info))
884 (loop (destructuring-bind (,form &key ,@keys &allow-other-keys)
885 (let* ((file-info (source-info-file-info ,info))
886 (stream (get-source-stream ,info))
887 (pos (file-position stream))
888 (form (read-for-compile-file stream pos)))
889 (if (eq form stream) ; i.e., if EOF
891 (let* ((forms (file-info-forms file-info))
892 (current-idx (+ (fill-pointer forms)
893 (file-info-source-root
895 (vector-push-extend form forms)
896 (vector-push-extend pos (file-info-positions
898 (list form :current-index current-idx))))
901 ;;; Read and compile the source file.
902 (defun sub-sub-compile-file (info)
903 (do-forms-from-info ((form current-index) info)
904 (find-source-paths form current-index)
905 (process-toplevel-form
906 form `(original-source-start 0 ,current-index) nil)))
908 ;;; Return the INDEX'th source form read from INFO and the position
909 ;;; where it was read.
910 (defun find-source-root (index info)
911 (declare (type index index) (type source-info info))
912 (let ((file-info (source-info-file-info info)))
913 (values (aref (file-info-forms file-info) index)
914 (aref (file-info-positions file-info) index))))
916 ;;;; processing of top level forms
918 ;;; This is called by top level form processing when we are ready to
919 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
920 ;;; convert the form, but delay compilation, pushing the result on
921 ;;; *TOPLEVEL-LAMBDAS* instead.
922 (defun convert-and-maybe-compile (form path)
923 (declare (list path))
924 (let ((*top-level-form-noted* (note-top-level-form form t)))
925 ;; Don't bother to compile simple objects that just sit there.
926 (when (and form (or (symbolp form) (consp form)))
927 (if (fopcompilable-p form)
928 (let ((*fopcompile-label-counter* 0))
929 (fopcompile form path nil))
930 (let ((*lexenv* (make-lexenv
932 :handled-conditions *handled-conditions*
933 :disabled-package-locks *disabled-package-locks*))
934 (tll (ir1-toplevel form path nil)))
935 (if (eq *block-compile* t)
936 (push tll *toplevel-lambdas*)
937 (compile-toplevel (list tll) nil))
940 ;;; Macroexpand FORM in the current environment with an error handler.
941 ;;; We only expand one level, so that we retain all the intervening
942 ;;; forms in the source path.
943 (defun preprocessor-macroexpand-1 (form)
944 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
946 (compiler-error "(during macroexpansion of ~A)~%~A"
947 (let ((*print-level* 2)
949 (format nil "~S" form))
952 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
953 ;;; the forms, and PATH is the source path of the FORM they came out of.
954 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
955 (defun process-toplevel-progn (forms path compile-time-too)
956 (declare (list forms) (list path))
958 (process-toplevel-form form path compile-time-too)))
960 ;;; Process a top level use of LOCALLY, or anything else (e.g.
961 ;;; MACROLET) at top level which has declarations and ordinary forms.
962 ;;; We parse declarations and then recursively process the body.
963 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
964 (declare (list path))
965 (multiple-value-bind (forms decls)
966 (parse-body body :doc-string-allowed nil :toplevel t)
967 (let* ((*lexenv* (process-decls decls vars funs))
968 ;; FIXME: VALUES declaration
970 ;; Binding *POLICY* is pretty much of a hack, since it
971 ;; causes LOCALLY to "capture" enclosed proclamations. It
972 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
973 ;; value of *POLICY* as the policy. The need for this hack
974 ;; is due to the quirk that there is no way to represent in
975 ;; a POLICY that an optimize quality came from the default.
977 ;; FIXME: Ideally, something should be done so that DECLAIM
978 ;; inside LOCALLY works OK. Failing that, at least we could
979 ;; issue a warning instead of silently screwing up.
980 (*policy* (lexenv-policy *lexenv*))
981 ;; This is probably also a hack
982 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
984 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*)))
985 (process-toplevel-progn forms path compile-time-too))))
987 ;;; Parse an EVAL-WHEN situations list, returning three flags,
988 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
989 ;;; the types of situations present in the list.
990 (defun parse-eval-when-situations (situations)
991 (when (or (not (listp situations))
992 (set-difference situations
999 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
1000 (let ((deprecated-names (intersection situations '(compile load eval))))
1001 (when deprecated-names
1002 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
1004 (values (intersection '(:compile-toplevel compile)
1006 (intersection '(:load-toplevel load) situations)
1007 (intersection '(:execute eval) situations)))
1010 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
1011 (defun functional-components (f)
1012 (declare (type functional f))
1014 (clambda (list (lambda-component f)))
1015 (optional-dispatch (let ((result nil))
1016 (flet ((maybe-frob (maybe-clambda)
1017 (when (and maybe-clambda
1018 (promise-ready-p maybe-clambda))
1019 (pushnew (lambda-component
1020 (force maybe-clambda))
1022 (map nil #'maybe-frob (optional-dispatch-entry-points f))
1023 (maybe-frob (optional-dispatch-more-entry f))
1024 (maybe-frob (optional-dispatch-main-entry f)))
1027 (defun make-functional-from-toplevel-lambda (lambda-expression
1031 ;; I'd thought NIL should
1032 ;; work, but it doesn't.
1033 ;; -- WHN 2001-09-20
1035 (let* ((*current-path* path)
1036 (component (make-empty-component))
1037 (*current-component* component)
1038 (debug-name-tail (or name (name-lambdalike lambda-expression)))
1039 (source-name (or name '.anonymous.)))
1040 (setf (component-name component) (debug-name 'initial-component debug-name-tail)
1041 (component-kind component) :initial)
1042 (let* ((locall-fun (let ((*allow-instrumenting* t))
1043 (funcall #'ir1-convert-lambdalike
1045 :source-name source-name)))
1046 ;; Convert the XEP using the policy of the real
1047 ;; function. Otherwise the wrong policy will be used for
1048 ;; deciding whether to type-check the parameters of the
1049 ;; real function (via CONVERT-CALL / PROPAGATE-TO-ARGS).
1050 ;; -- JES, 2007-02-27
1051 (*lexenv* (make-lexenv :policy (lexenv-policy
1052 (functional-lexenv locall-fun))))
1053 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
1054 :source-name source-name
1055 :debug-name (debug-name 'tl-xep debug-name-tail)
1058 (assert-global-function-definition-type name locall-fun))
1059 (setf (functional-entry-fun fun) locall-fun
1060 (functional-kind fun) :external
1061 (functional-has-external-references-p locall-fun) t
1062 (functional-has-external-references-p fun) t)
1065 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
1066 ;;; description of the result.
1067 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
1068 ;;; into core and return the compiled FUNCTION value.
1069 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
1070 ;;; into the fasl file and return a dump handle.
1072 ;;; If NAME is provided, then we try to use it as the name of the
1073 ;;; function for debugging/diagnostic information.
1074 (defun %compile (lambda-expression
1079 ;; This magical idiom seems to be the appropriate
1080 ;; path for compiling standalone LAMBDAs, judging
1081 ;; from the CMU CL code and experiment, so it's a
1082 ;; nice default for things where we don't have a
1083 ;; real source path (as in e.g. inside CL:COMPILE).
1084 '(original-source-start 0 0)))
1086 (legal-fun-name-or-type-error name))
1087 (let* ((*lexenv* (make-lexenv
1089 :handled-conditions *handled-conditions*
1090 :disabled-package-locks *disabled-package-locks*))
1091 (*compiler-sset-counter* 0)
1092 (fun (make-functional-from-toplevel-lambda lambda-expression
1096 ;; FIXME: The compile-it code from here on is sort of a
1097 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1098 ;; better to find a way to share the code there; or
1099 ;; alternatively, to use this code to replace the code there.
1100 ;; (The second alternative might be pretty easy if we used
1101 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1102 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1104 (locall-analyze-clambdas-until-done (list fun))
1106 (let ((components-from-dfo (find-initial-dfo (list fun))))
1107 (dolist (component-from-dfo components-from-dfo)
1108 (compile-component component-from-dfo)
1109 (replace-toplevel-xeps component-from-dfo))
1111 (let ((entry-table (etypecase *compile-object*
1112 (fasl-output (fasl-output-entry-table
1114 (core-object (core-object-entry-table
1115 *compile-object*)))))
1116 (multiple-value-bind (result found-p)
1117 (gethash (leaf-info fun) entry-table)
1121 ;; KLUDGE: This code duplicates some other code in this
1122 ;; file. In the great reorganzation, the flow of program
1123 ;; logic changed from the original CMUCL model, and that
1124 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1125 ;; longer followed for CORE-OBJECTS, leading to BUG
1126 ;; 156. This place is transparently not the right one for
1127 ;; this code, but I don't have a clear enough overview of
1128 ;; the compiler to know how to rearrange it all so that
1129 ;; this operation fits in nicely, and it was blocking
1130 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1131 ;; ((..)) (DEFUN FOO ...))
1133 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1134 ;; ideal way, as (1) definitions typed in at the REPL
1135 ;; without an INLINE declaration will give a NULL
1136 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1137 ;; and (2) INLINE declarations will yield a
1138 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1139 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1142 ;; (2) is probably fairly easy to fix -- it is, after all,
1143 ;; a matter of list manipulation (or possibly of teaching
1144 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1145 ;; significantly harder, as the association between
1146 ;; function object and source is a tricky one.
1148 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1149 ;; non-NULL list) when the function in question has been
1150 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1151 ;; work when it has been compiled as part of the top-level
1152 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1153 ;; ...). -- CSR, 2002-11-02
1154 (when (core-object-p *compile-object*)
1155 (fix-core-source-info *source-info* *compile-object* result))
1157 (mapc #'clear-ir1-info components-from-dfo)
1160 (defun process-toplevel-cold-fset (name lambda-expression path)
1161 (unless (producing-fasl-file)
1162 (error "can't COLD-FSET except in a fasl file"))
1163 (legal-fun-name-or-type-error name)
1164 (fasl-dump-cold-fset name
1165 (%compile lambda-expression
1172 (defun note-top-level-form (form &optional finalp)
1173 (when *compile-print*
1174 (cond ((not *top-level-form-noted*)
1175 (let ((*print-length* 2)
1177 (*print-pretty* nil))
1178 (with-compiler-io-syntax
1179 (compiler-mumble "~&; ~:[compiling~;converting~] ~S"
1180 *block-compile* form)))
1183 (eq :top-level-forms *compile-print*)
1184 (neq form *top-level-form-noted*))
1185 (let ((*print-length* 1)
1187 (*print-pretty* nil))
1188 (with-compiler-io-syntax
1189 (compiler-mumble "~&; ... top level ~S" form)))
1192 *top-level-form-noted*))))
1194 ;;; Process a top level FORM with the specified source PATH.
1195 ;;; * If this is a magic top level form, then do stuff.
1196 ;;; * If this is a macro, then expand it.
1197 ;;; * Otherwise, just compile it.
1199 ;;; COMPILE-TIME-TOO is as defined in ANSI
1200 ;;; "3.2.3.1 Processing of Top Level Forms".
1201 (defun process-toplevel-form (form path compile-time-too)
1202 (declare (list path))
1204 (catch 'process-toplevel-form-error-abort
1205 (let* ((path (or (get-source-path form) (cons form path)))
1206 (*current-path* path)
1207 (*compiler-error-bailout*
1208 (lambda (&optional condition)
1209 (convert-and-maybe-compile
1210 (make-compiler-error-form condition form)
1212 (throw 'process-toplevel-form-error-abort nil))))
1214 (flet ((default-processor (form)
1215 (let ((*top-level-form-noted* (note-top-level-form form)))
1216 ;; When we're cross-compiling, consider: what should we
1217 ;; do when we hit e.g.
1218 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1219 ;; (DEFUN FOO (X) (+ 7 X)))?
1220 ;; DEFUN has a macro definition in the cross-compiler,
1221 ;; and a different macro definition in the target
1222 ;; compiler. The only sensible thing is to use the
1223 ;; target compiler's macro definition, since the
1224 ;; cross-compiler's macro is in general into target
1225 ;; functions which can't meaningfully be executed at
1226 ;; cross-compilation time. So make sure we do the EVAL
1227 ;; here, before we macroexpand.
1229 ;; Then things get even dicier with something like
1230 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1231 ;; where we have to make sure that we don't uncross
1232 ;; the SB!XC: prefix before we do EVAL, because otherwise
1233 ;; we'd be trying to redefine the cross-compilation host's
1236 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1239 (when compile-time-too
1240 (eval form)) ; letting xc host EVAL do its own macroexpansion
1241 (let* (;; (We uncross the operator name because things
1242 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1243 ;; should be equivalent to their CL: counterparts
1244 ;; when being compiled as target code. We leave
1245 ;; the rest of the form uncrossed because macros
1246 ;; might yet expand into EVAL-WHEN stuff, and
1247 ;; things inside EVAL-WHEN can't be uncrossed
1248 ;; until after we've EVALed them in the
1249 ;; cross-compilation host.)
1250 (slightly-uncrossed (cons (uncross (first form))
1252 (expanded (preprocessor-macroexpand-1
1253 slightly-uncrossed)))
1254 (if (eq expanded slightly-uncrossed)
1255 ;; (Now that we're no longer processing toplevel
1256 ;; forms, and hence no longer need to worry about
1257 ;; EVAL-WHEN, we can uncross everything.)
1258 (convert-and-maybe-compile expanded path)
1259 ;; (We have to demote COMPILE-TIME-TOO to NIL
1260 ;; here, no matter what it was before, since
1261 ;; otherwise we'd tend to EVAL subforms more than
1262 ;; once, because of WHEN COMPILE-TIME-TOO form
1264 (process-toplevel-form expanded path nil))))
1265 ;; When we're not cross-compiling, we only need to
1266 ;; macroexpand once, so we can follow the 1-thru-6
1267 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1268 ;; Top Level Forms".
1270 (let ((expanded (preprocessor-macroexpand-1 form)))
1271 (cond ((eq expanded form)
1272 (when compile-time-too
1273 (eval-in-lexenv form *lexenv*))
1274 (convert-and-maybe-compile form path))
1276 (process-toplevel-form expanded
1278 compile-time-too)))))))
1281 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1282 ;; (1) SBCL gets smart enough to handle global
1283 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1284 ;; implementors start using symbol macros in a way which
1285 ;; interacts with SB-XC/CL distinction.)
1286 (convert-and-maybe-compile form path)
1288 (default-processor form)
1289 (flet ((need-at-least-one-arg (form)
1291 (compiler-error "~S form is too short: ~S"
1295 ;; In the cross-compiler, top level COLD-FSET arranges
1296 ;; for static linking at cold init time.
1299 (aver (not compile-time-too))
1300 (destructuring-bind (cold-fset fun-name lambda-expression) form
1301 (declare (ignore cold-fset))
1302 (process-toplevel-cold-fset fun-name
1305 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1306 (need-at-least-one-arg form)
1307 (destructuring-bind (special-operator magic &rest body) form
1308 (ecase special-operator
1310 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1311 ;; "3.2.3.1 Processing of Top Level Forms".
1312 (multiple-value-bind (ct lt e)
1313 (parse-eval-when-situations magic)
1314 (let ((new-compile-time-too (or ct
1315 (and compile-time-too
1317 (cond (lt (process-toplevel-progn
1318 body path new-compile-time-too))
1319 (new-compile-time-too (eval-in-lexenv
1323 (funcall-in-macrolet-lexenv
1325 (lambda (&key funs prepend)
1326 (declare (ignore funs))
1327 (aver (null prepend))
1328 (process-toplevel-locally body
1333 (funcall-in-symbol-macrolet-lexenv
1335 (lambda (&key vars prepend)
1336 (aver (null prepend))
1337 (process-toplevel-locally body
1343 (process-toplevel-locally (rest form) path compile-time-too))
1345 (process-toplevel-progn (rest form) path compile-time-too))
1346 (t (default-processor form))))))))
1350 ;;;; load time value support
1352 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1354 ;;; Return T if we are currently producing a fasl file and hence
1355 ;;; constants need to be dumped carefully.
1356 (defun producing-fasl-file ()
1357 (fasl-output-p *compile-object*))
1359 ;;; Compile FORM and arrange for it to be called at load-time. Return
1360 ;;; the dumper handle and our best guess at the type of the object.
1361 (defun compile-load-time-value (form)
1362 (let ((lambda (compile-load-time-stuff form t)))
1364 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1365 (let ((type (leaf-type lambda)))
1366 (if (fun-type-p type)
1367 (single-value-type (fun-type-returns type))
1370 ;;; Compile the FORMS and arrange for them to be called (for effect,
1371 ;;; not value) at load time.
1372 (defun compile-make-load-form-init-forms (forms)
1373 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1374 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1376 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1377 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1378 (defun compile-load-time-stuff (form for-value)
1380 (let* ((*lexenv* (make-null-lexenv))
1381 (lambda (ir1-toplevel form *current-path* for-value nil)))
1382 (compile-toplevel (list lambda) t)
1385 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1386 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1387 ;;; don't try to combine this component with anything else and frob
1388 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1389 ;;; compiling, because it was merged with a run-time component.
1390 (defun compile-load-time-value-lambda (lambdas)
1391 (aver (null (cdr lambdas)))
1392 (let* ((lambda (car lambdas))
1393 (component (lambda-component lambda)))
1394 (when (eql (component-kind component) :toplevel)
1395 (setf (component-name component) (leaf-debug-name lambda))
1396 (compile-component component)
1397 (clear-ir1-info component))))
1401 (defun object-call-toplevel-lambda (tll)
1402 (declare (type functional tll))
1403 (let ((object *compile-object*))
1405 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1406 (core-object (core-call-toplevel-lambda tll object))
1409 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1410 ;;; the resulting function to be called.
1411 (defun sub-compile-toplevel-lambdas (lambdas)
1412 (declare (list lambdas))
1414 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1415 (compile-component component)
1416 (clear-ir1-info component)
1417 (object-call-toplevel-lambda tll)))
1420 ;;; Compile top level code and call the top level lambdas. We pick off
1421 ;;; top level lambdas in non-top-level components here, calling
1422 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1423 (defun compile-toplevel-lambdas (lambdas)
1424 (declare (list lambdas))
1425 (let ((len (length lambdas)))
1426 (flet ((loser (start)
1427 (or (position-if (lambda (x)
1428 (not (eq (component-kind
1429 (node-component (lambda-bind x)))
1432 ;; this used to read ":start start", but
1433 ;; start can be greater than len, which
1434 ;; is an error according to ANSI - CSR,
1436 :start (min start len))
1438 (do* ((start 0 (1+ loser))
1439 (loser (loser start) (loser start)))
1441 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1442 (unless (= loser len)
1443 (object-call-toplevel-lambda (elt lambdas loser))))))
1446 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1449 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1450 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1451 (defun compile-toplevel (lambdas load-time-value-p)
1452 (declare (list lambdas))
1454 (maybe-mumble "locall ")
1455 (locall-analyze-clambdas-until-done lambdas)
1457 (maybe-mumble "IDFO ")
1458 (multiple-value-bind (components top-components hairy-top)
1459 (find-initial-dfo lambdas)
1460 (let ((all-components (append components top-components)))
1461 (when *check-consistency*
1462 (maybe-mumble "[check]~%")
1463 (check-ir1-consistency all-components))
1465 (dolist (component (append hairy-top top-components))
1466 (pre-physenv-analyze-toplevel component))
1468 (dolist (component components)
1469 (compile-component component)
1470 (replace-toplevel-xeps component))
1472 (when *check-consistency*
1473 (maybe-mumble "[check]~%")
1474 (check-ir1-consistency all-components))
1476 (if load-time-value-p
1477 (compile-load-time-value-lambda lambdas)
1478 (compile-toplevel-lambdas lambdas))
1480 (mapc #'clear-ir1-info components)
1484 ;;; Actually compile any stuff that has been queued up for block
1486 (defun finish-block-compilation ()
1487 (when *block-compile*
1488 (when *compile-print*
1489 (compiler-mumble "~&; block compiling converted top level forms..."))
1490 (when *toplevel-lambdas*
1491 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1492 (setq *toplevel-lambdas* ()))
1493 (setq *block-compile* nil)
1494 (setq *entry-points* nil)))
1496 (defun handle-condition-p (condition)
1498 (etypecase *compiler-error-context*
1500 (node-lexenv *compiler-error-context*))
1501 (compiler-error-context
1502 (let ((lexenv (compiler-error-context-lexenv
1503 *compiler-error-context*)))
1507 (let ((muffles (lexenv-handled-conditions lexenv)))
1508 (if (null muffles) ; common case
1510 (dolist (muffle muffles nil)
1511 (destructuring-bind (typespec . restart-name) muffle
1512 (when (and (typep condition typespec)
1513 (find-restart restart-name condition))
1516 (defun handle-condition-handler (condition)
1518 (etypecase *compiler-error-context*
1520 (node-lexenv *compiler-error-context*))
1521 (compiler-error-context
1522 (let ((lexenv (compiler-error-context-lexenv
1523 *compiler-error-context*)))
1527 (let ((muffles (lexenv-handled-conditions lexenv)))
1529 (dolist (muffle muffles (bug "fell through"))
1530 (destructuring-bind (typespec . restart-name) muffle
1531 (when (typep condition typespec)
1532 (awhen (find-restart restart-name condition)
1533 (invoke-restart it))))))))
1535 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1536 ;;; Return (VALUES ABORT-P WARNINGS-P FAILURE-P).
1537 (defun sub-compile-file (info)
1538 (declare (type source-info info))
1539 (let ((*package* (sane-package))
1540 (*readtable* *readtable*)
1541 (sb!xc:*compile-file-pathname* nil) ; really bound in
1542 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1544 (*code-coverage-records* (make-hash-table :test 'equal))
1545 (*code-coverage-blocks* (make-hash-table :test 'equal))
1546 (*handled-conditions* *handled-conditions*)
1547 (*disabled-package-locks* *disabled-package-locks*)
1548 (*lexenv* (make-null-lexenv))
1549 (*block-compile* *block-compile-arg*)
1550 (*toplevel-lambdas* ())
1551 (*fun-names-in-this-file* ())
1552 (*allow-instrumenting* nil)
1553 (*compiler-error-bailout*
1555 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1556 (return-from sub-compile-file (values t t t))))
1557 (*current-path* nil)
1558 (*last-source-context* nil)
1559 (*last-original-source* nil)
1560 (*last-source-form* nil)
1561 (*last-format-string* nil)
1562 (*last-format-args* nil)
1563 (*last-message-count* 0)
1564 ;; FIXME: Do we need this rebinding here? It's a literal
1565 ;; translation of the old CMU CL rebinding to
1566 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1567 ;; and it's not obvious whether the rebinding to itself is
1568 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1569 (*info-environment* *info-environment*)
1570 (*compiler-sset-counter* 0)
1571 (sb!xc:*gensym-counter* 0))
1573 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1574 (with-compilation-values
1575 (sb!xc:with-compilation-unit ()
1578 (sub-sub-compile-file info)
1580 (unless (zerop (hash-table-count *code-coverage-records*))
1581 ;; Dump the code coverage records into the fasl.
1582 (fopcompile `(record-code-coverage
1583 ',(namestring *compile-file-pathname*)
1585 (maphash (lambda (k v)
1586 (declare (ignore k))
1588 *code-coverage-records*)
1593 (finish-block-compilation)
1594 (let ((object *compile-object*))
1596 (fasl-output (fasl-dump-source-info info object))
1597 (core-object (fix-core-source-info info object))
1600 ;; Some errors are sufficiently bewildering that we just fail
1601 ;; immediately, without trying to recover and compile more of
1603 (fatal-compiler-error (condition)
1605 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
1606 (format *error-output*
1607 "~@<compilation aborted because of fatal error: ~2I~_~A~:>"
1609 (finish-output *error-output*)
1612 ;;; Return a pathname for the named file. The file must exist.
1613 (defun verify-source-file (pathname-designator)
1614 (let* ((pathname (pathname pathname-designator))
1615 (default-host (make-pathname :host (pathname-host pathname))))
1616 (flet ((try-with-type (path type error-p)
1617 (let ((new (merge-pathnames
1618 path (make-pathname :type type
1619 :defaults default-host))))
1620 (if (probe-file new)
1622 (and error-p (truename new))))))
1623 (cond ((typep pathname 'logical-pathname)
1624 (try-with-type pathname "LISP" t))
1625 ((probe-file pathname) pathname)
1626 ((try-with-type pathname "lisp" nil))
1627 ((try-with-type pathname "lisp" t))))))
1629 (defun elapsed-time-to-string (internal-time-delta)
1630 (multiple-value-bind (tsec remainder)
1631 (truncate internal-time-delta internal-time-units-per-second)
1632 (let ((ms (truncate remainder (/ internal-time-units-per-second 1000))))
1633 (multiple-value-bind (tmin sec) (truncate tsec 60)
1634 (multiple-value-bind (thr min) (truncate tmin 60)
1635 (format nil "~D:~2,'0D:~2,'0D.~3,'0D" thr min sec ms))))))
1637 ;;; Print some junk at the beginning and end of compilation.
1638 (defun print-compile-start-note (source-info)
1639 (declare (type source-info source-info))
1640 (let ((file-info (source-info-file-info source-info)))
1641 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1642 (namestring (file-info-name file-info))
1643 (sb!int:format-universal-time nil
1644 (file-info-write-date
1648 :print-timezone nil)))
1651 (defun print-compile-end-note (source-info won)
1652 (declare (type source-info source-info))
1653 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1655 (elapsed-time-to-string
1656 (- (get-internal-real-time)
1657 (source-info-start-time source-info))))
1660 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1661 ;;; out of the compile, then abort the writing of the output file, so
1662 ;;; that we don't overwrite it with known garbage.
1663 (defun sb!xc:compile-file
1668 (output-file (cfp-output-file-default input-file))
1669 ;; FIXME: ANSI doesn't seem to say anything about
1670 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1672 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1673 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1674 (external-format :default)
1678 ((:block-compile *block-compile-arg*) nil))
1680 "Compile INPUT-FILE, producing a corresponding fasl file and
1681 returning its filename.
1684 If true, a message per non-macroexpanded top level form is printed
1685 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1686 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1687 no such message, but their subforms do.
1689 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1690 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1691 For example, compiling an IN-PACKAGE form will result in a message about
1692 a top level SETQ in addition to the message about the IN-PACKAGE form'
1695 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1698 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1699 argument, it is not currently supported. (non-standard)
1702 If given, internal data structures are dumped to the specified
1703 file, or if a value of T is given, to a file of *.trace type
1704 derived from the input file name. (non-standard)"
1705 ;;; Block compilation is currently broken.
1707 "Also, as a workaround for vaguely-non-ANSI behavior, the
1708 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1709 multiple functions are compiled together as a unit, resolving function
1710 references at compile time. NIL means that global function names are
1711 never resolved at compilation time. Currently NIL is the default
1712 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1713 of the ANSI spec allows this behavior under all circumstances, the
1714 compiler's runtime scales badly when it tries to do this for large
1715 files. If/when this performance problem is fixed, the block
1716 compilation default behavior will probably be made dependent on the
1717 SPEED and COMPILATION-SPEED optimization values, and the
1718 :BLOCK-COMPILE argument will probably become deprecated."
1720 (let* ((fasl-output nil)
1721 (output-file-name nil)
1724 (failure-p t) ; T in case error keeps this from being set later
1725 (input-pathname (verify-source-file input-file))
1726 (source-info (make-file-source-info input-pathname external-format))
1727 (*compiler-trace-output* nil)) ; might be modified below
1732 (setq output-file-name
1733 (sb!xc:compile-file-pathname input-file
1734 :output-file output-file))
1736 (open-fasl-output output-file-name
1737 (namestring input-pathname))))
1739 (let* ((default-trace-file-pathname
1740 (make-pathname :type "trace" :defaults input-pathname))
1741 (trace-file-pathname
1742 (if (eql trace-file t)
1743 default-trace-file-pathname
1744 (merge-pathnames trace-file
1745 default-trace-file-pathname))))
1746 (setf *compiler-trace-output*
1747 (open trace-file-pathname
1748 :if-exists :supersede
1749 :direction :output))))
1751 (when sb!xc:*compile-verbose*
1752 (print-compile-start-note source-info))
1754 (let ((*compile-object* fasl-output))
1755 (setf (values abort-p warnings-p failure-p)
1756 (sub-compile-file source-info))))
1758 (close-source-info source-info)
1761 (close-fasl-output fasl-output abort-p)
1762 (setq output-file-name
1763 (pathname (fasl-output-stream fasl-output)))
1764 (when (and (not abort-p) sb!xc:*compile-verbose*)
1765 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1767 (when sb!xc:*compile-verbose*
1768 (print-compile-end-note source-info (not abort-p)))
1770 (when *compiler-trace-output*
1771 (close *compiler-trace-output*)))
1773 ;; CLHS says that the first value is NIL if the "file could not
1774 ;; be created". We interpret this to mean "a valid fasl could not
1775 ;; be created" -- which can happen if the compilation is aborted
1776 ;; before the whole file has been processed, due to eg. a reader
1778 (values (when (and (not abort-p) output-file)
1779 ;; Hack around filesystem race condition...
1780 (or (probe-file output-file-name) output-file-name))
1784 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1785 ;;; the OUTPUT-FILE argument
1787 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1788 ;;; that results from merging the INPUT-FILE with the value of
1789 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1790 ;;; default to the appropriate implementation-defined default type for
1792 (defun cfp-output-file-default (input-file)
1793 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1794 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1797 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1798 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1799 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1800 ;;; pathname, it is translated into a physical pathname as if by
1801 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1802 ;;; So I haven't really tried to make this precisely ANSI-compatible
1803 ;;; at the level of e.g. whether it returns logical pathname or a
1804 ;;; physical pathname. Patches to make it more correct are welcome.
1805 ;;; -- WHN 2000-12-09
1806 (defun sb!xc:compile-file-pathname (input-file
1808 (output-file nil output-file-p)
1811 "Return a pathname describing what file COMPILE-FILE would write to given
1814 (merge-pathnames output-file (cfp-output-file-default input-file))
1815 (cfp-output-file-default input-file)))
1817 ;;;; MAKE-LOAD-FORM stuff
1819 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1820 ;;; finds a constant structure, it invokes this to arrange for proper
1821 ;;; dumping. If it turns out that the constant has already been
1822 ;;; dumped, then we don't need to do anything.
1824 ;;; If the constant hasn't been dumped, then we check to see whether
1825 ;;; we are in the process of creating it. We detect this by
1826 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1827 ;;; the constants we are in the process of creating. Actually, each
1828 ;;; entry is a list of the constant and any init forms that need to be
1829 ;;; processed on behalf of that constant.
1831 ;;; It's not necessarily an error for this to happen. If we are
1832 ;;; processing the init form for some object that showed up *after*
1833 ;;; the original reference to this constant, then we just need to
1834 ;;; defer the processing of that init form. To detect this, we
1835 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1836 ;;; constants created since the last time we started processing an
1837 ;;; init form. If the constant passed to emit-make-load-form shows up
1838 ;;; in this list, then there is a circular chain through creation
1839 ;;; forms, which is an error.
1841 ;;; If there is some intervening init form, then we blow out of
1842 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1843 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1844 ;;; offending init form can be tacked onto the init forms for the
1845 ;;; circular object.
1847 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1848 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1849 ;;; whether the creation form is the magic value
1850 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1851 ;;; dumper will eventually get its hands on the object and use the
1852 ;;; normal structure dumping noise on it.
1854 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1855 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1856 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1857 ;;; dumper to use that result instead whenever it sees this constant.
1859 ;;; Now we try to compile the init form. We bind
1860 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1861 ;;; form (and any init forms that were added because of circularity
1862 ;;; detection). If this works, great. If not, we add the init forms to
1863 ;;; the init forms for the object that caused the problems and let it
1865 (defvar *constants-being-created* nil)
1866 (defvar *constants-created-since-last-init* nil)
1867 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1868 (defun emit-make-load-form (constant &optional (name nil namep))
1869 (aver (fasl-output-p *compile-object*))
1870 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1871 ;; KLUDGE: This special hack is because I was too lazy
1872 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1873 ;; function of LAYOUT returns nontrivial forms when
1874 ;; building the cross-compiler but :IGNORE-IT when
1875 ;; cross-compiling or running under the target Lisp. --
1877 #+sb-xc-host (typep constant 'layout))
1878 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1880 (when (find constant *constants-created-since-last-init* :test #'eq)
1882 (throw 'pending-init circular-ref)))
1883 (multiple-value-bind (creation-form init-form)
1885 ;; If the constant is a reference to a named constant, we can
1886 ;; just use SYMBOL-VALUE during LOAD.
1887 (values `(symbol-value ',name) nil)
1889 (sb!xc:make-load-form constant (make-null-lexenv))
1891 (compiler-error condition))))
1893 (:sb-just-dump-it-normally
1894 (fasl-validate-structure constant *compile-object*)
1899 (let* ((name (write-to-string constant :level 1 :length 2))
1901 (list constant name init-form)
1903 (let ((*constants-being-created*
1904 (cons info *constants-being-created*))
1905 (*constants-created-since-last-init*
1906 (cons constant *constants-created-since-last-init*)))
1909 (fasl-note-handle-for-constant
1911 (compile-load-time-value
1915 (compiler-error "circular references in creation form for ~S"
1918 (let* ((*constants-created-since-last-init* nil)
1920 (catch 'pending-init
1921 (loop for (name form) on (cdr info) by #'cddr
1922 collect name into names
1923 collect form into forms
1924 finally (compile-make-load-form-init-forms forms))
1927 (setf (cdr circular-ref)
1928 (append (cdr circular-ref) (cdr info))))))))))))
1931 ;;;; Host compile time definitions
1933 (defun compile-in-lexenv (name lambda lexenv)
1934 (declare (ignore lexenv))
1935 (compile name lambda))
1938 (defun eval-in-lexenv (form lexenv)
1939 (declare (ignore lexenv))