1 ;;;; the top level interfaces to the compiler, plus some other
2 ;;;; compiler-related stuff (e.g. CL:CALL-ARGUMENTS-LIMIT) which
3 ;;;; doesn't obviously belong anywhere else
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
16 ;;; FIXME: Doesn't this belong somewhere else, like early-c.lisp?
17 (declaim (special *constants* *free-vars* *component-being-compiled*
18 *code-vector* *next-location* *result-fixups*
19 *free-funs* *source-paths*
20 *seen-blocks* *seen-funs* *list-conflicts-table*
21 *continuation-number* *continuation-numbers*
22 *number-continuations* *tn-id* *tn-ids* *id-tns*
23 *label-ids* *label-id* *id-labels*
24 *undefined-warnings* *compiler-error-count*
25 *compiler-warning-count* *compiler-style-warning-count*
27 *compiler-error-bailout*
28 #!+sb-show *compiler-trace-output*
29 *last-source-context* *last-original-source*
30 *last-source-form* *last-format-string* *last-format-args*
31 *last-message-count* *last-error-context*
32 *lexenv* *fun-names-in-this-file*
33 *allow-instrumenting*))
35 ;;; Whether reference to a thing which cannot be defined causes a full
37 (defvar *flame-on-necessarily-undefined-thing* nil)
39 (defvar *check-consistency* nil)
41 ;;; Set to NIL to disable loop analysis for register allocation.
42 (defvar *loop-analyze* t)
44 ;;; Bind this to a stream to capture various internal debugging output.
45 (defvar *compiler-trace-output* nil)
47 ;;; The current block compilation state. These are initialized to the
48 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
51 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
52 ;;; argument, which overrides any internal declarations.
53 (defvar *block-compile*)
54 (defvar *block-compile-arg*)
55 (declaim (type (member nil t :specified) *block-compile* *block-compile-arg*))
56 (defvar *entry-points*)
57 (declaim (list *entry-points*))
59 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
60 ;;; lambdas resulting from compiling subforms. (In reverse order.)
61 (defvar *toplevel-lambdas*)
62 (declaim (list *toplevel-lambdas*))
64 ;;; The current non-macroexpanded toplevel form as printed when
65 ;;; *compile-print* is true.
66 (defvar *top-level-form-noted* nil)
68 (defvar sb!xc:*compile-verbose* t
70 "The default for the :VERBOSE argument to COMPILE-FILE.")
71 (defvar sb!xc:*compile-print* t
73 "The default for the :PRINT argument to COMPILE-FILE.")
74 (defvar *compile-progress* nil
76 "When this is true, the compiler prints to *STANDARD-OUTPUT* progress
77 information about the phases of compilation of each function. (This
78 is useful mainly in large block compilations.)")
80 (defvar sb!xc:*compile-file-pathname* nil
82 "The defaulted pathname of the file currently being compiled, or NIL if not
84 (defvar sb!xc:*compile-file-truename* nil
86 "The TRUENAME of the file currently being compiled, or NIL if not
89 (declaim (type (or pathname null)
90 sb!xc:*compile-file-pathname*
91 sb!xc:*compile-file-truename*))
93 ;;; the SOURCE-INFO structure for the current compilation. This is
94 ;;; null globally to indicate that we aren't currently in any
95 ;;; identifiable compilation.
96 (defvar *source-info* nil)
98 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
99 ;;; normally causes nested uses to be no-ops).
100 (defvar *in-compilation-unit* nil)
102 ;;; Count of the number of compilation units dynamically enclosed by
103 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
104 (defvar *aborted-compilation-unit-count*)
106 ;;; Mumble conditional on *COMPILE-PROGRESS*.
107 (defun maybe-mumble (&rest foo)
108 (when *compile-progress*
109 (compiler-mumble "~&")
110 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
111 (apply #'compiler-mumble foo))))
113 (deftype object () '(or fasl-output core-object null))
115 (defvar *compile-object* nil)
116 (declaim (type object *compile-object*))
117 (defvar *compile-toplevel-object* nil)
119 (defvar *emit-cfasl* nil)
121 (defvar *fopcompile-label-counter*)
123 ;; Used during compilation to map code paths to the matching
124 ;; instrumentation conses.
125 (defvar *code-coverage-records* nil)
126 ;; Used during compilation to keep track of with source paths have been
127 ;; instrumented in which blocks.
128 (defvar *code-coverage-blocks* nil)
129 ;; Stores the code coverage instrumentation results. Keys are namestrings,
130 ;; the value is a list of (CONS PATH STATE), where STATE is NIL for
131 ;; a path that has not been visited, and T for one that has.
132 (defvar *code-coverage-info* (make-hash-table :test 'equal))
135 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
137 (defmacro sb!xc:with-compilation-unit (options &body body)
139 "Affects compilations that take place within its dynamic extent. It is
140 intended to be eg. wrapped around the compilation of all files in the same system.
142 Following options are defined:
144 :OVERRIDE Boolean-Form
145 One of the effects of this form is to delay undefined warnings until the
146 end of the form, instead of giving them at the end of each compilation.
147 If OVERRIDE is NIL (the default), then the outermost
148 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
149 OVERRIDE true causes that form to grab any enclosed warnings, even if it
150 is enclosed by another WITH-COMPILATION-UNIT.
152 :POLICY Optimize-Declaration-Form
153 Provides dynamic scoping for global compiler optimization qualities and
154 restrictions, limiting effects of subsequent OPTIMIZE proclamations and
155 calls to SB-EXT:RESTRICT-COMPILER-POLICY to the dynamic scope of BODY.
157 If OVERRIDE is false, specified POLICY is merged with current global
158 policy. If OVERRIDE is true, current global policy, including any
159 restrictions, is discarded in favor of the specified POLICY.
161 Supplying POLICY NIL is equivalent to the option not being supplied at
162 all, ie. dynamic scoping of policy does not take place.
164 This option is an SBCL-specific experimental extension: Interface
167 :SOURCE-NAMESTRING Namestring-Form
168 Attaches the value returned by the Namestring-Form to the internal
169 debug-source information as the namestring of the source file. Normally
170 the namestring of the input-file for COMPILE-FILE is used: this option
171 can be used to provide source-file information for functions compiled
172 using COMPILE, or to override the input-file of COMPILE-FILE.
174 If both an outer and an inner WITH-COMPILATION-UNIT provide a
175 SOURCE-NAMESTRING, the inner one takes precedence. Unaffected
178 This is an SBCL-specific extension.
180 :SOURCE-PLIST Plist-Form
181 Attaches the value returned by the Plist-Form to internal debug-source
182 information of functions compiled in within the dynamic extent of BODY.
184 Primarily for use by development environments, in order to eg. associate
185 function definitions with editor-buffers. Can be accessed using
186 SB-INTROSPECT:DEFINITION-SOURCE-PLIST.
188 If an outer WITH-COMPILATION-UNIT form also provide a SOURCE-PLIST, it
189 is appended to the end of the provided SOURCE-PLIST. Unaffected
192 This is an SBCL-specific extension.
196 ;; Prevent proclamations from the file leaking, and restrict
197 ;; SAFETY to 3 -- otherwise uses the current global policy.
198 (with-compilation-unit (:policy '(optimize))
199 (restrict-compiler-policy 'safety 3)
202 ;; Using default policy instead of the current global one,
203 ;; except for DEBUG 3.
204 (with-compilation-unit (:policy '(optimize debug)
208 ;; Same as if :POLICY had not been specified at all: SAFETY 3
209 ;; proclamation leaks out from WITH-COMPILATION-UNIT.
210 (with-compilation-unit (:policy nil)
211 (declaim (optimize safety))
214 `(%with-compilation-unit (lambda () ,@body) ,@options))
216 (defvar *source-plist* nil)
217 (defvar *source-namestring* nil)
219 (defun %with-compilation-unit (fn &key override policy source-plist source-namestring)
220 (declare (type function fn))
222 (let ((succeeded-p nil)
223 (*source-plist* (append source-plist *source-plist*))
224 (*source-namestring* (or source-namestring *source-namestring*)))
225 (if (and *in-compilation-unit* (not override))
226 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
227 ;; ordinarily (unless OVERRIDE) basically a no-op.
229 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
231 (incf *aborted-compilation-unit-count*)))
232 (let ((*aborted-compilation-unit-count* 0)
233 (*compiler-error-count* 0)
234 (*compiler-warning-count* 0)
235 (*compiler-style-warning-count* 0)
236 (*compiler-note-count* 0)
237 (*undefined-warnings* nil)
238 (*in-compilation-unit* t))
239 (handler-bind ((parse-unknown-type
241 (note-undefined-reference
242 (parse-unknown-type-specifier c)
245 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
247 (incf *aborted-compilation-unit-count*))
248 (summarize-compilation-unit (not succeeded-p)))))))))
250 (let ((*policy* (process-optimize-decl policy (unless override *policy*)))
251 (*policy-restrictions* (unless override *policy-restrictions*)))
255 ;;; Is NAME something that no conforming program can rely on
257 (defun name-reserved-by-ansi-p (name kind)
260 (eq (symbol-package (fun-name-block-name name))
263 (let ((symbol (typecase name
265 ((cons symbol) (car name))
266 (t (return-from name-reserved-by-ansi-p nil)))))
267 (eq (symbol-package symbol) *cl-package*)))))
269 ;;; This is to be called at the end of a compilation unit. It signals
270 ;;; any residual warnings about unknown stuff, then prints the total
271 ;;; error counts. ABORT-P should be true when the compilation unit was
272 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
273 ;;; enclosed nested compilation units that were aborted.
274 (defun summarize-compilation-unit (abort-p)
277 (handler-bind ((style-warning #'compiler-style-warning-handler)
278 (warning #'compiler-warning-handler))
280 (let ((undefs (sort *undefined-warnings* #'string<
282 (let ((x (undefined-warning-name x)))
285 (prin1-to-string x)))))))
286 (dolist (kind '(:variable :function :type))
287 (let ((names (mapcar #'undefined-warning-name
288 (remove kind undefs :test #'neq
289 :key #'undefined-warning-kind))))
290 (when names (push (cons kind names) summary))))
291 (dolist (undef undefs)
292 (let ((name (undefined-warning-name undef))
293 (kind (undefined-warning-kind undef))
294 (warnings (undefined-warning-warnings undef))
295 (undefined-warning-count (undefined-warning-count undef)))
296 (dolist (*compiler-error-context* warnings)
297 (if #-sb-xc-host (and (member kind '(:function :type))
298 (name-reserved-by-ansi-p name kind)
299 *flame-on-necessarily-undefined-thing*)
306 "~@<There is no function named ~S. References to ~S ~
307 in some contexts (like starts of blocks) have ~
308 special meaning, but here it would have to be a ~
309 function, and that shouldn't be right.~:@>" name
313 "~@<The function ~S is undefined, and its name is ~
314 reserved by ANSI CL so that even if it were ~
315 defined later, the code doing so would not be ~
316 portable.~:@>" name))))
318 (if (and (consp name) (eq 'quote (car name)))
320 "~@<Undefined type ~S. The name starts with ~S: ~
321 probably use of a quoted type name in a context ~
322 where the name is not evaluated.~:@>"
325 "~@<Undefined type ~S. Note that name ~S is ~
326 reserved by ANSI CL, so code defining a type with ~
327 that name would not be portable.~:@>" name
329 (if (eq kind :variable)
330 (compiler-warn "undefined ~(~A~): ~S" kind name)
331 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
332 (let ((warn-count (length warnings)))
333 (when (and warnings (> undefined-warning-count warn-count))
334 (let ((more (- undefined-warning-count warn-count)))
335 (if (eq kind :variable)
337 "~W more use~:P of undefined ~(~A~) ~S"
340 "~W more use~:P of undefined ~(~A~) ~S"
341 more kind name))))))))))
343 (unless (and (not abort-p)
344 (zerop *aborted-compilation-unit-count*)
345 (zerop *compiler-error-count*)
346 (zerop *compiler-warning-count*)
347 (zerop *compiler-style-warning-count*)
348 (zerop *compiler-note-count*))
349 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
350 (format *error-output* "~&compilation unit ~:[finished~;aborted~]"
352 (dolist (cell summary)
353 (destructuring-bind (kind &rest names) cell
354 (format *error-output*
355 "~& Undefined ~(~A~)~p:~
356 ~% ~{~<~% ~1:;~S~>~^ ~}"
357 kind (length names) names)))
358 (format *error-output* "~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
359 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
360 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
361 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
362 ~[~:;~:*~& printed ~W note~:P~]"
363 *aborted-compilation-unit-count*
364 *compiler-error-count*
365 *compiler-warning-count*
366 *compiler-style-warning-count*
367 *compiler-note-count*))
368 (terpri *error-output*)
369 (force-output *error-output*))))
371 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
372 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
373 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
374 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
375 (defmacro with-compilation-values (&body body)
377 (let ((*warnings-p* nil)
379 (values (progn ,@body)
383 ;;;; component compilation
385 (defparameter *max-optimize-iterations* 3 ; ARB
387 "The upper limit on the number of times that we will consecutively do IR1
388 optimization that doesn't introduce any new code. A finite limit is
389 necessary, since type inference may take arbitrarily long to converge.")
391 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
392 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
394 ;;; Repeatedly optimize COMPONENT until no further optimizations can
395 ;;; be found or we hit our iteration limit. When we hit the limit, we
396 ;;; clear the component and block REOPTIMIZE flags to discourage the
397 ;;; next optimization attempt from pounding on the same code.
398 (defun ir1-optimize-until-done (component)
399 (declare (type component component))
401 (event ir1-optimize-until-done)
403 (cleared-reanalyze nil)
406 (when (component-reanalyze component)
408 (setq cleared-reanalyze t)
409 (setf (component-reanalyze component) nil))
410 (setf (component-reoptimize component) nil)
411 (ir1-optimize component fastp)
412 (cond ((component-reoptimize component)
414 (when (and (>= count *max-optimize-iterations*)
415 (not (component-reanalyze component))
416 (eq (component-reoptimize component) :maybe))
418 (cond ((retry-delayed-ir1-transforms :optimize)
422 (event ir1-optimize-maxed-out)
423 (setf (component-reoptimize component) nil)
424 (do-blocks (block component)
425 (setf (block-reoptimize block) nil))
427 ((retry-delayed-ir1-transforms :optimize)
433 (setq fastp (>= count *max-optimize-iterations*))
434 (maybe-mumble (if fastp "-" ".")))
435 (when cleared-reanalyze
436 (setf (component-reanalyze component) t)))
439 (defparameter *constraint-propagate* t)
441 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
442 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
443 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
444 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
446 ;;; FIXME: It would be good to document why it's important to have a
447 ;;; large value here, and what the drawbacks of an excessively large
448 ;;; value are; and it might also be good to make it depend on
449 ;;; optimization policy.
450 (defparameter *reoptimize-after-type-check-max* 10)
452 (defevent reoptimize-maxed-out
453 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
455 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
456 (defun dfo-as-needed (component)
457 (declare (type component component))
458 (when (component-reanalyze component)
462 (unless (component-reanalyze component)
468 ;;; Do all the IR1 phases for a non-top-level component.
469 (defun ir1-phases (component)
470 (declare (type component component))
471 (aver-live-component component)
472 (let ((*constraint-universe* (make-array 64 ; arbitrary, but don't
474 :fill-pointer 0 :adjustable t))
476 (*delayed-ir1-transforms* nil))
477 (declare (special *constraint-universe* *delayed-ir1-transforms*))
479 (ir1-optimize-until-done component)
480 (when (or (component-new-functionals component)
481 (component-reanalyze-functionals component))
482 (maybe-mumble "locall ")
483 (locall-analyze-component component))
484 (dfo-as-needed component)
485 (when *constraint-propagate*
486 (maybe-mumble "constraint ")
487 (constraint-propagate component))
488 (when (retry-delayed-ir1-transforms :constraint)
489 (maybe-mumble "Rtran "))
490 (flet ((want-reoptimization-p ()
491 (or (component-reoptimize component)
492 (component-reanalyze component)
493 (component-new-functionals component)
494 (component-reanalyze-functionals component))))
495 (unless (and (want-reoptimization-p)
496 ;; We delay the generation of type checks until
497 ;; the type constraints have had time to
498 ;; propagate, else the compiler can confuse itself.
499 (< loop-count (- *reoptimize-after-type-check-max* 4)))
500 (maybe-mumble "type ")
501 (generate-type-checks component)
502 (unless (want-reoptimization-p)
504 (when (>= loop-count *reoptimize-after-type-check-max*)
505 (maybe-mumble "[reoptimize limit]")
506 (event reoptimize-maxed-out)
510 (ir1-finalize component)
513 (defun %compile-component (component)
514 (let ((*code-segment* nil)
517 (*constant-segment* nil)
519 (*constant-table* nil)
521 (*constant-vector* nil))
522 (maybe-mumble "GTN ")
523 (gtn-analyze component)
524 (maybe-mumble "LTN ")
525 (ltn-analyze component)
526 (dfo-as-needed component)
527 (maybe-mumble "control ")
528 (control-analyze component #'make-ir2-block)
530 (when (or (ir2-component-values-receivers (component-info component))
531 (component-dx-lvars component))
532 (maybe-mumble "stack ")
533 (stack-analyze component)
534 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
535 ;; stack analysis. There shouldn't be any unreachable code after
536 ;; control, so this won't delete anything.
537 (dfo-as-needed component))
541 (maybe-mumble "IR2tran ")
543 (entry-analyze component)
544 (ir2-convert component)
546 (when (policy *lexenv* (>= speed compilation-speed))
547 (maybe-mumble "copy ")
548 (copy-propagate component))
550 (ir2-optimize component)
552 (select-representations component)
554 (when *check-consistency*
555 (maybe-mumble "check2 ")
556 (check-ir2-consistency component))
558 (delete-unreferenced-tns component)
560 (maybe-mumble "life ")
561 (lifetime-analyze component)
563 (when *compile-progress*
564 (compiler-mumble "") ; Sync before doing more output.
565 (pre-pack-tn-stats component *standard-output*))
567 (when *check-consistency*
568 (maybe-mumble "check-life ")
569 (check-life-consistency component))
571 (maybe-mumble "pack ")
574 (when *check-consistency*
575 (maybe-mumble "check-pack ")
576 (check-pack-consistency component))
578 (when *compiler-trace-output*
579 (describe-component component *compiler-trace-output*)
580 (describe-ir2-component component *compiler-trace-output*))
582 (maybe-mumble "code ")
583 (multiple-value-bind (code-length trace-table fixup-notes)
584 (generate-code component)
587 (when *compiler-trace-output*
588 (format *compiler-trace-output*
589 "~|~%disassembly of code for ~S~2%" component)
590 (sb!disassem:disassemble-assem-segment *code-segment*
591 *compiler-trace-output*))
593 (etypecase *compile-object*
595 (maybe-mumble "fasl")
596 (fasl-dump-component component
603 (maybe-mumble "core")
604 (make-core-component component
612 ;; We're done, so don't bother keeping anything around.
613 (setf (component-info component) :dead)
617 ;;; Delete components with no external entry points before we try to
618 ;;; generate code. Unreachable closures can cause IR2 conversion to
619 ;;; puke on itself, since it is the reference to the closure which
620 ;;; normally causes the components to be combined.
621 (defun delete-if-no-entries (component)
622 (dolist (fun (component-lambdas component) (delete-component component))
623 (when (functional-has-external-references-p fun)
625 (case (functional-kind fun)
628 (unless (every (lambda (ref)
629 (eq (node-component ref) component))
633 (defun compile-component (component)
635 ;; miscellaneous sanity checks
637 ;; FIXME: These are basically pretty wimpy compared to the checks done
638 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
639 ;; make those internal consistency checks work again and use them.
640 (aver-live-component component)
641 (do-blocks (block component)
642 (aver (eql (block-component block) component)))
643 (dolist (lambda (component-lambdas component))
644 ;; sanity check to prevent weirdness from propagating insidiously as
645 ;; far from its root cause as it did in bug 138: Make sure that
646 ;; thing-to-COMPONENT links are consistent.
647 (aver (eql (lambda-component lambda) component))
648 (aver (eql (node-component (lambda-bind lambda)) component)))
650 (let* ((*component-being-compiled* component))
652 ;; Record xref information before optimization. This way the
653 ;; stored xref data reflects the real source as closely as
655 (record-component-xrefs component)
657 (ir1-phases component)
660 (dfo-as-needed component)
661 (find-dominators component)
662 (loop-analyze component))
665 (when (and *loop-analyze* *compiler-trace-output*)
666 (labels ((print-blocks (block)
667 (format *compiler-trace-output* " ~A~%" block)
668 (when (block-loop-next block)
669 (print-blocks (block-loop-next block))))
671 (format *compiler-trace-output* "loop=~A~%" loop)
672 (print-blocks (loop-blocks loop))
673 (dolist (l (loop-inferiors loop))
675 (print-loop (component-outer-loop component))))
678 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
679 (maybe-mumble "env ")
680 (physenv-analyze component)
681 (dfo-as-needed component)
683 (delete-if-no-entries component)
685 (unless (eq (block-next (component-head component))
686 (component-tail component))
687 (%compile-component component)))
689 (clear-constant-info)
693 ;;;; clearing global data structures
695 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
696 ;;;; global data structures entirely when possible and consing up the
697 ;;;; others from scratch instead of clearing and reusing them?
699 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
700 ;;; addition to allowing stuff to be reclaimed, this is required for
701 ;;; correct assignment of constant offsets, since we need to assign a
702 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
703 ;;; slots, since they are used to keep track of functions across
704 ;;; component boundaries.
705 (defun clear-constant-info ()
706 (maphash (lambda (k v)
708 (setf (leaf-info v) nil)
709 (setf (constant-boxed-tn v) nil))
711 (maphash (lambda (k v)
714 (setf (leaf-info v) nil)
715 (setf (constant-boxed-tn v) nil)))
719 ;;; Blow away the REFS for all global variables, and let COMPONENT
721 (defun clear-ir1-info (component)
722 (declare (type component component))
724 (maphash (lambda (k v)
728 (delete-if #'here-p (leaf-refs v)))
729 (when (basic-var-p v)
730 (setf (basic-var-sets v)
731 (delete-if #'here-p (basic-var-sets v))))))
734 (eq (node-component x) component)))
740 ;;; Clear global variables used by the compiler.
742 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
743 ;;; and it adds overhead even when people aren't using the compiler.
744 ;;; Perhaps we could make these global vars unbound except when
745 ;;; actually in use, so that this function could go away.
746 (defun clear-stuff (&optional (debug-too t))
748 ;; Clear debug counters and tables.
749 (clrhash *seen-blocks*)
750 (clrhash *seen-funs*)
751 (clrhash *list-conflicts-table*)
754 (clrhash *continuation-numbers*)
755 (clrhash *number-continuations*)
756 (setq *continuation-number* 0)
760 (clrhash *label-ids*)
761 (clrhash *id-labels*)
764 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
765 ;; Superficially, this seemed harmful -- the user could reasonably be
766 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
767 ;; compiled. A closer inspection showed that this actually turned out to be
768 ;; harmless in practice, because CLEAR-STUFF was only called from within
769 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
770 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
771 ;; practice, it was also useless in practice. So we don't do it any more.)
777 ;;; Print out some useful info about COMPONENT to STREAM.
778 (defun describe-component (component *standard-output*)
779 (declare (type component component))
780 (format t "~|~%;;;; component: ~S~2%" (component-name component))
781 (print-all-blocks component)
784 (defun describe-ir2-component (component *standard-output*)
785 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
786 (format t "entries:~%")
787 (dolist (entry (ir2-component-entries (component-info component)))
788 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
789 (label-id (entry-info-offset entry))
790 (entry-info-name entry)
791 (entry-info-closure-tn entry)))
793 (pre-pack-tn-stats component *standard-output*)
795 (print-ir2-blocks component)
801 ;;;; When reading from a file, we have to keep track of some source
802 ;;;; information. We also exploit our ability to back up for printing
803 ;;;; the error context and for recovering from errors.
805 ;;;; The interface we provide to this stuff is the stream-oid
806 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
807 ;;;; of getting the next source form.
809 ;;; A FILE-INFO structure holds all the source information for a
811 (def!struct (file-info
813 #-no-ansi-print-object
814 (:print-object (lambda (s stream)
815 (print-unreadable-object (s stream :type t)
816 (princ (file-info-name s) stream)))))
817 ;; If a file, the truename of the corresponding source file. If from
818 ;; a Lisp form, :LISP. If from a stream, :STREAM.
819 (name (missing-arg) :type (or pathname (eql :lisp)))
820 ;; the external format that we'll call OPEN with, if NAME is a file.
821 (external-format nil)
822 ;; the defaulted, but not necessarily absolute file name (i.e. prior
823 ;; to TRUENAME call.) Null if not a file. This is used to set
824 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
826 (untruename nil :type (or pathname null))
827 ;; the file's write date (if relevant)
828 (write-date nil :type (or unsigned-byte null))
829 ;; the source path root number of the first form in this file (i.e.
830 ;; the total number of forms converted previously in this
832 (source-root 0 :type unsigned-byte)
833 ;; parallel vectors containing the forms read out of the file and
834 ;; the file positions that reading of each form started at (i.e. the
835 ;; end of the previous form)
836 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
837 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
839 ;;; The SOURCE-INFO structure provides a handle on all the source
840 ;;; information for an entire compilation.
841 (def!struct (source-info
842 #-no-ansi-print-object
843 (:print-object (lambda (s stream)
844 (print-unreadable-object (s stream :type t))))
846 ;; the UT that compilation started at
847 (start-time (get-universal-time) :type unsigned-byte)
848 ;; the IRT that compilation started at
849 (start-real-time (get-internal-real-time) :type unsigned-byte)
850 ;; the FILE-INFO structure for this compilation
851 (file-info nil :type (or file-info null))
852 ;; the stream that we are using to read the FILE-INFO, or NIL if
853 ;; no stream has been opened yet
854 (stream nil :type (or stream null))
855 ;; if the current compilation is recursive (e.g., due to EVAL-WHEN
856 ;; processing at compile-time), the invoking compilation's
858 (parent nil :type (or source-info null)))
860 ;;; Given a pathname, return a SOURCE-INFO structure.
861 (defun make-file-source-info (file external-format)
863 :file-info (make-file-info :name (truename file)
864 :untruename (merge-pathnames file)
865 :external-format external-format
866 :write-date (file-write-date file))))
868 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
869 (defun make-lisp-source-info (form &key parent)
871 :file-info (make-file-info :name :lisp
876 ;;; Walk up the SOURCE-INFO list until we either reach a SOURCE-INFO
877 ;;; with no parent (e.g., from a REPL evaluation) or until we reach a
878 ;;; SOURCE-INFO whose FILE-INFO denotes a file.
879 (defun get-toplevelish-file-info (&optional (source-info *source-info*))
881 (do* ((sinfo source-info (source-info-parent sinfo))
882 (finfo (source-info-file-info sinfo)
883 (source-info-file-info sinfo)))
884 ((or (not (source-info-p (source-info-parent sinfo)))
885 (pathnamep (file-info-name finfo)))
888 ;;; Return a form read from STREAM; or for EOF use the trick,
889 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
890 ;;; error happens, then convert it to standard abort-the-compilation
891 ;;; error condition (possibly recording some extra location
893 (defun read-for-compile-file (stream position)
895 (read-preserving-whitespace stream nil stream)
896 (reader-error (condition)
897 (compiler-error 'input-error-in-compile-file
898 ;; We don't need to supply :POSITION here because
899 ;; READER-ERRORs already know their position in the file.
902 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
903 ;; (and that this is not a READER-ERROR) when it encounters end of
904 ;; file in the middle of something it's trying to read.
905 (end-of-file (condition)
906 (compiler-error 'input-error-in-compile-file
908 ;; We need to supply :POSITION here because the END-OF-FILE
909 ;; condition doesn't carry the position that the user
910 ;; probably cares about, where the failed READ began.
914 (compiler-error 'input-error-in-compile-file
919 ;;; If STREAM is present, return it, otherwise open a stream to the
920 ;;; current file. There must be a current file.
922 ;;; FIXME: This is probably an unnecessarily roundabout way to do
923 ;;; things now that we process a single file in COMPILE-FILE (unlike
924 ;;; the old CMU CL code, which accepted multiple files). Also, the old
926 ;;; When we open a new file, we also reset *PACKAGE* and policy.
927 ;;; This gives the effect of rebinding around each file.
928 ;;; which doesn't seem to be true now. Check to make sure that if
929 ;;; such rebinding is necessary, it's still done somewhere.
930 (defun get-source-stream (info)
931 (declare (type source-info info))
932 (or (source-info-stream info)
933 (let* ((file-info (source-info-file-info info))
934 (name (file-info-name file-info))
935 (external-format (file-info-external-format file-info)))
936 (setf sb!xc:*compile-file-truename* name
937 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
938 (source-info-stream info)
939 (open name :direction :input
940 :external-format external-format)))))
942 ;;; Close the stream in INFO if it is open.
943 (defun close-source-info (info)
944 (declare (type source-info info))
945 (let ((stream (source-info-stream info)))
946 (when stream (close stream)))
947 (setf (source-info-stream info) nil)
950 ;;; Loop over FORMS retrieved from INFO. Used by COMPILE-FILE and
951 ;;; LOAD when loading from a FILE-STREAM associated with a source
953 (defmacro do-forms-from-info (((form &rest keys) info)
955 (aver (symbolp form))
956 (once-only ((info info))
957 `(let ((*source-info* ,info))
958 (loop (destructuring-bind (,form &key ,@keys &allow-other-keys)
959 (let* ((file-info (source-info-file-info ,info))
960 (stream (get-source-stream ,info))
961 (pos (file-position stream))
962 (form (read-for-compile-file stream pos)))
963 (if (eq form stream) ; i.e., if EOF
965 (let* ((forms (file-info-forms file-info))
966 (current-idx (+ (fill-pointer forms)
967 (file-info-source-root
969 (vector-push-extend form forms)
970 (vector-push-extend pos (file-info-positions
972 (list form :current-index current-idx))))
975 ;;; Read and compile the source file.
976 (defun sub-sub-compile-file (info)
977 (do-forms-from-info ((form current-index) info)
978 (find-source-paths form current-index)
979 (process-toplevel-form
980 form `(original-source-start 0 ,current-index) nil)))
982 ;;; Return the INDEX'th source form read from INFO and the position
983 ;;; where it was read.
984 (defun find-source-root (index info)
985 (declare (type index index) (type source-info info))
986 (let ((file-info (source-info-file-info info)))
987 (values (aref (file-info-forms file-info) index)
988 (aref (file-info-positions file-info) index))))
990 ;;;; processing of top level forms
992 ;;; This is called by top level form processing when we are ready to
993 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
994 ;;; convert the form, but delay compilation, pushing the result on
995 ;;; *TOPLEVEL-LAMBDAS* instead.
996 (defun convert-and-maybe-compile (form path)
997 (declare (list path))
998 (let ((*top-level-form-noted* (note-top-level-form form t)))
999 ;; Don't bother to compile simple objects that just sit there.
1000 (when (and form (or (symbolp form) (consp form)))
1001 (if (fopcompilable-p form)
1002 (let ((*fopcompile-label-counter* 0))
1003 (fopcompile form path nil))
1004 (let ((*lexenv* (make-lexenv
1006 :handled-conditions *handled-conditions*
1007 :disabled-package-locks *disabled-package-locks*))
1008 (tll (ir1-toplevel form path nil)))
1009 (if (eq *block-compile* t)
1010 (push tll *toplevel-lambdas*)
1011 (compile-toplevel (list tll) nil))
1014 ;;; Macroexpand FORM in the current environment with an error handler.
1015 ;;; We only expand one level, so that we retain all the intervening
1016 ;;; forms in the source path.
1017 (defun preprocessor-macroexpand-1 (form)
1018 (handler-case (%macroexpand-1 form *lexenv*)
1020 (compiler-error "(during macroexpansion of ~A)~%~A"
1021 (let ((*print-level* 2)
1023 (format nil "~S" form))
1026 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
1027 ;;; the forms, and PATH is the source path of the FORM they came out of.
1028 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
1029 (defun process-toplevel-progn (forms path compile-time-too)
1030 (declare (list forms) (list path))
1031 (dolist (form forms)
1032 (process-toplevel-form form path compile-time-too)))
1034 ;;; Process a top level use of LOCALLY, or anything else (e.g.
1035 ;;; MACROLET) at top level which has declarations and ordinary forms.
1036 ;;; We parse declarations and then recursively process the body.
1037 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
1038 (declare (list path))
1039 (multiple-value-bind (forms decls)
1040 (parse-body body :doc-string-allowed nil :toplevel t)
1041 (let* ((*lexenv* (process-decls decls vars funs))
1042 ;; FIXME: VALUES declaration
1044 ;; Binding *POLICY* is pretty much of a hack, since it
1045 ;; causes LOCALLY to "capture" enclosed proclamations. It
1046 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
1047 ;; value of *POLICY* as the policy. The need for this hack
1048 ;; is due to the quirk that there is no way to represent in
1049 ;; a POLICY that an optimize quality came from the default.
1051 ;; FIXME: Ideally, something should be done so that DECLAIM
1052 ;; inside LOCALLY works OK. Failing that, at least we could
1053 ;; issue a warning instead of silently screwing up.
1054 (*policy* (lexenv-policy *lexenv*))
1055 ;; This is probably also a hack
1056 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
1058 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*)))
1059 (process-toplevel-progn forms path compile-time-too))))
1061 ;;; Parse an EVAL-WHEN situations list, returning three flags,
1062 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
1063 ;;; the types of situations present in the list.
1064 (defun parse-eval-when-situations (situations)
1065 (when (or (not (listp situations))
1066 (set-difference situations
1073 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
1074 (let ((deprecated-names (intersection situations '(compile load eval))))
1075 (when deprecated-names
1076 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
1078 (values (intersection '(:compile-toplevel compile)
1080 (intersection '(:load-toplevel load) situations)
1081 (intersection '(:execute eval) situations)))
1084 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
1085 (defun functional-components (f)
1086 (declare (type functional f))
1088 (clambda (list (lambda-component f)))
1089 (optional-dispatch (let ((result nil))
1090 (flet ((maybe-frob (maybe-clambda)
1091 (when (and maybe-clambda
1092 (promise-ready-p maybe-clambda))
1093 (pushnew (lambda-component
1094 (force maybe-clambda))
1096 (map nil #'maybe-frob (optional-dispatch-entry-points f))
1097 (maybe-frob (optional-dispatch-more-entry f))
1098 (maybe-frob (optional-dispatch-main-entry f)))
1101 (defun make-functional-from-toplevel-lambda (lambda-expression
1105 ;; I'd thought NIL should
1106 ;; work, but it doesn't.
1107 ;; -- WHN 2001-09-20
1109 (let* ((*current-path* path)
1110 (component (make-empty-component))
1111 (*current-component* component)
1112 (debug-name-tail (or name (name-lambdalike lambda-expression)))
1113 (source-name (or name '.anonymous.)))
1114 (setf (component-name component) (debug-name 'initial-component debug-name-tail)
1115 (component-kind component) :initial)
1116 (let* ((fun (let ((*allow-instrumenting* t))
1117 (funcall #'ir1-convert-lambdalike
1119 :source-name source-name)))
1120 ;; Convert the XEP using the policy of the real function. Otherwise
1121 ;; the wrong policy will be used for deciding whether to type-check
1122 ;; the parameters of the real function (via CONVERT-CALL /
1123 ;; PROPAGATE-TO-ARGS). -- JES, 2007-02-27
1124 (*lexenv* (make-lexenv :policy (lexenv-policy (functional-lexenv fun))))
1125 (xep (ir1-convert-lambda (make-xep-lambda-expression fun)
1126 :source-name source-name
1127 :debug-name (debug-name 'tl-xep debug-name-tail)
1130 (assert-global-function-definition-type name fun))
1131 (setf (functional-kind xep) :external
1132 (functional-entry-fun xep) fun
1133 (functional-entry-fun fun) xep
1134 (component-reanalyze component) t
1135 (functional-has-external-references-p xep) t)
1136 (reoptimize-component component :maybe)
1137 (locall-analyze-xep-entry-point fun)
1138 ;; Any leftover REFs to FUN outside local calls get replaced with the
1140 (substitute-leaf-if (lambda (ref)
1141 (let* ((lvar (ref-lvar ref))
1142 (dest (when lvar (lvar-dest lvar)))
1143 (kind (when (basic-combination-p dest)
1144 (basic-combination-kind dest))))
1150 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
1151 ;;; description of the result.
1152 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
1153 ;;; into core and return the compiled FUNCTION value.
1154 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
1155 ;;; into the fasl file and return a dump handle.
1157 ;;; If NAME is provided, then we try to use it as the name of the
1158 ;;; function for debugging/diagnostic information.
1159 (defun %compile (lambda-expression
1164 ;; This magical idiom seems to be the appropriate
1165 ;; path for compiling standalone LAMBDAs, judging
1166 ;; from the CMU CL code and experiment, so it's a
1167 ;; nice default for things where we don't have a
1168 ;; real source path (as in e.g. inside CL:COMPILE).
1169 '(original-source-start 0 0)))
1171 (legal-fun-name-or-type-error name))
1172 (let* ((*lexenv* (make-lexenv
1174 :handled-conditions *handled-conditions*
1175 :disabled-package-locks *disabled-package-locks*))
1176 (*compiler-sset-counter* 0)
1177 (fun (make-functional-from-toplevel-lambda lambda-expression
1181 ;; FIXME: The compile-it code from here on is sort of a
1182 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1183 ;; better to find a way to share the code there; or
1184 ;; alternatively, to use this code to replace the code there.
1185 ;; (The second alternative might be pretty easy if we used
1186 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1187 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1189 (locall-analyze-clambdas-until-done (list fun))
1191 (let ((components-from-dfo (find-initial-dfo (list fun))))
1192 (dolist (component-from-dfo components-from-dfo)
1193 (compile-component component-from-dfo)
1194 (replace-toplevel-xeps component-from-dfo))
1196 (let ((entry-table (etypecase *compile-object*
1197 (fasl-output (fasl-output-entry-table
1199 (core-object (core-object-entry-table
1200 *compile-object*)))))
1201 (multiple-value-bind (result found-p)
1202 (gethash (leaf-info fun) entry-table)
1206 ;; KLUDGE: This code duplicates some other code in this
1207 ;; file. In the great reorganzation, the flow of program
1208 ;; logic changed from the original CMUCL model, and that
1209 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1210 ;; longer followed for CORE-OBJECTS, leading to BUG
1211 ;; 156. This place is transparently not the right one for
1212 ;; this code, but I don't have a clear enough overview of
1213 ;; the compiler to know how to rearrange it all so that
1214 ;; this operation fits in nicely, and it was blocking
1215 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1216 ;; ((..)) (DEFUN FOO ...))
1218 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1219 ;; ideal way, as (1) definitions typed in at the REPL
1220 ;; without an INLINE declaration will give a NULL
1221 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1222 ;; and (2) INLINE declarations will yield a
1223 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1224 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1227 ;; (2) is probably fairly easy to fix -- it is, after all,
1228 ;; a matter of list manipulation (or possibly of teaching
1229 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1230 ;; significantly harder, as the association between
1231 ;; function object and source is a tricky one.
1233 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1234 ;; non-NULL list) when the function in question has been
1235 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1236 ;; work when it has been compiled as part of the top-level
1237 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1238 ;; ...). -- CSR, 2002-11-02
1239 (when (core-object-p *compile-object*)
1240 (fix-core-source-info *source-info* *compile-object* result))
1242 (mapc #'clear-ir1-info components-from-dfo)
1245 (defun process-toplevel-cold-fset (name lambda-expression path)
1246 (unless (producing-fasl-file)
1247 (error "can't COLD-FSET except in a fasl file"))
1248 (legal-fun-name-or-type-error name)
1249 (fasl-dump-cold-fset name
1250 (%compile lambda-expression
1257 (defun note-top-level-form (form &optional finalp)
1258 (when *compile-print*
1259 (cond ((not *top-level-form-noted*)
1260 (let ((*print-length* 2)
1262 (*print-pretty* nil))
1263 (with-compiler-io-syntax
1265 #-sb-xc-host "~&; ~:[compiling~;converting~] ~S"
1266 #+sb-xc-host "~&; ~:[x-compiling~;x-converting~] ~S"
1267 *block-compile* form)))
1270 (eq :top-level-forms *compile-print*)
1271 (neq form *top-level-form-noted*))
1272 (let ((*print-length* 1)
1274 (*print-pretty* nil))
1275 (with-compiler-io-syntax
1276 (compiler-mumble "~&; ... top level ~S" form)))
1279 *top-level-form-noted*))))
1281 ;;; Handle the evaluation the a :COMPILE-TOPLEVEL body during
1282 ;;; compilation. Normally just evaluate in the appropriate
1283 ;;; environment, but also compile if outputting a CFASL.
1284 (defun eval-compile-toplevel (body path)
1285 (eval-tlf `(progn ,@body) (source-path-tlf-number path) *lexenv*)
1286 (when *compile-toplevel-object*
1287 (let ((*compile-object* *compile-toplevel-object*))
1288 (convert-and-maybe-compile `(progn ,@body) path))))
1290 ;;; Process a top level FORM with the specified source PATH.
1291 ;;; * If this is a magic top level form, then do stuff.
1292 ;;; * If this is a macro, then expand it.
1293 ;;; * Otherwise, just compile it.
1295 ;;; COMPILE-TIME-TOO is as defined in ANSI
1296 ;;; "3.2.3.1 Processing of Top Level Forms".
1297 (defun process-toplevel-form (form path compile-time-too)
1298 (declare (list path))
1300 (catch 'process-toplevel-form-error-abort
1301 (let* ((path (or (get-source-path form) (cons form path)))
1302 (*current-path* path)
1303 (*compiler-error-bailout*
1304 (lambda (&optional condition)
1305 (convert-and-maybe-compile
1306 (make-compiler-error-form condition form)
1308 (throw 'process-toplevel-form-error-abort nil))))
1310 (flet ((default-processor (form)
1311 (let ((*top-level-form-noted* (note-top-level-form form)))
1312 ;; When we're cross-compiling, consider: what should we
1313 ;; do when we hit e.g.
1314 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1315 ;; (DEFUN FOO (X) (+ 7 X)))?
1316 ;; DEFUN has a macro definition in the cross-compiler,
1317 ;; and a different macro definition in the target
1318 ;; compiler. The only sensible thing is to use the
1319 ;; target compiler's macro definition, since the
1320 ;; cross-compiler's macro is in general into target
1321 ;; functions which can't meaningfully be executed at
1322 ;; cross-compilation time. So make sure we do the EVAL
1323 ;; here, before we macroexpand.
1325 ;; Then things get even dicier with something like
1326 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1327 ;; where we have to make sure that we don't uncross
1328 ;; the SB!XC: prefix before we do EVAL, because otherwise
1329 ;; we'd be trying to redefine the cross-compilation host's
1332 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1335 (when compile-time-too
1336 (eval form)) ; letting xc host EVAL do its own macroexpansion
1337 (let* (;; (We uncross the operator name because things
1338 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1339 ;; should be equivalent to their CL: counterparts
1340 ;; when being compiled as target code. We leave
1341 ;; the rest of the form uncrossed because macros
1342 ;; might yet expand into EVAL-WHEN stuff, and
1343 ;; things inside EVAL-WHEN can't be uncrossed
1344 ;; until after we've EVALed them in the
1345 ;; cross-compilation host.)
1346 (slightly-uncrossed (cons (uncross (first form))
1348 (expanded (preprocessor-macroexpand-1
1349 slightly-uncrossed)))
1350 (if (eq expanded slightly-uncrossed)
1351 ;; (Now that we're no longer processing toplevel
1352 ;; forms, and hence no longer need to worry about
1353 ;; EVAL-WHEN, we can uncross everything.)
1354 (convert-and-maybe-compile expanded path)
1355 ;; (We have to demote COMPILE-TIME-TOO to NIL
1356 ;; here, no matter what it was before, since
1357 ;; otherwise we'd tend to EVAL subforms more than
1358 ;; once, because of WHEN COMPILE-TIME-TOO form
1360 (process-toplevel-form expanded path nil))))
1361 ;; When we're not cross-compiling, we only need to
1362 ;; macroexpand once, so we can follow the 1-thru-6
1363 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1364 ;; Top Level Forms".
1366 (let ((expanded (preprocessor-macroexpand-1 form)))
1367 (cond ((eq expanded form)
1368 (when compile-time-too
1369 (eval-compile-toplevel (list form) path))
1370 (convert-and-maybe-compile form path))
1372 (process-toplevel-form expanded
1374 compile-time-too)))))))
1377 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1378 ;; (1) SBCL gets smart enough to handle global
1379 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1380 ;; implementors start using symbol macros in a way which
1381 ;; interacts with SB-XC/CL distinction.)
1382 (convert-and-maybe-compile form path)
1384 (default-processor form)
1385 (flet ((need-at-least-one-arg (form)
1387 (compiler-error "~S form is too short: ~S"
1391 ;; In the cross-compiler, top level COLD-FSET arranges
1392 ;; for static linking at cold init time.
1395 (aver (not compile-time-too))
1396 (destructuring-bind (cold-fset fun-name lambda-expression) form
1397 (declare (ignore cold-fset))
1398 (process-toplevel-cold-fset fun-name
1401 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1402 (need-at-least-one-arg form)
1403 (destructuring-bind (special-operator magic &rest body) form
1404 (ecase special-operator
1406 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1407 ;; "3.2.3.1 Processing of Top Level Forms".
1408 (multiple-value-bind (ct lt e)
1409 (parse-eval-when-situations magic)
1410 (let ((new-compile-time-too (or ct
1411 (and compile-time-too
1413 (cond (lt (process-toplevel-progn
1414 body path new-compile-time-too))
1415 (new-compile-time-too
1416 (eval-compile-toplevel body path))))))
1418 (funcall-in-macrolet-lexenv
1420 (lambda (&key funs prepend)
1421 (declare (ignore funs))
1422 (aver (null prepend))
1423 (process-toplevel-locally body
1428 (funcall-in-symbol-macrolet-lexenv
1430 (lambda (&key vars prepend)
1431 (aver (null prepend))
1432 (process-toplevel-locally body
1438 (process-toplevel-locally (rest form) path compile-time-too))
1440 (process-toplevel-progn (rest form) path compile-time-too))
1441 (t (default-processor form))))))))
1445 ;;;; load time value support
1447 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1449 ;;; Return T if we are currently producing a fasl file and hence
1450 ;;; constants need to be dumped carefully.
1451 (defun producing-fasl-file ()
1452 (fasl-output-p *compile-object*))
1454 ;;; Compile FORM and arrange for it to be called at load-time. Return
1455 ;;; the dumper handle and our best guess at the type of the object.
1456 (defun compile-load-time-value (form)
1457 (let ((lambda (compile-load-time-stuff form t)))
1459 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1460 (let ((type (leaf-type lambda)))
1461 (if (fun-type-p type)
1462 (single-value-type (fun-type-returns type))
1465 ;;; Compile the FORMS and arrange for them to be called (for effect,
1466 ;;; not value) at load time.
1467 (defun compile-make-load-form-init-forms (forms)
1468 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1469 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1471 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1472 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1473 (defun compile-load-time-stuff (form for-value)
1475 (let* ((*lexenv* (make-null-lexenv))
1476 (lambda (ir1-toplevel form *current-path* for-value nil)))
1477 (compile-toplevel (list lambda) t)
1480 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1481 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1482 ;;; don't try to combine this component with anything else and frob
1483 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1484 ;;; compiling, because it was merged with a run-time component.
1485 (defun compile-load-time-value-lambda (lambdas)
1486 (aver (null (cdr lambdas)))
1487 (let* ((lambda (car lambdas))
1488 (component (lambda-component lambda)))
1489 (when (eql (component-kind component) :toplevel)
1490 (setf (component-name component) (leaf-debug-name lambda))
1491 (compile-component component)
1492 (clear-ir1-info component))))
1496 (defun object-call-toplevel-lambda (tll)
1497 (declare (type functional tll))
1498 (let ((object *compile-object*))
1500 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1501 (core-object (core-call-toplevel-lambda tll object))
1504 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1505 ;;; the resulting function to be called.
1506 (defun sub-compile-toplevel-lambdas (lambdas)
1507 (declare (list lambdas))
1509 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1510 (compile-component component)
1511 (clear-ir1-info component)
1512 (object-call-toplevel-lambda tll)))
1515 ;;; Compile top level code and call the top level lambdas. We pick off
1516 ;;; top level lambdas in non-top-level components here, calling
1517 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1518 (defun compile-toplevel-lambdas (lambdas)
1519 (declare (list lambdas))
1520 (let ((len (length lambdas)))
1521 (flet ((loser (start)
1522 (or (position-if (lambda (x)
1523 (not (eq (component-kind
1524 (node-component (lambda-bind x)))
1527 ;; this used to read ":start start", but
1528 ;; start can be greater than len, which
1529 ;; is an error according to ANSI - CSR,
1531 :start (min start len))
1533 (do* ((start 0 (1+ loser))
1534 (loser (loser start) (loser start)))
1536 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1537 (unless (= loser len)
1538 (object-call-toplevel-lambda (elt lambdas loser))))))
1541 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1544 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1545 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1546 (defun compile-toplevel (lambdas load-time-value-p)
1547 (declare (list lambdas))
1549 (maybe-mumble "locall ")
1550 (locall-analyze-clambdas-until-done lambdas)
1552 (maybe-mumble "IDFO ")
1553 (multiple-value-bind (components top-components hairy-top)
1554 (find-initial-dfo lambdas)
1555 (let ((all-components (append components top-components)))
1556 (when *check-consistency*
1557 (maybe-mumble "[check]~%")
1558 (check-ir1-consistency all-components))
1560 (dolist (component (append hairy-top top-components))
1561 (pre-physenv-analyze-toplevel component))
1563 (dolist (component components)
1564 (compile-component component)
1565 (replace-toplevel-xeps component))
1567 (when *check-consistency*
1568 (maybe-mumble "[check]~%")
1569 (check-ir1-consistency all-components))
1571 (if load-time-value-p
1572 (compile-load-time-value-lambda lambdas)
1573 (compile-toplevel-lambdas lambdas))
1575 (mapc #'clear-ir1-info components)
1579 ;;; Actually compile any stuff that has been queued up for block
1581 (defun finish-block-compilation ()
1582 (when *block-compile*
1583 (when *compile-print*
1584 (compiler-mumble "~&; block compiling converted top level forms..."))
1585 (when *toplevel-lambdas*
1586 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1587 (setq *toplevel-lambdas* ()))
1588 (setq *block-compile* nil)
1589 (setq *entry-points* nil)))
1591 (defun handle-condition-p (condition)
1593 (etypecase *compiler-error-context*
1595 (node-lexenv *compiler-error-context*))
1596 (compiler-error-context
1597 (let ((lexenv (compiler-error-context-lexenv
1598 *compiler-error-context*)))
1602 (let ((muffles (lexenv-handled-conditions lexenv)))
1603 (if (null muffles) ; common case
1605 (dolist (muffle muffles nil)
1606 (destructuring-bind (typespec . restart-name) muffle
1607 (when (and (typep condition typespec)
1608 (find-restart restart-name condition))
1611 (defun handle-condition-handler (condition)
1613 (etypecase *compiler-error-context*
1615 (node-lexenv *compiler-error-context*))
1616 (compiler-error-context
1617 (let ((lexenv (compiler-error-context-lexenv
1618 *compiler-error-context*)))
1622 (let ((muffles (lexenv-handled-conditions lexenv)))
1624 (dolist (muffle muffles (bug "fell through"))
1625 (destructuring-bind (typespec . restart-name) muffle
1626 (when (typep condition typespec)
1627 (awhen (find-restart restart-name condition)
1628 (invoke-restart it))))))))
1630 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1631 ;;; Return (VALUES ABORT-P WARNINGS-P FAILURE-P).
1632 (defun sub-compile-file (info)
1633 (declare (type source-info info))
1634 (let ((*package* (sane-package))
1635 (*readtable* *readtable*)
1636 (sb!xc:*compile-file-pathname* nil) ; really bound in
1637 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1639 (*code-coverage-records* (make-hash-table :test 'equal))
1640 (*code-coverage-blocks* (make-hash-table :test 'equal))
1641 (*handled-conditions* *handled-conditions*)
1642 (*disabled-package-locks* *disabled-package-locks*)
1643 (*lexenv* (make-null-lexenv))
1644 (*block-compile* *block-compile-arg*)
1645 (*toplevel-lambdas* ())
1646 (*fun-names-in-this-file* ())
1647 (*allow-instrumenting* nil)
1648 (*compiler-error-bailout*
1649 (lambda (&optional error)
1650 (declare (ignore error))
1651 (return-from sub-compile-file (values t t t))))
1652 (*current-path* nil)
1653 (*last-source-context* nil)
1654 (*last-original-source* nil)
1655 (*last-source-form* nil)
1656 (*last-format-string* nil)
1657 (*last-format-args* nil)
1658 (*last-message-count* 0)
1659 ;; FIXME: Do we need this rebinding here? It's a literal
1660 ;; translation of the old CMU CL rebinding to
1661 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1662 ;; and it's not obvious whether the rebinding to itself is
1663 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1664 (*info-environment* *info-environment*)
1665 (*compiler-sset-counter* 0)
1666 (sb!xc:*gensym-counter* 0))
1668 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1669 (with-compilation-values
1670 (sb!xc:with-compilation-unit ()
1673 (sub-sub-compile-file info)
1674 (unless (zerop (hash-table-count *code-coverage-records*))
1675 ;; Dump the code coverage records into the fasl.
1676 (fopcompile `(record-code-coverage
1677 ',(namestring *compile-file-pathname*)
1679 (maphash (lambda (k v)
1680 (declare (ignore k))
1682 *code-coverage-records*)
1686 (finish-block-compilation)
1687 (let ((object *compile-object*))
1689 (fasl-output (fasl-dump-source-info info object))
1690 (core-object (fix-core-source-info info object))
1693 ;; Some errors are sufficiently bewildering that we just fail
1694 ;; immediately, without trying to recover and compile more of
1696 (fatal-compiler-error (condition)
1698 (fresh-line *error-output*)
1699 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
1700 (format *error-output*
1701 "~@<~@:_compilation aborted because of fatal error: ~2I~_~A~@:_~:>"
1702 (encapsulated-condition condition)))
1703 (finish-output *error-output*)
1706 ;;; Return a pathname for the named file. The file must exist.
1707 (defun verify-source-file (pathname-designator)
1708 (let* ((pathname (pathname pathname-designator))
1709 (default-host (make-pathname :host (pathname-host pathname))))
1710 (flet ((try-with-type (path type error-p)
1711 (let ((new (merge-pathnames
1712 path (make-pathname :type type
1713 :defaults default-host))))
1714 (if (probe-file new)
1716 (and error-p (truename new))))))
1717 (cond ((typep pathname 'logical-pathname)
1718 (try-with-type pathname "LISP" t))
1719 ((probe-file pathname) pathname)
1720 ((try-with-type pathname "lisp" nil))
1721 ((try-with-type pathname "lisp" t))))))
1723 (defun elapsed-time-to-string (internal-time-delta)
1724 (multiple-value-bind (tsec remainder)
1725 (truncate internal-time-delta internal-time-units-per-second)
1726 (let ((ms (truncate remainder (/ internal-time-units-per-second 1000))))
1727 (multiple-value-bind (tmin sec) (truncate tsec 60)
1728 (multiple-value-bind (thr min) (truncate tmin 60)
1729 (format nil "~D:~2,'0D:~2,'0D.~3,'0D" thr min sec ms))))))
1731 ;;; Print some junk at the beginning and end of compilation.
1732 (defun print-compile-start-note (source-info)
1733 (declare (type source-info source-info))
1734 (let ((file-info (source-info-file-info source-info)))
1735 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1736 (namestring (file-info-name file-info))
1737 (sb!int:format-universal-time nil
1738 (file-info-write-date
1742 :print-timezone nil)))
1745 (defun print-compile-end-note (source-info won)
1746 (declare (type source-info source-info))
1747 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1749 (elapsed-time-to-string
1750 (- (get-internal-real-time)
1751 (source-info-start-real-time source-info))))
1754 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1755 ;;; out of the compile, then abort the writing of the output file, so
1756 ;;; that we don't overwrite it with known garbage.
1757 (defun sb!xc:compile-file
1762 (output-file (cfp-output-file-default input-file))
1763 ;; FIXME: ANSI doesn't seem to say anything about
1764 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1766 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1767 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1768 (external-format :default)
1772 ((:block-compile *block-compile-arg*) nil)
1773 (emit-cfasl *emit-cfasl*))
1775 "Compile INPUT-FILE, producing a corresponding fasl file and
1776 returning its filename.
1779 If true, a message per non-macroexpanded top level form is printed
1780 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1781 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1782 no such message, but their subforms do.
1784 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1785 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1786 For example, compiling an IN-PACKAGE form will result in a message about
1787 a top level SETQ in addition to the message about the IN-PACKAGE form'
1790 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1793 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1794 argument, it is not currently supported. (non-standard)
1797 If given, internal data structures are dumped to the specified
1798 file, or if a value of T is given, to a file of *.trace type
1799 derived from the input file name. (non-standard)
1802 (Experimental). If true, outputs the toplevel compile-time effects
1803 of this file into a separate .cfasl file."
1804 ;;; Block compilation is currently broken.
1806 "Also, as a workaround for vaguely-non-ANSI behavior, the
1807 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1808 multiple functions are compiled together as a unit, resolving function
1809 references at compile time. NIL means that global function names are
1810 never resolved at compilation time. Currently NIL is the default
1811 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1812 of the ANSI spec allows this behavior under all circumstances, the
1813 compiler's runtime scales badly when it tries to do this for large
1814 files. If/when this performance problem is fixed, the block
1815 compilation default behavior will probably be made dependent on the
1816 SPEED and COMPILATION-SPEED optimization values, and the
1817 :BLOCK-COMPILE argument will probably become deprecated."
1819 (let* ((fasl-output nil)
1821 (output-file-name nil)
1822 (coutput-file-name nil)
1825 (failure-p t) ; T in case error keeps this from being set later
1826 (input-pathname (verify-source-file input-file))
1827 (source-info (make-file-source-info input-pathname external-format))
1828 (*compiler-trace-output* nil)) ; might be modified below
1833 (setq output-file-name
1834 (sb!xc:compile-file-pathname input-file
1835 :output-file output-file))
1837 (open-fasl-output output-file-name
1838 (namestring input-pathname))))
1840 (setq coutput-file-name
1841 (make-pathname :type "cfasl"
1842 :defaults output-file-name))
1844 (open-fasl-output coutput-file-name
1845 (namestring input-pathname))))
1847 (let* ((default-trace-file-pathname
1848 (make-pathname :type "trace" :defaults input-pathname))
1849 (trace-file-pathname
1850 (if (eql trace-file t)
1851 default-trace-file-pathname
1852 (merge-pathnames trace-file
1853 default-trace-file-pathname))))
1854 (setf *compiler-trace-output*
1855 (open trace-file-pathname
1856 :if-exists :supersede
1857 :direction :output))))
1859 (when sb!xc:*compile-verbose*
1860 (print-compile-start-note source-info))
1862 (let ((*compile-object* fasl-output)
1863 (*compile-toplevel-object* cfasl-output))
1864 (setf (values abort-p warnings-p failure-p)
1865 (sub-compile-file source-info))))
1867 (close-source-info source-info)
1870 (close-fasl-output fasl-output abort-p)
1871 (setq output-file-name
1872 (pathname (fasl-output-stream fasl-output)))
1873 (when (and (not abort-p) sb!xc:*compile-verbose*)
1874 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1877 (close-fasl-output cfasl-output abort-p)
1878 (when (and (not abort-p) sb!xc:*compile-verbose*)
1879 (compiler-mumble "; ~A written~%" (namestring coutput-file-name))))
1881 (when sb!xc:*compile-verbose*
1882 (print-compile-end-note source-info (not abort-p)))
1884 (when *compiler-trace-output*
1885 (close *compiler-trace-output*)))
1887 ;; CLHS says that the first value is NIL if the "file could not
1888 ;; be created". We interpret this to mean "a valid fasl could not
1889 ;; be created" -- which can happen if the compilation is aborted
1890 ;; before the whole file has been processed, due to eg. a reader
1892 (values (when (and (not abort-p) output-file)
1893 ;; Hack around filesystem race condition...
1894 (or (probe-file output-file-name) output-file-name))
1898 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1899 ;;; the OUTPUT-FILE argument
1901 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1902 ;;; that results from merging the INPUT-FILE with the value of
1903 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1904 ;;; default to the appropriate implementation-defined default type for
1906 (defun cfp-output-file-default (input-file)
1907 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1908 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1911 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1912 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1913 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1914 ;;; pathname, it is translated into a physical pathname as if by
1915 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1916 ;;; So I haven't really tried to make this precisely ANSI-compatible
1917 ;;; at the level of e.g. whether it returns logical pathname or a
1918 ;;; physical pathname. Patches to make it more correct are welcome.
1919 ;;; -- WHN 2000-12-09
1920 (defun sb!xc:compile-file-pathname (input-file
1922 (output-file nil output-file-p)
1925 "Return a pathname describing what file COMPILE-FILE would write to given
1928 (merge-pathnames output-file (cfp-output-file-default input-file))
1929 (cfp-output-file-default input-file)))
1931 ;;;; MAKE-LOAD-FORM stuff
1933 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1934 ;;; finds a constant structure, it invokes this to arrange for proper
1935 ;;; dumping. If it turns out that the constant has already been
1936 ;;; dumped, then we don't need to do anything.
1938 ;;; If the constant hasn't been dumped, then we check to see whether
1939 ;;; we are in the process of creating it. We detect this by
1940 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1941 ;;; the constants we are in the process of creating. Actually, each
1942 ;;; entry is a list of the constant and any init forms that need to be
1943 ;;; processed on behalf of that constant.
1945 ;;; It's not necessarily an error for this to happen. If we are
1946 ;;; processing the init form for some object that showed up *after*
1947 ;;; the original reference to this constant, then we just need to
1948 ;;; defer the processing of that init form. To detect this, we
1949 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1950 ;;; constants created since the last time we started processing an
1951 ;;; init form. If the constant passed to emit-make-load-form shows up
1952 ;;; in this list, then there is a circular chain through creation
1953 ;;; forms, which is an error.
1955 ;;; If there is some intervening init form, then we blow out of
1956 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1957 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1958 ;;; offending init form can be tacked onto the init forms for the
1959 ;;; circular object.
1961 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1962 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1963 ;;; whether the creation form is the magic value
1964 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1965 ;;; dumper will eventually get its hands on the object and use the
1966 ;;; normal structure dumping noise on it.
1968 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1969 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1970 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1971 ;;; dumper to use that result instead whenever it sees this constant.
1973 ;;; Now we try to compile the init form. We bind
1974 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1975 ;;; form (and any init forms that were added because of circularity
1976 ;;; detection). If this works, great. If not, we add the init forms to
1977 ;;; the init forms for the object that caused the problems and let it
1979 (defvar *constants-being-created* nil)
1980 (defvar *constants-created-since-last-init* nil)
1981 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1982 (defun emit-make-load-form (constant &optional (name nil namep))
1983 (aver (fasl-output-p *compile-object*))
1984 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1985 ;; KLUDGE: This special hack is because I was too lazy
1986 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1987 ;; function of LAYOUT returns nontrivial forms when
1988 ;; building the cross-compiler but :IGNORE-IT when
1989 ;; cross-compiling or running under the target Lisp. --
1991 #+sb-xc-host (typep constant 'layout))
1992 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1994 (when (find constant *constants-created-since-last-init* :test #'eq)
1996 (throw 'pending-init circular-ref)))
1997 (multiple-value-bind (creation-form init-form)
1999 ;; If the constant is a reference to a named constant, we can
2000 ;; just use SYMBOL-VALUE during LOAD.
2001 (values `(symbol-value ',name) nil)
2003 (sb!xc:make-load-form constant (make-null-lexenv))
2005 (compiler-error condition))))
2007 (:sb-just-dump-it-normally
2008 (fasl-validate-structure constant *compile-object*)
2013 (let* ((name (write-to-string constant :level 1 :length 2))
2015 (list constant name init-form)
2017 (let ((*constants-being-created*
2018 (cons info *constants-being-created*))
2019 (*constants-created-since-last-init*
2020 (cons constant *constants-created-since-last-init*)))
2023 (fasl-note-handle-for-constant
2025 (compile-load-time-value
2029 (compiler-error "circular references in creation form for ~S"
2032 (let* ((*constants-created-since-last-init* nil)
2034 (catch 'pending-init
2035 (loop for (name form) on (cdr info) by #'cddr
2036 collect name into names
2037 collect form into forms
2038 finally (compile-make-load-form-init-forms forms))
2041 (setf (cdr circular-ref)
2042 (append (cdr circular-ref) (cdr info))))))))))))
2045 ;;;; Host compile time definitions
2047 (defun compile-in-lexenv (name lambda lexenv)
2048 (declare (ignore lexenv))
2049 (compile name lambda))
2052 (defun eval-tlf (form index &optional lexenv)
2053 (declare (ignore index lexenv))