1 ;;;; the top level interfaces to the compiler, plus some other
2 ;;;; compiler-related stuff (e.g. CL:CALL-ARGUMENTS-LIMIT) which
3 ;;;; doesn't obviously belong anywhere else
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
16 ;;; FIXME: Doesn't this belong somewhere else, like early-c.lisp?
17 (declaim (special *constants* *free-vars* *component-being-compiled*
18 *code-vector* *next-location* *result-fixups*
19 *free-funs* *source-paths*
20 *seen-blocks* *seen-funs* *list-conflicts-table*
21 *continuation-number* *continuation-numbers*
22 *number-continuations* *tn-id* *tn-ids* *id-tns*
23 *label-ids* *label-id* *id-labels*
24 *undefined-warnings* *compiler-error-count*
25 *compiler-warning-count* *compiler-style-warning-count*
27 *compiler-error-bailout*
28 #!+sb-show *compiler-trace-output*
29 *last-source-context* *last-original-source*
30 *last-source-form* *last-format-string* *last-format-args*
31 *last-message-count* *last-error-context*
32 *lexenv* *fun-names-in-this-file*
33 *allow-instrumenting*))
35 ;;; Whether call of a function which cannot be defined causes a full
37 (defvar *flame-on-necessarily-undefined-function* nil)
39 (defvar *check-consistency* nil)
40 (defvar *all-components*)
42 ;;; Set to NIL to disable loop analysis for register allocation.
43 (defvar *loop-analyze* t)
45 ;;; Bind this to a stream to capture various internal debugging output.
46 (defvar *compiler-trace-output* nil)
48 ;;; The current block compilation state. These are initialized to the
49 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
52 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
53 ;;; argument, which overrides any internal declarations.
54 (defvar *block-compile*)
55 (defvar *block-compile-arg*)
56 (declaim (type (member nil t :specified) *block-compile* *block-compile-arg*))
57 (defvar *entry-points*)
58 (declaim (list *entry-points*))
60 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
61 ;;; lambdas resulting from compiling subforms. (In reverse order.)
62 (defvar *toplevel-lambdas*)
63 (declaim (list *toplevel-lambdas*))
65 ;;; The current non-macroexpanded toplevel form as printed when
66 ;;; *compile-print* is true.
67 (defvar *top-level-form-noted* nil)
69 (defvar sb!xc:*compile-verbose* t
71 "The default for the :VERBOSE argument to COMPILE-FILE.")
72 (defvar sb!xc:*compile-print* t
74 "The default for the :PRINT argument to COMPILE-FILE.")
75 (defvar *compile-progress* nil
77 "When this is true, the compiler prints to *STANDARD-OUTPUT* progress
78 information about the phases of compilation of each function. (This
79 is useful mainly in large block compilations.)")
81 (defvar sb!xc:*compile-file-pathname* nil
83 "The defaulted pathname of the file currently being compiled, or NIL if not
85 (defvar sb!xc:*compile-file-truename* nil
87 "The TRUENAME of the file currently being compiled, or NIL if not
90 (declaim (type (or pathname null)
91 sb!xc:*compile-file-pathname*
92 sb!xc:*compile-file-truename*))
94 ;;; the SOURCE-INFO structure for the current compilation. This is
95 ;;; null globally to indicate that we aren't currently in any
96 ;;; identifiable compilation.
97 (defvar *source-info* nil)
99 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
100 ;;; normally causes nested uses to be no-ops).
101 (defvar *in-compilation-unit* nil)
103 ;;; Count of the number of compilation units dynamically enclosed by
104 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
105 (defvar *aborted-compilation-unit-count*)
107 ;;; Mumble conditional on *COMPILE-PROGRESS*.
108 (defun maybe-mumble (&rest foo)
109 (when *compile-progress*
110 (compiler-mumble "~&")
111 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
112 (apply #'compiler-mumble foo))))
114 (deftype object () '(or fasl-output core-object null))
116 (defvar *compile-object* nil)
117 (declaim (type object *compile-object*))
119 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
121 (defmacro sb!xc:with-compilation-unit (options &body body)
123 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
124 This form affects compilations that take place within its dynamic extent. It
125 is intended to be wrapped around the compilation of all files in the same
126 system. These keywords are defined:
128 :OVERRIDE Boolean-Form
129 One of the effects of this form is to delay undefined warnings
130 until the end of the form, instead of giving them at the end of each
131 compilation. If OVERRIDE is NIL (the default), then the outermost
132 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
133 OVERRIDE true causes that form to grab any enclosed warnings, even if
134 it is enclosed by another WITH-COMPILATION-UNIT.
136 :SOURCE-PLIST Plist-Form
137 Attaches the value returned by the Plist-Form to internal debug-source
138 information of functions compiled in within the dynamic contour.
139 Primarily for use by development environments, in order to eg. associate
140 function definitions with editor-buffers. Can be accessed as
141 SB-INTROSPECT:DEFINITION-SOURCE-PLIST. If multiple, nested
142 WITH-COMPILATION-UNITs provide :SOURCE-PLISTs, they are appended
143 togather, innermost left. If Unaffected by :OVERRIDE."
144 `(%with-compilation-unit (lambda () ,@body) ,@options))
146 (defvar *source-plist* nil)
148 (defun %with-compilation-unit (fn &key override source-plist)
149 (declare (type function fn))
150 (let ((succeeded-p nil)
151 (*source-plist* (append source-plist *source-plist*)))
152 (if (and *in-compilation-unit* (not override))
153 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
154 ;; ordinarily (unless OVERRIDE) basically a no-op.
156 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
158 (incf *aborted-compilation-unit-count*)))
159 (let ((*aborted-compilation-unit-count* 0)
160 (*compiler-error-count* 0)
161 (*compiler-warning-count* 0)
162 (*compiler-style-warning-count* 0)
163 (*compiler-note-count* 0)
164 (*undefined-warnings* nil)
165 (*in-compilation-unit* t))
166 (sb!thread:with-recursive-lock (*big-compiler-lock*)
167 (handler-bind ((parse-unknown-type
169 (note-undefined-reference
170 (parse-unknown-type-specifier c)
173 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
175 (incf *aborted-compilation-unit-count*))
176 (summarize-compilation-unit (not succeeded-p)))))))))
178 ;;; Is FUN-NAME something that no conforming program can rely on
179 ;;; defining as a function?
180 (defun fun-name-reserved-by-ansi-p (fun-name)
181 (eq (symbol-package (fun-name-block-name fun-name))
184 ;;; This is to be called at the end of a compilation unit. It signals
185 ;;; any residual warnings about unknown stuff, then prints the total
186 ;;; error counts. ABORT-P should be true when the compilation unit was
187 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
188 ;;; enclosed nested compilation units that were aborted.
189 (defun summarize-compilation-unit (abort-p)
191 (handler-bind ((style-warning #'compiler-style-warning-handler)
192 (warning #'compiler-warning-handler))
194 (let ((undefs (sort *undefined-warnings* #'string<
196 (let ((x (undefined-warning-name x)))
199 (prin1-to-string x)))))))
200 (dolist (undef undefs)
201 (let ((name (undefined-warning-name undef))
202 (kind (undefined-warning-kind undef))
203 (warnings (undefined-warning-warnings undef))
204 (undefined-warning-count (undefined-warning-count undef)))
205 (dolist (*compiler-error-context* warnings)
206 (if #-sb-xc-host (and (eq kind :function)
207 (fun-name-reserved-by-ansi-p name)
208 *flame-on-necessarily-undefined-function*)
213 "~@<There is no function named ~S. References to ~S in ~
214 some contexts (like starts of blocks) have special ~
215 meaning, but here it would have to be a function, ~
216 and that shouldn't be right.~:@>"
220 "~@<The ~(~A~) ~S is undefined, and its name is ~
221 reserved by ANSI CL so that even if it were ~
222 defined later, the code doing so would not be ~
225 (if (eq kind :variable)
226 (compiler-warn "undefined ~(~A~): ~S" kind name)
227 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
228 (let ((warn-count (length warnings)))
229 (when (and warnings (> undefined-warning-count warn-count))
230 (let ((more (- undefined-warning-count warn-count)))
231 (if (eq kind :variable)
233 "~W more use~:P of undefined ~(~A~) ~S"
236 "~W more use~:P of undefined ~(~A~) ~S"
237 more kind name)))))))
239 (dolist (kind '(:variable :function :type))
240 (let ((summary (mapcar #'undefined-warning-name
241 (remove kind undefs :test #'neq
242 :key #'undefined-warning-kind))))
244 (if (eq kind :variable)
246 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
247 ~% ~{~<~% ~1:;~S~>~^ ~}"
248 (cdr summary) kind summary)
250 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
251 ~% ~{~<~% ~1:;~S~>~^ ~}"
252 (cdr summary) kind summary))))))))
254 (unless (and (not abort-p)
255 (zerop *aborted-compilation-unit-count*)
256 (zerop *compiler-error-count*)
257 (zerop *compiler-warning-count*)
258 (zerop *compiler-style-warning-count*)
259 (zerop *compiler-note-count*))
260 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
261 (format *error-output* "~&compilation unit ~:[finished~;aborted~]~
262 ~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
263 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
264 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
265 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
266 ~[~:;~:*~& printed ~W note~:P~]"
268 *aborted-compilation-unit-count*
269 *compiler-error-count*
270 *compiler-warning-count*
271 *compiler-style-warning-count*
272 *compiler-note-count*))
273 (terpri *error-output*)
274 (force-output *error-output*)))
276 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
277 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
278 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
279 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
280 (defmacro with-compilation-values (&body body)
282 (let ((*warnings-p* nil)
284 (values (progn ,@body)
288 ;;;; component compilation
290 (defparameter *max-optimize-iterations* 3 ; ARB
292 "The upper limit on the number of times that we will consecutively do IR1
293 optimization that doesn't introduce any new code. A finite limit is
294 necessary, since type inference may take arbitrarily long to converge.")
296 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
297 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
299 ;;; Repeatedly optimize COMPONENT until no further optimizations can
300 ;;; be found or we hit our iteration limit. When we hit the limit, we
301 ;;; clear the component and block REOPTIMIZE flags to discourage the
302 ;;; next optimization attempt from pounding on the same code.
303 (defun ir1-optimize-until-done (component)
304 (declare (type component component))
306 (event ir1-optimize-until-done)
308 (cleared-reanalyze nil)
311 (when (component-reanalyze component)
313 (setq cleared-reanalyze t)
314 (setf (component-reanalyze component) nil))
315 (setf (component-reoptimize component) nil)
316 (ir1-optimize component fastp)
317 (cond ((component-reoptimize component)
319 (when (and (>= count *max-optimize-iterations*)
320 (not (component-reanalyze component))
321 (eq (component-reoptimize component) :maybe))
323 (cond ((retry-delayed-ir1-transforms :optimize)
327 (event ir1-optimize-maxed-out)
328 (setf (component-reoptimize component) nil)
329 (do-blocks (block component)
330 (setf (block-reoptimize block) nil))
332 ((retry-delayed-ir1-transforms :optimize)
338 (setq fastp (>= count *max-optimize-iterations*))
339 (maybe-mumble (if fastp "-" ".")))
340 (when cleared-reanalyze
341 (setf (component-reanalyze component) t)))
344 (defparameter *constraint-propagate* t)
346 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
347 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
348 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
349 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
351 ;;; FIXME: It would be good to document why it's important to have a
352 ;;; large value here, and what the drawbacks of an excessively large
353 ;;; value are; and it might also be good to make it depend on
354 ;;; optimization policy.
355 (defparameter *reoptimize-after-type-check-max* 10)
357 (defevent reoptimize-maxed-out
358 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
360 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
361 (defun dfo-as-needed (component)
362 (declare (type component component))
363 (when (component-reanalyze component)
367 (unless (component-reanalyze component)
373 ;;; Do all the IR1 phases for a non-top-level component.
374 (defun ir1-phases (component)
375 (declare (type component component))
376 (aver-live-component component)
377 (let ((*constraint-number* 0)
379 (*delayed-ir1-transforms* nil))
380 (declare (special *constraint-number* *delayed-ir1-transforms*))
382 (ir1-optimize-until-done component)
383 (when (or (component-new-functionals component)
384 (component-reanalyze-functionals component))
385 (maybe-mumble "locall ")
386 (locall-analyze-component component))
387 (dfo-as-needed component)
388 (when *constraint-propagate*
389 (maybe-mumble "constraint ")
390 (constraint-propagate component))
391 (when (retry-delayed-ir1-transforms :constraint)
392 (maybe-mumble "Rtran "))
393 (flet ((want-reoptimization-p ()
394 (or (component-reoptimize component)
395 (component-reanalyze component)
396 (component-new-functionals component)
397 (component-reanalyze-functionals component))))
398 (unless (and (want-reoptimization-p)
399 ;; We delay the generation of type checks until
400 ;; the type constraints have had time to
401 ;; propagate, else the compiler can confuse itself.
402 (< loop-count (- *reoptimize-after-type-check-max* 4)))
403 (maybe-mumble "type ")
404 (generate-type-checks component)
405 (unless (want-reoptimization-p)
407 (when (>= loop-count *reoptimize-after-type-check-max*)
408 (maybe-mumble "[reoptimize limit]")
409 (event reoptimize-maxed-out)
413 (ir1-finalize component)
416 (defun %compile-component (component)
417 (let ((*code-segment* nil)
419 (maybe-mumble "GTN ")
420 (gtn-analyze component)
421 (maybe-mumble "LTN ")
422 (ltn-analyze component)
423 (dfo-as-needed component)
424 (maybe-mumble "control ")
425 (control-analyze component #'make-ir2-block)
427 (when (or (ir2-component-values-receivers (component-info component))
428 (component-dx-lvars component))
429 (maybe-mumble "stack ")
430 (stack-analyze component)
431 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
432 ;; stack analysis. There shouldn't be any unreachable code after
433 ;; control, so this won't delete anything.
434 (dfo-as-needed component))
438 (maybe-mumble "IR2tran ")
440 (entry-analyze component)
441 (ir2-convert component)
443 (when (policy *lexenv* (>= speed compilation-speed))
444 (maybe-mumble "copy ")
445 (copy-propagate component))
447 (select-representations component)
449 (when *check-consistency*
450 (maybe-mumble "check2 ")
451 (check-ir2-consistency component))
453 (delete-unreferenced-tns component)
455 (maybe-mumble "life ")
456 (lifetime-analyze component)
458 (when *compile-progress*
459 (compiler-mumble "") ; Sync before doing more output.
460 (pre-pack-tn-stats component *standard-output*))
462 (when *check-consistency*
463 (maybe-mumble "check-life ")
464 (check-life-consistency component))
466 (maybe-mumble "pack ")
469 (when *check-consistency*
470 (maybe-mumble "check-pack ")
471 (check-pack-consistency component))
473 (when *compiler-trace-output*
474 (describe-component component *compiler-trace-output*)
475 (describe-ir2-component component *compiler-trace-output*))
477 (maybe-mumble "code ")
478 (multiple-value-bind (code-length trace-table fixup-notes)
479 (generate-code component)
482 (when *compiler-trace-output*
483 (format *compiler-trace-output*
484 "~|~%disassembly of code for ~S~2%" component)
485 (sb!disassem:disassemble-assem-segment *code-segment*
486 *compiler-trace-output*))
488 (etypecase *compile-object*
490 (maybe-mumble "fasl")
491 (fasl-dump-component component
498 (maybe-mumble "core")
499 (make-core-component component
507 ;; We're done, so don't bother keeping anything around.
508 (setf (component-info component) :dead)
512 ;;; Delete components with no external entry points before we try to
513 ;;; generate code. Unreachable closures can cause IR2 conversion to
514 ;;; puke on itself, since it is the reference to the closure which
515 ;;; normally causes the components to be combined.
516 (defun delete-if-no-entries (component)
517 (dolist (fun (component-lambdas component) (delete-component component))
518 (when (functional-has-external-references-p fun)
520 (case (functional-kind fun)
523 (unless (every (lambda (ref)
524 (eq (node-component ref) component))
528 (defun compile-component (component)
530 ;; miscellaneous sanity checks
532 ;; FIXME: These are basically pretty wimpy compared to the checks done
533 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
534 ;; make those internal consistency checks work again and use them.
535 (aver-live-component component)
536 (do-blocks (block component)
537 (aver (eql (block-component block) component)))
538 (dolist (lambda (component-lambdas component))
539 ;; sanity check to prevent weirdness from propagating insidiously as
540 ;; far from its root cause as it did in bug 138: Make sure that
541 ;; thing-to-COMPONENT links are consistent.
542 (aver (eql (lambda-component lambda) component))
543 (aver (eql (node-component (lambda-bind lambda)) component)))
545 (let* ((*component-being-compiled* component))
547 (ir1-phases component)
550 (dfo-as-needed component)
551 (find-dominators component)
552 (loop-analyze component))
555 (when (and *loop-analyze* *compiler-trace-output*)
556 (labels ((print-blocks (block)
557 (format *compiler-trace-output* " ~A~%" block)
558 (when (block-loop-next block)
559 (print-blocks (block-loop-next block))))
561 (format *compiler-trace-output* "loop=~A~%" loop)
562 (print-blocks (loop-blocks loop))
563 (dolist (l (loop-inferiors loop))
565 (print-loop (component-outer-loop component))))
568 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
569 (maybe-mumble "env ")
570 (physenv-analyze component)
571 (dfo-as-needed component)
573 (delete-if-no-entries component)
575 (unless (eq (block-next (component-head component))
576 (component-tail component))
577 (%compile-component component)))
579 (clear-constant-info)
583 ;;;; clearing global data structures
585 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
586 ;;;; global data structures entirely when possible and consing up the
587 ;;;; others from scratch instead of clearing and reusing them?
589 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
590 ;;; addition to allowing stuff to be reclaimed, this is required for
591 ;;; correct assignment of constant offsets, since we need to assign a
592 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
593 ;;; slots, since they are used to keep track of functions across
594 ;;; component boundaries.
595 (defun clear-constant-info ()
596 (maphash (lambda (k v)
598 (setf (leaf-info v) nil))
600 (maphash (lambda (k v)
603 (setf (leaf-info v) nil)))
607 ;;; Blow away the REFS for all global variables, and let COMPONENT
609 (defun clear-ir1-info (component)
610 (declare (type component component))
612 (maphash (lambda (k v)
616 (delete-if #'here-p (leaf-refs v)))
617 (when (basic-var-p v)
618 (setf (basic-var-sets v)
619 (delete-if #'here-p (basic-var-sets v))))))
622 (eq (node-component x) component)))
628 ;;; Clear global variables used by the compiler.
630 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
631 ;;; and it adds overhead even when people aren't using the compiler.
632 ;;; Perhaps we could make these global vars unbound except when
633 ;;; actually in use, so that this function could go away.
634 (defun clear-stuff (&optional (debug-too t))
636 ;; Clear global tables.
637 (when (boundp '*free-funs*)
638 (clrhash *free-funs*)
639 (clrhash *free-vars*)
640 (clrhash *constants*))
642 ;; Clear debug counters and tables.
643 (clrhash *seen-blocks*)
644 (clrhash *seen-funs*)
645 (clrhash *list-conflicts-table*)
648 (clrhash *continuation-numbers*)
649 (clrhash *number-continuations*)
650 (setq *continuation-number* 0)
654 (clrhash *label-ids*)
655 (clrhash *id-labels*)
658 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
659 ;; Superficially, this seemed harmful -- the user could reasonably be
660 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
661 ;; compiled. A closer inspection showed that this actually turned out to be
662 ;; harmless in practice, because CLEAR-STUFF was only called from within
663 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
664 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
665 ;; practice, it was also useless in practice. So we don't do it any more.)
671 ;;; Print out some useful info about COMPONENT to STREAM.
672 (defun describe-component (component *standard-output*)
673 (declare (type component component))
674 (format t "~|~%;;;; component: ~S~2%" (component-name component))
675 (print-all-blocks component)
678 (defun describe-ir2-component (component *standard-output*)
679 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
680 (format t "entries:~%")
681 (dolist (entry (ir2-component-entries (component-info component)))
682 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
683 (label-id (entry-info-offset entry))
684 (entry-info-name entry)
685 (entry-info-closure-tn entry)))
687 (pre-pack-tn-stats component *standard-output*)
689 (print-ir2-blocks component)
695 ;;;; When reading from a file, we have to keep track of some source
696 ;;;; information. We also exploit our ability to back up for printing
697 ;;;; the error context and for recovering from errors.
699 ;;;; The interface we provide to this stuff is the stream-oid
700 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
701 ;;;; of getting the next source form.
703 ;;; A FILE-INFO structure holds all the source information for a
705 (def!struct (file-info (:copier nil))
706 ;; If a file, the truename of the corresponding source file. If from
707 ;; a Lisp form, :LISP. If from a stream, :STREAM.
708 (name (missing-arg) :type (or pathname (member :lisp :stream)))
709 ;; the external format that we'll call OPEN with, if NAME is a file.
710 (external-format nil)
711 ;; the defaulted, but not necessarily absolute file name (i.e. prior
712 ;; to TRUENAME call.) Null if not a file. This is used to set
713 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
715 (untruename nil :type (or pathname null))
716 ;; the file's write date (if relevant)
717 (write-date nil :type (or unsigned-byte null))
718 ;; the source path root number of the first form in this file (i.e.
719 ;; the total number of forms converted previously in this
721 (source-root 0 :type unsigned-byte)
722 ;; parallel vectors containing the forms read out of the file and
723 ;; the file positions that reading of each form started at (i.e. the
724 ;; end of the previous form)
725 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
726 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
728 ;;; The SOURCE-INFO structure provides a handle on all the source
729 ;;; information for an entire compilation.
730 (def!struct (source-info
731 #-no-ansi-print-object
732 (:print-object (lambda (s stream)
733 (print-unreadable-object (s stream :type t))))
735 ;; the UT that compilation started at
736 (start-time (get-universal-time) :type unsigned-byte)
737 ;; the FILE-INFO structure for this compilation
738 (file-info nil :type (or file-info null))
739 ;; the stream that we are using to read the FILE-INFO, or NIL if
740 ;; no stream has been opened yet
741 (stream nil :type (or stream null)))
743 ;;; Given a pathname, return a SOURCE-INFO structure.
744 (defun make-file-source-info (file external-format)
745 (let ((file-info (make-file-info :name (truename file)
746 :untruename (merge-pathnames file)
747 :external-format external-format
748 :write-date (file-write-date file))))
750 (make-source-info :file-info file-info)))
752 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
753 (defun make-lisp-source-info (form)
754 (make-source-info :start-time (get-universal-time)
755 :file-info (make-file-info :name :lisp
759 ;;; Return a SOURCE-INFO which will read from STREAM.
760 (defun make-stream-source-info (stream)
761 (let ((file-info (make-file-info :name :stream)))
762 (make-source-info :file-info file-info
765 ;;; Return a form read from STREAM; or for EOF use the trick,
766 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
767 ;;; error happens, then convert it to standard abort-the-compilation
768 ;;; error condition (possibly recording some extra location
770 (defun read-for-compile-file (stream position)
771 (handler-case (read stream nil stream)
772 (reader-error (condition)
773 (error 'input-error-in-compile-file
775 ;; We don't need to supply :POSITION here because
776 ;; READER-ERRORs already know their position in the file.
778 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
779 ;; (and that this is not a READER-ERROR) when it encounters end of
780 ;; file in the middle of something it's trying to read.
781 (end-of-file (condition)
782 (error 'input-error-in-compile-file
784 ;; We need to supply :POSITION here because the END-OF-FILE
785 ;; condition doesn't carry the position that the user
786 ;; probably cares about, where the failed READ began.
787 :position position))))
789 ;;; If STREAM is present, return it, otherwise open a stream to the
790 ;;; current file. There must be a current file.
792 ;;; FIXME: This is probably an unnecessarily roundabout way to do
793 ;;; things now that we process a single file in COMPILE-FILE (unlike
794 ;;; the old CMU CL code, which accepted multiple files). Also, the old
796 ;;; When we open a new file, we also reset *PACKAGE* and policy.
797 ;;; This gives the effect of rebinding around each file.
798 ;;; which doesn't seem to be true now. Check to make sure that if
799 ;;; such rebinding is necessary, it's still done somewhere.
800 (defun get-source-stream (info)
801 (declare (type source-info info))
802 (or (source-info-stream info)
803 (let* ((file-info (source-info-file-info info))
804 (name (file-info-name file-info))
805 (external-format (file-info-external-format file-info)))
806 (setf sb!xc:*compile-file-truename* name
807 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
808 (source-info-stream info)
809 (open name :direction :input
810 :external-format external-format)))))
812 ;;; Close the stream in INFO if it is open.
813 (defun close-source-info (info)
814 (declare (type source-info info))
815 (let ((stream (source-info-stream info)))
816 (when stream (close stream)))
817 (setf (source-info-stream info) nil)
820 ;;; Read and compile the source file.
821 (defun sub-sub-compile-file (info)
822 (let* ((file-info (source-info-file-info info))
823 (stream (get-source-stream info)))
825 (let* ((pos (file-position stream))
826 (form (read-for-compile-file stream pos)))
827 (if (eq form stream) ; i.e., if EOF
829 (let* ((forms (file-info-forms file-info))
830 (current-idx (+ (fill-pointer forms)
831 (file-info-source-root file-info))))
832 (vector-push-extend form forms)
833 (vector-push-extend pos (file-info-positions file-info))
834 (find-source-paths form current-idx)
835 (process-toplevel-form form
836 `(original-source-start 0 ,current-idx)
839 ;;; Return the INDEX'th source form read from INFO and the position
840 ;;; where it was read.
841 (defun find-source-root (index info)
842 (declare (type index index) (type source-info info))
843 (let ((file-info (source-info-file-info info)))
844 (values (aref (file-info-forms file-info) index)
845 (aref (file-info-positions file-info) index))))
847 ;;;; processing of top level forms
849 ;;; This is called by top level form processing when we are ready to
850 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
851 ;;; convert the form, but delay compilation, pushing the result on
852 ;;; *TOPLEVEL-LAMBDAS* instead.
853 (defun convert-and-maybe-compile (form path)
854 (declare (list path))
855 (let* ((*top-level-form-noted* (note-top-level-form form t))
856 (*lexenv* (make-lexenv
858 :handled-conditions *handled-conditions*
859 :disabled-package-locks *disabled-package-locks*))
860 (tll (ir1-toplevel form path nil)))
861 (if (eq *block-compile* t)
862 (push tll *toplevel-lambdas*)
863 (compile-toplevel (list tll) nil))
866 ;;; Macroexpand FORM in the current environment with an error handler.
867 ;;; We only expand one level, so that we retain all the intervening
868 ;;; forms in the source path.
869 (defun preprocessor-macroexpand-1 (form)
870 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
872 (compiler-error "(during macroexpansion of ~A)~%~A"
873 (let ((*print-level* 2)
875 (format nil "~S" form))
878 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
879 ;;; the forms, and PATH is the source path of the FORM they came out of.
880 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
881 (defun process-toplevel-progn (forms path compile-time-too)
882 (declare (list forms) (list path))
884 (process-toplevel-form form path compile-time-too)))
886 ;;; Process a top level use of LOCALLY, or anything else (e.g.
887 ;;; MACROLET) at top level which has declarations and ordinary forms.
888 ;;; We parse declarations and then recursively process the body.
889 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
890 (declare (list path))
891 (multiple-value-bind (forms decls)
892 (parse-body body :doc-string-allowed nil :toplevel t)
893 (let* ((*lexenv* (process-decls decls vars funs))
894 ;; FIXME: VALUES declaration
896 ;; Binding *POLICY* is pretty much of a hack, since it
897 ;; causes LOCALLY to "capture" enclosed proclamations. It
898 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
899 ;; value of *POLICY* as the policy. The need for this hack
900 ;; is due to the quirk that there is no way to represent in
901 ;; a POLICY that an optimize quality came from the default.
903 ;; FIXME: Ideally, something should be done so that DECLAIM
904 ;; inside LOCALLY works OK. Failing that, at least we could
905 ;; issue a warning instead of silently screwing up.
906 (*policy* (lexenv-policy *lexenv*))
907 ;; This is probably also a hack
908 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
910 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*)))
911 (process-toplevel-progn forms path compile-time-too))))
913 ;;; Parse an EVAL-WHEN situations list, returning three flags,
914 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
915 ;;; the types of situations present in the list.
916 (defun parse-eval-when-situations (situations)
917 (when (or (not (listp situations))
918 (set-difference situations
925 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
926 (let ((deprecated-names (intersection situations '(compile load eval))))
927 (when deprecated-names
928 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
930 (values (intersection '(:compile-toplevel compile)
932 (intersection '(:load-toplevel load) situations)
933 (intersection '(:execute eval) situations)))
936 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
937 (defun functional-components (f)
938 (declare (type functional f))
940 (clambda (list (lambda-component f)))
941 (optional-dispatch (let ((result nil))
942 (flet ((maybe-frob (maybe-clambda)
943 (when (and maybe-clambda
944 (promise-ready-p maybe-clambda))
945 (pushnew (lambda-component
946 (force maybe-clambda))
948 (map nil #'maybe-frob (optional-dispatch-entry-points f))
949 (maybe-frob (optional-dispatch-more-entry f))
950 (maybe-frob (optional-dispatch-main-entry f)))
953 (defun make-functional-from-toplevel-lambda (definition
957 ;; I'd thought NIL should
958 ;; work, but it doesn't.
961 (let* ((*current-path* path)
962 (component (make-empty-component))
963 (*current-component* component))
964 (setf (component-name component)
965 (debug-name 'initial-component name))
966 (setf (component-kind component) :initial)
967 (let* ((locall-fun (let ((*allow-instrumenting* t))
968 (apply #'ir1-convert-lambdalike
970 (list :source-name name))))
971 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
972 :source-name (or name '.anonymous.)
973 :debug-name (debug-name 'tl-xep name))))
975 (assert-global-function-definition-type name locall-fun))
976 (setf (functional-entry-fun fun) locall-fun
977 (functional-kind fun) :external
978 (functional-has-external-references-p fun) t)
981 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
982 ;;; description of the result.
983 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
984 ;;; into core and return the compiled FUNCTION value.
985 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
986 ;;; into the fasl file and return a dump handle.
988 ;;; If NAME is provided, then we try to use it as the name of the
989 ;;; function for debugging/diagnostic information.
990 (defun %compile (lambda-expression
995 ;; This magical idiom seems to be the appropriate
996 ;; path for compiling standalone LAMBDAs, judging
997 ;; from the CMU CL code and experiment, so it's a
998 ;; nice default for things where we don't have a
999 ;; real source path (as in e.g. inside CL:COMPILE).
1000 '(original-source-start 0 0)))
1002 (legal-fun-name-or-type-error name))
1003 (let* ((*lexenv* (make-lexenv
1005 :handled-conditions *handled-conditions*
1006 :disabled-package-locks *disabled-package-locks*))
1007 (fun (make-functional-from-toplevel-lambda lambda-expression
1011 ;; FIXME: The compile-it code from here on is sort of a
1012 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1013 ;; better to find a way to share the code there; or
1014 ;; alternatively, to use this code to replace the code there.
1015 ;; (The second alternative might be pretty easy if we used
1016 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1017 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1019 (locall-analyze-clambdas-until-done (list fun))
1021 (multiple-value-bind (components-from-dfo top-components hairy-top)
1022 (find-initial-dfo (list fun))
1023 (declare (ignore hairy-top))
1025 (let ((*all-components* (append components-from-dfo top-components)))
1026 (dolist (component-from-dfo components-from-dfo)
1027 (compile-component component-from-dfo)
1028 (replace-toplevel-xeps component-from-dfo)))
1030 (let ((entry-table (etypecase *compile-object*
1031 (fasl-output (fasl-output-entry-table
1033 (core-object (core-object-entry-table
1034 *compile-object*)))))
1035 (multiple-value-bind (result found-p)
1036 (gethash (leaf-info fun) entry-table)
1040 ;; KLUDGE: This code duplicates some other code in this
1041 ;; file. In the great reorganzation, the flow of program
1042 ;; logic changed from the original CMUCL model, and that
1043 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1044 ;; longer followed for CORE-OBJECTS, leading to BUG
1045 ;; 156. This place is transparently not the right one for
1046 ;; this code, but I don't have a clear enough overview of
1047 ;; the compiler to know how to rearrange it all so that
1048 ;; this operation fits in nicely, and it was blocking
1049 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1050 ;; ((..)) (DEFUN FOO ...))
1052 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1053 ;; ideal way, as (1) definitions typed in at the REPL
1054 ;; without an INLINE declaration will give a NULL
1055 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1056 ;; and (2) INLINE declarations will yield a
1057 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1058 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1061 ;; (2) is probably fairly easy to fix -- it is, after all,
1062 ;; a matter of list manipulation (or possibly of teaching
1063 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1064 ;; significantly harder, as the association between
1065 ;; function object and source is a tricky one.
1067 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1068 ;; non-NULL list) when the function in question has been
1069 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1070 ;; work when it has been compiled as part of the top-level
1071 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1072 ;; ...). -- CSR, 2002-11-02
1073 (when (core-object-p *compile-object*)
1074 (fix-core-source-info *source-info* *compile-object* result))
1076 (mapc #'clear-ir1-info components-from-dfo)
1079 (defun process-toplevel-cold-fset (name lambda-expression path)
1080 (unless (producing-fasl-file)
1081 (error "can't COLD-FSET except in a fasl file"))
1082 (legal-fun-name-or-type-error name)
1083 (fasl-dump-cold-fset name
1084 (%compile lambda-expression
1091 (defun note-top-level-form (form &optional finalp)
1092 (when *compile-print*
1093 (cond ((not *top-level-form-noted*)
1094 (let ((*print-length* 2)
1096 (*print-pretty* nil))
1097 (with-compiler-io-syntax
1098 (compiler-mumble "~&; ~:[compiling~;converting~] ~S"
1099 *block-compile* form)))
1102 (eq :top-level-forms *compile-print*)
1103 (neq form *top-level-form-noted*))
1104 (let ((*print-length* 1)
1106 (*print-pretty* nil))
1107 (with-compiler-io-syntax
1108 (compiler-mumble "~&; ... top level ~S" form)))
1111 *top-level-form-noted*))))
1113 ;;; Process a top level FORM with the specified source PATH.
1114 ;;; * If this is a magic top level form, then do stuff.
1115 ;;; * If this is a macro, then expand it.
1116 ;;; * Otherwise, just compile it.
1118 ;;; COMPILE-TIME-TOO is as defined in ANSI
1119 ;;; "3.2.3.1 Processing of Top Level Forms".
1120 (defun process-toplevel-form (form path compile-time-too)
1121 (declare (list path))
1123 (catch 'process-toplevel-form-error-abort
1124 (let* ((path (or (gethash form *source-paths*) (cons form path)))
1125 (*compiler-error-bailout*
1126 (lambda (&optional condition)
1127 (convert-and-maybe-compile
1128 (make-compiler-error-form condition form)
1130 (throw 'process-toplevel-form-error-abort nil))))
1132 (flet ((default-processor (form)
1133 (let ((*top-level-form-noted* (note-top-level-form form)))
1134 ;; When we're cross-compiling, consider: what should we
1135 ;; do when we hit e.g.
1136 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1137 ;; (DEFUN FOO (X) (+ 7 X)))?
1138 ;; DEFUN has a macro definition in the cross-compiler,
1139 ;; and a different macro definition in the target
1140 ;; compiler. The only sensible thing is to use the
1141 ;; target compiler's macro definition, since the
1142 ;; cross-compiler's macro is in general into target
1143 ;; functions which can't meaningfully be executed at
1144 ;; cross-compilation time. So make sure we do the EVAL
1145 ;; here, before we macroexpand.
1147 ;; Then things get even dicier with something like
1148 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1149 ;; where we have to make sure that we don't uncross
1150 ;; the SB!XC: prefix before we do EVAL, because otherwise
1151 ;; we'd be trying to redefine the cross-compilation host's
1154 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1157 (when compile-time-too
1158 (eval form)) ; letting xc host EVAL do its own macroexpansion
1159 (let* (;; (We uncross the operator name because things
1160 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1161 ;; should be equivalent to their CL: counterparts
1162 ;; when being compiled as target code. We leave
1163 ;; the rest of the form uncrossed because macros
1164 ;; might yet expand into EVAL-WHEN stuff, and
1165 ;; things inside EVAL-WHEN can't be uncrossed
1166 ;; until after we've EVALed them in the
1167 ;; cross-compilation host.)
1168 (slightly-uncrossed (cons (uncross (first form))
1170 (expanded (preprocessor-macroexpand-1
1171 slightly-uncrossed)))
1172 (if (eq expanded slightly-uncrossed)
1173 ;; (Now that we're no longer processing toplevel
1174 ;; forms, and hence no longer need to worry about
1175 ;; EVAL-WHEN, we can uncross everything.)
1176 (convert-and-maybe-compile expanded path)
1177 ;; (We have to demote COMPILE-TIME-TOO to NIL
1178 ;; here, no matter what it was before, since
1179 ;; otherwise we'd tend to EVAL subforms more than
1180 ;; once, because of WHEN COMPILE-TIME-TOO form
1182 (process-toplevel-form expanded path nil))))
1183 ;; When we're not cross-compiling, we only need to
1184 ;; macroexpand once, so we can follow the 1-thru-6
1185 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1186 ;; Top Level Forms".
1188 (let ((expanded (preprocessor-macroexpand-1 form)))
1189 (cond ((eq expanded form)
1190 (when compile-time-too
1191 (eval-in-lexenv form *lexenv*))
1192 (convert-and-maybe-compile form path))
1194 (process-toplevel-form expanded
1196 compile-time-too)))))))
1199 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1200 ;; (1) SBCL gets smart enough to handle global
1201 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1202 ;; implementors start using symbol macros in a way which
1203 ;; interacts with SB-XC/CL distinction.)
1204 (convert-and-maybe-compile form path)
1206 (default-processor form)
1207 (flet ((need-at-least-one-arg (form)
1209 (compiler-error "~S form is too short: ~S"
1213 ;; In the cross-compiler, top level COLD-FSET arranges
1214 ;; for static linking at cold init time.
1217 (aver (not compile-time-too))
1218 (destructuring-bind (cold-fset fun-name lambda-expression) form
1219 (declare (ignore cold-fset))
1220 (process-toplevel-cold-fset fun-name
1223 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1224 (need-at-least-one-arg form)
1225 (destructuring-bind (special-operator magic &rest body) form
1226 (ecase special-operator
1228 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1229 ;; "3.2.3.1 Processing of Top Level Forms".
1230 (multiple-value-bind (ct lt e)
1231 (parse-eval-when-situations magic)
1232 (let ((new-compile-time-too (or ct
1233 (and compile-time-too
1235 (cond (lt (process-toplevel-progn
1236 body path new-compile-time-too))
1237 (new-compile-time-too (eval-in-lexenv
1241 (funcall-in-macrolet-lexenv
1243 (lambda (&key funs prepend)
1244 (declare (ignore funs))
1245 (aver (null prepend))
1246 (process-toplevel-locally body
1251 (funcall-in-symbol-macrolet-lexenv
1253 (lambda (&key vars prepend)
1254 (aver (null prepend))
1255 (process-toplevel-locally body
1261 (process-toplevel-locally (rest form) path compile-time-too))
1263 (process-toplevel-progn (rest form) path compile-time-too))
1264 (t (default-processor form))))))))
1268 ;;;; load time value support
1270 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1272 ;;; Return T if we are currently producing a fasl file and hence
1273 ;;; constants need to be dumped carefully.
1274 (defun producing-fasl-file ()
1275 (fasl-output-p *compile-object*))
1277 ;;; Compile FORM and arrange for it to be called at load-time. Return
1278 ;;; the dumper handle and our best guess at the type of the object.
1279 (defun compile-load-time-value (form)
1280 (let ((lambda (compile-load-time-stuff form t)))
1282 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1283 (let ((type (leaf-type lambda)))
1284 (if (fun-type-p type)
1285 (single-value-type (fun-type-returns type))
1288 ;;; Compile the FORMS and arrange for them to be called (for effect,
1289 ;;; not value) at load time.
1290 (defun compile-make-load-form-init-forms (forms)
1291 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1292 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1294 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1295 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1296 (defun compile-load-time-stuff (form for-value)
1298 (let* ((*lexenv* (make-null-lexenv))
1299 (lambda (ir1-toplevel form *current-path* for-value)))
1300 (compile-toplevel (list lambda) t)
1303 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1304 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1305 ;;; don't try to combine this component with anything else and frob
1306 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1307 ;;; compiling, because it was merged with a run-time component.
1308 (defun compile-load-time-value-lambda (lambdas)
1309 (aver (null (cdr lambdas)))
1310 (let* ((lambda (car lambdas))
1311 (component (lambda-component lambda)))
1312 (when (eql (component-kind component) :toplevel)
1313 (setf (component-name component) (leaf-debug-name lambda))
1314 (compile-component component)
1315 (clear-ir1-info component))))
1319 (defun object-call-toplevel-lambda (tll)
1320 (declare (type functional tll))
1321 (let ((object *compile-object*))
1323 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1324 (core-object (core-call-toplevel-lambda tll object))
1327 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1328 ;;; the resulting function to be called.
1329 (defun sub-compile-toplevel-lambdas (lambdas)
1330 (declare (list lambdas))
1332 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1333 (compile-component component)
1334 (clear-ir1-info component)
1335 (object-call-toplevel-lambda tll)))
1338 ;;; Compile top level code and call the top level lambdas. We pick off
1339 ;;; top level lambdas in non-top-level components here, calling
1340 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1341 (defun compile-toplevel-lambdas (lambdas)
1342 (declare (list lambdas))
1343 (let ((len (length lambdas)))
1344 (flet ((loser (start)
1345 (or (position-if (lambda (x)
1346 (not (eq (component-kind
1347 (node-component (lambda-bind x)))
1350 ;; this used to read ":start start", but
1351 ;; start can be greater than len, which
1352 ;; is an error according to ANSI - CSR,
1354 :start (min start len))
1356 (do* ((start 0 (1+ loser))
1357 (loser (loser start) (loser start)))
1359 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1360 (unless (= loser len)
1361 (object-call-toplevel-lambda (elt lambdas loser))))))
1364 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1367 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1368 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1369 (defun compile-toplevel (lambdas load-time-value-p)
1370 (declare (list lambdas))
1372 (maybe-mumble "locall ")
1373 (locall-analyze-clambdas-until-done lambdas)
1375 (maybe-mumble "IDFO ")
1376 (multiple-value-bind (components top-components hairy-top)
1377 (find-initial-dfo lambdas)
1378 (let ((*all-components* (append components top-components)))
1379 (when *check-consistency*
1380 (maybe-mumble "[check]~%")
1381 (check-ir1-consistency *all-components*))
1383 (dolist (component (append hairy-top top-components))
1384 (pre-physenv-analyze-toplevel component))
1386 (dolist (component components)
1387 (compile-component component)
1388 (replace-toplevel-xeps component))
1390 (when *check-consistency*
1391 (maybe-mumble "[check]~%")
1392 (check-ir1-consistency *all-components*))
1394 (if load-time-value-p
1395 (compile-load-time-value-lambda lambdas)
1396 (compile-toplevel-lambdas lambdas))
1398 (mapc #'clear-ir1-info components)
1402 ;;; Actually compile any stuff that has been queued up for block
1404 (defun finish-block-compilation ()
1405 (when *block-compile*
1406 (when *compile-print*
1407 (compiler-mumble "~&; block compiling converted top level forms..."))
1408 (when *toplevel-lambdas*
1409 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1410 (setq *toplevel-lambdas* ()))
1411 (setq *block-compile* nil)
1412 (setq *entry-points* nil)))
1414 (defun handle-condition-p (condition)
1416 (etypecase *compiler-error-context*
1418 (node-lexenv *compiler-error-context*))
1419 (compiler-error-context
1420 (let ((lexenv (compiler-error-context-lexenv
1421 *compiler-error-context*)))
1425 (let ((muffles (lexenv-handled-conditions lexenv)))
1426 (if (null muffles) ; common case
1428 (dolist (muffle muffles nil)
1429 (destructuring-bind (typespec . restart-name) muffle
1430 (when (and (typep condition typespec)
1431 (find-restart restart-name condition))
1434 (defun handle-condition-handler (condition)
1436 (etypecase *compiler-error-context*
1438 (node-lexenv *compiler-error-context*))
1439 (compiler-error-context
1440 (let ((lexenv (compiler-error-context-lexenv
1441 *compiler-error-context*)))
1445 (let ((muffles (lexenv-handled-conditions lexenv)))
1447 (dolist (muffle muffles (bug "fell through"))
1448 (destructuring-bind (typespec . restart-name) muffle
1449 (when (typep condition typespec)
1450 (awhen (find-restart restart-name condition)
1451 (invoke-restart it))))))))
1453 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1454 ;;; Return (VALUES NIL WARNINGS-P FAILURE-P).
1455 (defun sub-compile-file (info)
1456 (declare (type source-info info))
1457 (let ((*package* (sane-package))
1458 (*readtable* *readtable*)
1459 (sb!xc:*compile-file-pathname* nil) ; really bound in
1460 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1462 (*handled-conditions* *handled-conditions*)
1463 (*disabled-package-locks* *disabled-package-locks*)
1464 (*lexenv* (make-null-lexenv))
1465 (*block-compile* *block-compile-arg*)
1466 (*source-info* info)
1467 (*toplevel-lambdas* ())
1468 (*fun-names-in-this-file* ())
1469 (*allow-instrumenting* nil)
1470 (*compiler-error-bailout*
1472 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1473 (return-from sub-compile-file (values nil t t))))
1474 (*current-path* nil)
1475 (*last-source-context* nil)
1476 (*last-original-source* nil)
1477 (*last-source-form* nil)
1478 (*last-format-string* nil)
1479 (*last-format-args* nil)
1480 (*last-message-count* 0)
1481 ;; FIXME: Do we need this rebinding here? It's a literal
1482 ;; translation of the old CMU CL rebinding to
1483 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1484 ;; and it's not obvious whether the rebinding to itself is
1485 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1486 (*info-environment* *info-environment*)
1487 (*gensym-counter* 0))
1489 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1490 (with-compilation-values
1491 (sb!xc:with-compilation-unit ()
1494 (sub-sub-compile-file info)
1496 (finish-block-compilation)
1497 (let ((object *compile-object*))
1499 (fasl-output (fasl-dump-source-info info object))
1500 (core-object (fix-core-source-info info object))
1503 ;; Some errors are sufficiently bewildering that we just fail
1504 ;; immediately, without trying to recover and compile more of
1506 (fatal-compiler-error (condition)
1508 (when *compile-verbose*
1509 (format *standard-output*
1510 "~@<compilation aborted because of fatal error: ~2I~_~A~:>"
1512 (values nil t t)))))
1514 ;;; Return a pathname for the named file. The file must exist.
1515 (defun verify-source-file (pathname-designator)
1516 (let* ((pathname (pathname pathname-designator))
1517 (default-host (make-pathname :host (pathname-host pathname))))
1518 (flet ((try-with-type (path type error-p)
1519 (let ((new (merge-pathnames
1520 path (make-pathname :type type
1521 :defaults default-host))))
1522 (if (probe-file new)
1524 (and error-p (truename new))))))
1525 (cond ((typep pathname 'logical-pathname)
1526 (try-with-type pathname "LISP" t))
1527 ((probe-file pathname) pathname)
1528 ((try-with-type pathname "lisp" nil))
1529 ((try-with-type pathname "lisp" t))))))
1531 (defun elapsed-time-to-string (tsec)
1532 (multiple-value-bind (tmin sec) (truncate tsec 60)
1533 (multiple-value-bind (thr min) (truncate tmin 60)
1534 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1536 ;;; Print some junk at the beginning and end of compilation.
1537 (defun print-compile-start-note (source-info)
1538 (declare (type source-info source-info))
1539 (let ((file-info (source-info-file-info source-info)))
1540 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1541 (namestring (file-info-name file-info))
1542 (sb!int:format-universal-time nil
1543 (file-info-write-date
1547 :print-timezone nil)))
1550 (defun print-compile-end-note (source-info won)
1551 (declare (type source-info source-info))
1552 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1554 (elapsed-time-to-string
1555 (- (get-universal-time)
1556 (source-info-start-time source-info))))
1559 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1560 ;;; out of the compile, then abort the writing of the output file, so
1561 ;;; that we don't overwrite it with known garbage.
1562 (defun sb!xc:compile-file
1567 (output-file (cfp-output-file-default input-file))
1568 ;; FIXME: ANSI doesn't seem to say anything about
1569 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1571 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1572 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1573 (external-format :default)
1577 ((:block-compile *block-compile-arg*) nil))
1579 "Compile INPUT-FILE, producing a corresponding fasl file and
1580 returning its filename.
1583 If true, a message per non-macroexpanded top level form is printed
1584 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1585 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1586 no such message, but their subforms do.
1588 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1589 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1590 For example, compiling an IN-PACKAGE form will result in a message about
1591 a top level SETQ in addition to the message about the IN-PACKAGE form'
1594 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1597 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1598 argument, it is not currently supported. (non-standard)
1601 If given, internal data structures are dumped to the specified
1602 file, or if a value of T is given, to a file of *.trace type
1603 derived from the input file name. (non-standard)"
1604 ;;; Block compilation is currently broken.
1606 "Also, as a workaround for vaguely-non-ANSI behavior, the
1607 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1608 multiple functions are compiled together as a unit, resolving function
1609 references at compile time. NIL means that global function names are
1610 never resolved at compilation time. Currently NIL is the default
1611 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1612 of the ANSI spec allows this behavior under all circumstances, the
1613 compiler's runtime scales badly when it tries to do this for large
1614 files. If/when this performance problem is fixed, the block
1615 compilation default behavior will probably be made dependent on the
1616 SPEED and COMPILATION-SPEED optimization values, and the
1617 :BLOCK-COMPILE argument will probably become deprecated."
1619 (let* ((fasl-output nil)
1620 (output-file-name nil)
1623 (failure-p t) ; T in case error keeps this from being set later
1624 (input-pathname (verify-source-file input-file))
1625 (source-info (make-file-source-info input-pathname external-format))
1626 (*compiler-trace-output* nil)) ; might be modified below
1631 (setq output-file-name
1632 (sb!xc:compile-file-pathname input-file
1633 :output-file output-file))
1635 (open-fasl-output output-file-name
1636 (namestring input-pathname))))
1638 (let* ((default-trace-file-pathname
1639 (make-pathname :type "trace" :defaults input-pathname))
1640 (trace-file-pathname
1641 (if (eql trace-file t)
1642 default-trace-file-pathname
1643 (merge-pathnames trace-file
1644 default-trace-file-pathname))))
1645 (setf *compiler-trace-output*
1646 (open trace-file-pathname
1647 :if-exists :supersede
1648 :direction :output))))
1650 (when sb!xc:*compile-verbose*
1651 (print-compile-start-note source-info))
1652 (let ((*compile-object* fasl-output)
1654 (multiple-value-setq (dummy warnings-p failure-p)
1655 (sub-compile-file source-info)))
1656 (setq compile-won t))
1658 (close-source-info source-info)
1661 (close-fasl-output fasl-output (not compile-won))
1662 (setq output-file-name
1663 (pathname (fasl-output-stream fasl-output)))
1664 (when (and compile-won sb!xc:*compile-verbose*)
1665 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1667 (when sb!xc:*compile-verbose*
1668 (print-compile-end-note source-info compile-won))
1670 (when *compiler-trace-output*
1671 (close *compiler-trace-output*)))
1673 (values (if output-file
1674 ;; Hack around filesystem race condition...
1675 (or (probe-file output-file-name) output-file-name)
1680 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1681 ;;; the OUTPUT-FILE argument
1683 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1684 ;;; that results from merging the INPUT-FILE with the value of
1685 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1686 ;;; default to the appropriate implementation-defined default type for
1688 (defun cfp-output-file-default (input-file)
1689 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1690 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1693 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1694 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1695 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1696 ;;; pathname, it is translated into a physical pathname as if by
1697 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1698 ;;; So I haven't really tried to make this precisely ANSI-compatible
1699 ;;; at the level of e.g. whether it returns logical pathname or a
1700 ;;; physical pathname. Patches to make it more correct are welcome.
1701 ;;; -- WHN 2000-12-09
1702 (defun sb!xc:compile-file-pathname (input-file
1704 (output-file (cfp-output-file-default
1708 "Return a pathname describing what file COMPILE-FILE would write to given
1710 (merge-pathnames output-file (merge-pathnames input-file)))
1712 ;;;; MAKE-LOAD-FORM stuff
1714 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1715 ;;; finds a constant structure, it invokes this to arrange for proper
1716 ;;; dumping. If it turns out that the constant has already been
1717 ;;; dumped, then we don't need to do anything.
1719 ;;; If the constant hasn't been dumped, then we check to see whether
1720 ;;; we are in the process of creating it. We detect this by
1721 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1722 ;;; the constants we are in the process of creating. Actually, each
1723 ;;; entry is a list of the constant and any init forms that need to be
1724 ;;; processed on behalf of that constant.
1726 ;;; It's not necessarily an error for this to happen. If we are
1727 ;;; processing the init form for some object that showed up *after*
1728 ;;; the original reference to this constant, then we just need to
1729 ;;; defer the processing of that init form. To detect this, we
1730 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1731 ;;; constants created since the last time we started processing an
1732 ;;; init form. If the constant passed to emit-make-load-form shows up
1733 ;;; in this list, then there is a circular chain through creation
1734 ;;; forms, which is an error.
1736 ;;; If there is some intervening init form, then we blow out of
1737 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1738 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1739 ;;; offending init form can be tacked onto the init forms for the
1740 ;;; circular object.
1742 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1743 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1744 ;;; whether the creation form is the magic value
1745 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1746 ;;; dumper will eventually get its hands on the object and use the
1747 ;;; normal structure dumping noise on it.
1749 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1750 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1751 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1752 ;;; dumper to use that result instead whenever it sees this constant.
1754 ;;; Now we try to compile the init form. We bind
1755 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1756 ;;; form (and any init forms that were added because of circularity
1757 ;;; detection). If this works, great. If not, we add the init forms to
1758 ;;; the init forms for the object that caused the problems and let it
1760 (defvar *constants-being-created* nil)
1761 (defvar *constants-created-since-last-init* nil)
1762 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1763 (defun emit-make-load-form (constant)
1764 (aver (fasl-output-p *compile-object*))
1765 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1766 ;; KLUDGE: This special hack is because I was too lazy
1767 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1768 ;; function of LAYOUT returns nontrivial forms when
1769 ;; building the cross-compiler but :IGNORE-IT when
1770 ;; cross-compiling or running under the target Lisp. --
1772 #+sb-xc-host (typep constant 'layout))
1773 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1775 (when (find constant *constants-created-since-last-init* :test #'eq)
1777 (throw 'pending-init circular-ref)))
1778 (multiple-value-bind (creation-form init-form)
1780 (sb!xc:make-load-form constant (make-null-lexenv))
1782 (compiler-error condition)))
1784 (:sb-just-dump-it-normally
1785 (fasl-validate-structure constant *compile-object*)
1790 (when (fasl-constant-already-dumped-p constant *compile-object*)
1791 (return-from emit-make-load-form nil))
1792 (let* ((name (write-to-string constant :level 1 :length 2))
1794 (list constant name init-form)
1796 (let ((*constants-being-created*
1797 (cons info *constants-being-created*))
1798 (*constants-created-since-last-init*
1799 (cons constant *constants-created-since-last-init*)))
1802 (fasl-note-handle-for-constant
1804 (compile-load-time-value
1808 (compiler-error "circular references in creation form for ~S"
1811 (let* ((*constants-created-since-last-init* nil)
1813 (catch 'pending-init
1814 (loop for (name form) on (cdr info) by #'cddr
1815 collect name into names
1816 collect form into forms
1817 finally (compile-make-load-form-init-forms forms))
1820 (setf (cdr circular-ref)
1821 (append (cdr circular-ref) (cdr info))))))))))))
1824 ;;;; Host compile time definitions
1826 (defun compile-in-lexenv (name lambda lexenv)
1827 (declare (ignore lexenv))
1828 (compile name lambda))
1831 (defun eval-in-lexenv (form lexenv)
1832 (declare (ignore lexenv))