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)
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 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
122 (defmacro sb!xc:with-compilation-unit (options &body body)
124 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
125 This form affects compilations that take place within its dynamic extent. It
126 is intended to be wrapped around the compilation of all files in the same
127 system. These keywords are defined:
129 :OVERRIDE Boolean-Form
130 One of the effects of this form is to delay undefined warnings
131 until the end of the form, instead of giving them at the end of each
132 compilation. If OVERRIDE is NIL (the default), then the outermost
133 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
134 OVERRIDE true causes that form to grab any enclosed warnings, even if
135 it is enclosed by another WITH-COMPILATION-UNIT.
137 :SOURCE-PLIST Plist-Form
138 Attaches the value returned by the Plist-Form to internal debug-source
139 information of functions compiled in within the dynamic contour.
140 Primarily for use by development environments, in order to eg. associate
141 function definitions with editor-buffers. Can be accessed as
142 SB-INTROSPECT:DEFINITION-SOURCE-PLIST. If multiple, nested
143 WITH-COMPILATION-UNITs provide :SOURCE-PLISTs, they are appended
144 togather, innermost left. If Unaffected by :OVERRIDE."
145 `(%with-compilation-unit (lambda () ,@body) ,@options))
147 (defvar *source-plist* nil)
149 (defun %with-compilation-unit (fn &key override source-plist)
150 (declare (type function fn))
151 (let ((succeeded-p nil)
152 (*source-plist* (append source-plist *source-plist*)))
153 (if (and *in-compilation-unit* (not override))
154 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
155 ;; ordinarily (unless OVERRIDE) basically a no-op.
157 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
159 (incf *aborted-compilation-unit-count*)))
160 (let ((*aborted-compilation-unit-count* 0)
161 (*compiler-error-count* 0)
162 (*compiler-warning-count* 0)
163 (*compiler-style-warning-count* 0)
164 (*compiler-note-count* 0)
165 (*undefined-warnings* nil)
166 (*in-compilation-unit* t))
167 (sb!thread:with-recursive-lock (*big-compiler-lock*)
168 (handler-bind ((parse-unknown-type
170 (note-undefined-reference
171 (parse-unknown-type-specifier c)
174 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
176 (incf *aborted-compilation-unit-count*))
177 (summarize-compilation-unit (not succeeded-p)))))))))
179 ;;; Is FUN-NAME something that no conforming program can rely on
180 ;;; defining as a function?
181 (defun fun-name-reserved-by-ansi-p (fun-name)
182 (eq (symbol-package (fun-name-block-name fun-name))
185 ;;; This is to be called at the end of a compilation unit. It signals
186 ;;; any residual warnings about unknown stuff, then prints the total
187 ;;; error counts. ABORT-P should be true when the compilation unit was
188 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
189 ;;; enclosed nested compilation units that were aborted.
190 (defun summarize-compilation-unit (abort-p)
192 (handler-bind ((style-warning #'compiler-style-warning-handler)
193 (warning #'compiler-warning-handler))
195 (let ((undefs (sort *undefined-warnings* #'string<
197 (let ((x (undefined-warning-name x)))
200 (prin1-to-string x)))))))
201 (dolist (undef undefs)
202 (let ((name (undefined-warning-name undef))
203 (kind (undefined-warning-kind undef))
204 (warnings (undefined-warning-warnings undef))
205 (undefined-warning-count (undefined-warning-count undef)))
206 (dolist (*compiler-error-context* warnings)
207 (if #-sb-xc-host (and (eq kind :function)
208 (fun-name-reserved-by-ansi-p name)
209 *flame-on-necessarily-undefined-function*)
214 "~@<There is no function named ~S. References to ~S in ~
215 some contexts (like starts of blocks) have special ~
216 meaning, but here it would have to be a function, ~
217 and that shouldn't be right.~:@>"
221 "~@<The ~(~A~) ~S is undefined, and its name is ~
222 reserved by ANSI CL so that even if it were ~
223 defined later, the code doing so would not be ~
226 (if (eq kind :variable)
227 (compiler-warn "undefined ~(~A~): ~S" kind name)
228 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
229 (let ((warn-count (length warnings)))
230 (when (and warnings (> undefined-warning-count warn-count))
231 (let ((more (- undefined-warning-count warn-count)))
232 (if (eq kind :variable)
234 "~W more use~:P of undefined ~(~A~) ~S"
237 "~W more use~:P of undefined ~(~A~) ~S"
238 more kind name)))))))
240 (dolist (kind '(:variable :function :type))
241 (let ((summary (mapcar #'undefined-warning-name
242 (remove kind undefs :test #'neq
243 :key #'undefined-warning-kind))))
245 (if (eq kind :variable)
247 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
248 ~% ~{~<~% ~1:;~S~>~^ ~}"
249 (cdr summary) kind summary)
251 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
252 ~% ~{~<~% ~1:;~S~>~^ ~}"
253 (cdr summary) kind summary))))))))
255 (unless (and (not abort-p)
256 (zerop *aborted-compilation-unit-count*)
257 (zerop *compiler-error-count*)
258 (zerop *compiler-warning-count*)
259 (zerop *compiler-style-warning-count*)
260 (zerop *compiler-note-count*))
261 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
262 (format *error-output* "~&compilation unit ~:[finished~;aborted~]~
263 ~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
264 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
265 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
266 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
267 ~[~:;~:*~& printed ~W note~:P~]"
269 *aborted-compilation-unit-count*
270 *compiler-error-count*
271 *compiler-warning-count*
272 *compiler-style-warning-count*
273 *compiler-note-count*))
274 (terpri *error-output*)
275 (force-output *error-output*)))
277 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
278 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
279 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
280 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
281 (defmacro with-compilation-values (&body body)
283 (let ((*warnings-p* nil)
285 (values (progn ,@body)
289 ;;;; component compilation
291 (defparameter *max-optimize-iterations* 3 ; ARB
293 "The upper limit on the number of times that we will consecutively do IR1
294 optimization that doesn't introduce any new code. A finite limit is
295 necessary, since type inference may take arbitrarily long to converge.")
297 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
298 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
300 ;;; Repeatedly optimize COMPONENT until no further optimizations can
301 ;;; be found or we hit our iteration limit. When we hit the limit, we
302 ;;; clear the component and block REOPTIMIZE flags to discourage the
303 ;;; next optimization attempt from pounding on the same code.
304 (defun ir1-optimize-until-done (component)
305 (declare (type component component))
307 (event ir1-optimize-until-done)
309 (cleared-reanalyze nil)
312 (when (component-reanalyze component)
314 (setq cleared-reanalyze t)
315 (setf (component-reanalyze component) nil))
316 (setf (component-reoptimize component) nil)
317 (ir1-optimize component fastp)
318 (cond ((component-reoptimize component)
320 (when (and (>= count *max-optimize-iterations*)
321 (not (component-reanalyze component))
322 (eq (component-reoptimize component) :maybe))
324 (cond ((retry-delayed-ir1-transforms :optimize)
328 (event ir1-optimize-maxed-out)
329 (setf (component-reoptimize component) nil)
330 (do-blocks (block component)
331 (setf (block-reoptimize block) nil))
333 ((retry-delayed-ir1-transforms :optimize)
339 (setq fastp (>= count *max-optimize-iterations*))
340 (maybe-mumble (if fastp "-" ".")))
341 (when cleared-reanalyze
342 (setf (component-reanalyze component) t)))
345 (defparameter *constraint-propagate* t)
347 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
348 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
349 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
350 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
352 ;;; FIXME: It would be good to document why it's important to have a
353 ;;; large value here, and what the drawbacks of an excessively large
354 ;;; value are; and it might also be good to make it depend on
355 ;;; optimization policy.
356 (defparameter *reoptimize-after-type-check-max* 10)
358 (defevent reoptimize-maxed-out
359 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
361 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
362 (defun dfo-as-needed (component)
363 (declare (type component component))
364 (when (component-reanalyze component)
368 (unless (component-reanalyze component)
374 ;;; Do all the IR1 phases for a non-top-level component.
375 (defun ir1-phases (component)
376 (declare (type component component))
377 (aver-live-component component)
378 (let ((*constraint-number* 0)
380 (*delayed-ir1-transforms* nil))
381 (declare (special *constraint-number* *delayed-ir1-transforms*))
383 (ir1-optimize-until-done component)
384 (when (or (component-new-functionals component)
385 (component-reanalyze-functionals component))
386 (maybe-mumble "locall ")
387 (locall-analyze-component component))
388 (dfo-as-needed component)
389 (when *constraint-propagate*
390 (maybe-mumble "constraint ")
391 (constraint-propagate component))
392 (when (retry-delayed-ir1-transforms :constraint)
393 (maybe-mumble "Rtran "))
394 (flet ((want-reoptimization-p ()
395 (or (component-reoptimize component)
396 (component-reanalyze component)
397 (component-new-functionals component)
398 (component-reanalyze-functionals component))))
399 (unless (and (want-reoptimization-p)
400 ;; We delay the generation of type checks until
401 ;; the type constraints have had time to
402 ;; propagate, else the compiler can confuse itself.
403 (< loop-count (- *reoptimize-after-type-check-max* 4)))
404 (maybe-mumble "type ")
405 (generate-type-checks component)
406 (unless (want-reoptimization-p)
408 (when (>= loop-count *reoptimize-after-type-check-max*)
409 (maybe-mumble "[reoptimize limit]")
410 (event reoptimize-maxed-out)
414 (ir1-finalize component)
417 (defun %compile-component (component)
418 (let ((*code-segment* nil)
420 (maybe-mumble "GTN ")
421 (gtn-analyze component)
422 (maybe-mumble "LTN ")
423 (ltn-analyze component)
424 (dfo-as-needed component)
425 (maybe-mumble "control ")
426 (control-analyze component #'make-ir2-block)
428 (when (or (ir2-component-values-receivers (component-info component))
429 (component-dx-lvars component))
430 (maybe-mumble "stack ")
431 (stack-analyze component)
432 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
433 ;; stack analysis. There shouldn't be any unreachable code after
434 ;; control, so this won't delete anything.
435 (dfo-as-needed component))
439 (maybe-mumble "IR2tran ")
441 (entry-analyze component)
442 (ir2-convert component)
444 (when (policy *lexenv* (>= speed compilation-speed))
445 (maybe-mumble "copy ")
446 (copy-propagate component))
448 (select-representations component)
450 (when *check-consistency*
451 (maybe-mumble "check2 ")
452 (check-ir2-consistency component))
454 (delete-unreferenced-tns component)
456 (maybe-mumble "life ")
457 (lifetime-analyze component)
459 (when *compile-progress*
460 (compiler-mumble "") ; Sync before doing more output.
461 (pre-pack-tn-stats component *standard-output*))
463 (when *check-consistency*
464 (maybe-mumble "check-life ")
465 (check-life-consistency component))
467 (maybe-mumble "pack ")
470 (when *check-consistency*
471 (maybe-mumble "check-pack ")
472 (check-pack-consistency component))
474 (when *compiler-trace-output*
475 (describe-component component *compiler-trace-output*)
476 (describe-ir2-component component *compiler-trace-output*))
478 (maybe-mumble "code ")
479 (multiple-value-bind (code-length trace-table fixup-notes)
480 (generate-code component)
483 (when *compiler-trace-output*
484 (format *compiler-trace-output*
485 "~|~%disassembly of code for ~S~2%" component)
486 (sb!disassem:disassemble-assem-segment *code-segment*
487 *compiler-trace-output*))
489 (etypecase *compile-object*
491 (maybe-mumble "fasl")
492 (fasl-dump-component component
499 (maybe-mumble "core")
500 (make-core-component component
508 ;; We're done, so don't bother keeping anything around.
509 (setf (component-info component) :dead)
513 ;;; Delete components with no external entry points before we try to
514 ;;; generate code. Unreachable closures can cause IR2 conversion to
515 ;;; puke on itself, since it is the reference to the closure which
516 ;;; normally causes the components to be combined.
517 (defun delete-if-no-entries (component)
518 (dolist (fun (component-lambdas component) (delete-component component))
519 (when (functional-has-external-references-p fun)
521 (case (functional-kind fun)
524 (unless (every (lambda (ref)
525 (eq (node-component ref) component))
529 (defun compile-component (component)
531 ;; miscellaneous sanity checks
533 ;; FIXME: These are basically pretty wimpy compared to the checks done
534 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
535 ;; make those internal consistency checks work again and use them.
536 (aver-live-component component)
537 (do-blocks (block component)
538 (aver (eql (block-component block) component)))
539 (dolist (lambda (component-lambdas component))
540 ;; sanity check to prevent weirdness from propagating insidiously as
541 ;; far from its root cause as it did in bug 138: Make sure that
542 ;; thing-to-COMPONENT links are consistent.
543 (aver (eql (lambda-component lambda) component))
544 (aver (eql (node-component (lambda-bind lambda)) component)))
546 (let* ((*component-being-compiled* component))
548 ;; Record xref information before optimization. This way the
549 ;; stored xref data reflects the real source as closely as
551 (record-component-xrefs component)
553 (ir1-phases component)
556 (dfo-as-needed component)
557 (find-dominators component)
558 (loop-analyze component))
561 (when (and *loop-analyze* *compiler-trace-output*)
562 (labels ((print-blocks (block)
563 (format *compiler-trace-output* " ~A~%" block)
564 (when (block-loop-next block)
565 (print-blocks (block-loop-next block))))
567 (format *compiler-trace-output* "loop=~A~%" loop)
568 (print-blocks (loop-blocks loop))
569 (dolist (l (loop-inferiors loop))
571 (print-loop (component-outer-loop component))))
574 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
575 (maybe-mumble "env ")
576 (physenv-analyze component)
577 (dfo-as-needed component)
579 (delete-if-no-entries component)
581 (unless (eq (block-next (component-head component))
582 (component-tail component))
583 (%compile-component component)))
585 (clear-constant-info)
589 ;;;; clearing global data structures
591 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
592 ;;;; global data structures entirely when possible and consing up the
593 ;;;; others from scratch instead of clearing and reusing them?
595 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
596 ;;; addition to allowing stuff to be reclaimed, this is required for
597 ;;; correct assignment of constant offsets, since we need to assign a
598 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
599 ;;; slots, since they are used to keep track of functions across
600 ;;; component boundaries.
601 (defun clear-constant-info ()
602 (maphash (lambda (k v)
604 (setf (leaf-info v) nil))
606 (maphash (lambda (k v)
609 (setf (leaf-info v) nil)))
613 ;;; Blow away the REFS for all global variables, and let COMPONENT
615 (defun clear-ir1-info (component)
616 (declare (type component component))
618 (maphash (lambda (k v)
622 (delete-if #'here-p (leaf-refs v)))
623 (when (basic-var-p v)
624 (setf (basic-var-sets v)
625 (delete-if #'here-p (basic-var-sets v))))))
628 (eq (node-component x) component)))
634 ;;; Clear global variables used by the compiler.
636 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
637 ;;; and it adds overhead even when people aren't using the compiler.
638 ;;; Perhaps we could make these global vars unbound except when
639 ;;; actually in use, so that this function could go away.
640 (defun clear-stuff (&optional (debug-too t))
642 ;; Clear global tables.
643 (when (boundp '*free-funs*)
644 (clrhash *free-funs*)
645 (clrhash *free-vars*)
646 (clrhash *constants*))
648 ;; Clear debug counters and tables.
649 (clrhash *seen-blocks*)
650 (clrhash *seen-funs*)
651 (clrhash *list-conflicts-table*)
654 (clrhash *continuation-numbers*)
655 (clrhash *number-continuations*)
656 (setq *continuation-number* 0)
660 (clrhash *label-ids*)
661 (clrhash *id-labels*)
664 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
665 ;; Superficially, this seemed harmful -- the user could reasonably be
666 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
667 ;; compiled. A closer inspection showed that this actually turned out to be
668 ;; harmless in practice, because CLEAR-STUFF was only called from within
669 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
670 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
671 ;; practice, it was also useless in practice. So we don't do it any more.)
677 ;;; Print out some useful info about COMPONENT to STREAM.
678 (defun describe-component (component *standard-output*)
679 (declare (type component component))
680 (format t "~|~%;;;; component: ~S~2%" (component-name component))
681 (print-all-blocks component)
684 (defun describe-ir2-component (component *standard-output*)
685 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
686 (format t "entries:~%")
687 (dolist (entry (ir2-component-entries (component-info component)))
688 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
689 (label-id (entry-info-offset entry))
690 (entry-info-name entry)
691 (entry-info-closure-tn entry)))
693 (pre-pack-tn-stats component *standard-output*)
695 (print-ir2-blocks component)
701 ;;;; When reading from a file, we have to keep track of some source
702 ;;;; information. We also exploit our ability to back up for printing
703 ;;;; the error context and for recovering from errors.
705 ;;;; The interface we provide to this stuff is the stream-oid
706 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
707 ;;;; of getting the next source form.
709 ;;; A FILE-INFO structure holds all the source information for a
711 (def!struct (file-info (:copier nil))
712 ;; If a file, the truename of the corresponding source file. If from
713 ;; a Lisp form, :LISP. If from a stream, :STREAM.
714 (name (missing-arg) :type (or pathname (member :lisp :stream)))
715 ;; the external format that we'll call OPEN with, if NAME is a file.
716 (external-format nil)
717 ;; the defaulted, but not necessarily absolute file name (i.e. prior
718 ;; to TRUENAME call.) Null if not a file. This is used to set
719 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
721 (untruename nil :type (or pathname null))
722 ;; the file's write date (if relevant)
723 (write-date nil :type (or unsigned-byte null))
724 ;; the source path root number of the first form in this file (i.e.
725 ;; the total number of forms converted previously in this
727 (source-root 0 :type unsigned-byte)
728 ;; parallel vectors containing the forms read out of the file and
729 ;; the file positions that reading of each form started at (i.e. the
730 ;; end of the previous form)
731 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
732 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
734 ;;; The SOURCE-INFO structure provides a handle on all the source
735 ;;; information for an entire compilation.
736 (def!struct (source-info
737 #-no-ansi-print-object
738 (:print-object (lambda (s stream)
739 (print-unreadable-object (s stream :type t))))
741 ;; the UT that compilation started at
742 (start-time (get-universal-time) :type unsigned-byte)
743 ;; the FILE-INFO structure for this compilation
744 (file-info nil :type (or file-info null))
745 ;; the stream that we are using to read the FILE-INFO, or NIL if
746 ;; no stream has been opened yet
747 (stream nil :type (or stream null)))
749 ;;; Given a pathname, return a SOURCE-INFO structure.
750 (defun make-file-source-info (file external-format)
751 (let ((file-info (make-file-info :name (truename file)
752 :untruename (merge-pathnames file)
753 :external-format external-format
754 :write-date (file-write-date file))))
756 (make-source-info :file-info file-info)))
758 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
759 (defun make-lisp-source-info (form)
760 (make-source-info :start-time (get-universal-time)
761 :file-info (make-file-info :name :lisp
765 ;;; Return a SOURCE-INFO which will read from STREAM.
766 (defun make-stream-source-info (stream)
767 (let ((file-info (make-file-info :name :stream)))
768 (make-source-info :file-info file-info
771 ;;; Return a form read from STREAM; or for EOF use the trick,
772 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
773 ;;; error happens, then convert it to standard abort-the-compilation
774 ;;; error condition (possibly recording some extra location
776 (defun read-for-compile-file (stream position)
777 (handler-case (read stream nil stream)
778 (reader-error (condition)
779 (error 'input-error-in-compile-file
781 ;; We don't need to supply :POSITION here because
782 ;; READER-ERRORs already know their position in the file.
784 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
785 ;; (and that this is not a READER-ERROR) when it encounters end of
786 ;; file in the middle of something it's trying to read.
787 (end-of-file (condition)
788 (error 'input-error-in-compile-file
790 ;; We need to supply :POSITION here because the END-OF-FILE
791 ;; condition doesn't carry the position that the user
792 ;; probably cares about, where the failed READ began.
793 :position position))))
795 ;;; If STREAM is present, return it, otherwise open a stream to the
796 ;;; current file. There must be a current file.
798 ;;; FIXME: This is probably an unnecessarily roundabout way to do
799 ;;; things now that we process a single file in COMPILE-FILE (unlike
800 ;;; the old CMU CL code, which accepted multiple files). Also, the old
802 ;;; When we open a new file, we also reset *PACKAGE* and policy.
803 ;;; This gives the effect of rebinding around each file.
804 ;;; which doesn't seem to be true now. Check to make sure that if
805 ;;; such rebinding is necessary, it's still done somewhere.
806 (defun get-source-stream (info)
807 (declare (type source-info info))
808 (or (source-info-stream info)
809 (let* ((file-info (source-info-file-info info))
810 (name (file-info-name file-info))
811 (external-format (file-info-external-format file-info)))
812 (setf sb!xc:*compile-file-truename* name
813 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
814 (source-info-stream info)
815 (open name :direction :input
816 :external-format external-format)))))
818 ;;; Close the stream in INFO if it is open.
819 (defun close-source-info (info)
820 (declare (type source-info info))
821 (let ((stream (source-info-stream info)))
822 (when stream (close stream)))
823 (setf (source-info-stream info) nil)
826 ;;; Read and compile the source file.
827 (defun sub-sub-compile-file (info)
828 (let* ((file-info (source-info-file-info info))
829 (stream (get-source-stream info)))
831 (let* ((pos (file-position stream))
832 (form (read-for-compile-file stream pos)))
833 (if (eq form stream) ; i.e., if EOF
835 (let* ((forms (file-info-forms file-info))
836 (current-idx (+ (fill-pointer forms)
837 (file-info-source-root file-info))))
838 (vector-push-extend form forms)
839 (vector-push-extend pos (file-info-positions file-info))
840 (find-source-paths form current-idx)
841 (process-toplevel-form form
842 `(original-source-start 0 ,current-idx)
845 ;;; Return the INDEX'th source form read from INFO and the position
846 ;;; where it was read.
847 (defun find-source-root (index info)
848 (declare (type index index) (type source-info info))
849 (let ((file-info (source-info-file-info info)))
850 (values (aref (file-info-forms file-info) index)
851 (aref (file-info-positions file-info) index))))
853 ;;;; processing of top level forms
855 ;;; This is called by top level form processing when we are ready to
856 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
857 ;;; convert the form, but delay compilation, pushing the result on
858 ;;; *TOPLEVEL-LAMBDAS* instead.
859 (defun convert-and-maybe-compile (form path)
860 (declare (list path))
861 (if (fopcompilable-p form)
862 (let ((*fopcompile-label-counter* 0))
863 (fopcompile form path nil))
864 (let* ((*top-level-form-noted* (note-top-level-form form t))
865 (*lexenv* (make-lexenv
867 :handled-conditions *handled-conditions*
868 :disabled-package-locks *disabled-package-locks*))
869 (tll (ir1-toplevel form path nil)))
870 (if (eq *block-compile* t)
871 (push tll *toplevel-lambdas*)
872 (compile-toplevel (list tll) nil))
875 ;;; Macroexpand FORM in the current environment with an error handler.
876 ;;; We only expand one level, so that we retain all the intervening
877 ;;; forms in the source path.
878 (defun preprocessor-macroexpand-1 (form)
879 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
881 (compiler-error "(during macroexpansion of ~A)~%~A"
882 (let ((*print-level* 2)
884 (format nil "~S" form))
887 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
888 ;;; the forms, and PATH is the source path of the FORM they came out of.
889 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
890 (defun process-toplevel-progn (forms path compile-time-too)
891 (declare (list forms) (list path))
893 (process-toplevel-form form path compile-time-too)))
895 ;;; Process a top level use of LOCALLY, or anything else (e.g.
896 ;;; MACROLET) at top level which has declarations and ordinary forms.
897 ;;; We parse declarations and then recursively process the body.
898 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
899 (declare (list path))
900 (multiple-value-bind (forms decls)
901 (parse-body body :doc-string-allowed nil :toplevel t)
902 (let* ((*lexenv* (process-decls decls vars funs))
903 ;; FIXME: VALUES declaration
905 ;; Binding *POLICY* is pretty much of a hack, since it
906 ;; causes LOCALLY to "capture" enclosed proclamations. It
907 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
908 ;; value of *POLICY* as the policy. The need for this hack
909 ;; is due to the quirk that there is no way to represent in
910 ;; a POLICY that an optimize quality came from the default.
912 ;; FIXME: Ideally, something should be done so that DECLAIM
913 ;; inside LOCALLY works OK. Failing that, at least we could
914 ;; issue a warning instead of silently screwing up.
915 (*policy* (lexenv-policy *lexenv*))
916 ;; This is probably also a hack
917 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
919 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*)))
920 (process-toplevel-progn forms path compile-time-too))))
922 ;;; Parse an EVAL-WHEN situations list, returning three flags,
923 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
924 ;;; the types of situations present in the list.
925 (defun parse-eval-when-situations (situations)
926 (when (or (not (listp situations))
927 (set-difference situations
934 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
935 (let ((deprecated-names (intersection situations '(compile load eval))))
936 (when deprecated-names
937 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
939 (values (intersection '(:compile-toplevel compile)
941 (intersection '(:load-toplevel load) situations)
942 (intersection '(:execute eval) situations)))
945 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
946 (defun functional-components (f)
947 (declare (type functional f))
949 (clambda (list (lambda-component f)))
950 (optional-dispatch (let ((result nil))
951 (flet ((maybe-frob (maybe-clambda)
952 (when (and maybe-clambda
953 (promise-ready-p maybe-clambda))
954 (pushnew (lambda-component
955 (force maybe-clambda))
957 (map nil #'maybe-frob (optional-dispatch-entry-points f))
958 (maybe-frob (optional-dispatch-more-entry f))
959 (maybe-frob (optional-dispatch-main-entry f)))
962 (defun make-functional-from-toplevel-lambda (definition
966 ;; I'd thought NIL should
967 ;; work, but it doesn't.
970 (let* ((*current-path* path)
971 (component (make-empty-component))
972 (*current-component* component))
973 (setf (component-name component)
974 (debug-name 'initial-component name))
975 (setf (component-kind component) :initial)
976 (let* ((locall-fun (let ((*allow-instrumenting* t))
977 (funcall #'ir1-convert-lambdalike
980 (debug-name (debug-name 'tl-xep name))
981 ;; Convert the XEP using the policy of the real
982 ;; function. Otherwise the wrong policy will be used for
983 ;; deciding whether to type-check the parameters of the
984 ;; real function (via CONVERT-CALL / PROPAGATE-TO-ARGS).
985 ;; -- JES, 2007-02-27
986 (*lexenv* (make-lexenv :policy (lexenv-policy
987 (functional-lexenv locall-fun))))
988 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
989 :source-name (or name '.anonymous.)
990 :debug-name debug-name)))
992 (assert-global-function-definition-type name locall-fun))
993 (setf (functional-entry-fun fun) locall-fun
994 (functional-kind fun) :external
995 (functional-has-external-references-p locall-fun) t
996 (functional-has-external-references-p fun) t)
999 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
1000 ;;; description of the result.
1001 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
1002 ;;; into core and return the compiled FUNCTION value.
1003 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
1004 ;;; into the fasl file and return a dump handle.
1006 ;;; If NAME is provided, then we try to use it as the name of the
1007 ;;; function for debugging/diagnostic information.
1008 (defun %compile (lambda-expression
1013 ;; This magical idiom seems to be the appropriate
1014 ;; path for compiling standalone LAMBDAs, judging
1015 ;; from the CMU CL code and experiment, so it's a
1016 ;; nice default for things where we don't have a
1017 ;; real source path (as in e.g. inside CL:COMPILE).
1018 '(original-source-start 0 0)))
1020 (legal-fun-name-or-type-error name))
1021 (let* ((*lexenv* (make-lexenv
1023 :handled-conditions *handled-conditions*
1024 :disabled-package-locks *disabled-package-locks*))
1025 (*compiler-sset-counter* 0)
1026 (fun (make-functional-from-toplevel-lambda lambda-expression
1030 ;; FIXME: The compile-it code from here on is sort of a
1031 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1032 ;; better to find a way to share the code there; or
1033 ;; alternatively, to use this code to replace the code there.
1034 ;; (The second alternative might be pretty easy if we used
1035 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1036 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1038 (locall-analyze-clambdas-until-done (list fun))
1040 (let ((components-from-dfo (find-initial-dfo (list fun))))
1041 (dolist (component-from-dfo components-from-dfo)
1042 (compile-component component-from-dfo)
1043 (replace-toplevel-xeps component-from-dfo))
1045 (let ((entry-table (etypecase *compile-object*
1046 (fasl-output (fasl-output-entry-table
1048 (core-object (core-object-entry-table
1049 *compile-object*)))))
1050 (multiple-value-bind (result found-p)
1051 (gethash (leaf-info fun) entry-table)
1055 ;; KLUDGE: This code duplicates some other code in this
1056 ;; file. In the great reorganzation, the flow of program
1057 ;; logic changed from the original CMUCL model, and that
1058 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1059 ;; longer followed for CORE-OBJECTS, leading to BUG
1060 ;; 156. This place is transparently not the right one for
1061 ;; this code, but I don't have a clear enough overview of
1062 ;; the compiler to know how to rearrange it all so that
1063 ;; this operation fits in nicely, and it was blocking
1064 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1065 ;; ((..)) (DEFUN FOO ...))
1067 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1068 ;; ideal way, as (1) definitions typed in at the REPL
1069 ;; without an INLINE declaration will give a NULL
1070 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1071 ;; and (2) INLINE declarations will yield a
1072 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1073 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1076 ;; (2) is probably fairly easy to fix -- it is, after all,
1077 ;; a matter of list manipulation (or possibly of teaching
1078 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1079 ;; significantly harder, as the association between
1080 ;; function object and source is a tricky one.
1082 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1083 ;; non-NULL list) when the function in question has been
1084 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1085 ;; work when it has been compiled as part of the top-level
1086 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1087 ;; ...). -- CSR, 2002-11-02
1088 (when (core-object-p *compile-object*)
1089 (fix-core-source-info *source-info* *compile-object* result))
1091 (mapc #'clear-ir1-info components-from-dfo)
1094 (defun process-toplevel-cold-fset (name lambda-expression path)
1095 (unless (producing-fasl-file)
1096 (error "can't COLD-FSET except in a fasl file"))
1097 (legal-fun-name-or-type-error name)
1098 (fasl-dump-cold-fset name
1099 (%compile lambda-expression
1106 (defun note-top-level-form (form &optional finalp)
1107 (when *compile-print*
1108 (cond ((not *top-level-form-noted*)
1109 (let ((*print-length* 2)
1111 (*print-pretty* nil))
1112 (with-compiler-io-syntax
1113 (compiler-mumble "~&; ~:[compiling~;converting~] ~S"
1114 *block-compile* form)))
1117 (eq :top-level-forms *compile-print*)
1118 (neq form *top-level-form-noted*))
1119 (let ((*print-length* 1)
1121 (*print-pretty* nil))
1122 (with-compiler-io-syntax
1123 (compiler-mumble "~&; ... top level ~S" form)))
1126 *top-level-form-noted*))))
1128 ;;; Process a top level FORM with the specified source PATH.
1129 ;;; * If this is a magic top level form, then do stuff.
1130 ;;; * If this is a macro, then expand it.
1131 ;;; * Otherwise, just compile it.
1133 ;;; COMPILE-TIME-TOO is as defined in ANSI
1134 ;;; "3.2.3.1 Processing of Top Level Forms".
1135 (defun process-toplevel-form (form path compile-time-too)
1136 (declare (list path))
1138 (catch 'process-toplevel-form-error-abort
1139 (let* ((path (or (gethash form *source-paths*) (cons form path)))
1140 (*compiler-error-bailout*
1141 (lambda (&optional condition)
1142 (convert-and-maybe-compile
1143 (make-compiler-error-form condition form)
1145 (throw 'process-toplevel-form-error-abort nil))))
1147 (flet ((default-processor (form)
1148 (let ((*top-level-form-noted* (note-top-level-form form)))
1149 ;; When we're cross-compiling, consider: what should we
1150 ;; do when we hit e.g.
1151 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1152 ;; (DEFUN FOO (X) (+ 7 X)))?
1153 ;; DEFUN has a macro definition in the cross-compiler,
1154 ;; and a different macro definition in the target
1155 ;; compiler. The only sensible thing is to use the
1156 ;; target compiler's macro definition, since the
1157 ;; cross-compiler's macro is in general into target
1158 ;; functions which can't meaningfully be executed at
1159 ;; cross-compilation time. So make sure we do the EVAL
1160 ;; here, before we macroexpand.
1162 ;; Then things get even dicier with something like
1163 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1164 ;; where we have to make sure that we don't uncross
1165 ;; the SB!XC: prefix before we do EVAL, because otherwise
1166 ;; we'd be trying to redefine the cross-compilation host's
1169 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1172 (when compile-time-too
1173 (eval form)) ; letting xc host EVAL do its own macroexpansion
1174 (let* (;; (We uncross the operator name because things
1175 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1176 ;; should be equivalent to their CL: counterparts
1177 ;; when being compiled as target code. We leave
1178 ;; the rest of the form uncrossed because macros
1179 ;; might yet expand into EVAL-WHEN stuff, and
1180 ;; things inside EVAL-WHEN can't be uncrossed
1181 ;; until after we've EVALed them in the
1182 ;; cross-compilation host.)
1183 (slightly-uncrossed (cons (uncross (first form))
1185 (expanded (preprocessor-macroexpand-1
1186 slightly-uncrossed)))
1187 (if (eq expanded slightly-uncrossed)
1188 ;; (Now that we're no longer processing toplevel
1189 ;; forms, and hence no longer need to worry about
1190 ;; EVAL-WHEN, we can uncross everything.)
1191 (convert-and-maybe-compile expanded path)
1192 ;; (We have to demote COMPILE-TIME-TOO to NIL
1193 ;; here, no matter what it was before, since
1194 ;; otherwise we'd tend to EVAL subforms more than
1195 ;; once, because of WHEN COMPILE-TIME-TOO form
1197 (process-toplevel-form expanded path nil))))
1198 ;; When we're not cross-compiling, we only need to
1199 ;; macroexpand once, so we can follow the 1-thru-6
1200 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1201 ;; Top Level Forms".
1204 (let ((*current-path* path))
1205 (preprocessor-macroexpand-1 form))))
1206 (cond ((eq expanded form)
1207 (when compile-time-too
1208 (eval-in-lexenv form *lexenv*))
1209 (convert-and-maybe-compile form path))
1211 (process-toplevel-form expanded
1213 compile-time-too)))))))
1216 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1217 ;; (1) SBCL gets smart enough to handle global
1218 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1219 ;; implementors start using symbol macros in a way which
1220 ;; interacts with SB-XC/CL distinction.)
1221 (convert-and-maybe-compile form path)
1223 (default-processor form)
1224 (flet ((need-at-least-one-arg (form)
1226 (compiler-error "~S form is too short: ~S"
1230 ;; In the cross-compiler, top level COLD-FSET arranges
1231 ;; for static linking at cold init time.
1234 (aver (not compile-time-too))
1235 (destructuring-bind (cold-fset fun-name lambda-expression) form
1236 (declare (ignore cold-fset))
1237 (process-toplevel-cold-fset fun-name
1240 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1241 (need-at-least-one-arg form)
1242 (destructuring-bind (special-operator magic &rest body) form
1243 (ecase special-operator
1245 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1246 ;; "3.2.3.1 Processing of Top Level Forms".
1247 (multiple-value-bind (ct lt e)
1248 (parse-eval-when-situations magic)
1249 (let ((new-compile-time-too (or ct
1250 (and compile-time-too
1252 (cond (lt (process-toplevel-progn
1253 body path new-compile-time-too))
1254 (new-compile-time-too (eval-in-lexenv
1258 (funcall-in-macrolet-lexenv
1260 (lambda (&key funs prepend)
1261 (declare (ignore funs))
1262 (aver (null prepend))
1263 (process-toplevel-locally body
1268 (funcall-in-symbol-macrolet-lexenv
1270 (lambda (&key vars prepend)
1271 (aver (null prepend))
1272 (process-toplevel-locally body
1278 (process-toplevel-locally (rest form) path compile-time-too))
1280 (process-toplevel-progn (rest form) path compile-time-too))
1281 (t (default-processor form))))))))
1285 ;;;; load time value support
1287 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1289 ;;; Return T if we are currently producing a fasl file and hence
1290 ;;; constants need to be dumped carefully.
1291 (defun producing-fasl-file ()
1292 (fasl-output-p *compile-object*))
1294 ;;; Compile FORM and arrange for it to be called at load-time. Return
1295 ;;; the dumper handle and our best guess at the type of the object.
1296 (defun compile-load-time-value (form)
1297 (let ((lambda (compile-load-time-stuff form t)))
1299 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1300 (let ((type (leaf-type lambda)))
1301 (if (fun-type-p type)
1302 (single-value-type (fun-type-returns type))
1305 ;;; Compile the FORMS and arrange for them to be called (for effect,
1306 ;;; not value) at load time.
1307 (defun compile-make-load-form-init-forms (forms)
1308 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1309 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1311 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1312 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1313 (defun compile-load-time-stuff (form for-value)
1315 (let* ((*lexenv* (make-null-lexenv))
1316 (lambda (ir1-toplevel form *current-path* for-value)))
1317 (compile-toplevel (list lambda) t)
1320 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1321 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1322 ;;; don't try to combine this component with anything else and frob
1323 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1324 ;;; compiling, because it was merged with a run-time component.
1325 (defun compile-load-time-value-lambda (lambdas)
1326 (aver (null (cdr lambdas)))
1327 (let* ((lambda (car lambdas))
1328 (component (lambda-component lambda)))
1329 (when (eql (component-kind component) :toplevel)
1330 (setf (component-name component) (leaf-debug-name lambda))
1331 (compile-component component)
1332 (clear-ir1-info component))))
1336 (defun object-call-toplevel-lambda (tll)
1337 (declare (type functional tll))
1338 (let ((object *compile-object*))
1340 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1341 (core-object (core-call-toplevel-lambda tll object))
1344 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1345 ;;; the resulting function to be called.
1346 (defun sub-compile-toplevel-lambdas (lambdas)
1347 (declare (list lambdas))
1349 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1350 (compile-component component)
1351 (clear-ir1-info component)
1352 (object-call-toplevel-lambda tll)))
1355 ;;; Compile top level code and call the top level lambdas. We pick off
1356 ;;; top level lambdas in non-top-level components here, calling
1357 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1358 (defun compile-toplevel-lambdas (lambdas)
1359 (declare (list lambdas))
1360 (let ((len (length lambdas)))
1361 (flet ((loser (start)
1362 (or (position-if (lambda (x)
1363 (not (eq (component-kind
1364 (node-component (lambda-bind x)))
1367 ;; this used to read ":start start", but
1368 ;; start can be greater than len, which
1369 ;; is an error according to ANSI - CSR,
1371 :start (min start len))
1373 (do* ((start 0 (1+ loser))
1374 (loser (loser start) (loser start)))
1376 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1377 (unless (= loser len)
1378 (object-call-toplevel-lambda (elt lambdas loser))))))
1381 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1384 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1385 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1386 (defun compile-toplevel (lambdas load-time-value-p)
1387 (declare (list lambdas))
1389 (maybe-mumble "locall ")
1390 (locall-analyze-clambdas-until-done lambdas)
1392 (maybe-mumble "IDFO ")
1393 (multiple-value-bind (components top-components hairy-top)
1394 (find-initial-dfo lambdas)
1395 (let ((all-components (append components top-components)))
1396 (when *check-consistency*
1397 (maybe-mumble "[check]~%")
1398 (check-ir1-consistency all-components))
1400 (dolist (component (append hairy-top top-components))
1401 (pre-physenv-analyze-toplevel component))
1403 (dolist (component components)
1404 (compile-component component)
1405 (replace-toplevel-xeps component))
1407 (when *check-consistency*
1408 (maybe-mumble "[check]~%")
1409 (check-ir1-consistency all-components))
1411 (if load-time-value-p
1412 (compile-load-time-value-lambda lambdas)
1413 (compile-toplevel-lambdas lambdas))
1415 (mapc #'clear-ir1-info components)
1419 ;;; Actually compile any stuff that has been queued up for block
1421 (defun finish-block-compilation ()
1422 (when *block-compile*
1423 (when *compile-print*
1424 (compiler-mumble "~&; block compiling converted top level forms..."))
1425 (when *toplevel-lambdas*
1426 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1427 (setq *toplevel-lambdas* ()))
1428 (setq *block-compile* nil)
1429 (setq *entry-points* nil)))
1431 (defun handle-condition-p (condition)
1433 (etypecase *compiler-error-context*
1435 (node-lexenv *compiler-error-context*))
1436 (compiler-error-context
1437 (let ((lexenv (compiler-error-context-lexenv
1438 *compiler-error-context*)))
1442 (let ((muffles (lexenv-handled-conditions lexenv)))
1443 (if (null muffles) ; common case
1445 (dolist (muffle muffles nil)
1446 (destructuring-bind (typespec . restart-name) muffle
1447 (when (and (typep condition typespec)
1448 (find-restart restart-name condition))
1451 (defun handle-condition-handler (condition)
1453 (etypecase *compiler-error-context*
1455 (node-lexenv *compiler-error-context*))
1456 (compiler-error-context
1457 (let ((lexenv (compiler-error-context-lexenv
1458 *compiler-error-context*)))
1462 (let ((muffles (lexenv-handled-conditions lexenv)))
1464 (dolist (muffle muffles (bug "fell through"))
1465 (destructuring-bind (typespec . restart-name) muffle
1466 (when (typep condition typespec)
1467 (awhen (find-restart restart-name condition)
1468 (invoke-restart it))))))))
1470 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1471 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1472 (defun sub-compile-file (info)
1473 (declare (type source-info info))
1474 (let ((*package* (sane-package))
1475 (*readtable* *readtable*)
1476 (sb!xc:*compile-file-pathname* nil) ; really bound in
1477 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1479 (*handled-conditions* *handled-conditions*)
1480 (*disabled-package-locks* *disabled-package-locks*)
1481 (*lexenv* (make-null-lexenv))
1482 (*block-compile* *block-compile-arg*)
1483 (*source-info* info)
1484 (*toplevel-lambdas* ())
1485 (*fun-names-in-this-file* ())
1486 (*allow-instrumenting* nil)
1487 (*compiler-error-bailout*
1489 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1490 (return-from sub-compile-file (values nil t t))))
1491 (*current-path* nil)
1492 (*last-source-context* nil)
1493 (*last-original-source* nil)
1494 (*last-source-form* nil)
1495 (*last-format-string* nil)
1496 (*last-format-args* nil)
1497 (*last-message-count* 0)
1498 ;; FIXME: Do we need this rebinding here? It's a literal
1499 ;; translation of the old CMU CL rebinding to
1500 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1501 ;; and it's not obvious whether the rebinding to itself is
1502 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1503 (*info-environment* *info-environment*)
1504 (*compiler-sset-counter* 0)
1505 (*gensym-counter* 0))
1507 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1508 (with-compilation-values
1509 (sb!xc:with-compilation-unit ()
1512 (sub-sub-compile-file info)
1514 (finish-block-compilation)
1515 (let ((object *compile-object*))
1517 (fasl-output (fasl-dump-source-info info object))
1518 (core-object (fix-core-source-info info object))
1521 ;; Some errors are sufficiently bewildering that we just fail
1522 ;; immediately, without trying to recover and compile more of
1524 (fatal-compiler-error (condition)
1526 (when *compile-verbose*
1527 (format *standard-output*
1528 "~@<compilation aborted because of fatal error: ~2I~_~A~:>"
1530 (values nil t t)))))
1532 ;;; Return a pathname for the named file. The file must exist.
1533 (defun verify-source-file (pathname-designator)
1534 (let* ((pathname (pathname pathname-designator))
1535 (default-host (make-pathname :host (pathname-host pathname))))
1536 (flet ((try-with-type (path type error-p)
1537 (let ((new (merge-pathnames
1538 path (make-pathname :type type
1539 :defaults default-host))))
1540 (if (probe-file new)
1542 (and error-p (truename new))))))
1543 (cond ((typep pathname 'logical-pathname)
1544 (try-with-type pathname "LISP" t))
1545 ((probe-file pathname) pathname)
1546 ((try-with-type pathname "lisp" nil))
1547 ((try-with-type pathname "lisp" t))))))
1549 (defun elapsed-time-to-string (tsec)
1550 (multiple-value-bind (tmin sec) (truncate tsec 60)
1551 (multiple-value-bind (thr min) (truncate tmin 60)
1552 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1554 ;;; Print some junk at the beginning and end of compilation.
1555 (defun print-compile-start-note (source-info)
1556 (declare (type source-info source-info))
1557 (let ((file-info (source-info-file-info source-info)))
1558 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1559 (namestring (file-info-name file-info))
1560 (sb!int:format-universal-time nil
1561 (file-info-write-date
1565 :print-timezone nil)))
1568 (defun print-compile-end-note (source-info won)
1569 (declare (type source-info source-info))
1570 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1572 (elapsed-time-to-string
1573 (- (get-universal-time)
1574 (source-info-start-time source-info))))
1577 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1578 ;;; out of the compile, then abort the writing of the output file, so
1579 ;;; that we don't overwrite it with known garbage.
1580 (defun sb!xc:compile-file
1585 (output-file (cfp-output-file-default input-file))
1586 ;; FIXME: ANSI doesn't seem to say anything about
1587 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1589 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1590 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1591 (external-format :default)
1595 ((:block-compile *block-compile-arg*) nil))
1597 "Compile INPUT-FILE, producing a corresponding fasl file and
1598 returning its filename.
1601 If true, a message per non-macroexpanded top level form is printed
1602 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1603 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1604 no such message, but their subforms do.
1606 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1607 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1608 For example, compiling an IN-PACKAGE form will result in a message about
1609 a top level SETQ in addition to the message about the IN-PACKAGE form'
1612 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1615 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1616 argument, it is not currently supported. (non-standard)
1619 If given, internal data structures are dumped to the specified
1620 file, or if a value of T is given, to a file of *.trace type
1621 derived from the input file name. (non-standard)"
1622 ;;; Block compilation is currently broken.
1624 "Also, as a workaround for vaguely-non-ANSI behavior, the
1625 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1626 multiple functions are compiled together as a unit, resolving function
1627 references at compile time. NIL means that global function names are
1628 never resolved at compilation time. Currently NIL is the default
1629 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1630 of the ANSI spec allows this behavior under all circumstances, the
1631 compiler's runtime scales badly when it tries to do this for large
1632 files. If/when this performance problem is fixed, the block
1633 compilation default behavior will probably be made dependent on the
1634 SPEED and COMPILATION-SPEED optimization values, and the
1635 :BLOCK-COMPILE argument will probably become deprecated."
1637 (let* ((fasl-output nil)
1638 (output-file-name nil)
1641 (failure-p t) ; T in case error keeps this from being set later
1642 (input-pathname (verify-source-file input-file))
1643 (source-info (make-file-source-info input-pathname external-format))
1644 (*compiler-trace-output* nil)) ; might be modified below
1649 (setq output-file-name
1650 (sb!xc:compile-file-pathname input-file
1651 :output-file output-file))
1653 (open-fasl-output output-file-name
1654 (namestring input-pathname))))
1656 (let* ((default-trace-file-pathname
1657 (make-pathname :type "trace" :defaults input-pathname))
1658 (trace-file-pathname
1659 (if (eql trace-file t)
1660 default-trace-file-pathname
1661 (merge-pathnames trace-file
1662 default-trace-file-pathname))))
1663 (setf *compiler-trace-output*
1664 (open trace-file-pathname
1665 :if-exists :supersede
1666 :direction :output))))
1668 (when sb!xc:*compile-verbose*
1669 (print-compile-start-note source-info))
1670 (let ((*compile-object* fasl-output)
1672 (multiple-value-setq (dummy warnings-p failure-p)
1673 (sub-compile-file source-info)))
1674 (setq compile-won t))
1676 (close-source-info source-info)
1679 (close-fasl-output fasl-output (not compile-won))
1680 (setq output-file-name
1681 (pathname (fasl-output-stream fasl-output)))
1682 (when (and compile-won sb!xc:*compile-verbose*)
1683 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1685 (when sb!xc:*compile-verbose*
1686 (print-compile-end-note source-info compile-won))
1688 (when *compiler-trace-output*
1689 (close *compiler-trace-output*)))
1691 (values (if output-file
1692 ;; Hack around filesystem race condition...
1693 (or (probe-file output-file-name) output-file-name)
1698 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1699 ;;; the OUTPUT-FILE argument
1701 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1702 ;;; that results from merging the INPUT-FILE with the value of
1703 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1704 ;;; default to the appropriate implementation-defined default type for
1706 (defun cfp-output-file-default (input-file)
1707 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1708 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1711 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1712 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1713 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1714 ;;; pathname, it is translated into a physical pathname as if by
1715 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1716 ;;; So I haven't really tried to make this precisely ANSI-compatible
1717 ;;; at the level of e.g. whether it returns logical pathname or a
1718 ;;; physical pathname. Patches to make it more correct are welcome.
1719 ;;; -- WHN 2000-12-09
1720 (defun sb!xc:compile-file-pathname (input-file
1722 (output-file nil output-file-p)
1725 "Return a pathname describing what file COMPILE-FILE would write to given
1728 (merge-pathnames output-file (cfp-output-file-default input-file))
1729 (cfp-output-file-default input-file)))
1731 ;;;; MAKE-LOAD-FORM stuff
1733 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1734 ;;; finds a constant structure, it invokes this to arrange for proper
1735 ;;; dumping. If it turns out that the constant has already been
1736 ;;; dumped, then we don't need to do anything.
1738 ;;; If the constant hasn't been dumped, then we check to see whether
1739 ;;; we are in the process of creating it. We detect this by
1740 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1741 ;;; the constants we are in the process of creating. Actually, each
1742 ;;; entry is a list of the constant and any init forms that need to be
1743 ;;; processed on behalf of that constant.
1745 ;;; It's not necessarily an error for this to happen. If we are
1746 ;;; processing the init form for some object that showed up *after*
1747 ;;; the original reference to this constant, then we just need to
1748 ;;; defer the processing of that init form. To detect this, we
1749 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1750 ;;; constants created since the last time we started processing an
1751 ;;; init form. If the constant passed to emit-make-load-form shows up
1752 ;;; in this list, then there is a circular chain through creation
1753 ;;; forms, which is an error.
1755 ;;; If there is some intervening init form, then we blow out of
1756 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1757 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1758 ;;; offending init form can be tacked onto the init forms for the
1759 ;;; circular object.
1761 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1762 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1763 ;;; whether the creation form is the magic value
1764 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1765 ;;; dumper will eventually get its hands on the object and use the
1766 ;;; normal structure dumping noise on it.
1768 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1769 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1770 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1771 ;;; dumper to use that result instead whenever it sees this constant.
1773 ;;; Now we try to compile the init form. We bind
1774 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1775 ;;; form (and any init forms that were added because of circularity
1776 ;;; detection). If this works, great. If not, we add the init forms to
1777 ;;; the init forms for the object that caused the problems and let it
1779 (defvar *constants-being-created* nil)
1780 (defvar *constants-created-since-last-init* nil)
1781 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1782 (defun emit-make-load-form (constant)
1783 (aver (fasl-output-p *compile-object*))
1784 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1785 ;; KLUDGE: This special hack is because I was too lazy
1786 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1787 ;; function of LAYOUT returns nontrivial forms when
1788 ;; building the cross-compiler but :IGNORE-IT when
1789 ;; cross-compiling or running under the target Lisp. --
1791 #+sb-xc-host (typep constant 'layout))
1792 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1794 (when (find constant *constants-created-since-last-init* :test #'eq)
1796 (throw 'pending-init circular-ref)))
1797 (multiple-value-bind (creation-form init-form)
1799 (sb!xc:make-load-form constant (make-null-lexenv))
1801 (compiler-error condition)))
1803 (:sb-just-dump-it-normally
1804 (fasl-validate-structure constant *compile-object*)
1809 (let* ((name (write-to-string constant :level 1 :length 2))
1811 (list constant name init-form)
1813 (let ((*constants-being-created*
1814 (cons info *constants-being-created*))
1815 (*constants-created-since-last-init*
1816 (cons constant *constants-created-since-last-init*)))
1819 (fasl-note-handle-for-constant
1821 (compile-load-time-value
1825 (compiler-error "circular references in creation form for ~S"
1828 (let* ((*constants-created-since-last-init* nil)
1830 (catch 'pending-init
1831 (loop for (name form) on (cdr info) by #'cddr
1832 collect name into names
1833 collect form into forms
1834 finally (compile-make-load-form-init-forms forms))
1837 (setf (cdr circular-ref)
1838 (append (cdr circular-ref) (cdr info))))))))))))
1841 ;;;; Host compile time definitions
1843 (defun compile-in-lexenv (name lambda lexenv)
1844 (declare (ignore lexenv))
1845 (compile name lambda))
1848 (defun eval-in-lexenv (form lexenv)
1849 (declare (ignore lexenv))