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 *free-funs
* *source-paths
*
19 *undefined-warnings
* *compiler-error-count
*
20 *compiler-warning-count
* *compiler-style-warning-count
*
22 *compiler-error-bailout
*
23 *last-format-string
* *last-format-args
*
24 *last-message-count
* *last-error-context
*
25 *lexenv
* *fun-names-in-this-file
*
26 *allow-instrumenting
*))
28 ;;; Whether reference to a thing which cannot be defined causes a full
30 (defvar *flame-on-necessarily-undefined-thing
* nil
)
32 (defvar *check-consistency
* nil
)
34 ;;; Set to NIL to disable loop analysis for register allocation.
35 (defvar *loop-analyze
* t
)
37 ;;; Bind this to a stream to capture various internal debugging output.
38 (defvar *compiler-trace-output
* nil
)
40 ;;; The current block compilation state. These are initialized to the
41 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
44 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
45 ;;; argument, which overrides any internal declarations.
46 (defvar *block-compile
*)
47 (defvar *block-compile-arg
*)
48 (declaim (type (member nil t
:specified
) *block-compile
* *block-compile-arg
*))
49 (defvar *entry-points
*)
50 (declaim (list *entry-points
*))
52 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
53 ;;; lambdas resulting from compiling subforms. (In reverse order.)
54 (defvar *toplevel-lambdas
*)
55 (declaim (list *toplevel-lambdas
*))
57 ;;; The current non-macroexpanded toplevel form as printed when
58 ;;; *compile-print* is true.
59 (defvar *top-level-form-noted
* nil
)
61 (defvar sb
!xc
:*compile-verbose
* t
63 "The default for the :VERBOSE argument to COMPILE-FILE.")
64 (defvar sb
!xc
:*compile-print
* t
66 "The default for the :PRINT argument to COMPILE-FILE.")
67 (defvar *compile-progress
* nil
69 "When this is true, the compiler prints to *STANDARD-OUTPUT* progress
70 information about the phases of compilation of each function. (This
71 is useful mainly in large block compilations.)")
73 (defvar sb
!xc
:*compile-file-pathname
* nil
75 "The defaulted pathname of the file currently being compiled, or NIL if not
77 (defvar sb
!xc
:*compile-file-truename
* nil
79 "The TRUENAME of the file currently being compiled, or NIL if not
82 (declaim (type (or pathname null
)
83 sb
!xc
:*compile-file-pathname
*
84 sb
!xc
:*compile-file-truename
*))
86 ;;; the SOURCE-INFO structure for the current compilation. This is
87 ;;; null globally to indicate that we aren't currently in any
88 ;;; identifiable compilation.
89 (defvar *source-info
* nil
)
91 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
92 ;;; normally causes nested uses to be no-ops).
93 (defvar *in-compilation-unit
* nil
)
95 ;;; Count of the number of compilation units dynamically enclosed by
96 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
97 (defvar *aborted-compilation-unit-count
*)
99 ;;; Mumble conditional on *COMPILE-PROGRESS*.
100 (defun maybe-mumble (&rest foo
)
101 (when *compile-progress
*
102 (compiler-mumble "~&")
103 (pprint-logical-block (*standard-output
* nil
:per-line-prefix
"; ")
104 (apply #'compiler-mumble foo
))))
106 (deftype object
() '(or fasl-output core-object null
))
108 (defvar *compile-object
* nil
)
109 (declaim (type object
*compile-object
*))
110 (defvar *compile-toplevel-object
* nil
)
112 (defvar *emit-cfasl
* nil
)
114 (defvar *fopcompile-label-counter
*)
116 ;; Used during compilation to map code paths to the matching
117 ;; instrumentation conses.
118 (defvar *code-coverage-records
* nil
)
119 ;; Used during compilation to keep track of with source paths have been
120 ;; instrumented in which blocks.
121 (defvar *code-coverage-blocks
* nil
)
122 ;; Stores the code coverage instrumentation results. Keys are namestrings, the
123 ;; value is a list of (CONS PATH STATE), where STATE is +CODE-COVERAGE-UNMARKED+
124 ;; for a path that has not been visited, and T for one that has.
125 (defvar *code-coverage-info
* (make-hash-table :test
'equal
))
128 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
130 (defmacro sb
!xc
:with-compilation-unit
(options &body body
)
132 "Affects compilations that take place within its dynamic extent. It is
133 intended to be eg. wrapped around the compilation of all files in the same system.
135 Following options are defined:
137 :OVERRIDE Boolean-Form
138 One of the effects of this form is to delay undefined warnings until the
139 end of the form, instead of giving them at the end of each compilation.
140 If OVERRIDE is NIL (the default), then the outermost
141 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
142 OVERRIDE true causes that form to grab any enclosed warnings, even if it
143 is enclosed by another WITH-COMPILATION-UNIT.
145 :POLICY Optimize-Declaration-Form
146 Provides dynamic scoping for global compiler optimization qualities and
147 restrictions, limiting effects of subsequent OPTIMIZE proclamations and
148 calls to SB-EXT:RESTRICT-COMPILER-POLICY to the dynamic scope of BODY.
150 If OVERRIDE is false, specified POLICY is merged with current global
151 policy. If OVERRIDE is true, current global policy, including any
152 restrictions, is discarded in favor of the specified POLICY.
154 Supplying POLICY NIL is equivalent to the option not being supplied at
155 all, ie. dynamic scoping of policy does not take place.
157 This option is an SBCL-specific experimental extension: Interface
160 :SOURCE-NAMESTRING Namestring-Form
161 Attaches the value returned by the Namestring-Form to the internal
162 debug-source information as the namestring of the source file. Normally
163 the namestring of the input-file for COMPILE-FILE is used: this option
164 can be used to provide source-file information for functions compiled
165 using COMPILE, or to override the input-file of COMPILE-FILE.
167 If both an outer and an inner WITH-COMPILATION-UNIT provide a
168 SOURCE-NAMESTRING, the inner one takes precedence. Unaffected
171 This is an SBCL-specific extension.
173 :SOURCE-PLIST Plist-Form
174 Attaches the value returned by the Plist-Form to internal debug-source
175 information of functions compiled in within the dynamic extent of BODY.
177 Primarily for use by development environments, in order to eg. associate
178 function definitions with editor-buffers. Can be accessed using
179 SB-INTROSPECT:DEFINITION-SOURCE-PLIST.
181 If an outer WITH-COMPILATION-UNIT form also provide a SOURCE-PLIST, it
182 is appended to the end of the provided SOURCE-PLIST. Unaffected
185 This is an SBCL-specific extension.
189 ;; Prevent proclamations from the file leaking, and restrict
190 ;; SAFETY to 3 -- otherwise uses the current global policy.
191 (with-compilation-unit (:policy '(optimize))
192 (restrict-compiler-policy 'safety 3)
195 ;; Using default policy instead of the current global one,
196 ;; except for DEBUG 3.
197 (with-compilation-unit (:policy '(optimize debug)
201 ;; Same as if :POLICY had not been specified at all: SAFETY 3
202 ;; proclamation leaks out from WITH-COMPILATION-UNIT.
203 (with-compilation-unit (:policy nil)
204 (declaim (optimize safety))
207 `(%with-compilation-unit
(lambda () ,@body
) ,@options
))
209 (defvar *source-plist
* nil
)
210 (defvar *source-namestring
* nil
)
212 (defun %with-compilation-unit
(fn &key override policy source-plist source-namestring
)
213 (declare (type function fn
))
215 (let ((succeeded-p nil
)
216 (*source-plist
* (append source-plist
*source-plist
*))
218 (possibly-base-stringize
219 (or source-namestring
*source-namestring
*))))
220 (if (and *in-compilation-unit
* (not override
))
221 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
222 ;; ordinarily (unless OVERRIDE) basically a no-op.
224 (multiple-value-prog1 (funcall fn
) (setf succeeded-p t
))
226 (incf *aborted-compilation-unit-count
*)))
227 (let ((*aborted-compilation-unit-count
* 0)
228 (*compiler-error-count
* 0)
229 (*compiler-warning-count
* 0)
230 (*compiler-style-warning-count
* 0)
231 (*compiler-note-count
* 0)
232 (*undefined-warnings
* nil
)
233 (*in-compilation-unit
* t
))
234 (handler-bind ((parse-unknown-type
236 (note-undefined-reference
237 (parse-unknown-type-specifier c
)
240 (multiple-value-prog1 (funcall fn
) (setf succeeded-p t
))
242 (incf *aborted-compilation-unit-count
*))
243 (summarize-compilation-unit (not succeeded-p
)))))))))
245 (let ((*policy
* (process-optimize-decl policy
(unless override
*policy
*)))
246 (*policy-restrictions
* (unless override
*policy-restrictions
*)))
250 ;;; Is NAME something that no conforming program can rely on
252 (defun name-reserved-by-ansi-p (name kind
)
255 (eq (symbol-package (fun-name-block-name name
))
258 (let ((symbol (typecase name
260 ((cons symbol
) (car name
))
261 (t (return-from name-reserved-by-ansi-p nil
)))))
262 (eq (symbol-package symbol
) *cl-package
*)))))
264 ;;; This is to be called at the end of a compilation unit. It signals
265 ;;; any residual warnings about unknown stuff, then prints the total
266 ;;; error counts. ABORT-P should be true when the compilation unit was
267 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
268 ;;; enclosed nested compilation units that were aborted.
269 (defun summarize-compilation-unit (abort-p)
272 (handler-bind ((style-warning #'compiler-style-warning-handler
)
273 (warning #'compiler-warning-handler
))
275 (let ((undefs (sort *undefined-warnings
* #'string
<
277 (let ((x (undefined-warning-name x
)))
280 (prin1-to-string x
)))))))
281 (dolist (kind '(:variable
:function
:type
))
282 (let ((names (mapcar #'undefined-warning-name
283 (remove kind undefs
:test
#'neq
284 :key
#'undefined-warning-kind
))))
285 (when names
(push (cons kind names
) summary
))))
286 (dolist (undef undefs
)
287 (let ((name (undefined-warning-name undef
))
288 (kind (undefined-warning-kind undef
))
289 (warnings (undefined-warning-warnings undef
))
290 (undefined-warning-count (undefined-warning-count undef
)))
291 (dolist (*compiler-error-context
* warnings
)
292 (if #-sb-xc-host
(and (member kind
'(:function
:type
))
293 (name-reserved-by-ansi-p name kind
)
294 *flame-on-necessarily-undefined-thing
*)
299 "~@<The function ~S is undefined, and its name is ~
300 reserved by ANSI CL so that even if it were ~
301 defined later, the code doing so would not be ~
302 portable.~:@>" name
))
304 (if (and (consp name
) (eq 'quote
(car name
)))
306 "~@<Undefined type ~S. The name starts with ~S: ~
307 probably use of a quoted type name in a context ~
308 where the name is not evaluated.~:@>"
311 "~@<Undefined type ~S. Note that name ~S is ~
312 reserved by ANSI CL, so code defining a type with ~
313 that name would not be portable.~:@>" name
315 (if (eq kind
:variable
)
316 (compiler-warn "undefined ~(~A~): ~S" kind name
)
317 (compiler-style-warn "undefined ~(~A~): ~S" kind name
))))
318 (let ((warn-count (length warnings
)))
319 (when (and warnings
(> undefined-warning-count warn-count
))
320 (let ((more (- undefined-warning-count warn-count
)))
321 (if (eq kind
:variable
)
323 "~W more use~:P of undefined ~(~A~) ~S"
326 "~W more use~:P of undefined ~(~A~) ~S"
327 more kind name
))))))))))
329 (unless (and (not abort-p
)
330 (zerop *aborted-compilation-unit-count
*)
331 (zerop *compiler-error-count
*)
332 (zerop *compiler-warning-count
*)
333 (zerop *compiler-style-warning-count
*)
334 (zerop *compiler-note-count
*))
335 (pprint-logical-block (*error-output
* nil
:per-line-prefix
"; ")
336 (format *error-output
* "~&compilation unit ~:[finished~;aborted~]"
338 (dolist (cell summary
)
339 (destructuring-bind (kind &rest names
) cell
340 (format *error-output
*
341 "~& Undefined ~(~A~)~p:~
342 ~% ~{~<~% ~1:;~S~>~^ ~}"
343 kind
(length names
) names
)))
344 (format *error-output
* "~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
345 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
346 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
347 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
348 ~[~:;~:*~& printed ~W note~:P~]"
349 *aborted-compilation-unit-count
*
350 *compiler-error-count
*
351 *compiler-warning-count
*
352 *compiler-style-warning-count
*
353 *compiler-note-count
*))
354 (terpri *error-output
*)
355 (force-output *error-output
*))))
357 ;; Bidrectional map between IR1/IR2/assembler abstractions
358 ;; and a corresponding small integer identifier. One direction could be done
359 ;; by adding the integer ID as an object slot, but we want both directions.
360 (defstruct (compiler-ir-obj-map (:conc-name objmap-
)
361 (:constructor make-compiler-ir-obj-map
())
364 (obj-to-id (make-hash-table :test
'eq
) :read-only t
)
365 (id-to-cont (make-array 10) :type simple-vector
) ; number -> CTRAN or LVAR
366 (id-to-tn (make-array 10) :type simple-vector
) ; number -> TN
367 (id-to-label (make-array 10) :type simple-vector
) ; number -> LABEL
368 (cont-num 0 :type fixnum
)
369 (tn-id 0 :type fixnum
)
370 (label-id 0 :type fixnum
))
372 (declaim (type compiler-ir-obj-map
*compiler-ir-obj-map
*))
373 (defvar *compiler-ir-obj-map
*)
375 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
376 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
377 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
378 (defmacro with-compilation-values
(&body body
)
379 ;; This binding could just as well be in WITH-IR1-NAMESPACE, but
380 ;; since it's primarily a debugging tool, it's nicer to have
381 ;; a wider unique scope by ID.
382 `(let ((*compiler-ir-obj-map
* (make-compiler-ir-obj-map)))
384 (let ((*warnings-p
* nil
)
386 (handler-bind ((compiler-error #'compiler-error-handler
)
387 (style-warning #'compiler-style-warning-handler
)
388 (warning #'compiler-warning-handler
))
389 (values (progn ,@body
) *warnings-p
* *failure-p
*)))
390 (let ((map *compiler-ir-obj-map
*))
391 (clrhash (objmap-obj-to-id map
))
392 (fill (objmap-id-to-cont map
) nil
)
393 (fill (objmap-id-to-tn map
) nil
)
394 (fill (objmap-id-to-label map
) nil
)))))
396 ;;; THING is a kind of thing about which we'd like to issue a warning,
397 ;;; but showing at most one warning for a given set of <THING,FMT,ARGS>.
398 ;;; The compiler does a good job of making sure not to print repetitive
399 ;;; warnings for code that it compiles, but this solves a different problem.
400 ;;; Specifically, for a warning from PARSE-LAMBDA-LIST, there are three calls:
401 ;;; - once in the expander for defmacro itself, as it calls MAKE-MACRO-LAMBDA
402 ;;; which calls PARSE-LAMBDA-LIST. This is the toplevel form processing.
403 ;;; - again for :compile-toplevel, where the DS-BIND calls PARSE-LAMBDA-LIST.
404 ;;; If compiling in compile-toplevel, then *COMPILE-OBJECT* is a core object,
405 ;;; but if interpreting, then it is still a fasl.
406 ;;; - once for compiling to fasl. *COMPILE-OBJECT* is a fasl.
407 ;;; I'd have liked the data to be associated with the fasl, except that
408 ;;; as indicated above, the second line hides some information.
409 (defun style-warn-once (thing fmt
&rest args
)
410 (declare (special *compile-object
*))
411 (let* ((source-info *source-info
*)
412 (file-info (and (source-info-p source-info
)
413 (source-info-file-info source-info
)))
414 (file-compiling-p (file-info-p file-info
)))
415 (flet ((match-p (entry &aux
(rest (cdr entry
)))
416 ;; THING is compared by EQ, FMT by STRING=.
417 (and (eq (car entry
) thing
)
418 (string= (car rest
) fmt
)
419 ;; We don't want to walk into default values,
420 ;; e.g. (&optional (b #<insane-struct))
421 ;; because #<insane-struct> might be circular.
422 (equal-but-no-car-recursion (cdr rest
) args
))))
423 (unless (and file-compiling-p
425 (file-info-style-warning-tracker file-info
)))
426 (when file-compiling-p
427 (push (list* thing fmt args
)
428 (file-info-style-warning-tracker file-info
)))
429 (apply 'style-warn fmt args
)))))
431 ;;;; component compilation
433 (defparameter *max-optimize-iterations
* 3 ; ARB
435 "The upper limit on the number of times that we will consecutively do IR1
436 optimization that doesn't introduce any new code. A finite limit is
437 necessary, since type inference may take arbitrarily long to converge.")
439 (defevent ir1-optimize-until-done
"IR1-OPTIMIZE-UNTIL-DONE called")
440 (defevent ir1-optimize-maxed-out
"hit *MAX-OPTIMIZE-ITERATIONS* limit")
442 ;;; Repeatedly optimize COMPONENT until no further optimizations can
443 ;;; be found or we hit our iteration limit. When we hit the limit, we
444 ;;; clear the component and block REOPTIMIZE flags to discourage the
445 ;;; next optimization attempt from pounding on the same code.
446 (defun ir1-optimize-until-done (component)
447 (declare (type component component
))
449 (event ir1-optimize-until-done
)
451 (cleared-reanalyze nil
)
454 (when (component-reanalyze component
)
456 (setq cleared-reanalyze t
)
457 (setf (component-reanalyze component
) nil
))
458 (setf (component-reoptimize component
) nil
)
459 (ir1-optimize component fastp
)
460 (cond ((component-reoptimize component
)
462 (when (and (>= count
*max-optimize-iterations
*)
463 (not (component-reanalyze component
))
464 (eq (component-reoptimize component
) :maybe
))
466 (cond ((retry-delayed-ir1-transforms :optimize
)
470 (event ir1-optimize-maxed-out
)
471 (setf (component-reoptimize component
) nil
)
472 (do-blocks (block component
)
473 (setf (block-reoptimize block
) nil
))
475 ((retry-delayed-ir1-transforms :optimize
)
481 (setq fastp
(>= count
*max-optimize-iterations
*))
482 (maybe-mumble (if fastp
"-" ".")))
483 (when cleared-reanalyze
484 (setf (component-reanalyze component
) t
)))
487 (defparameter *constraint-propagate
* t
)
489 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
490 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
491 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
492 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
494 ;;; FIXME: It would be good to document why it's important to have a
495 ;;; large value here, and what the drawbacks of an excessively large
496 ;;; value are; and it might also be good to make it depend on
497 ;;; optimization policy.
498 (defparameter *reoptimize-after-type-check-max
* 10)
500 (defevent reoptimize-maxed-out
501 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
503 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
504 (defun dfo-as-needed (component)
505 (declare (type component component
))
506 (when (component-reanalyze component
)
510 (unless (component-reanalyze component
)
516 ;;; Do all the IR1 phases for a non-top-level component.
517 (defun ir1-phases (component)
518 (declare (type component component
))
519 (aver-live-component component
)
520 (let ((*constraint-universe
* (make-array 64 ; arbitrary, but don't
522 :fill-pointer
0 :adjustable t
))
524 (*delayed-ir1-transforms
* nil
))
525 (declare (special *constraint-universe
* *delayed-ir1-transforms
*))
527 (ir1-optimize-until-done component
)
528 (when (or (component-new-functionals component
)
529 (component-reanalyze-functionals component
))
530 (maybe-mumble "locall ")
531 (locall-analyze-component component
))
532 (dfo-as-needed component
)
533 (when *constraint-propagate
*
534 (maybe-mumble "constraint ")
535 (constraint-propagate component
))
536 (when (retry-delayed-ir1-transforms :constraint
)
537 (maybe-mumble "Rtran "))
538 (flet ((want-reoptimization-p ()
539 (or (component-reoptimize component
)
540 (component-reanalyze component
)
541 (component-new-functionals component
)
542 (component-reanalyze-functionals component
))))
543 (unless (and (want-reoptimization-p)
544 ;; We delay the generation of type checks until
545 ;; the type constraints have had time to
546 ;; propagate, else the compiler can confuse itself.
547 (< loop-count
(- *reoptimize-after-type-check-max
* 4)))
548 (maybe-mumble "type ")
549 (generate-type-checks component
)
550 (unless (want-reoptimization-p)
552 (when (>= loop-count
*reoptimize-after-type-check-max
*)
553 (maybe-mumble "[reoptimize limit]")
554 (event reoptimize-maxed-out
)
558 (when *check-consistency
*
559 (do-blocks-backwards (block component
)
560 (awhen (flush-dead-code block
)
561 (let ((*compiler-error-context
* it
))
562 (compiler-warn "dead code detected at the end of ~S"
565 (ir1-finalize component
)
570 (declaim (type (member :immobile
:dynamic
) *compile-to-memory-space
*))
571 ;; COMPILE-FILE puts all nontoplevel code in immobile space, but COMPILE
572 ;; offers a choice. Because the collector does not run often enough (yet),
573 ;; COMPILE usually places code in the dynamic space managed by our copying GC.
574 ;; Change this variable if your application always demands immobile code.
575 ;; The real default is set to :DYNAMIC in make-target-2-load.lisp
576 (defvar *compile-to-memory-space
* :immobile
) ; BUILD-TIME default
577 (defun code-immobile-p (node-or-component)
578 (if (fasl-output-p *compile-object
*)
579 (neq (component-kind (if (node-p node-or-component
)
580 (node-component node-or-component
)
583 (eq *compile-to-memory-space
* :immobile
))))
585 (defun %compile-component
(component)
586 (let ((*code-segment
* nil
)
588 #!+inline-constants
(*unboxed-constants
* nil
))
589 (maybe-mumble "GTN ")
590 (gtn-analyze component
)
591 (maybe-mumble "LTN ")
592 (ltn-analyze component
)
593 (dfo-as-needed component
)
594 (maybe-mumble "control ")
595 (control-analyze component
#'make-ir2-block
)
597 (when (or (ir2-component-values-receivers (component-info component
))
598 (component-dx-lvars component
))
599 (maybe-mumble "stack ")
600 (find-dominators component
)
601 (stack-analyze component
)
602 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
603 ;; stack analysis. There shouldn't be any unreachable code after
604 ;; control, so this won't delete anything.
605 (dfo-as-needed component
))
609 (maybe-mumble "IR2tran ")
611 (entry-analyze component
)
612 (ir2-convert component
)
614 (when (policy *lexenv
* (>= speed compilation-speed
))
615 (maybe-mumble "copy ")
616 (copy-propagate component
))
618 (ir2-optimize component
)
620 (select-representations component
)
622 (when *check-consistency
*
623 (maybe-mumble "check2 ")
624 (check-ir2-consistency component
))
626 (delete-unreferenced-tns component
)
628 (maybe-mumble "life ")
629 (lifetime-analyze component
)
631 (when *compile-progress
*
632 (compiler-mumble "") ; Sync before doing more output.
633 (pre-pack-tn-stats component
*standard-output
*))
635 (when *check-consistency
*
636 (maybe-mumble "check-life ")
637 (check-life-consistency component
))
639 (maybe-mumble "pack ")
640 (sb!regalloc
:pack component
)
642 (when *check-consistency
*
643 (maybe-mumble "check-pack ")
644 (check-pack-consistency component
))
646 (optimize-constant-loads component
)
647 (when *compiler-trace-output
*
648 (describe-component component
*compiler-trace-output
*)
649 (describe-ir2-component component
*compiler-trace-output
*))
651 (maybe-mumble "code ")
653 (multiple-value-bind (code-length fixup-notes
)
654 (let (#!+immobile-code
655 (*code-is-immobile
* (code-immobile-p component
)))
656 (generate-code component
))
659 (when *compiler-trace-output
*
660 (format *compiler-trace-output
*
661 "~|~%disassembly of code for ~S~2%" component
)
662 (sb!disassem
:disassemble-assem-segment
*code-segment
*
663 *compiler-trace-output
*))
665 (etypecase *compile-object
*
667 (maybe-mumble "fasl")
668 (fasl-dump-component component
673 #-sb-xc-host
; no compiling to core
675 (maybe-mumble "core")
676 (make-core-component component
683 ;; We're done, so don't bother keeping anything around.
684 (setf (component-info component
) :dead
)
688 ;;; Delete components with no external entry points before we try to
689 ;;; generate code. Unreachable closures can cause IR2 conversion to
690 ;;; puke on itself, since it is the reference to the closure which
691 ;;; normally causes the components to be combined.
692 (defun delete-if-no-entries (component)
693 (dolist (fun (component-lambdas component
) (delete-component component
))
694 (when (functional-has-external-references-p fun
)
696 (case (functional-kind fun
)
699 (unless (every (lambda (ref)
700 (eq (node-component ref
) component
))
704 (defun compile-component (component)
706 ;; miscellaneous sanity checks
708 ;; FIXME: These are basically pretty wimpy compared to the checks done
709 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
710 ;; make those internal consistency checks work again and use them.
711 (aver-live-component component
)
712 (do-blocks (block component
)
713 (aver (eql (block-component block
) component
)))
714 (dolist (lambda (component-lambdas component
))
715 ;; sanity check to prevent weirdness from propagating insidiously as
716 ;; far from its root cause as it did in bug 138: Make sure that
717 ;; thing-to-COMPONENT links are consistent.
718 (aver (eql (lambda-component lambda
) component
))
719 (aver (eql (node-component (lambda-bind lambda
)) component
)))
721 (let* ((*component-being-compiled
* component
))
723 ;; Record xref information before optimization. This way the
724 ;; stored xref data reflects the real source as closely as
726 (record-component-xrefs component
)
728 (ir1-phases component
)
731 (dfo-as-needed component
)
732 (find-dominators component
)
733 (loop-analyze component
))
736 (when (and *loop-analyze
* *compiler-trace-output
*)
737 (labels ((print-blocks (block)
738 (format *compiler-trace-output
* " ~A~%" block
)
739 (when (block-loop-next block
)
740 (print-blocks (block-loop-next block
))))
742 (format *compiler-trace-output
* "loop=~A~%" loop
)
743 (print-blocks (loop-blocks loop
))
744 (dolist (l (loop-inferiors loop
))
746 (print-loop (component-outer-loop component
))))
749 ;; This should happen at some point before PHYSENV-ANALYZE, and
750 ;; after RECORD-COMPONENT-XREFS. Beyond that, I haven't really
751 ;; thought things through. -- AJB, 2014-Jun-08
752 (eliminate-dead-code component
)
754 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
755 (maybe-mumble "env ")
756 (physenv-analyze component
)
757 (dfo-as-needed component
)
759 (delete-if-no-entries component
)
761 (unless (eq (block-next (component-head component
))
762 (component-tail component
))
763 (%compile-component component
)))
765 (clear-constant-info)
769 ;;;; clearing global data structures
771 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
772 ;;;; global data structures entirely when possible and consing up the
773 ;;;; others from scratch instead of clearing and reusing them?
775 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
776 ;;; addition to allowing stuff to be reclaimed, this is required for
777 ;;; correct assignment of constant offsets, since we need to assign a
778 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
779 ;;; slots, since they are used to keep track of functions across
780 ;;; component boundaries.
781 (defun clear-constant-info ()
782 (maphash (lambda (k v
)
784 (setf (leaf-info v
) nil
)
785 (setf (constant-boxed-tn v
) nil
))
787 (maphash (lambda (k v
)
790 (setf (leaf-info v
) nil
)
791 (setf (constant-boxed-tn v
) nil
)))
795 ;;; Blow away the REFS for all global variables, and let COMPONENT
797 (defun clear-ir1-info (component)
798 (declare (type component component
))
800 (maphash (lambda (k v
)
804 (delete-if #'here-p
(leaf-refs v
)))
805 (when (basic-var-p v
)
806 (setf (basic-var-sets v
)
807 (delete-if #'here-p
(basic-var-sets v
))))))
810 (eq (node-component x
) component
)))
818 ;;; Print out some useful info about COMPONENT to STREAM.
819 (defun describe-component (component *standard-output
*)
820 (declare (type component component
))
821 (format t
"~|~%;;;; component: ~S~2%" (component-name component
))
822 (print-all-blocks component
)
825 (defun describe-ir2-component (component *standard-output
*)
826 (format t
"~%~|~%;;;; IR2 component: ~S~2%" (component-name component
))
827 (format t
"entries:~%")
828 (dolist (entry (ir2-component-entries (component-info component
)))
829 (format t
"~4TL~D: ~S~:[~; [closure]~]~%"
830 (label-id (entry-info-offset entry
))
831 (entry-info-name entry
)
832 (entry-info-closure-tn entry
)))
834 (pre-pack-tn-stats component
*standard-output
*)
836 (print-ir2-blocks component
)
840 ;;; Given a pathname, return a SOURCE-INFO structure.
841 (defun make-file-source-info (file external-format
&optional form-tracking-p
)
843 :file-info
(make-file-info :name
(truename file
)
844 :untruename
(merge-pathnames file
)
845 :external-format external-format
848 (make-array 100 :fill-pointer
0 :adjustable t
))
849 :write-date
(file-write-date file
))))
851 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
852 (defun make-lisp-source-info (form &key parent
)
854 :file-info
(make-file-info :name
:lisp
859 ;;; Walk up the SOURCE-INFO list until we either reach a SOURCE-INFO
860 ;;; with no parent (e.g., from a REPL evaluation) or until we reach a
861 ;;; SOURCE-INFO whose FILE-INFO denotes a file.
862 (defun get-toplevelish-file-info (&optional
(source-info *source-info
*))
864 (do* ((sinfo source-info
(source-info-parent sinfo
))
865 (finfo (source-info-file-info sinfo
)
866 (source-info-file-info sinfo
)))
867 ((or (not (source-info-p (source-info-parent sinfo
)))
868 (pathnamep (file-info-name finfo
)))
871 ;;; If STREAM is present, return it, otherwise open a stream to the
872 ;;; current file. There must be a current file.
874 ;;; FIXME: This is probably an unnecessarily roundabout way to do
875 ;;; things now that we process a single file in COMPILE-FILE (unlike
876 ;;; the old CMU CL code, which accepted multiple files). Also, the old
878 ;;; When we open a new file, we also reset *PACKAGE* and policy.
879 ;;; This gives the effect of rebinding around each file.
880 ;;; which doesn't seem to be true now. Check to make sure that if
881 ;;; such rebinding is necessary, it's still done somewhere.
882 (defun get-source-stream (info)
883 (declare (type source-info info
))
884 (or (source-info-stream info
)
885 (let* ((file-info (source-info-file-info info
))
886 (name (file-info-name file-info
))
887 (external-format (file-info-external-format file-info
)))
888 (setf sb
!xc
:*compile-file-truename
* name
889 sb
!xc
:*compile-file-pathname
* (file-info-untruename file-info
)
890 (source-info-stream info
)
894 :external-format external-format
895 ;; SBCL stream classes aren't available in the host
897 #-sb-xc-host
'form-tracking-stream
)))
898 (when (file-info-subforms file-info
)
899 (setf (form-tracking-stream-observer stream
)
900 (make-form-tracking-stream-observer file-info
)))
903 ;;; Close the stream in INFO if it is open.
904 (defun close-source-info (info)
905 (declare (type source-info info
))
906 (let ((stream (source-info-stream info
)))
907 (when stream
(close stream
)))
908 (setf (source-info-stream info
) nil
)
911 ;; Loop over forms read from INFO's stream, calling FUNCTION with each.
912 ;; CONDITION-NAME is signaled if there is a reader error, and should be
913 ;; a subtype of not-so-aptly-named INPUT-ERROR-IN-COMPILE-FILE.
914 (defun %do-forms-from-info
(function info condition-name
)
915 (declare (function function
))
916 (let* ((file-info (source-info-file-info info
))
917 (stream (get-source-stream info
))
918 (pos (file-position stream
))
920 ;; Return a form read from STREAM; or for EOF use the trick,
921 ;; popularized by Kent Pitman, of returning STREAM itself.
924 ;; Reset for a new toplevel form.
925 (when (form-tracking-stream-p stream
)
926 (setf (form-tracking-stream-form-start-char-pos stream
) nil
))
927 (awhen (file-info-subforms file-info
)
928 (setf (fill-pointer it
) 0))
929 (read-preserving-whitespace stream nil stream
))
930 (reader-error (condition)
931 (compiler-error condition-name
932 ;; We don't need to supply :POSITION here because
933 ;; READER-ERRORs already know their position in the file.
936 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
937 ;; (and that this is not a READER-ERROR) when it encounters end of
938 ;; file in the middle of something it's trying to read,
939 ;; making it unfortunately indistinguishable from legal EOF.
940 ;; Were it not for that, it would be more elegant to just
941 ;; handle one more condition in the HANDLER-CASE.
942 ((or end-of-file error
) (condition)
946 ;; We need to supply :POSITION here because the END-OF-FILE
947 ;; condition doesn't carry the position that the user
948 ;; probably cares about, where the failed READ began.
950 (or (and (form-tracking-stream-p stream
)
951 (form-tracking-stream-form-start-byte-pos stream
))
954 (and (form-tracking-stream-p stream
)
955 (line/col-from-charpos
957 (form-tracking-stream-form-start-char-pos stream
)))
959 (unless (eq form stream
) ; not EOF
960 (funcall function form
962 (let* ((forms (file-info-forms file-info
))
963 (current-idx (fill-pointer forms
)))
964 (vector-push-extend form forms
)
965 (vector-push-extend pos
(file-info-positions file-info
))
967 (%do-forms-from-info function info condition-name
))))
969 ;;; Loop over FORMS retrieved from INFO. Used by COMPILE-FILE and
970 ;;; LOAD when loading from a FILE-STREAM associated with a source
971 ;;; file. ON-ERROR is the name of a condition class that should
972 ;;; be signaled if anything goes wrong during a READ.
973 (defmacro do-forms-from-info
(((form &rest keys
) info
974 &optional
(on-error ''input-error-in-load
))
976 (aver (symbolp form
))
977 (once-only ((info info
))
978 `(let ((*source-info
* ,info
))
979 (%do-forms-from-info
(lambda (,form
&key
,@keys
&allow-other-keys
)
983 ;;; Read and compile the source file.
984 (defun sub-sub-compile-file (info)
985 (do-forms-from-info ((form current-index
) info
986 'input-error-in-compile-file
)
988 (find-source-paths form current-index
)
989 (process-toplevel-form
990 form
`(original-source-start 0 ,current-index
) nil
)))
991 ;; It's easy to get into a situation where cold-init crashes and the only
992 ;; backtrace you get from ldb is TOP-LEVEL-FORM, which means you're anywhere
993 ;; within the 23000 or so blobs of code deferred until cold-init.
994 ;; Seeing each file finish narrows things down without the noise of :sb-show,
995 ;; but this hack messes up form positions, so it's not on unless asked for.
996 #+nil
; change to #+sb-xc-host if desired
997 (let ((file-info (get-toplevelish-file-info info
)))
998 (declare (ignorable file-info
))
999 (let* ((forms (file-info-forms file-info
))
1002 ,(format nil
"Completed TLFs: ~A~%" (file-info-name file-info
))))
1003 (index (fill-pointer forms
)))
1005 (find-source-paths form index
)
1006 (process-toplevel-form
1007 form
`(original-source-start 0 ,index
) nil
)))))
1009 ;;; Return the INDEX'th source form read from INFO and the position
1010 ;;; where it was read.
1011 (defun find-source-root (index info
)
1012 (declare (type index index
) (type source-info info
))
1013 (let ((file-info (source-info-file-info info
)))
1014 (values (aref (file-info-forms file-info
) index
)
1015 (aref (file-info-positions file-info
) index
))))
1017 ;;;; processing of top level forms
1019 ;;; This is called by top level form processing when we are ready to
1020 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
1021 ;;; convert the form, but delay compilation, pushing the result on
1022 ;;; *TOPLEVEL-LAMBDAS* instead.
1023 (defun convert-and-maybe-compile (form path
&optional
(expand t
))
1024 (declare (list path
))
1026 (when sb-cold
::*compile-for-effect-only
*
1027 (return-from convert-and-maybe-compile
))
1028 (let ((*top-level-form-noted
* (note-top-level-form form t
)))
1029 ;; Don't bother to compile simple objects that just sit there.
1030 (when (and form
(or (symbolp form
) (consp form
)))
1031 (if (fopcompilable-p form expand
)
1032 (let ((*fopcompile-label-counter
* 0))
1033 (fopcompile form path nil expand
))
1035 (let ((*lexenv
* (make-lexenv
1037 :handled-conditions
*handled-conditions
*
1038 :disabled-package-locks
*disabled-package-locks
*))
1039 (tll (ir1-toplevel form path nil
)))
1040 (if (eq *block-compile
* t
)
1041 (push tll
*toplevel-lambdas
*)
1042 (compile-toplevel (list tll
) nil
))
1045 ;;; Macroexpand FORM in the current environment with an error handler.
1046 ;;; We only expand one level, so that we retain all the intervening
1047 ;;; forms in the source path. A compiler-macro takes precedence over
1048 ;;; an ordinary macro as specified in CLHS 3.2.3.1
1049 ;;; Note that this function is _only_ for processing of toplevel forms.
1050 ;;; Non-toplevel forms use IR1-CONVERT-FUNCTOID which considers compiler macros.
1051 (defun preprocessor-macroexpand-1 (form)
1053 (let ((expansion (expand-compiler-macro form
)))
1054 (if (neq expansion form
)
1055 (return-from preprocessor-macroexpand-1
1056 (values expansion t
)))))
1057 (handler-case (%macroexpand-1 form
*lexenv
*)
1059 (compiler-error "(during macroexpansion of ~A)~%~A"
1060 (let ((*print-level
* 2)
1062 (format nil
"~S" form
))
1065 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
1066 ;;; the forms, and PATH is the source path of the FORM they came out of.
1067 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
1068 (defun process-toplevel-progn (forms path compile-time-too
)
1069 (declare (list forms
) (list path
))
1070 (dolist (form forms
)
1071 (process-toplevel-form form path compile-time-too
)))
1073 ;;; Process a top level use of LOCALLY, or anything else (e.g.
1074 ;;; MACROLET) at top level which has declarations and ordinary forms.
1075 ;;; We parse declarations and then recursively process the body.
1076 (defun process-toplevel-locally (body path compile-time-too
&key vars funs
)
1077 (declare (list path
))
1078 (multiple-value-bind (forms decls
) (parse-body body nil t
)
1080 (let* ((*lexenv
* (process-decls decls vars funs
))
1081 ;; FIXME: VALUES declaration
1083 ;; Binding *POLICY* is pretty much of a hack, since it
1084 ;; causes LOCALLY to "capture" enclosed proclamations. It
1085 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
1086 ;; value of *POLICY* as the policy. The need for this hack
1087 ;; is due to the quirk that there is no way to represent in
1088 ;; a POLICY that an optimize quality came from the default.
1090 ;; FIXME: Ideally, something should be done so that DECLAIM
1091 ;; inside LOCALLY works OK. Failing that, at least we could
1092 ;; issue a warning instead of silently screwing up.
1093 ;; Here's how to fix this: a POLICY object can in fact represent
1094 ;; absence of qualitities. Whenever we rebind *POLICY* (here and
1095 ;; elsewhere), it should be bound to a policy that expresses no
1096 ;; qualities. Proclamations should update SYMBOL-GLOBAL-VALUE of
1097 ;; *POLICY*, which can be seen irrespective of dynamic bindings,
1098 ;; and declarations should update the lexical policy.
1099 ;; The POLICY macro can be amended to merge the dynamic *POLICY*
1100 ;; (or whatever it came from, like a LEXENV) with the global
1101 ;; *POLICY*. COERCE-TO-POLICY can do the merge, employing a 1-line
1102 ;; cache so that repeated calls for any two fixed policy objects
1103 ;; return the identical value (since policies are immutable).
1104 (*policy
* (lexenv-policy *lexenv
*))
1105 ;; This is probably also a hack
1106 (*handled-conditions
* (lexenv-handled-conditions *lexenv
*))
1108 (*disabled-package-locks
* (lexenv-disabled-package-locks *lexenv
*)))
1109 (process-toplevel-progn forms path compile-time-too
)))))
1111 ;;; Parse an EVAL-WHEN situations list, returning three flags,
1112 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
1113 ;;; the types of situations present in the list.
1114 (defun parse-eval-when-situations (situations)
1115 (when (or (not (listp situations
))
1116 (set-difference situations
1123 (compiler-error "bad EVAL-WHEN situation list: ~S" situations
))
1124 (let ((deprecated-names (intersection situations
'(compile load eval
))))
1125 (when deprecated-names
1126 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
1128 (values (intersection '(:compile-toplevel compile
)
1130 (intersection '(:load-toplevel load
) situations
)
1131 (intersection '(:execute eval
) situations
)))
1134 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
1135 (defun functional-components (f)
1136 (declare (type functional f
))
1138 (clambda (list (lambda-component f
)))
1139 (optional-dispatch (let ((result nil
))
1140 (flet ((maybe-frob (maybe-clambda)
1141 (when (and maybe-clambda
1142 (promise-ready-p maybe-clambda
))
1143 (pushnew (lambda-component
1144 (force maybe-clambda
))
1146 (map nil
#'maybe-frob
(optional-dispatch-entry-points f
))
1147 (maybe-frob (optional-dispatch-more-entry f
))
1148 (maybe-frob (optional-dispatch-main-entry f
)))
1151 (defun make-functional-from-toplevel-lambda (lambda-expression
1155 ;; I'd thought NIL should
1156 ;; work, but it doesn't.
1157 ;; -- WHN 2001-09-20
1159 (let* ((*current-path
* path
)
1160 (component (make-empty-component))
1161 (*current-component
* component
)
1162 (debug-name-tail (or name
(name-lambdalike lambda-expression
)))
1163 (source-name (or name
'.anonymous.
)))
1164 (setf (component-name component
) (debug-name 'initial-component debug-name-tail
)
1165 (component-kind component
) :initial
)
1166 (let* ((fun (let ((*allow-instrumenting
* t
))
1167 (funcall #'ir1-convert-lambdalike
1169 :source-name source-name
)))
1170 ;; Convert the XEP using the policy of the real function. Otherwise
1171 ;; the wrong policy will be used for deciding whether to type-check
1172 ;; the parameters of the real function (via CONVERT-CALL /
1173 ;; PROPAGATE-TO-ARGS). -- JES, 2007-02-27
1174 (*lexenv
* (make-lexenv :policy
(lexenv-policy (functional-lexenv fun
))))
1175 (xep (ir1-convert-lambda (make-xep-lambda-expression fun
)
1176 :source-name source-name
1177 :debug-name
(debug-name 'tl-xep debug-name-tail
)
1180 (assert-global-function-definition-type name fun
))
1181 (setf (functional-kind xep
) :external
1182 (functional-entry-fun xep
) fun
1183 (functional-entry-fun fun
) xep
1184 (component-reanalyze component
) t
1185 (functional-has-external-references-p xep
) t
)
1186 (reoptimize-component component
:maybe
)
1187 (locall-analyze-xep-entry-point fun
)
1188 ;; Any leftover REFs to FUN outside local calls get replaced with the
1190 (substitute-leaf-if (lambda (ref)
1191 (let* ((lvar (ref-lvar ref
))
1192 (dest (when lvar
(lvar-dest lvar
)))
1193 (kind (when (basic-combination-p dest
)
1194 (basic-combination-kind dest
))))
1200 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
1201 ;;; description of the result.
1202 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
1203 ;;; into core and return the compiled FUNCTION value.
1204 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
1205 ;;; into the fasl file and return a dump handle.
1207 ;;; If NAME is provided, then we try to use it as the name of the
1208 ;;; function for debugging/diagnostic information.
1209 (defun %compile
(lambda-expression
1214 ;; This magical idiom seems to be the appropriate
1215 ;; path for compiling standalone LAMBDAs, judging
1216 ;; from the CMU CL code and experiment, so it's a
1217 ;; nice default for things where we don't have a
1218 ;; real source path (as in e.g. inside CL:COMPILE).
1219 '(original-source-start 0 0)))
1221 (legal-fun-name-or-type-error name
))
1223 (let* ((*lexenv
* (make-lexenv
1225 :handled-conditions
*handled-conditions
*
1226 :disabled-package-locks
*disabled-package-locks
*))
1227 (*compiler-sset-counter
* 0)
1228 (fun (make-functional-from-toplevel-lambda lambda-expression
1232 ;; FIXME: The compile-it code from here on is sort of a
1233 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1234 ;; better to find a way to share the code there; or
1235 ;; alternatively, to use this code to replace the code there.
1236 ;; (The second alternative might be pretty easy if we used
1237 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1238 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1240 (locall-analyze-clambdas-until-done (list fun
))
1242 (let ((components-from-dfo (find-initial-dfo (list fun
))))
1243 (dolist (component-from-dfo components-from-dfo
)
1244 (compile-component component-from-dfo
)
1245 (replace-toplevel-xeps component-from-dfo
))
1247 (let ((entry-table (etypecase *compile-object
*
1248 (fasl-output (fasl-output-entry-table
1250 (core-object (core-object-entry-table
1251 *compile-object
*)))))
1252 (multiple-value-bind (result found-p
)
1253 (gethash (leaf-info fun
) entry-table
)
1257 ;; KLUDGE: This code duplicates some other code in this
1258 ;; file. In the great reorganzation, the flow of program
1259 ;; logic changed from the original CMUCL model, and that
1260 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1261 ;; longer followed for CORE-OBJECTS, leading to BUG
1262 ;; 156. This place is transparently not the right one for
1263 ;; this code, but I don't have a clear enough overview of
1264 ;; the compiler to know how to rearrange it all so that
1265 ;; this operation fits in nicely, and it was blocking
1266 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1267 ;; ((..)) (DEFUN FOO ...))
1269 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1270 ;; ideal way, as (1) definitions typed in at the REPL
1271 ;; without an INLINE declaration will give a NULL
1272 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1273 ;; and (2) INLINE declarations will yield a
1274 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1275 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1278 ;; (2) is probably fairly easy to fix -- it is, after all,
1279 ;; a matter of list manipulation (or possibly of teaching
1280 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1281 ;; significantly harder, as the association between
1282 ;; function object and source is a tricky one.
1284 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1285 ;; non-NULL list) when the function in question has been
1286 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1287 ;; work when it has been compiled as part of the top-level
1288 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1289 ;; ...). -- CSR, 2002-11-02
1290 (when (core-object-p *compile-object
*)
1291 (fix-core-source-info *source-info
* *compile-object
* result
))
1293 (mapc #'clear-ir1-info components-from-dfo
))))))))
1295 (defun note-top-level-form (form &optional finalp
)
1296 (when *compile-print
*
1297 (cond ((not *top-level-form-noted
*)
1298 (let ((*print-length
* 2)
1300 (*print-pretty
* nil
))
1301 (with-compiler-io-syntax
1303 #-sb-xc-host
"~&; ~:[compiling~;converting~] ~S"
1304 #+sb-xc-host
"~&; ~:[x-compiling~;x-converting~] ~S"
1305 *block-compile
* form
)))
1308 (eq :top-level-forms
*compile-print
*)
1309 (neq form
*top-level-form-noted
*))
1310 (let ((*print-length
* 1)
1312 (*print-pretty
* nil
))
1313 (with-compiler-io-syntax
1314 (compiler-mumble "~&; ... top level ~S" form
)))
1317 *top-level-form-noted
*))))
1319 ;;; Handle the evaluation the a :COMPILE-TOPLEVEL body during
1320 ;;; compilation. Normally just evaluate in the appropriate
1321 ;;; environment, but also compile if outputting a CFASL.
1322 (defun eval-compile-toplevel (body path
)
1324 (eval-tlf `(progn ,@body
) (source-path-tlf-number path
) *lexenv
*)
1325 (when *compile-toplevel-object
*
1326 (let ((*compile-object
* *compile-toplevel-object
*))
1327 (convert-and-maybe-compile `(progn ,@body
) path
)))))
1328 (if (null *macro-policy
*)
1332 :policy
(process-optimize-decl
1333 `(optimize ,@(policy-to-decl-spec *macro-policy
*))
1334 (lexenv-policy *lexenv
*))
1336 ;; In case a null lexenv is created, it needs to get the newly
1337 ;; effective global policy, not the policy currently in *POLICY*.
1338 (*policy
* (lexenv-policy *lexenv
*)))
1341 ;;; Process a top level FORM with the specified source PATH.
1342 ;;; * If this is a magic top level form, then do stuff.
1343 ;;; * If this is a macro, then expand it.
1344 ;;; * Otherwise, just compile it.
1346 ;;; COMPILE-TIME-TOO is as defined in ANSI
1347 ;;; "3.2.3.1 Processing of Top Level Forms".
1348 (defun process-toplevel-form (form path compile-time-too
)
1349 (declare (list path
))
1351 (catch 'process-toplevel-form-error-abort
1352 (let* ((path (or (get-source-path form
) (cons form path
)))
1353 (*current-path
* path
)
1354 (*compiler-error-bailout
*
1355 (lambda (&optional condition
)
1356 (convert-and-maybe-compile
1357 (make-compiler-error-form condition form
)
1359 (throw 'process-toplevel-form-error-abort nil
))))
1361 (flet ((default-processor (form)
1362 (let ((*top-level-form-noted
* (note-top-level-form form
)))
1363 ;; When we're cross-compiling, consider: what should we
1364 ;; do when we hit e.g.
1365 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1366 ;; (DEFUN FOO (X) (+ 7 X)))?
1367 ;; DEFUN has a macro definition in the cross-compiler,
1368 ;; and a different macro definition in the target
1369 ;; compiler. The only sensible thing is to use the
1370 ;; target compiler's macro definition, since the
1371 ;; cross-compiler's macro is in general into target
1372 ;; functions which can't meaningfully be executed at
1373 ;; cross-compilation time. So make sure we do the EVAL
1374 ;; here, before we macroexpand.
1376 ;; Then things get even dicier with something like
1377 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1378 ;; where we have to make sure that we don't uncross
1379 ;; the SB!XC: prefix before we do EVAL, because otherwise
1380 ;; we'd be trying to redefine the cross-compilation host's
1383 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1386 (when compile-time-too
1387 (eval form
)) ; letting xc host EVAL do its own macroexpansion
1388 (let* (;; (We uncross the operator name because things
1389 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1390 ;; should be equivalent to their CL: counterparts
1391 ;; when being compiled as target code. We leave
1392 ;; the rest of the form uncrossed because macros
1393 ;; might yet expand into EVAL-WHEN stuff, and
1394 ;; things inside EVAL-WHEN can't be uncrossed
1395 ;; until after we've EVALed them in the
1396 ;; cross-compilation host.)
1397 (slightly-uncrossed (cons (uncross (first form
))
1399 (expanded (preprocessor-macroexpand-1
1400 slightly-uncrossed
)))
1401 (if (eq expanded slightly-uncrossed
)
1402 ;; (Now that we're no longer processing toplevel
1403 ;; forms, and hence no longer need to worry about
1404 ;; EVAL-WHEN, we can uncross everything.)
1405 (convert-and-maybe-compile expanded path
)
1406 ;; (We have to demote COMPILE-TIME-TOO to NIL
1407 ;; here, no matter what it was before, since
1408 ;; otherwise we'd tend to EVAL subforms more than
1409 ;; once, because of WHEN COMPILE-TIME-TOO form
1411 (process-toplevel-form expanded path nil
))))
1412 ;; When we're not cross-compiling, we only need to
1413 ;; macroexpand once, so we can follow the 1-thru-6
1414 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1415 ;; Top Level Forms".
1417 (let ((expanded (preprocessor-macroexpand-1 form
)))
1418 (cond ((eq expanded form
)
1419 (when compile-time-too
1420 (eval-compile-toplevel (list form
) path
))
1421 (convert-and-maybe-compile form path nil
))
1423 (process-toplevel-form expanded
1425 compile-time-too
)))))))
1428 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1429 ;; (1) SBCL gets smart enough to handle global
1430 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1431 ;; implementors start using symbol macros in a way which
1432 ;; interacts with SB-XC/CL distinction.)
1433 (convert-and-maybe-compile form path
)
1435 (default-processor form
)
1436 (flet ((need-at-least-one-arg (form)
1438 (compiler-error "~S form is too short: ~S"
1442 ((eval-when macrolet symbol-macrolet
);things w/ 1 arg before body
1443 (need-at-least-one-arg form
)
1444 (destructuring-bind (special-operator magic
&rest body
) form
1445 (ecase special-operator
1447 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1448 ;; "3.2.3.1 Processing of Top Level Forms".
1449 (multiple-value-bind (ct lt e
)
1450 (parse-eval-when-situations magic
)
1451 (let ((new-compile-time-too (or ct
1452 (and compile-time-too
1454 (cond (lt (process-toplevel-progn
1455 body path new-compile-time-too
))
1456 (new-compile-time-too
1457 (eval-compile-toplevel body path
))))))
1459 (funcall-in-macrolet-lexenv
1461 (lambda (&key funs prepend
)
1462 (declare (ignore funs
))
1463 (aver (null prepend
))
1464 (process-toplevel-locally body
1469 (funcall-in-symbol-macrolet-lexenv
1471 (lambda (&key vars prepend
)
1472 (aver (null prepend
))
1473 (process-toplevel-locally body
1479 (process-toplevel-locally (rest form
) path compile-time-too
))
1481 (process-toplevel-progn (rest form
) path compile-time-too
))
1482 (t (default-processor form
))))))))
1486 ;;;; load time value support
1488 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1490 ;;; Return T if we are currently producing a fasl file and hence
1491 ;;; constants need to be dumped carefully.
1492 (declaim (inline producing-fasl-file
))
1493 (defun producing-fasl-file ()
1494 (fasl-output-p *compile-object
*))
1496 ;;; Compile the FORMS and arrange for them to be called (for effect,
1497 ;;; not value) at load time.
1498 (defun compile-make-load-form-init-forms (forms fasl
)
1499 ;; If FORMS has exactly one PROGN containing a call of SB-PCL::SET-SLOTS,
1500 ;; then fopcompile it, otherwise use the main compiler.
1501 (when (singleton-p forms
)
1502 (let ((call (car forms
)))
1503 (when (typep call
'(cons (eql sb
!pcl
::set-slots
) (cons instance
)))
1505 (let ((instance (pop call
))
1506 (slot-names (pop call
))
1509 (when (and (every #'symbolp slot-names
)
1510 (every #'sb
!xc
:constantp value-forms
))
1511 (dolist (x value-forms
)
1512 (let ((val (constant-form-value x
)))
1513 ;; invoke recursive MAKE-LOAD-FORM stuff as necessary
1516 (mapc (lambda (x) (dump-object x fasl
)) (nreverse values
))
1517 (dump-object (cons (length slot-names
) slot-names
) fasl
)
1518 (dump-object instance fasl
)
1519 (dump-fop 'sb
!fasl
::fop-set-slot-values fasl
)
1520 (return-from compile-make-load-form-init-forms
))))))
1521 (let ((lambda (compile-load-time-stuff `(progn ,@forms
) nil
)))
1522 (fasl-dump-toplevel-lambda-call lambda
*compile-object
*)))
1524 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1525 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1526 (defun compile-load-time-stuff (form for-value
)
1528 (let* ((*lexenv
* (make-null-lexenv))
1529 (lambda (ir1-toplevel form
*current-path
* for-value nil
)))
1530 (compile-toplevel (list lambda
) t
)
1533 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1534 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1535 ;;; don't try to combine this component with anything else and frob
1536 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1537 ;;; compiling, because it was merged with a run-time component.
1538 (defun compile-load-time-value-lambda (lambdas)
1539 (aver (null (cdr lambdas
)))
1540 (let* ((lambda (car lambdas
))
1541 (component (lambda-component lambda
)))
1542 (when (eql (component-kind component
) :toplevel
)
1543 (setf (component-name component
) (leaf-debug-name lambda
))
1544 (compile-component component
)
1545 (clear-ir1-info component
))))
1549 (defun object-call-toplevel-lambda (tll)
1550 (declare (type functional tll
))
1551 (let ((object *compile-object
*))
1553 (fasl-output (fasl-dump-toplevel-lambda-call tll object
))
1554 (core-object (core-call-toplevel-lambda tll object
))
1557 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1558 ;;; the resulting function to be called.
1559 (defun sub-compile-toplevel-lambdas (lambdas)
1560 (declare (list lambdas
))
1562 (multiple-value-bind (component tll
) (merge-toplevel-lambdas lambdas
)
1563 (compile-component component
)
1564 (clear-ir1-info component
)
1565 (object-call-toplevel-lambda tll
)))
1568 ;;; Compile top level code and call the top level lambdas. We pick off
1569 ;;; top level lambdas in non-top-level components here, calling
1570 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1571 (defun compile-toplevel-lambdas (lambdas)
1572 (declare (list lambdas
))
1573 (let ((len (length lambdas
)))
1574 (flet ((loser (start)
1575 (or (position-if (lambda (x)
1576 (not (eq (component-kind
1577 (node-component (lambda-bind x
)))
1580 ;; this used to read ":start start", but
1581 ;; start can be greater than len, which
1582 ;; is an error according to ANSI - CSR,
1584 :start
(min start len
))
1586 (do* ((start 0 (1+ loser
))
1587 (loser (loser start
) (loser start
)))
1589 (sub-compile-toplevel-lambdas (subseq lambdas start loser
))
1590 (unless (= loser len
)
1591 (object-call-toplevel-lambda (elt lambdas loser
))))))
1594 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1597 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1598 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1599 (defun compile-toplevel (lambdas load-time-value-p
)
1600 (declare (list lambdas
))
1602 (maybe-mumble "locall ")
1603 (locall-analyze-clambdas-until-done lambdas
)
1605 (maybe-mumble "IDFO ")
1606 (multiple-value-bind (components top-components hairy-top
)
1607 (find-initial-dfo lambdas
)
1608 (let ((all-components (append components top-components
)))
1609 (when *check-consistency
*
1610 (maybe-mumble "[check]~%")
1611 (check-ir1-consistency all-components
))
1613 (dolist (component (append hairy-top top-components
))
1614 (pre-physenv-analyze-toplevel component
))
1616 (dolist (component components
)
1617 (compile-component component
)
1618 (replace-toplevel-xeps component
))
1620 (when *check-consistency
*
1621 (maybe-mumble "[check]~%")
1622 (check-ir1-consistency all-components
))
1624 (if load-time-value-p
1625 (compile-load-time-value-lambda lambdas
)
1626 (compile-toplevel-lambdas lambdas
))
1628 (mapc #'clear-ir1-info components
)))
1631 ;;; Actually compile any stuff that has been queued up for block
1633 (defun finish-block-compilation ()
1634 (when *block-compile
*
1635 (when *compile-print
*
1636 (compiler-mumble "~&; block compiling converted top level forms..."))
1637 (when *toplevel-lambdas
*
1638 (compile-toplevel (nreverse *toplevel-lambdas
*) nil
)
1639 (setq *toplevel-lambdas
* ()))
1640 (setq *block-compile
* nil
)
1641 (setq *entry-points
* nil
)))
1643 (flet ((get-handled-conditions ()
1644 (let ((ctxt *compiler-error-context
*))
1645 (lexenv-handled-conditions
1647 (node (node-lexenv ctxt
))
1648 (compiler-error-context
1649 (let ((lexenv (compiler-error-context-lexenv ctxt
)))
1652 ;; Is this right? I would think that if lexenv is null
1653 ;; we should look at *HANDLED-CONDITIONS*.
1655 (handle-p (condition ctype
)
1656 #+sb-xc-host
(typep condition
(type-specifier ctype
))
1657 #-sb-xc-host
(%%typep condition ctype
)))
1658 (declare (inline handle-p
))
1660 (defun handle-condition-p (condition)
1661 (dolist (muffle (get-handled-conditions) nil
)
1662 (destructuring-bind (ctype . restart-name
) muffle
1663 (when (and (handle-p condition ctype
)
1664 (find-restart restart-name condition
))
1667 (defun handle-condition-handler (condition)
1668 (let ((muffles (get-handled-conditions)))
1669 (aver muffles
) ; FIXME: looks redundant with "fell through"
1670 (dolist (muffle muffles
(bug "fell through"))
1671 (destructuring-bind (ctype . restart-name
) muffle
1672 (when (handle-p condition ctype
)
1673 (awhen (find-restart restart-name condition
)
1674 (invoke-restart it
)))))))
1676 ;; WOULD-MUFFLE-P is called (incorrectly) only by NOTE-UNDEFINED-REFERENCE.
1677 ;; It is not wrong per se, but as used, it is wrong, making it nearly
1678 ;; impossible to muffle a subset of undefind warnings whose NAME and KIND
1679 ;; slots match specific things tested by a user-defined predicate.
1680 ;; Attempting to do that might muffle everything, depending on how your
1681 ;; predicate responds to a vanilla WARNING. Consider e.g.
1682 ;; (AND WARNING (NOT (SATISFIES HAIRYFN)))
1683 ;; where HAIRYFN depends on the :FORMAT-CONTROL and :FORMAT-ARGUMENTS.
1684 (defun would-muffle-p (condition)
1685 (let ((ctype (rassoc 'muffle-warning
1686 (lexenv-handled-conditions *lexenv
*))))
1687 (and ctype
(handle-p condition
(car ctype
))))))
1689 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1690 ;;; Return (VALUES ABORT-P WARNINGS-P FAILURE-P).
1691 (defun sub-compile-file (info)
1692 (declare (type source-info info
))
1693 (let ((*package
* (sane-package))
1694 (*readtable
* *readtable
*)
1695 (sb!xc
:*compile-file-pathname
* nil
) ; really bound in
1696 (sb!xc
:*compile-file-truename
* nil
) ; SUB-SUB-COMPILE-FILE
1698 (*macro-policy
* *macro-policy
*)
1699 (*code-coverage-records
* (make-hash-table :test
'equal
))
1700 (*code-coverage-blocks
* (make-hash-table :test
'equal
))
1701 (*handled-conditions
* *handled-conditions
*)
1702 (*disabled-package-locks
* *disabled-package-locks
*)
1703 (*lexenv
* (make-null-lexenv))
1704 (*block-compile
* *block-compile-arg
*)
1705 (*toplevel-lambdas
* ())
1706 (*fun-names-in-this-file
* ())
1707 (*allow-instrumenting
* nil
)
1708 (*compiler-error-bailout
*
1709 (lambda (&optional error
)
1710 (declare (ignore error
))
1711 (return-from sub-compile-file
(values t t t
))))
1712 (*current-path
* nil
)
1713 (*last-format-string
* nil
)
1714 (*last-format-args
* nil
)
1715 (*last-message-count
* 0)
1716 (*compiler-sset-counter
* 0)
1717 (sb!xc
:*gensym-counter
* 0))
1719 (handler-bind (((satisfies handle-condition-p
) #'handle-condition-handler
))
1720 (with-compilation-values
1721 (sb!xc
:with-compilation-unit
()
1723 (sub-sub-compile-file info
)
1724 (unless (zerop (hash-table-count *code-coverage-records
*))
1725 ;; Dump the code coverage records into the fasl.
1727 (fopcompile `(record-code-coverage
1728 ',(namestring *compile-file-pathname
*)
1730 (maphash (lambda (k v
)
1731 (declare (ignore k
))
1733 *code-coverage-records
*)
1737 (finish-block-compilation)
1738 (let ((object *compile-object
*))
1740 (fasl-output (fasl-dump-source-info info object
))
1741 (core-object (fix-core-source-info info object
))
1744 ;; Some errors are sufficiently bewildering that we just fail
1745 ;; immediately, without trying to recover and compile more of
1747 (fatal-compiler-error (condition)
1749 (fresh-line *error-output
*)
1750 (pprint-logical-block (*error-output
* nil
:per-line-prefix
"; ")
1751 (format *error-output
*
1752 "~@<~@:_compilation aborted because of fatal error: ~2I~_~A~@:_~:>"
1753 (encapsulated-condition condition
)))
1754 (finish-output *error-output
*)
1757 ;;; Return a pathname for the named file. The file must exist.
1758 (defun verify-source-file (pathname-designator)
1759 (let* ((pathname (pathname pathname-designator
))
1760 (default-host (make-pathname :host
(pathname-host pathname
))))
1761 (flet ((try-with-type (path type error-p
)
1762 (let ((new (merge-pathnames
1763 path
(make-pathname :type type
1764 :defaults default-host
))))
1765 (if (probe-file new
)
1767 (and error-p
(truename new
))))))
1768 (cond ((typep pathname
'logical-pathname
)
1769 (try-with-type pathname
"LISP" t
))
1770 ((probe-file pathname
) pathname
)
1771 ((try-with-type pathname
"lisp" nil
))
1772 ((try-with-type pathname
"lisp" t
))))))
1774 (defun elapsed-time-to-string (internal-time-delta)
1775 (multiple-value-bind (tsec remainder
)
1776 (truncate internal-time-delta internal-time-units-per-second
)
1777 (let ((ms (truncate remainder
(/ internal-time-units-per-second
1000))))
1778 (multiple-value-bind (tmin sec
) (truncate tsec
60)
1779 (multiple-value-bind (thr min
) (truncate tmin
60)
1780 (format nil
"~D:~2,'0D:~2,'0D.~3,'0D" thr min sec ms
))))))
1782 ;;; Print some junk at the beginning and end of compilation.
1783 (defun print-compile-start-note (source-info)
1784 (declare (type source-info source-info
))
1785 (let ((file-info (source-info-file-info source-info
)))
1786 (compiler-mumble #+sb-xc-host
"~&; ~A file ~S (written ~A):~%"
1787 #+sb-xc-host
(if sb-cold
::*compile-for-effect-only
*
1790 #-sb-xc-host
"~&; compiling file ~S (written ~A):~%"
1791 (namestring (file-info-name file-info
))
1792 (sb!int
:format-universal-time nil
1793 (file-info-write-date
1797 :print-timezone nil
)))
1800 (defun print-compile-end-note (source-info won
)
1801 (declare (type source-info source-info
))
1802 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1804 (elapsed-time-to-string
1805 (- (get-internal-real-time)
1806 (source-info-start-real-time source-info
))))
1809 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1810 ;;; out of the compile, then abort the writing of the output file, so
1811 ;;; that we don't overwrite it with known garbage.
1812 (defun sb!xc
:compile-file
1817 (output-file (cfp-output-file-default input-file
))
1818 ;; FIXME: ANSI doesn't seem to say anything about
1819 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1821 ((:verbose sb
!xc
:*compile-verbose
*) sb
!xc
:*compile-verbose
*)
1822 ((:print sb
!xc
:*compile-print
*) sb
!xc
:*compile-print
*)
1823 (external-format :default
)
1827 ((:block-compile
*block-compile-arg
*) nil
)
1828 (emit-cfasl *emit-cfasl
*))
1830 "Compile INPUT-FILE, producing a corresponding fasl file and
1831 returning its filename.
1834 If true, a message per non-macroexpanded top level form is printed
1835 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1836 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1837 no such message, but their subforms do.
1839 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1840 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1841 For example, compiling an IN-PACKAGE form will result in a message about
1842 a top level SETQ in addition to the message about the IN-PACKAGE form'
1845 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1848 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1849 argument, it is not currently supported. (non-standard)
1852 If given, internal data structures are dumped to the specified
1853 file, or if a value of T is given, to a file of *.trace type
1854 derived from the input file name. (non-standard)
1857 (Experimental). If true, outputs the toplevel compile-time effects
1858 of this file into a separate .cfasl file."
1859 ;;; Block compilation is currently broken.
1861 "Also, as a workaround for vaguely-non-ANSI behavior, the
1862 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1863 multiple functions are compiled together as a unit, resolving function
1864 references at compile time. NIL means that global function names are
1865 never resolved at compilation time. Currently NIL is the default
1866 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1867 of the ANSI spec allows this behavior under all circumstances, the
1868 compiler's runtime scales badly when it tries to do this for large
1869 files. If/when this performance problem is fixed, the block
1870 compilation default behavior will probably be made dependent on the
1871 SPEED and COMPILATION-SPEED optimization values, and the
1872 :BLOCK-COMPILE argument will probably become deprecated."
1874 (let* ((fasl-output nil
)
1876 (output-file-name nil
)
1877 (coutput-file-name nil
)
1880 (failure-p t
) ; T in case error keeps this from being set later
1881 (input-pathname (verify-source-file input-file
))
1883 (make-file-source-info input-pathname external-format
1884 #-sb-xc-host t
)) ; can't track, no SBCL streams
1885 (*compiler-trace-output
* nil
)) ; might be modified below
1890 (setq output-file-name
1891 (sb!xc
:compile-file-pathname input-file
1892 :output-file output-file
))
1894 (open-fasl-output output-file-name
1895 (namestring input-pathname
))))
1897 (setq coutput-file-name
1898 (make-pathname :type
"cfasl"
1899 :defaults output-file-name
))
1901 (open-fasl-output coutput-file-name
1902 (namestring input-pathname
))))
1904 (let* ((default-trace-file-pathname
1905 (make-pathname :type
"trace" :defaults input-pathname
))
1906 (trace-file-pathname
1907 (if (eql trace-file t
)
1908 default-trace-file-pathname
1909 (merge-pathnames trace-file
1910 default-trace-file-pathname
))))
1911 (setf *compiler-trace-output
*
1912 (open trace-file-pathname
1913 :if-exists
:supersede
1914 :direction
:output
))))
1916 (when sb
!xc
:*compile-verbose
*
1917 (print-compile-start-note source-info
))
1919 (let ((*compile-object
* fasl-output
)
1920 (*compile-toplevel-object
* cfasl-output
))
1921 (setf (values abort-p warnings-p failure-p
)
1922 (sub-compile-file source-info
))))
1924 (close-source-info source-info
)
1927 (close-fasl-output fasl-output abort-p
)
1928 (setq output-file-name
1929 (pathname (fasl-output-stream fasl-output
)))
1930 (when (and (not abort-p
) sb
!xc
:*compile-verbose
*)
1931 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name
))))
1934 (close-fasl-output cfasl-output abort-p
)
1935 (when (and (not abort-p
) sb
!xc
:*compile-verbose
*)
1936 (compiler-mumble "; ~A written~%" (namestring coutput-file-name
))))
1938 (when sb
!xc
:*compile-verbose
*
1939 (print-compile-end-note source-info
(not abort-p
)))
1941 (when *compiler-trace-output
*
1942 (close *compiler-trace-output
*)))
1944 ;; CLHS says that the first value is NIL if the "file could not
1945 ;; be created". We interpret this to mean "a valid fasl could not
1946 ;; be created" -- which can happen if the compilation is aborted
1947 ;; before the whole file has been processed, due to eg. a reader
1949 (values (when (and (not abort-p
) output-file
)
1950 ;; Hack around filesystem race condition...
1951 (or (probe-file output-file-name
) output-file-name
))
1955 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1956 ;;; the OUTPUT-FILE argument
1958 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1959 ;;; that results from merging the INPUT-FILE with the value of
1960 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1961 ;;; default to the appropriate implementation-defined default type for
1963 (defun cfp-output-file-default (input-file)
1964 (let* ((defaults (merge-pathnames input-file
*default-pathname-defaults
*))
1965 (retyped (make-pathname :type
*fasl-file-type
* :defaults defaults
)))
1968 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1969 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1970 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1971 ;;; pathname, it is translated into a physical pathname as if by
1972 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1973 ;;; So I haven't really tried to make this precisely ANSI-compatible
1974 ;;; at the level of e.g. whether it returns logical pathname or a
1975 ;;; physical pathname. Patches to make it more correct are welcome.
1976 ;;; -- WHN 2000-12-09
1977 (defun sb!xc
:compile-file-pathname
(input-file
1979 (output-file nil output-file-p
)
1982 "Return a pathname describing what file COMPILE-FILE would write to given
1985 (merge-pathnames output-file
(cfp-output-file-default input-file
))
1986 (cfp-output-file-default input-file
)))
1988 ;;;; MAKE-LOAD-FORM stuff
1990 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1991 ;;; finds a constant structure, it invokes this to arrange for proper
1992 ;;; dumping. If it turns out that the constant has already been
1993 ;;; dumped, then we don't need to do anything.
1995 ;;; If the constant hasn't been dumped, then we check to see whether
1996 ;;; we are in the process of creating it. We detect this by
1997 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1998 ;;; the constants we are in the process of creating. Actually, each
1999 ;;; entry is a list of the constant and any init forms that need to be
2000 ;;; processed on behalf of that constant.
2002 ;;; It's not necessarily an error for this to happen. If we are
2003 ;;; processing the init form for some object that showed up *after*
2004 ;;; the original reference to this constant, then we just need to
2005 ;;; defer the processing of that init form. To detect this, we
2006 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
2007 ;;; constants created since the last time we started processing an
2008 ;;; init form. If the constant passed to emit-make-load-form shows up
2009 ;;; in this list, then there is a circular chain through creation
2010 ;;; forms, which is an error.
2012 ;;; If there is some intervening init form, then we blow out of
2013 ;;; processing it by throwing to the tag PENDING-INIT. The value we
2014 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
2015 ;;; offending init form can be tacked onto the init forms for the
2016 ;;; circular object.
2018 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
2019 ;;; we have to create it. We call %MAKE-LOAD-FORM and check
2020 ;;; if the result is 'FOP-STRUCT, and if so we don't do anything.
2021 ;;; The dumper will eventually get its hands on the object and use the
2022 ;;; normal structure dumping noise on it.
2024 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
2025 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
2026 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
2027 ;;; dumper to use that result instead whenever it sees this constant.
2029 ;;; Now we try to compile the init form. We bind
2030 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
2031 ;;; form (and any init forms that were added because of circularity
2032 ;;; detection). If this works, great. If not, we add the init forms to
2033 ;;; the init forms for the object that caused the problems and let it
2035 (defvar *constants-being-created
* nil
)
2036 (defvar *constants-created-since-last-init
* nil
)
2037 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
2038 (defun emit-make-load-form (constant &optional
(name nil namep
)
2039 &aux
(fasl *compile-object
*))
2040 (aver (fasl-output-p fasl
))
2041 (unless (fasl-constant-already-dumped-p constant fasl
)
2042 (let ((circular-ref (assoc constant
*constants-being-created
* :test
#'eq
)))
2044 (when (find constant
*constants-created-since-last-init
* :test
#'eq
)
2046 (throw 'pending-init circular-ref
)))
2047 ;; If this is a global constant reference, we can call SYMBOL-GLOBAL-VALUE
2048 ;; during LOAD as a fasl op, and not compile a lambda.
2050 (fopcompile `(symbol-global-value ',name
) nil t nil
)
2051 (fasl-note-handle-for-constant constant
(sb!fasl
::dump-pop fasl
) fasl
)
2052 (return-from emit-make-load-form nil
))
2053 (multiple-value-bind (creation-form init-form
) (%make-load-form constant
)
2055 (sb!fasl
::fop-struct
2056 (fasl-validate-structure constant fasl
)
2061 (let* ((name (write-to-string constant
:level
1 :length
2))
2063 (list constant name init-form
)
2065 (let ((*constants-being-created
*
2066 (cons info
*constants-being-created
*))
2067 (*constants-created-since-last-init
*
2068 (cons constant
*constants-created-since-last-init
*)))
2071 (fasl-note-handle-for-constant
2073 (cond ((typep creation-form
2074 '(cons (eql sb
!kernel
::new-instance
)
2075 (cons symbol null
)))
2076 (dump-object (cadr creation-form
) fasl
)
2077 (dump-fop 'sb
!fasl
::fop-allocate-instance fasl
)
2078 (let ((index (sb!fasl
::fasl-output-table-free fasl
)))
2079 (setf (sb!fasl
::fasl-output-table-free fasl
) (1+ index
))
2082 (compile-load-time-value creation-form
)))
2085 (compiler-error "circular references in creation form for ~S"
2088 (let* ((*constants-created-since-last-init
* nil
)
2090 (catch 'pending-init
2091 (loop for
(nil form
) on
(cdr info
) by
#'cddr
2092 collect form into forms
2093 finally
(compile-make-load-form-init-forms forms fasl
))
2096 (setf (cdr circular-ref
)
2097 (append (cdr circular-ref
) (cdr info
)))))))
2101 ;;;; Host compile time definitions
2103 (defun compile-in-lexenv (name lambda lexenv
)
2104 (declare (ignore lexenv
))
2105 (compile name lambda
))
2108 (defun eval-tlf (form index
&optional lexenv
)
2109 (declare (ignore index lexenv
))