Fix some style-warnings when bootstrapping with CCL
[sbcl.git] / src / compiler / main.lisp
blob51373939ce787f68c7f12db0b66036918dc77f85
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
6 ;;;; more information.
7 ;;;;
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.
14 (in-package "SB!C")
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 *continuation-number* *continuation-numbers*
20 *number-continuations* *tn-id* *tn-ids* *id-tns*
21 *label-ids* *label-id* *id-labels*
22 *undefined-warnings* *compiler-error-count*
23 *compiler-warning-count* *compiler-style-warning-count*
24 *compiler-note-count*
25 *compiler-error-bailout*
26 *last-source-context* *last-original-source*
27 *last-source-form* *last-format-string* *last-format-args*
28 *last-message-count* *last-error-context*
29 *lexenv* *fun-names-in-this-file*
30 *allow-instrumenting*))
32 ;;; Whether reference to a thing which cannot be defined causes a full
33 ;;; warning.
34 (defvar *flame-on-necessarily-undefined-thing* nil)
36 (defvar *check-consistency* nil)
38 ;;; Set to NIL to disable loop analysis for register allocation.
39 (defvar *loop-analyze* t)
41 ;;; Bind this to a stream to capture various internal debugging output.
42 (defvar *compiler-trace-output* nil)
44 ;;; The current block compilation state. These are initialized to the
45 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
46 ;;; called with.
47 ;;;
48 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
49 ;;; argument, which overrides any internal declarations.
50 (defvar *block-compile*)
51 (defvar *block-compile-arg*)
52 (declaim (type (member nil t :specified) *block-compile* *block-compile-arg*))
53 (defvar *entry-points*)
54 (declaim (list *entry-points*))
56 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
57 ;;; lambdas resulting from compiling subforms. (In reverse order.)
58 (defvar *toplevel-lambdas*)
59 (declaim (list *toplevel-lambdas*))
61 ;;; The current non-macroexpanded toplevel form as printed when
62 ;;; *compile-print* is true.
63 (defvar *top-level-form-noted* nil)
65 (defvar sb!xc:*compile-verbose* t
66 #!+sb-doc
67 "The default for the :VERBOSE argument to COMPILE-FILE.")
68 (defvar sb!xc:*compile-print* t
69 #!+sb-doc
70 "The default for the :PRINT argument to COMPILE-FILE.")
71 (defvar *compile-progress* nil
72 #!+sb-doc
73 "When this is true, the compiler prints to *STANDARD-OUTPUT* progress
74 information about the phases of compilation of each function. (This
75 is useful mainly in large block compilations.)")
77 (defvar sb!xc:*compile-file-pathname* nil
78 #!+sb-doc
79 "The defaulted pathname of the file currently being compiled, or NIL if not
80 compiling.")
81 (defvar sb!xc:*compile-file-truename* nil
82 #!+sb-doc
83 "The TRUENAME of the file currently being compiled, or NIL if not
84 compiling.")
86 (declaim (type (or pathname null)
87 sb!xc:*compile-file-pathname*
88 sb!xc:*compile-file-truename*))
90 ;;; the SOURCE-INFO structure for the current compilation. This is
91 ;;; null globally to indicate that we aren't currently in any
92 ;;; identifiable compilation.
93 (defvar *source-info* nil)
95 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
96 ;;; normally causes nested uses to be no-ops).
97 (defvar *in-compilation-unit* nil)
99 ;;; Count of the number of compilation units dynamically enclosed by
100 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
101 (defvar *aborted-compilation-unit-count*)
103 ;;; Mumble conditional on *COMPILE-PROGRESS*.
104 (defun maybe-mumble (&rest foo)
105 (when *compile-progress*
106 (compiler-mumble "~&")
107 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
108 (apply #'compiler-mumble foo))))
110 (deftype object () '(or fasl-output core-object null))
112 (defvar *compile-object* nil)
113 (declaim (type object *compile-object*))
114 (defvar *compile-toplevel-object* nil)
116 (defvar *emit-cfasl* nil)
118 (defvar *fopcompile-label-counter*)
120 ;; Used during compilation to map code paths to the matching
121 ;; instrumentation conses.
122 (defvar *code-coverage-records* nil)
123 ;; Used during compilation to keep track of with source paths have been
124 ;; instrumented in which blocks.
125 (defvar *code-coverage-blocks* nil)
126 ;; Stores the code coverage instrumentation results. Keys are namestrings,
127 ;; the value is a list of (CONS PATH STATE), where STATE is NIL for
128 ;; a path that has not been visited, and T for one that has.
129 (defvar *code-coverage-info* (make-hash-table :test 'equal))
132 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
134 (defmacro sb!xc:with-compilation-unit (options &body body)
135 #!+sb-doc
136 "Affects compilations that take place within its dynamic extent. It is
137 intended to be eg. wrapped around the compilation of all files in the same system.
139 Following options are defined:
141 :OVERRIDE Boolean-Form
142 One of the effects of this form is to delay undefined warnings until the
143 end of the form, instead of giving them at the end of each compilation.
144 If OVERRIDE is NIL (the default), then the outermost
145 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
146 OVERRIDE true causes that form to grab any enclosed warnings, even if it
147 is enclosed by another WITH-COMPILATION-UNIT.
149 :POLICY Optimize-Declaration-Form
150 Provides dynamic scoping for global compiler optimization qualities and
151 restrictions, limiting effects of subsequent OPTIMIZE proclamations and
152 calls to SB-EXT:RESTRICT-COMPILER-POLICY to the dynamic scope of BODY.
154 If OVERRIDE is false, specified POLICY is merged with current global
155 policy. If OVERRIDE is true, current global policy, including any
156 restrictions, is discarded in favor of the specified POLICY.
158 Supplying POLICY NIL is equivalent to the option not being supplied at
159 all, ie. dynamic scoping of policy does not take place.
161 This option is an SBCL-specific experimental extension: Interface
162 subject to change.
164 :SOURCE-NAMESTRING Namestring-Form
165 Attaches the value returned by the Namestring-Form to the internal
166 debug-source information as the namestring of the source file. Normally
167 the namestring of the input-file for COMPILE-FILE is used: this option
168 can be used to provide source-file information for functions compiled
169 using COMPILE, or to override the input-file of COMPILE-FILE.
171 If both an outer and an inner WITH-COMPILATION-UNIT provide a
172 SOURCE-NAMESTRING, the inner one takes precedence. Unaffected
173 by :OVERRIDE.
175 This is an SBCL-specific extension.
177 :SOURCE-PLIST Plist-Form
178 Attaches the value returned by the Plist-Form to internal debug-source
179 information of functions compiled in within the dynamic extent of BODY.
181 Primarily for use by development environments, in order to eg. associate
182 function definitions with editor-buffers. Can be accessed using
183 SB-INTROSPECT:DEFINITION-SOURCE-PLIST.
185 If an outer WITH-COMPILATION-UNIT form also provide a SOURCE-PLIST, it
186 is appended to the end of the provided SOURCE-PLIST. Unaffected
187 by :OVERRIDE.
189 This is an SBCL-specific extension.
191 Examples:
193 ;; Prevent proclamations from the file leaking, and restrict
194 ;; SAFETY to 3 -- otherwise uses the current global policy.
195 (with-compilation-unit (:policy '(optimize))
196 (restrict-compiler-policy 'safety 3)
197 (load \"foo.lisp\"))
199 ;; Using default policy instead of the current global one,
200 ;; except for DEBUG 3.
201 (with-compilation-unit (:policy '(optimize debug)
202 :override t)
203 (load \"foo.lisp\"))
205 ;; Same as if :POLICY had not been specified at all: SAFETY 3
206 ;; proclamation leaks out from WITH-COMPILATION-UNIT.
207 (with-compilation-unit (:policy nil)
208 (declaim (optimize safety))
209 (load \"foo.lisp\"))
211 `(%with-compilation-unit (lambda () ,@body) ,@options))
213 (defvar *source-plist* nil)
214 (defvar *source-namestring* nil)
216 (defun %with-compilation-unit (fn &key override policy source-plist source-namestring)
217 (declare (type function fn))
218 (flet ((with-it ()
219 (let ((succeeded-p nil)
220 (*source-plist* (append source-plist *source-plist*))
221 (*source-namestring* (or source-namestring *source-namestring*)))
222 (if (and *in-compilation-unit* (not override))
223 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
224 ;; ordinarily (unless OVERRIDE) basically a no-op.
225 (unwind-protect
226 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
227 (unless succeeded-p
228 (incf *aborted-compilation-unit-count*)))
229 (let ((*aborted-compilation-unit-count* 0)
230 (*compiler-error-count* 0)
231 (*compiler-warning-count* 0)
232 (*compiler-style-warning-count* 0)
233 (*compiler-note-count* 0)
234 (*undefined-warnings* nil)
235 (*in-compilation-unit* t))
236 (handler-bind ((parse-unknown-type
237 (lambda (c)
238 (note-undefined-reference
239 (parse-unknown-type-specifier c)
240 :type))))
241 (unwind-protect
242 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
243 (unless succeeded-p
244 (incf *aborted-compilation-unit-count*))
245 (summarize-compilation-unit (not succeeded-p)))))))))
246 (if policy
247 (let ((*policy* (process-optimize-decl policy (unless override *policy*)))
248 (*policy-restrictions* (unless override *policy-restrictions*)))
249 (with-it))
250 (with-it))))
252 ;;; Is NAME something that no conforming program can rely on
253 ;;; defining?
254 (defun name-reserved-by-ansi-p (name kind)
255 (ecase kind
256 (:function
257 (eq (symbol-package (fun-name-block-name name))
258 *cl-package*))
259 (:type
260 (let ((symbol (typecase name
261 (symbol name)
262 ((cons symbol) (car name))
263 (t (return-from name-reserved-by-ansi-p nil)))))
264 (eq (symbol-package symbol) *cl-package*)))))
266 ;;; This is to be called at the end of a compilation unit. It signals
267 ;;; any residual warnings about unknown stuff, then prints the total
268 ;;; error counts. ABORT-P should be true when the compilation unit was
269 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
270 ;;; enclosed nested compilation units that were aborted.
271 (defun summarize-compilation-unit (abort-p)
272 (let (summary)
273 (unless abort-p
274 (handler-bind ((style-warning #'compiler-style-warning-handler)
275 (warning #'compiler-warning-handler))
277 (let ((undefs (sort *undefined-warnings* #'string<
278 :key (lambda (x)
279 (let ((x (undefined-warning-name x)))
280 (if (symbolp x)
281 (symbol-name x)
282 (prin1-to-string x)))))))
283 (dolist (kind '(:variable :function :type))
284 (let ((names (mapcar #'undefined-warning-name
285 (remove kind undefs :test #'neq
286 :key #'undefined-warning-kind))))
287 (when names (push (cons kind names) summary))))
288 (dolist (undef undefs)
289 (let ((name (undefined-warning-name undef))
290 (kind (undefined-warning-kind undef))
291 (warnings (undefined-warning-warnings undef))
292 (undefined-warning-count (undefined-warning-count undef)))
293 (dolist (*compiler-error-context* warnings)
294 (if #-sb-xc-host (and (member kind '(:function :type))
295 (name-reserved-by-ansi-p name kind)
296 *flame-on-necessarily-undefined-thing*)
297 #+sb-xc-host nil
298 (ecase kind
299 (:function
300 (compiler-warn
301 "~@<The function ~S is undefined, and its name is ~
302 reserved by ANSI CL so that even if it were ~
303 defined later, the code doing so would not be ~
304 portable.~:@>" name))
305 (:type
306 (if (and (consp name) (eq 'quote (car name)))
307 (compiler-warn
308 "~@<Undefined type ~S. The name starts with ~S: ~
309 probably use of a quoted type name in a context ~
310 where the name is not evaluated.~:@>"
311 name 'quote)
312 (compiler-warn
313 "~@<Undefined type ~S. Note that name ~S is ~
314 reserved by ANSI CL, so code defining a type with ~
315 that name would not be portable.~:@>" name
316 name))))
317 (if (eq kind :variable)
318 (compiler-warn "undefined ~(~A~): ~S" kind name)
319 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
320 (let ((warn-count (length warnings)))
321 (when (and warnings (> undefined-warning-count warn-count))
322 (let ((more (- undefined-warning-count warn-count)))
323 (if (eq kind :variable)
324 (compiler-warn
325 "~W more use~:P of undefined ~(~A~) ~S"
326 more kind name)
327 (compiler-style-warn
328 "~W more use~:P of undefined ~(~A~) ~S"
329 more kind name))))))))))
331 (unless (and (not abort-p)
332 (zerop *aborted-compilation-unit-count*)
333 (zerop *compiler-error-count*)
334 (zerop *compiler-warning-count*)
335 (zerop *compiler-style-warning-count*)
336 (zerop *compiler-note-count*))
337 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
338 (format *error-output* "~&compilation unit ~:[finished~;aborted~]"
339 abort-p)
340 (dolist (cell summary)
341 (destructuring-bind (kind &rest names) cell
342 (format *error-output*
343 "~& Undefined ~(~A~)~p:~
344 ~% ~{~<~% ~1:;~S~>~^ ~}"
345 kind (length names) names)))
346 (format *error-output* "~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
347 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
348 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
349 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
350 ~[~:;~:*~& printed ~W note~:P~]"
351 *aborted-compilation-unit-count*
352 *compiler-error-count*
353 *compiler-warning-count*
354 *compiler-style-warning-count*
355 *compiler-note-count*))
356 (terpri *error-output*)
357 (force-output *error-output*))))
359 ;; Bidrectional map between IR1/IR2/assembler abstractions
360 ;; and a corresponding small integer identifier. One direction could be done
361 ;; by adding the integer ID as an object slot, but we want both directions.
362 (defstruct (compiler-ir-obj-map (:conc-name objmap-)
363 (:constructor make-compiler-ir-obj-map ())
364 (:copier nil)
365 (:predicate nil))
366 (obj-to-id (make-hash-table :test 'eq) :read-only t)
367 (id-to-cont (make-array 10) :type simple-vector) ; number -> CTRAN or LVAR
368 (id-to-tn (make-array 10) :type simple-vector) ; number -> TN
369 (id-to-label (make-array 10) :type simple-vector) ; number -> LABEL
370 (cont-num 0 :type fixnum)
371 (tn-id 0 :type fixnum)
372 (label-id 0 :type fixnum))
374 (declaim (type compiler-ir-obj-map *compiler-ir-obj-map*))
375 (defvar *compiler-ir-obj-map*)
377 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
378 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
379 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
380 (defmacro with-compilation-values (&body body)
381 ;; This binding could just as well be in WITH-IR1-NAMESPACE, but
382 ;; since it's primarily a debugging tool, it's nicer to have
383 ;; a wider unique scope by ID.
384 `(let ((*compiler-ir-obj-map* (make-compiler-ir-obj-map)))
385 (unwind-protect
386 (let ((*warnings-p* nil)
387 (*failure-p* nil))
388 (handler-bind ((compiler-error #'compiler-error-handler)
389 (style-warning #'compiler-style-warning-handler)
390 (warning #'compiler-warning-handler))
391 (values (progn ,@body)
392 *warnings-p*
393 *failure-p*)))
394 (let ((map *compiler-ir-obj-map*))
395 (clrhash (objmap-obj-to-id map))
396 (fill (objmap-id-to-cont map) nil)
397 (fill (objmap-id-to-tn map) nil)
398 (fill (objmap-id-to-label map) nil)))))
400 ;;; THING is a kind of thing about which we'd like to issue a warning,
401 ;;; but showing at most one warning for a given set of <THING,FMT,ARGS>.
402 ;;; The compiler does a good job of making sure not to print repetitive
403 ;;; warnings for code that it compiles, but this solves a different problem.
404 ;;; Specifically, for a warning from PARSE-LAMBDA-LIST, there are three calls:
405 ;;; - once in the expander for defmacro itself, as it calls MAKE-MACRO-LAMBDA
406 ;;; which calls PARSE-LAMBDA-LIST. This is the toplevel form processing.
407 ;;; - again for :compile-toplevel, where the DS-BIND calls PARSE-LAMBDA-LIST.
408 ;;; If compiling in compile-toplevel, then *COMPILE-OBJECT* is a core object,
409 ;;; but if interpreting, then it is still a fasl.
410 ;;; - once for compiling to fasl. *COMPILE-OBJECT* is a fasl.
411 ;;; I'd have liked the data to be associated with the fasl, except that
412 ;;; as indicated above, the second line hides some information.
413 (defun style-warn-once (thing fmt &rest args)
414 (declare (special *compile-object*))
415 (let* ((source-info *source-info*)
416 (file-info (and (source-info-p source-info)
417 (source-info-file-info source-info)))
418 (file-compiling-p (file-info-p file-info)))
419 (flet ((match-p (entry &aux (rest (cdr entry)))
420 ;; THING is compared by EQ, FMT by STRING=.
421 (and (eq (car entry) thing)
422 (string= (car rest) fmt)
423 ;; We don't want to walk into default values,
424 ;; e.g. (&optional (b #<insane-struct))
425 ;; because #<insane-struct> might be circular.
426 (equal-but-no-car-recursion (cdr rest) args))))
427 (unless (and file-compiling-p
428 (find-if #'match-p
429 (file-info-style-warning-tracker file-info)))
430 (when file-compiling-p
431 (push (list* thing fmt args)
432 (file-info-style-warning-tracker file-info)))
433 (apply 'style-warn fmt args)))))
435 ;;;; component compilation
437 (defparameter *max-optimize-iterations* 3 ; ARB
438 #!+sb-doc
439 "The upper limit on the number of times that we will consecutively do IR1
440 optimization that doesn't introduce any new code. A finite limit is
441 necessary, since type inference may take arbitrarily long to converge.")
443 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
444 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
446 ;;; Repeatedly optimize COMPONENT until no further optimizations can
447 ;;; be found or we hit our iteration limit. When we hit the limit, we
448 ;;; clear the component and block REOPTIMIZE flags to discourage the
449 ;;; next optimization attempt from pounding on the same code.
450 (defun ir1-optimize-until-done (component)
451 (declare (type component component))
452 (maybe-mumble "opt")
453 (event ir1-optimize-until-done)
454 (let ((count 0)
455 (cleared-reanalyze nil)
456 (fastp nil))
457 (loop
458 (when (component-reanalyze component)
459 (setq count 0)
460 (setq cleared-reanalyze t)
461 (setf (component-reanalyze component) nil))
462 (setf (component-reoptimize component) nil)
463 (ir1-optimize component fastp)
464 (cond ((component-reoptimize component)
465 (incf count)
466 (when (and (>= count *max-optimize-iterations*)
467 (not (component-reanalyze component))
468 (eq (component-reoptimize component) :maybe))
469 (maybe-mumble "*")
470 (cond ((retry-delayed-ir1-transforms :optimize)
471 (maybe-mumble "+")
472 (setq count 0))
474 (event ir1-optimize-maxed-out)
475 (setf (component-reoptimize component) nil)
476 (do-blocks (block component)
477 (setf (block-reoptimize block) nil))
478 (return)))))
479 ((retry-delayed-ir1-transforms :optimize)
480 (setf count 0)
481 (maybe-mumble "+"))
483 (maybe-mumble " ")
484 (return)))
485 (setq fastp (>= count *max-optimize-iterations*))
486 (maybe-mumble (if fastp "-" ".")))
487 (when cleared-reanalyze
488 (setf (component-reanalyze component) t)))
489 (values))
491 (defparameter *constraint-propagate* t)
493 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
494 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
495 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
496 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
498 ;;; FIXME: It would be good to document why it's important to have a
499 ;;; large value here, and what the drawbacks of an excessively large
500 ;;; value are; and it might also be good to make it depend on
501 ;;; optimization policy.
502 (defparameter *reoptimize-after-type-check-max* 10)
504 (defevent reoptimize-maxed-out
505 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
507 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
508 (defun dfo-as-needed (component)
509 (declare (type component component))
510 (when (component-reanalyze component)
511 (maybe-mumble "DFO")
512 (loop
513 (find-dfo component)
514 (unless (component-reanalyze component)
515 (maybe-mumble " ")
516 (return))
517 (maybe-mumble ".")))
518 (values))
520 ;;; Do all the IR1 phases for a non-top-level component.
521 (defun ir1-phases (component)
522 (declare (type component component))
523 (aver-live-component component)
524 (let ((*constraint-universe* (make-array 64 ; arbitrary, but don't
525 ;make this 0.
526 :fill-pointer 0 :adjustable t))
527 (loop-count 1)
528 (*delayed-ir1-transforms* nil))
529 (declare (special *constraint-universe* *delayed-ir1-transforms*))
530 (loop
531 (ir1-optimize-until-done component)
532 (when (or (component-new-functionals component)
533 (component-reanalyze-functionals component))
534 (maybe-mumble "locall ")
535 (locall-analyze-component component))
536 (dfo-as-needed component)
537 (when *constraint-propagate*
538 (maybe-mumble "constraint ")
539 (constraint-propagate component))
540 (when (retry-delayed-ir1-transforms :constraint)
541 (maybe-mumble "Rtran "))
542 (flet ((want-reoptimization-p ()
543 (or (component-reoptimize component)
544 (component-reanalyze component)
545 (component-new-functionals component)
546 (component-reanalyze-functionals component))))
547 (unless (and (want-reoptimization-p)
548 ;; We delay the generation of type checks until
549 ;; the type constraints have had time to
550 ;; propagate, else the compiler can confuse itself.
551 (< loop-count (- *reoptimize-after-type-check-max* 4)))
552 (maybe-mumble "type ")
553 (generate-type-checks component)
554 (unless (want-reoptimization-p)
555 (return))))
556 (when (>= loop-count *reoptimize-after-type-check-max*)
557 (maybe-mumble "[reoptimize limit]")
558 (event reoptimize-maxed-out)
559 (return))
560 (incf loop-count)))
562 (when *check-consistency*
563 (do-blocks-backwards (block component)
564 (awhen (flush-dead-code block)
565 (let ((*compiler-error-context* it))
566 (compiler-warn "dead code detected at the end of ~S"
567 'ir1-phases)))))
569 (ir1-finalize component)
570 (values))
572 (defun %compile-component (component)
573 (let ((*code-segment* nil)
574 (*elsewhere* nil)
575 #!+inline-constants
576 (*constant-segment* nil)
577 #!+inline-constants
578 (*constant-table* nil)
579 #!+inline-constants
580 (*constant-vector* nil))
581 (maybe-mumble "GTN ")
582 (gtn-analyze component)
583 (maybe-mumble "LTN ")
584 (ltn-analyze component)
585 (dfo-as-needed component)
586 (maybe-mumble "control ")
587 (control-analyze component #'make-ir2-block)
589 (when (or (ir2-component-values-receivers (component-info component))
590 (component-dx-lvars component))
591 (maybe-mumble "stack ")
592 (find-dominators component)
593 (stack-analyze component)
594 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
595 ;; stack analysis. There shouldn't be any unreachable code after
596 ;; control, so this won't delete anything.
597 (dfo-as-needed component))
599 (unwind-protect
600 (progn
601 (maybe-mumble "IR2tran ")
602 (init-assembler)
603 (entry-analyze component)
604 (ir2-convert component)
606 (when (policy *lexenv* (>= speed compilation-speed))
607 (maybe-mumble "copy ")
608 (copy-propagate component))
610 (ir2-optimize component)
612 (select-representations component)
614 (when *check-consistency*
615 (maybe-mumble "check2 ")
616 (check-ir2-consistency component))
618 (delete-unreferenced-tns component)
620 (maybe-mumble "life ")
621 (lifetime-analyze component)
623 (when *compile-progress*
624 (compiler-mumble "") ; Sync before doing more output.
625 (pre-pack-tn-stats component *standard-output*))
627 (when *check-consistency*
628 (maybe-mumble "check-life ")
629 (check-life-consistency component))
631 (maybe-mumble "pack ")
632 (sb!regalloc:pack component)
634 (when *check-consistency*
635 (maybe-mumble "check-pack ")
636 (check-pack-consistency component))
638 (when *compiler-trace-output*
639 (describe-component component *compiler-trace-output*)
640 (describe-ir2-component component *compiler-trace-output*))
642 (maybe-mumble "code ")
643 (optimize-constant-loads component)
644 (multiple-value-bind (code-length fixup-notes)
645 (generate-code component)
647 #-sb-xc-host
648 (when *compiler-trace-output*
649 (format *compiler-trace-output*
650 "~|~%disassembly of code for ~S~2%" component)
651 (sb!disassem:disassemble-assem-segment *code-segment*
652 *compiler-trace-output*))
654 (etypecase *compile-object*
655 (fasl-output
656 (maybe-mumble "fasl")
657 (fasl-dump-component component
658 *code-segment*
659 code-length
660 fixup-notes
661 *compile-object*))
662 #-sb-xc-host ; no compiling to core
663 (core-object
664 (maybe-mumble "core")
665 (make-core-component component
666 *code-segment*
667 code-length
668 fixup-notes
669 *compile-object*))
670 (null))))))
672 ;; We're done, so don't bother keeping anything around.
673 (setf (component-info component) :dead)
675 (values))
677 ;;; Delete components with no external entry points before we try to
678 ;;; generate code. Unreachable closures can cause IR2 conversion to
679 ;;; puke on itself, since it is the reference to the closure which
680 ;;; normally causes the components to be combined.
681 (defun delete-if-no-entries (component)
682 (dolist (fun (component-lambdas component) (delete-component component))
683 (when (functional-has-external-references-p fun)
684 (return))
685 (case (functional-kind fun)
686 (:toplevel (return))
687 (:external
688 (unless (every (lambda (ref)
689 (eq (node-component ref) component))
690 (leaf-refs fun))
691 (return))))))
693 (defun compile-component (component)
695 ;; miscellaneous sanity checks
697 ;; FIXME: These are basically pretty wimpy compared to the checks done
698 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
699 ;; make those internal consistency checks work again and use them.
700 (aver-live-component component)
701 (do-blocks (block component)
702 (aver (eql (block-component block) component)))
703 (dolist (lambda (component-lambdas component))
704 ;; sanity check to prevent weirdness from propagating insidiously as
705 ;; far from its root cause as it did in bug 138: Make sure that
706 ;; thing-to-COMPONENT links are consistent.
707 (aver (eql (lambda-component lambda) component))
708 (aver (eql (node-component (lambda-bind lambda)) component)))
710 (let* ((*component-being-compiled* component))
712 ;; Record xref information before optimization. This way the
713 ;; stored xref data reflects the real source as closely as
714 ;; possible.
715 (record-component-xrefs component)
717 (ir1-phases component)
719 (when *loop-analyze*
720 (dfo-as-needed component)
721 (find-dominators component)
722 (loop-analyze component))
725 (when (and *loop-analyze* *compiler-trace-output*)
726 (labels ((print-blocks (block)
727 (format *compiler-trace-output* " ~A~%" block)
728 (when (block-loop-next block)
729 (print-blocks (block-loop-next block))))
730 (print-loop (loop)
731 (format *compiler-trace-output* "loop=~A~%" loop)
732 (print-blocks (loop-blocks loop))
733 (dolist (l (loop-inferiors loop))
734 (print-loop l))))
735 (print-loop (component-outer-loop component))))
738 ;; This should happen at some point before PHYSENV-ANALYZE, and
739 ;; after RECORD-COMPONENT-XREFS. Beyond that, I haven't really
740 ;; thought things through. -- AJB, 2014-Jun-08
741 (eliminate-dead-code component)
743 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
744 (maybe-mumble "env ")
745 (physenv-analyze component)
746 (dfo-as-needed component)
748 (delete-if-no-entries component)
750 (unless (eq (block-next (component-head component))
751 (component-tail component))
752 (%compile-component component)))
754 (clear-constant-info)
756 (values))
758 ;;;; clearing global data structures
759 ;;;;
760 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
761 ;;;; global data structures entirely when possible and consing up the
762 ;;;; others from scratch instead of clearing and reusing them?
764 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
765 ;;; addition to allowing stuff to be reclaimed, this is required for
766 ;;; correct assignment of constant offsets, since we need to assign a
767 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
768 ;;; slots, since they are used to keep track of functions across
769 ;;; component boundaries.
770 (defun clear-constant-info ()
771 (maphash (lambda (k v)
772 (declare (ignore k))
773 (setf (leaf-info v) nil)
774 (setf (constant-boxed-tn v) nil))
775 *constants*)
776 (maphash (lambda (k v)
777 (declare (ignore k))
778 (when (constant-p v)
779 (setf (leaf-info v) nil)
780 (setf (constant-boxed-tn v) nil)))
781 *free-vars*)
782 (values))
784 ;;; Blow away the REFS for all global variables, and let COMPONENT
785 ;;; be recycled.
786 (defun clear-ir1-info (component)
787 (declare (type component component))
788 (labels ((blast (x)
789 (maphash (lambda (k v)
790 (declare (ignore k))
791 (when (leaf-p v)
792 (setf (leaf-refs v)
793 (delete-if #'here-p (leaf-refs v)))
794 (when (basic-var-p v)
795 (setf (basic-var-sets v)
796 (delete-if #'here-p (basic-var-sets v))))))
798 (here-p (x)
799 (eq (node-component x) component)))
800 (blast *free-vars*)
801 (blast *free-funs*)
802 (blast *constants*))
803 (values))
805 ;;;; trace output
807 ;;; Print out some useful info about COMPONENT to STREAM.
808 (defun describe-component (component *standard-output*)
809 (declare (type component component))
810 (format t "~|~%;;;; component: ~S~2%" (component-name component))
811 (print-all-blocks component)
812 (values))
814 (defun describe-ir2-component (component *standard-output*)
815 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
816 (format t "entries:~%")
817 (dolist (entry (ir2-component-entries (component-info component)))
818 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
819 (label-id (entry-info-offset entry))
820 (entry-info-name entry)
821 (entry-info-closure-tn entry)))
822 (terpri)
823 (pre-pack-tn-stats component *standard-output*)
824 (terpri)
825 (print-ir2-blocks component)
826 (terpri)
827 (values))
829 ;;; Given a pathname, return a SOURCE-INFO structure.
830 (defun make-file-source-info (file external-format &optional form-tracking-p)
831 (make-source-info
832 :file-info (make-file-info :name (truename file)
833 :untruename (merge-pathnames file)
834 :external-format external-format
835 :subforms
836 (if form-tracking-p
837 (make-array 100 :fill-pointer 0 :adjustable t))
838 :write-date (file-write-date file))))
840 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
841 (defun make-lisp-source-info (form &key parent)
842 (make-source-info
843 :file-info (make-file-info :name :lisp
844 :forms (vector form)
845 :positions '#(0))
846 :parent parent))
848 ;;; Walk up the SOURCE-INFO list until we either reach a SOURCE-INFO
849 ;;; with no parent (e.g., from a REPL evaluation) or until we reach a
850 ;;; SOURCE-INFO whose FILE-INFO denotes a file.
851 (defun get-toplevelish-file-info (&optional (source-info *source-info*))
852 (if source-info
853 (do* ((sinfo source-info (source-info-parent sinfo))
854 (finfo (source-info-file-info sinfo)
855 (source-info-file-info sinfo)))
856 ((or (not (source-info-p (source-info-parent sinfo)))
857 (pathnamep (file-info-name finfo)))
858 finfo))))
860 ;;; If STREAM is present, return it, otherwise open a stream to the
861 ;;; current file. There must be a current file.
863 ;;; FIXME: This is probably an unnecessarily roundabout way to do
864 ;;; things now that we process a single file in COMPILE-FILE (unlike
865 ;;; the old CMU CL code, which accepted multiple files). Also, the old
866 ;;; comment said
867 ;;; When we open a new file, we also reset *PACKAGE* and policy.
868 ;;; This gives the effect of rebinding around each file.
869 ;;; which doesn't seem to be true now. Check to make sure that if
870 ;;; such rebinding is necessary, it's still done somewhere.
871 (defun get-source-stream (info)
872 (declare (type source-info info))
873 (or (source-info-stream info)
874 (let* ((file-info (source-info-file-info info))
875 (name (file-info-name file-info))
876 (external-format (file-info-external-format file-info)))
877 (setf sb!xc:*compile-file-truename* name
878 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
879 (source-info-stream info)
880 (let ((stream
881 (open name
882 :direction :input
883 :external-format external-format
884 ;; SBCL stream classes aren't available in the host
885 #-sb-xc-host :class
886 #-sb-xc-host 'form-tracking-stream)))
887 (when (file-info-subforms file-info)
888 (setf (form-tracking-stream-observer stream)
889 (make-form-tracking-stream-observer file-info)))
890 stream)))))
892 ;;; Close the stream in INFO if it is open.
893 (defun close-source-info (info)
894 (declare (type source-info info))
895 (let ((stream (source-info-stream info)))
896 (when stream (close stream)))
897 (setf (source-info-stream info) nil)
898 (values))
900 ;; Loop over forms read from INFO's stream, calling FUNCTION with each.
901 ;; CONDITION-NAME is signaled if there is a reader error, and should be
902 ;; a subtype of not-so-aptly-named INPUT-ERROR-IN-COMPILE-FILE.
903 (defun %do-forms-from-info (function info condition-name)
904 (declare (function function))
905 (let* ((file-info (source-info-file-info info))
906 (stream (get-source-stream info))
907 (pos (file-position stream))
908 (form
909 ;; Return a form read from STREAM; or for EOF use the trick,
910 ;; popularized by Kent Pitman, of returning STREAM itself.
911 (handler-case
912 (progn
913 ;; Reset for a new toplevel form.
914 (when (form-tracking-stream-p stream)
915 (setf (form-tracking-stream-form-start-char-pos stream) nil))
916 (awhen (file-info-subforms file-info)
917 (setf (fill-pointer it) 0))
918 (read-preserving-whitespace stream nil stream))
919 (reader-error (condition)
920 (compiler-error condition-name
921 ;; We don't need to supply :POSITION here because
922 ;; READER-ERRORs already know their position in the file.
923 :condition condition
924 :stream stream))
925 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
926 ;; (and that this is not a READER-ERROR) when it encounters end of
927 ;; file in the middle of something it's trying to read,
928 ;; making it unfortunately indistinguishable from legal EOF.
929 ;; Were it not for that, it would be more elegant to just
930 ;; handle one more condition in the HANDLER-CASE.
931 ((or end-of-file error) (condition)
932 (compiler-error
933 condition-name
934 :condition condition
935 ;; We need to supply :POSITION here because the END-OF-FILE
936 ;; condition doesn't carry the position that the user
937 ;; probably cares about, where the failed READ began.
938 :position
939 (or (and (form-tracking-stream-p stream)
940 (form-tracking-stream-form-start-byte-pos stream))
941 pos)
942 :line/col
943 (and (form-tracking-stream-p stream)
944 (line/col-from-charpos
945 stream
946 (form-tracking-stream-form-start-char-pos stream)))
947 :stream stream)))))
948 (unless (eq form stream) ; not EOF
949 (funcall function form
950 :current-index
951 (let* ((forms (file-info-forms file-info))
952 (current-idx (+ (fill-pointer forms)
953 (file-info-source-root file-info))))
954 (vector-push-extend form forms)
955 (vector-push-extend pos (file-info-positions file-info))
956 current-idx))
957 (%do-forms-from-info function info condition-name))))
959 ;;; Loop over FORMS retrieved from INFO. Used by COMPILE-FILE and
960 ;;; LOAD when loading from a FILE-STREAM associated with a source
961 ;;; file. ON-ERROR is the name of a condition class that should
962 ;;; be signaled if anything goes wrong during a READ.
963 (defmacro do-forms-from-info (((form &rest keys) info
964 &optional (on-error ''input-error-in-load))
965 &body body)
966 (aver (symbolp form))
967 (once-only ((info info))
968 `(let ((*source-info* ,info))
969 (%do-forms-from-info (lambda (,form &key ,@keys &allow-other-keys)
970 ,@body)
971 ,info ,on-error))))
973 ;;; Read and compile the source file.
974 (defun sub-sub-compile-file (info)
975 (do-forms-from-info ((form current-index) info
976 'input-error-in-compile-file)
977 (with-source-paths
978 (find-source-paths form current-index)
979 (process-toplevel-form
980 form `(original-source-start 0 ,current-index) nil)))
981 ;; It's easy to get into a situation where cold-init crashes and the only
982 ;; backtrace you get from ldb is TOP-LEVEL-FORM, which means you're anywhere
983 ;; within the 23000 or so blobs of code deferred until cold-init.
984 ;; Seeing each file finish narrows things down without the noise of :sb-show,
985 ;; but this hack messes up form positions, so it's not on unless asked for.
986 #+nil ; change to #+sb-xc-host if desired
987 (let ((file-info (get-toplevelish-file-info info)))
988 (declare (ignorable file-info))
989 (let* ((forms (file-info-forms file-info))
990 (form
991 `(write-string
992 ,(format nil "Completed TLFs: ~A~%" (file-info-name file-info))))
993 (index
994 (+ (fill-pointer forms) (file-info-source-root file-info))))
995 (with-source-paths
996 (find-source-paths form index)
997 (process-toplevel-form
998 form `(original-source-start 0 ,index) nil)))))
1000 ;;; Return the INDEX'th source form read from INFO and the position
1001 ;;; where it was read.
1002 (defun find-source-root (index info)
1003 (declare (type index index) (type source-info info))
1004 (let ((file-info (source-info-file-info info)))
1005 (values (aref (file-info-forms file-info) index)
1006 (aref (file-info-positions file-info) index))))
1008 ;;;; processing of top level forms
1010 ;;; This is called by top level form processing when we are ready to
1011 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
1012 ;;; convert the form, but delay compilation, pushing the result on
1013 ;;; *TOPLEVEL-LAMBDAS* instead.
1014 (defun convert-and-maybe-compile (form path &optional (expand t))
1015 (declare (list path))
1016 #+sb-xc-host
1017 (when sb-cold::*compile-for-effect-only*
1018 (return-from convert-and-maybe-compile))
1019 (let ((*top-level-form-noted* (note-top-level-form form t)))
1020 ;; Don't bother to compile simple objects that just sit there.
1021 (when (and form (or (symbolp form) (consp form)))
1022 (if (fopcompilable-p form expand)
1023 (let ((*fopcompile-label-counter* 0))
1024 (fopcompile form path nil expand))
1025 (with-ir1-namespace
1026 (let ((*lexenv* (make-lexenv
1027 :policy *policy*
1028 :handled-conditions *handled-conditions*
1029 :disabled-package-locks *disabled-package-locks*))
1030 (tll (ir1-toplevel form path nil)))
1031 (if (eq *block-compile* t)
1032 (push tll *toplevel-lambdas*)
1033 (compile-toplevel (list tll) nil))
1034 nil))))))
1036 ;;; Macroexpand FORM in the current environment with an error handler.
1037 ;;; We only expand one level, so that we retain all the intervening
1038 ;;; forms in the source path. A compiler-macro takes precedence over
1039 ;;; an ordinary macro as specified in CLHS 3.2.3.1
1040 ;;; Note that this function is _only_ for processing of toplevel forms.
1041 ;;; Non-toplevel forms use IR1-CONVERT-FUNCTOID which considers compiler macros.
1042 (defun preprocessor-macroexpand-1 (form)
1043 (if (listp form)
1044 (let ((expansion (expand-compiler-macro form)))
1045 (if (neq expansion form)
1046 (return-from preprocessor-macroexpand-1
1047 (values expansion t)))))
1048 (handler-case (%macroexpand-1 form *lexenv*)
1049 (error (condition)
1050 (compiler-error "(during macroexpansion of ~A)~%~A"
1051 (let ((*print-level* 2)
1052 (*print-length* 2))
1053 (format nil "~S" form))
1054 condition))))
1056 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
1057 ;;; the forms, and PATH is the source path of the FORM they came out of.
1058 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
1059 (defun process-toplevel-progn (forms path compile-time-too)
1060 (declare (list forms) (list path))
1061 (dolist (form forms)
1062 (process-toplevel-form form path compile-time-too)))
1064 ;;; Process a top level use of LOCALLY, or anything else (e.g.
1065 ;;; MACROLET) at top level which has declarations and ordinary forms.
1066 ;;; We parse declarations and then recursively process the body.
1067 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
1068 (declare (list path))
1069 (multiple-value-bind (forms decls) (parse-body body nil t)
1070 (with-ir1-namespace
1071 (let* ((*lexenv* (process-decls decls vars funs))
1072 ;; FIXME: VALUES declaration
1074 ;; Binding *POLICY* is pretty much of a hack, since it
1075 ;; causes LOCALLY to "capture" enclosed proclamations. It
1076 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
1077 ;; value of *POLICY* as the policy. The need for this hack
1078 ;; is due to the quirk that there is no way to represent in
1079 ;; a POLICY that an optimize quality came from the default.
1081 ;; FIXME: Ideally, something should be done so that DECLAIM
1082 ;; inside LOCALLY works OK. Failing that, at least we could
1083 ;; issue a warning instead of silently screwing up.
1084 ;; Here's how to fix this: a POLICY object can in fact represent
1085 ;; absence of qualitities. Whenever we rebind *POLICY* (here and
1086 ;; elsewhere), it should be bound to a policy that expresses no
1087 ;; qualities. Proclamations should update SYMBOL-GLOBAL-VALUE of
1088 ;; *POLICY*, which can be seen irrespective of dynamic bindings,
1089 ;; and declarations should update the lexical policy.
1090 ;; The POLICY macro can be amended to merge the dynamic *POLICY*
1091 ;; (or whatever it came from, like a LEXENV) with the global
1092 ;; *POLICY*. COERCE-TO-POLICY can do the merge, employing a 1-line
1093 ;; cache so that repeated calls for any two fixed policy objects
1094 ;; return the identical value (since policies are immutable).
1095 (*policy* (lexenv-policy *lexenv*))
1096 ;; This is probably also a hack
1097 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
1098 ;; ditto
1099 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*)))
1100 (process-toplevel-progn forms path compile-time-too)))))
1102 ;;; Parse an EVAL-WHEN situations list, returning three flags,
1103 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
1104 ;;; the types of situations present in the list.
1105 (defun parse-eval-when-situations (situations)
1106 (when (or (not (listp situations))
1107 (set-difference situations
1108 '(:compile-toplevel
1109 compile
1110 :load-toplevel
1111 load
1112 :execute
1113 eval)))
1114 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
1115 (let ((deprecated-names (intersection situations '(compile load eval))))
1116 (when deprecated-names
1117 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
1118 deprecated-names)))
1119 (values (intersection '(:compile-toplevel compile)
1120 situations)
1121 (intersection '(:load-toplevel load) situations)
1122 (intersection '(:execute eval) situations)))
1125 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
1126 (defun functional-components (f)
1127 (declare (type functional f))
1128 (etypecase f
1129 (clambda (list (lambda-component f)))
1130 (optional-dispatch (let ((result nil))
1131 (flet ((maybe-frob (maybe-clambda)
1132 (when (and maybe-clambda
1133 (promise-ready-p maybe-clambda))
1134 (pushnew (lambda-component
1135 (force maybe-clambda))
1136 result))))
1137 (map nil #'maybe-frob (optional-dispatch-entry-points f))
1138 (maybe-frob (optional-dispatch-more-entry f))
1139 (maybe-frob (optional-dispatch-main-entry f)))
1140 result))))
1142 (defun make-functional-from-toplevel-lambda (lambda-expression
1143 &key
1144 name
1145 (path
1146 ;; I'd thought NIL should
1147 ;; work, but it doesn't.
1148 ;; -- WHN 2001-09-20
1149 (missing-arg)))
1150 (let* ((*current-path* path)
1151 (component (make-empty-component))
1152 (*current-component* component)
1153 (debug-name-tail (or name (name-lambdalike lambda-expression)))
1154 (source-name (or name '.anonymous.)))
1155 (setf (component-name component) (debug-name 'initial-component debug-name-tail)
1156 (component-kind component) :initial)
1157 (let* ((fun (let ((*allow-instrumenting* t))
1158 (funcall #'ir1-convert-lambdalike
1159 lambda-expression
1160 :source-name source-name)))
1161 ;; Convert the XEP using the policy of the real function. Otherwise
1162 ;; the wrong policy will be used for deciding whether to type-check
1163 ;; the parameters of the real function (via CONVERT-CALL /
1164 ;; PROPAGATE-TO-ARGS). -- JES, 2007-02-27
1165 (*lexenv* (make-lexenv :policy (lexenv-policy (functional-lexenv fun))))
1166 (xep (ir1-convert-lambda (make-xep-lambda-expression fun)
1167 :source-name source-name
1168 :debug-name (debug-name 'tl-xep debug-name-tail)
1169 :system-lambda t)))
1170 (when name
1171 (assert-global-function-definition-type name fun))
1172 (setf (functional-kind xep) :external
1173 (functional-entry-fun xep) fun
1174 (functional-entry-fun fun) xep
1175 (component-reanalyze component) t
1176 (functional-has-external-references-p xep) t)
1177 (reoptimize-component component :maybe)
1178 (locall-analyze-xep-entry-point fun)
1179 ;; Any leftover REFs to FUN outside local calls get replaced with the
1180 ;; XEP.
1181 (substitute-leaf-if (lambda (ref)
1182 (let* ((lvar (ref-lvar ref))
1183 (dest (when lvar (lvar-dest lvar)))
1184 (kind (when (basic-combination-p dest)
1185 (basic-combination-kind dest))))
1186 (neq :local kind)))
1188 fun)
1189 xep)))
1191 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
1192 ;;; description of the result.
1193 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
1194 ;;; into core and return the compiled FUNCTION value.
1195 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
1196 ;;; into the fasl file and return a dump handle.
1198 ;;; If NAME is provided, then we try to use it as the name of the
1199 ;;; function for debugging/diagnostic information.
1200 (defun %compile (lambda-expression
1201 *compile-object*
1202 &key
1203 name
1204 (path
1205 ;; This magical idiom seems to be the appropriate
1206 ;; path for compiling standalone LAMBDAs, judging
1207 ;; from the CMU CL code and experiment, so it's a
1208 ;; nice default for things where we don't have a
1209 ;; real source path (as in e.g. inside CL:COMPILE).
1210 '(original-source-start 0 0)))
1211 (when name
1212 (legal-fun-name-or-type-error name))
1213 (with-ir1-namespace
1214 (let* ((*lexenv* (make-lexenv
1215 :policy *policy*
1216 :handled-conditions *handled-conditions*
1217 :disabled-package-locks *disabled-package-locks*))
1218 (*compiler-sset-counter* 0)
1219 (fun (make-functional-from-toplevel-lambda lambda-expression
1220 :name name
1221 :path path)))
1223 ;; FIXME: The compile-it code from here on is sort of a
1224 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1225 ;; better to find a way to share the code there; or
1226 ;; alternatively, to use this code to replace the code there.
1227 ;; (The second alternative might be pretty easy if we used
1228 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1229 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1231 (locall-analyze-clambdas-until-done (list fun))
1233 (let ((components-from-dfo (find-initial-dfo (list fun))))
1234 (dolist (component-from-dfo components-from-dfo)
1235 (compile-component component-from-dfo)
1236 (replace-toplevel-xeps component-from-dfo))
1238 (let ((entry-table (etypecase *compile-object*
1239 (fasl-output (fasl-output-entry-table
1240 *compile-object*))
1241 (core-object (core-object-entry-table
1242 *compile-object*)))))
1243 (multiple-value-bind (result found-p)
1244 (gethash (leaf-info fun) entry-table)
1245 (aver found-p)
1246 (prog1
1247 result
1248 ;; KLUDGE: This code duplicates some other code in this
1249 ;; file. In the great reorganzation, the flow of program
1250 ;; logic changed from the original CMUCL model, and that
1251 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1252 ;; longer followed for CORE-OBJECTS, leading to BUG
1253 ;; 156. This place is transparently not the right one for
1254 ;; this code, but I don't have a clear enough overview of
1255 ;; the compiler to know how to rearrange it all so that
1256 ;; this operation fits in nicely, and it was blocking
1257 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1258 ;; ((..)) (DEFUN FOO ...))
1260 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1261 ;; ideal way, as (1) definitions typed in at the REPL
1262 ;; without an INLINE declaration will give a NULL
1263 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1264 ;; and (2) INLINE declarations will yield a
1265 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1266 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1267 ;; 2002-07-02
1269 ;; (2) is probably fairly easy to fix -- it is, after all,
1270 ;; a matter of list manipulation (or possibly of teaching
1271 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1272 ;; significantly harder, as the association between
1273 ;; function object and source is a tricky one.
1275 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1276 ;; non-NULL list) when the function in question has been
1277 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1278 ;; work when it has been compiled as part of the top-level
1279 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1280 ;; ...). -- CSR, 2002-11-02
1281 (when (core-object-p *compile-object*)
1282 (fix-core-source-info *source-info* *compile-object* result))
1284 (mapc #'clear-ir1-info components-from-dfo))))))))
1286 (defun note-top-level-form (form &optional finalp)
1287 (when *compile-print*
1288 (cond ((not *top-level-form-noted*)
1289 (let ((*print-length* 2)
1290 (*print-level* 2)
1291 (*print-pretty* nil))
1292 (with-compiler-io-syntax
1293 (compiler-mumble
1294 #-sb-xc-host "~&; ~:[compiling~;converting~] ~S"
1295 #+sb-xc-host "~&; ~:[x-compiling~;x-converting~] ~S"
1296 *block-compile* form)))
1297 form)
1298 ((and finalp
1299 (eq :top-level-forms *compile-print*)
1300 (neq form *top-level-form-noted*))
1301 (let ((*print-length* 1)
1302 (*print-level* 1)
1303 (*print-pretty* nil))
1304 (with-compiler-io-syntax
1305 (compiler-mumble "~&; ... top level ~S" form)))
1306 form)
1308 *top-level-form-noted*))))
1310 ;;; Handle the evaluation the a :COMPILE-TOPLEVEL body during
1311 ;;; compilation. Normally just evaluate in the appropriate
1312 ;;; environment, but also compile if outputting a CFASL.
1313 (defun eval-compile-toplevel (body path)
1314 (flet ((frob ()
1315 (eval-tlf `(progn ,@body) (source-path-tlf-number path) *lexenv*)
1316 (when *compile-toplevel-object*
1317 (let ((*compile-object* *compile-toplevel-object*))
1318 (convert-and-maybe-compile `(progn ,@body) path)))))
1319 (if (null *macro-policy*)
1320 (frob)
1321 (let* ((*lexenv*
1322 (make-lexenv
1323 :policy (process-optimize-decl
1324 `(optimize ,@(policy-to-decl-spec *macro-policy*))
1325 (lexenv-policy *lexenv*))
1326 :default *lexenv*))
1327 ;; In case a null lexenv is created, it needs to get the newly
1328 ;; effective global policy, not the policy currently in *POLICY*.
1329 (*policy* (lexenv-policy *lexenv*)))
1330 (frob)))))
1332 ;;; Process a top level FORM with the specified source PATH.
1333 ;;; * If this is a magic top level form, then do stuff.
1334 ;;; * If this is a macro, then expand it.
1335 ;;; * Otherwise, just compile it.
1337 ;;; COMPILE-TIME-TOO is as defined in ANSI
1338 ;;; "3.2.3.1 Processing of Top Level Forms".
1339 (defun process-toplevel-form (form path compile-time-too)
1340 (declare (list path))
1342 (catch 'process-toplevel-form-error-abort
1343 (let* ((path (or (get-source-path form) (cons form path)))
1344 (*current-path* path)
1345 (*compiler-error-bailout*
1346 (lambda (&optional condition)
1347 (convert-and-maybe-compile
1348 (make-compiler-error-form condition form)
1349 path)
1350 (throw 'process-toplevel-form-error-abort nil))))
1352 (flet ((default-processor (form)
1353 (let ((*top-level-form-noted* (note-top-level-form form)))
1354 ;; When we're cross-compiling, consider: what should we
1355 ;; do when we hit e.g.
1356 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1357 ;; (DEFUN FOO (X) (+ 7 X)))?
1358 ;; DEFUN has a macro definition in the cross-compiler,
1359 ;; and a different macro definition in the target
1360 ;; compiler. The only sensible thing is to use the
1361 ;; target compiler's macro definition, since the
1362 ;; cross-compiler's macro is in general into target
1363 ;; functions which can't meaningfully be executed at
1364 ;; cross-compilation time. So make sure we do the EVAL
1365 ;; here, before we macroexpand.
1367 ;; Then things get even dicier with something like
1368 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1369 ;; where we have to make sure that we don't uncross
1370 ;; the SB!XC: prefix before we do EVAL, because otherwise
1371 ;; we'd be trying to redefine the cross-compilation host's
1372 ;; constants.
1374 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1375 #+sb-xc-host
1376 (progn
1377 (when compile-time-too
1378 (eval form)) ; letting xc host EVAL do its own macroexpansion
1379 (let* (;; (We uncross the operator name because things
1380 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1381 ;; should be equivalent to their CL: counterparts
1382 ;; when being compiled as target code. We leave
1383 ;; the rest of the form uncrossed because macros
1384 ;; might yet expand into EVAL-WHEN stuff, and
1385 ;; things inside EVAL-WHEN can't be uncrossed
1386 ;; until after we've EVALed them in the
1387 ;; cross-compilation host.)
1388 (slightly-uncrossed (cons (uncross (first form))
1389 (rest form)))
1390 (expanded (preprocessor-macroexpand-1
1391 slightly-uncrossed)))
1392 (if (eq expanded slightly-uncrossed)
1393 ;; (Now that we're no longer processing toplevel
1394 ;; forms, and hence no longer need to worry about
1395 ;; EVAL-WHEN, we can uncross everything.)
1396 (convert-and-maybe-compile expanded path)
1397 ;; (We have to demote COMPILE-TIME-TOO to NIL
1398 ;; here, no matter what it was before, since
1399 ;; otherwise we'd tend to EVAL subforms more than
1400 ;; once, because of WHEN COMPILE-TIME-TOO form
1401 ;; above.)
1402 (process-toplevel-form expanded path nil))))
1403 ;; When we're not cross-compiling, we only need to
1404 ;; macroexpand once, so we can follow the 1-thru-6
1405 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1406 ;; Top Level Forms".
1407 #-sb-xc-host
1408 (let ((expanded (preprocessor-macroexpand-1 form)))
1409 (cond ((eq expanded form)
1410 (when compile-time-too
1411 (eval-compile-toplevel (list form) path))
1412 (convert-and-maybe-compile form path nil))
1414 (process-toplevel-form expanded
1415 path
1416 compile-time-too)))))))
1417 (if (atom form)
1418 #+sb-xc-host
1419 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1420 ;; (1) SBCL gets smart enough to handle global
1421 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1422 ;; implementors start using symbol macros in a way which
1423 ;; interacts with SB-XC/CL distinction.)
1424 (convert-and-maybe-compile form path)
1425 #-sb-xc-host
1426 (default-processor form)
1427 (flet ((need-at-least-one-arg (form)
1428 (unless (cdr form)
1429 (compiler-error "~S form is too short: ~S"
1430 (car form)
1431 form))))
1432 (case (car form)
1433 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1434 (need-at-least-one-arg form)
1435 (destructuring-bind (special-operator magic &rest body) form
1436 (ecase special-operator
1437 ((eval-when)
1438 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1439 ;; "3.2.3.1 Processing of Top Level Forms".
1440 (multiple-value-bind (ct lt e)
1441 (parse-eval-when-situations magic)
1442 (let ((new-compile-time-too (or ct
1443 (and compile-time-too
1444 e))))
1445 (cond (lt (process-toplevel-progn
1446 body path new-compile-time-too))
1447 (new-compile-time-too
1448 (eval-compile-toplevel body path))))))
1449 ((macrolet)
1450 (funcall-in-macrolet-lexenv
1451 magic
1452 (lambda (&key funs prepend)
1453 (declare (ignore funs))
1454 (aver (null prepend))
1455 (process-toplevel-locally body
1456 path
1457 compile-time-too))
1458 :compile))
1459 ((symbol-macrolet)
1460 (funcall-in-symbol-macrolet-lexenv
1461 magic
1462 (lambda (&key vars prepend)
1463 (aver (null prepend))
1464 (process-toplevel-locally body
1465 path
1466 compile-time-too
1467 :vars vars))
1468 :compile)))))
1469 ((locally)
1470 (process-toplevel-locally (rest form) path compile-time-too))
1471 ((progn)
1472 (process-toplevel-progn (rest form) path compile-time-too))
1473 (t (default-processor form))))))))
1475 (values))
1477 ;;;; load time value support
1478 ;;;;
1479 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1481 ;;; Return T if we are currently producing a fasl file and hence
1482 ;;; constants need to be dumped carefully.
1483 (defun producing-fasl-file ()
1484 (fasl-output-p *compile-object*))
1486 ;;; Compile FORM and arrange for it to be called at load-time. Return
1487 ;;; the dumper handle and our best guess at the type of the object.
1488 ;;; It would be nice if L-T-V forms were generally eligible
1489 ;;; for fopcompilation, as it could eliminate special cases below.
1490 (defun compile-load-time-value (form)
1491 (let ((ctype
1492 (cond
1493 ;; Ideally any ltv would test FOPCOMPILABLE-P on its form,
1494 ;; but be that as it may, this case is picked off because of
1495 ;; its importance during cross-compilation to ensure that
1496 ;; compiled lambdas don't cause a chicken-and-egg problem.
1497 ((typep form '(cons (eql find-package) (cons string null)))
1498 (specifier-type 'package))
1499 #+sb-xc-host
1500 ((typep form '(cons (eql find-classoid-cell)
1501 (cons (cons (eql quote)))))
1502 (aver (eq (getf (cddr form) :create) t))
1503 (specifier-type 'sb!kernel::classoid-cell))
1504 ;; Special case for the cross-compiler, necessary for at least
1505 ;; SETUP-PRINTER-STATE, but also anything that would be dumped
1506 ;; using FOP-KNOWN-FUN in the target compiler, to avoid going
1507 ;; through an fdefn.
1508 ;; I'm pretty sure that as of change 00298ec6, it works to
1509 ;; compile #'F before the defun would have been seen by Genesis.
1510 #+sb-xc-host
1511 ((typep form '(cons (eql function) (cons symbol null)))
1512 (specifier-type 'function)))))
1513 (when ctype
1514 (fopcompile form nil t)
1515 (return-from compile-load-time-value
1516 (values (sb!fasl::dump-pop *compile-object*) ctype))))
1517 (let ((lambda (compile-load-time-stuff form t)))
1518 (values
1519 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1520 (let ((type (leaf-type lambda)))
1521 (if (fun-type-p type)
1522 (single-value-type (fun-type-returns type))
1523 *wild-type*)))))
1525 ;;; Compile the FORMS and arrange for them to be called (for effect,
1526 ;;; not value) at load time.
1527 (defun compile-make-load-form-init-forms (forms)
1528 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1529 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1531 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1532 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1533 (defun compile-load-time-stuff (form for-value)
1534 (with-ir1-namespace
1535 (let* ((*lexenv* (make-null-lexenv))
1536 (lambda (ir1-toplevel form *current-path* for-value nil)))
1537 (compile-toplevel (list lambda) t)
1538 lambda)))
1540 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1541 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1542 ;;; don't try to combine this component with anything else and frob
1543 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1544 ;;; compiling, because it was merged with a run-time component.
1545 (defun compile-load-time-value-lambda (lambdas)
1546 (aver (null (cdr lambdas)))
1547 (let* ((lambda (car lambdas))
1548 (component (lambda-component lambda)))
1549 (when (eql (component-kind component) :toplevel)
1550 (setf (component-name component) (leaf-debug-name lambda))
1551 (compile-component component)
1552 (clear-ir1-info component))))
1554 ;;;; COMPILE-FILE
1556 (defun object-call-toplevel-lambda (tll)
1557 (declare (type functional tll))
1558 (let ((object *compile-object*))
1559 (etypecase object
1560 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1561 (core-object (core-call-toplevel-lambda tll object))
1562 (null))))
1564 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1565 ;;; the resulting function to be called.
1566 (defun sub-compile-toplevel-lambdas (lambdas)
1567 (declare (list lambdas))
1568 (when lambdas
1569 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1570 (compile-component component)
1571 (clear-ir1-info component)
1572 (object-call-toplevel-lambda tll)))
1573 (values))
1575 ;;; Compile top level code and call the top level lambdas. We pick off
1576 ;;; top level lambdas in non-top-level components here, calling
1577 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1578 (defun compile-toplevel-lambdas (lambdas)
1579 (declare (list lambdas))
1580 (let ((len (length lambdas)))
1581 (flet ((loser (start)
1582 (or (position-if (lambda (x)
1583 (not (eq (component-kind
1584 (node-component (lambda-bind x)))
1585 :toplevel)))
1586 lambdas
1587 ;; this used to read ":start start", but
1588 ;; start can be greater than len, which
1589 ;; is an error according to ANSI - CSR,
1590 ;; 2002-04-25
1591 :start (min start len))
1592 len)))
1593 (do* ((start 0 (1+ loser))
1594 (loser (loser start) (loser start)))
1595 ((>= start len))
1596 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1597 (unless (= loser len)
1598 (object-call-toplevel-lambda (elt lambdas loser))))))
1599 (values))
1601 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1602 ;;; object file.
1604 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1605 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1606 (defun compile-toplevel (lambdas load-time-value-p)
1607 (declare (list lambdas))
1609 (maybe-mumble "locall ")
1610 (locall-analyze-clambdas-until-done lambdas)
1612 (maybe-mumble "IDFO ")
1613 (multiple-value-bind (components top-components hairy-top)
1614 (find-initial-dfo lambdas)
1615 (let ((all-components (append components top-components)))
1616 (when *check-consistency*
1617 (maybe-mumble "[check]~%")
1618 (check-ir1-consistency all-components))
1620 (dolist (component (append hairy-top top-components))
1621 (pre-physenv-analyze-toplevel component))
1623 (dolist (component components)
1624 (compile-component component)
1625 (replace-toplevel-xeps component))
1627 (when *check-consistency*
1628 (maybe-mumble "[check]~%")
1629 (check-ir1-consistency all-components))
1631 (if load-time-value-p
1632 (compile-load-time-value-lambda lambdas)
1633 (compile-toplevel-lambdas lambdas))
1635 (mapc #'clear-ir1-info components)))
1636 (values))
1638 ;;; Actually compile any stuff that has been queued up for block
1639 ;;; compilation.
1640 (defun finish-block-compilation ()
1641 (when *block-compile*
1642 (when *compile-print*
1643 (compiler-mumble "~&; block compiling converted top level forms..."))
1644 (when *toplevel-lambdas*
1645 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1646 (setq *toplevel-lambdas* ()))
1647 (setq *block-compile* nil)
1648 (setq *entry-points* nil)))
1650 (flet ((get-handled-conditions ()
1651 (let ((ctxt *compiler-error-context*))
1652 (lexenv-handled-conditions
1653 (etypecase ctxt
1654 (node (node-lexenv ctxt))
1655 (compiler-error-context
1656 (let ((lexenv (compiler-error-context-lexenv ctxt)))
1657 (aver lexenv)
1658 lexenv))
1659 ;; Is this right? I would think that if lexenv is null
1660 ;; we should look at *HANDLED-CONDITIONS*.
1661 (null *lexenv*)))))
1662 (handle-p (condition ctype)
1663 #+sb-xc-host (typep condition (type-specifier ctype))
1664 #-sb-xc-host (%%typep condition ctype)))
1665 (declare (inline handle-p))
1667 (defun handle-condition-p (condition)
1668 (dolist (muffle (get-handled-conditions) nil)
1669 (destructuring-bind (ctype . restart-name) muffle
1670 (when (and (handle-p condition ctype)
1671 (find-restart restart-name condition))
1672 (return t)))))
1674 (defun handle-condition-handler (condition)
1675 (let ((muffles (get-handled-conditions)))
1676 (aver muffles) ; FIXME: looks redundant with "fell through"
1677 (dolist (muffle muffles (bug "fell through"))
1678 (destructuring-bind (ctype . restart-name) muffle
1679 (when (handle-p condition ctype)
1680 (awhen (find-restart restart-name condition)
1681 (invoke-restart it)))))))
1683 ;; WOULD-MUFFLE-P is called (incorrectly) only by NOTE-UNDEFINED-REFERENCE.
1684 ;; It is not wrong per se, but as used, it is wrong, making it nearly
1685 ;; impossible to muffle a subset of undefind warnings whose NAME and KIND
1686 ;; slots match specific things tested by a user-defined predicate.
1687 ;; Attempting to do that might muffle everything, depending on how your
1688 ;; predicate responds to a vanilla WARNING. Consider e.g.
1689 ;; (AND WARNING (NOT (SATISFIES HAIRYFN)))
1690 ;; where HAIRYFN depends on the :FORMAT-CONTROL and :FORMAT-ARGUMENTS.
1691 (defun would-muffle-p (condition)
1692 (let ((ctype (rassoc 'muffle-warning
1693 (lexenv-handled-conditions *lexenv*))))
1694 (and ctype (handle-p condition (car ctype))))))
1696 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1697 ;;; Return (VALUES ABORT-P WARNINGS-P FAILURE-P).
1698 (defun sub-compile-file (info)
1699 (declare (type source-info info))
1700 (let ((*package* (sane-package))
1701 (*readtable* *readtable*)
1702 (sb!xc:*compile-file-pathname* nil) ; really bound in
1703 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1704 (*policy* *policy*)
1705 (*macro-policy* *macro-policy*)
1706 (*code-coverage-records* (make-hash-table :test 'equal))
1707 (*code-coverage-blocks* (make-hash-table :test 'equal))
1708 (*handled-conditions* *handled-conditions*)
1709 (*disabled-package-locks* *disabled-package-locks*)
1710 (*lexenv* (make-null-lexenv))
1711 (*block-compile* *block-compile-arg*)
1712 (*toplevel-lambdas* ())
1713 (*fun-names-in-this-file* ())
1714 (*allow-instrumenting* nil)
1715 (*compiler-error-bailout*
1716 (lambda (&optional error)
1717 (declare (ignore error))
1718 (return-from sub-compile-file (values t t t))))
1719 (*current-path* nil)
1720 (*last-source-context* nil)
1721 (*last-original-source* nil)
1722 (*last-source-form* nil)
1723 (*last-format-string* nil)
1724 (*last-format-args* nil)
1725 (*last-message-count* 0)
1726 (*compiler-sset-counter* 0)
1727 (sb!xc:*gensym-counter* 0))
1728 (handler-case
1729 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1730 (with-compilation-values
1731 (sb!xc:with-compilation-unit ()
1732 (with-world-lock ()
1733 (sub-sub-compile-file info)
1734 (unless (zerop (hash-table-count *code-coverage-records*))
1735 ;; Dump the code coverage records into the fasl.
1736 (with-source-paths
1737 (fopcompile `(record-code-coverage
1738 ',(namestring *compile-file-pathname*)
1739 ',(let (list)
1740 (maphash (lambda (k v)
1741 (declare (ignore k))
1742 (push v list))
1743 *code-coverage-records*)
1744 list))
1746 nil)))
1747 (finish-block-compilation)
1748 (let ((object *compile-object*))
1749 (etypecase object
1750 (fasl-output (fasl-dump-source-info info object))
1751 (core-object (fix-core-source-info info object))
1752 (null)))
1753 nil))))
1754 ;; Some errors are sufficiently bewildering that we just fail
1755 ;; immediately, without trying to recover and compile more of
1756 ;; the input file.
1757 (fatal-compiler-error (condition)
1758 (signal condition)
1759 (fresh-line *error-output*)
1760 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
1761 (format *error-output*
1762 "~@<~@:_compilation aborted because of fatal error: ~2I~_~A~@:_~:>"
1763 (encapsulated-condition condition)))
1764 (finish-output *error-output*)
1765 (values t t t)))))
1767 ;;; Return a pathname for the named file. The file must exist.
1768 (defun verify-source-file (pathname-designator)
1769 (let* ((pathname (pathname pathname-designator))
1770 (default-host (make-pathname :host (pathname-host pathname))))
1771 (flet ((try-with-type (path type error-p)
1772 (let ((new (merge-pathnames
1773 path (make-pathname :type type
1774 :defaults default-host))))
1775 (if (probe-file new)
1777 (and error-p (truename new))))))
1778 (cond ((typep pathname 'logical-pathname)
1779 (try-with-type pathname "LISP" t))
1780 ((probe-file pathname) pathname)
1781 ((try-with-type pathname "lisp" nil))
1782 ((try-with-type pathname "lisp" t))))))
1784 (defun elapsed-time-to-string (internal-time-delta)
1785 (multiple-value-bind (tsec remainder)
1786 (truncate internal-time-delta internal-time-units-per-second)
1787 (let ((ms (truncate remainder (/ internal-time-units-per-second 1000))))
1788 (multiple-value-bind (tmin sec) (truncate tsec 60)
1789 (multiple-value-bind (thr min) (truncate tmin 60)
1790 (format nil "~D:~2,'0D:~2,'0D.~3,'0D" thr min sec ms))))))
1792 ;;; Print some junk at the beginning and end of compilation.
1793 (defun print-compile-start-note (source-info)
1794 (declare (type source-info source-info))
1795 (let ((file-info (source-info-file-info source-info)))
1796 (compiler-mumble #+sb-xc-host "~&; ~A file ~S (written ~A):~%"
1797 #+sb-xc-host (if sb-cold::*compile-for-effect-only*
1798 "preloading"
1799 "cross-compiling")
1800 #-sb-xc-host "~&; compiling file ~S (written ~A):~%"
1801 (namestring (file-info-name file-info))
1802 (sb!int:format-universal-time nil
1803 (file-info-write-date
1804 file-info)
1805 :style :government
1806 :print-weekday nil
1807 :print-timezone nil)))
1808 (values))
1810 (defun print-compile-end-note (source-info won)
1811 (declare (type source-info source-info))
1812 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1814 (elapsed-time-to-string
1815 (- (get-internal-real-time)
1816 (source-info-start-real-time source-info))))
1817 (values))
1819 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1820 ;;; out of the compile, then abort the writing of the output file, so
1821 ;;; that we don't overwrite it with known garbage.
1822 (defun sb!xc:compile-file
1823 (input-file
1824 &key
1826 ;; ANSI options
1827 (output-file (cfp-output-file-default input-file))
1828 ;; FIXME: ANSI doesn't seem to say anything about
1829 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1830 ;; function..
1831 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1832 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1833 (external-format :default)
1835 ;; extensions
1836 (trace-file nil)
1837 ((:block-compile *block-compile-arg*) nil)
1838 (emit-cfasl *emit-cfasl*))
1839 #!+sb-doc
1840 "Compile INPUT-FILE, producing a corresponding fasl file and
1841 returning its filename.
1843 :PRINT
1844 If true, a message per non-macroexpanded top level form is printed
1845 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1846 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1847 no such message, but their subforms do.
1849 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1850 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1851 For example, compiling an IN-PACKAGE form will result in a message about
1852 a top level SETQ in addition to the message about the IN-PACKAGE form'
1853 itself.
1855 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1857 :BLOCK-COMPILE
1858 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1859 argument, it is not currently supported. (non-standard)
1861 :TRACE-FILE
1862 If given, internal data structures are dumped to the specified
1863 file, or if a value of T is given, to a file of *.trace type
1864 derived from the input file name. (non-standard)
1866 :EMIT-CFASL
1867 (Experimental). If true, outputs the toplevel compile-time effects
1868 of this file into a separate .cfasl file."
1869 ;;; Block compilation is currently broken.
1871 "Also, as a workaround for vaguely-non-ANSI behavior, the
1872 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1873 multiple functions are compiled together as a unit, resolving function
1874 references at compile time. NIL means that global function names are
1875 never resolved at compilation time. Currently NIL is the default
1876 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1877 of the ANSI spec allows this behavior under all circumstances, the
1878 compiler's runtime scales badly when it tries to do this for large
1879 files. If/when this performance problem is fixed, the block
1880 compilation default behavior will probably be made dependent on the
1881 SPEED and COMPILATION-SPEED optimization values, and the
1882 :BLOCK-COMPILE argument will probably become deprecated."
1884 (let* ((fasl-output nil)
1885 (cfasl-output nil)
1886 (output-file-name nil)
1887 (coutput-file-name nil)
1888 (abort-p t)
1889 (warnings-p nil)
1890 (failure-p t) ; T in case error keeps this from being set later
1891 (input-pathname (verify-source-file input-file))
1892 (source-info
1893 (make-file-source-info input-pathname external-format
1894 #-sb-xc-host t)) ; can't track, no SBCL streams
1895 (*compiler-trace-output* nil)) ; might be modified below
1897 (unwind-protect
1898 (progn
1899 (when output-file
1900 (setq output-file-name
1901 (sb!xc:compile-file-pathname input-file
1902 :output-file output-file))
1903 (setq fasl-output
1904 (open-fasl-output output-file-name
1905 (namestring input-pathname))))
1906 (when emit-cfasl
1907 (setq coutput-file-name
1908 (make-pathname :type "cfasl"
1909 :defaults output-file-name))
1910 (setq cfasl-output
1911 (open-fasl-output coutput-file-name
1912 (namestring input-pathname))))
1913 (when trace-file
1914 (let* ((default-trace-file-pathname
1915 (make-pathname :type "trace" :defaults input-pathname))
1916 (trace-file-pathname
1917 (if (eql trace-file t)
1918 default-trace-file-pathname
1919 (merge-pathnames trace-file
1920 default-trace-file-pathname))))
1921 (setf *compiler-trace-output*
1922 (open trace-file-pathname
1923 :if-exists :supersede
1924 :direction :output))))
1926 (when sb!xc:*compile-verbose*
1927 (print-compile-start-note source-info))
1929 (let ((*compile-object* fasl-output)
1930 (*compile-toplevel-object* cfasl-output))
1931 (setf (values abort-p warnings-p failure-p)
1932 (sub-compile-file source-info))))
1934 (close-source-info source-info)
1936 (when fasl-output
1937 (close-fasl-output fasl-output abort-p)
1938 (setq output-file-name
1939 (pathname (fasl-output-stream fasl-output)))
1940 (when (and (not abort-p) sb!xc:*compile-verbose*)
1941 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1943 (when cfasl-output
1944 (close-fasl-output cfasl-output abort-p)
1945 (when (and (not abort-p) sb!xc:*compile-verbose*)
1946 (compiler-mumble "; ~A written~%" (namestring coutput-file-name))))
1948 (when sb!xc:*compile-verbose*
1949 (print-compile-end-note source-info (not abort-p)))
1951 (when *compiler-trace-output*
1952 (close *compiler-trace-output*)))
1954 ;; CLHS says that the first value is NIL if the "file could not
1955 ;; be created". We interpret this to mean "a valid fasl could not
1956 ;; be created" -- which can happen if the compilation is aborted
1957 ;; before the whole file has been processed, due to eg. a reader
1958 ;; error.
1959 (values (when (and (not abort-p) output-file)
1960 ;; Hack around filesystem race condition...
1961 (or (probe-file output-file-name) output-file-name))
1962 warnings-p
1963 failure-p)))
1965 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1966 ;;; the OUTPUT-FILE argument
1968 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1969 ;;; that results from merging the INPUT-FILE with the value of
1970 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1971 ;;; default to the appropriate implementation-defined default type for
1972 ;;; compiled files.
1973 (defun cfp-output-file-default (input-file)
1974 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1975 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1976 retyped))
1978 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1979 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1980 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1981 ;;; pathname, it is translated into a physical pathname as if by
1982 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1983 ;;; So I haven't really tried to make this precisely ANSI-compatible
1984 ;;; at the level of e.g. whether it returns logical pathname or a
1985 ;;; physical pathname. Patches to make it more correct are welcome.
1986 ;;; -- WHN 2000-12-09
1987 (defun sb!xc:compile-file-pathname (input-file
1988 &key
1989 (output-file nil output-file-p)
1990 &allow-other-keys)
1991 #!+sb-doc
1992 "Return a pathname describing what file COMPILE-FILE would write to given
1993 these arguments."
1994 (if output-file-p
1995 (merge-pathnames output-file (cfp-output-file-default input-file))
1996 (cfp-output-file-default input-file)))
1998 ;;;; MAKE-LOAD-FORM stuff
2000 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
2001 ;;; finds a constant structure, it invokes this to arrange for proper
2002 ;;; dumping. If it turns out that the constant has already been
2003 ;;; dumped, then we don't need to do anything.
2005 ;;; If the constant hasn't been dumped, then we check to see whether
2006 ;;; we are in the process of creating it. We detect this by
2007 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
2008 ;;; the constants we are in the process of creating. Actually, each
2009 ;;; entry is a list of the constant and any init forms that need to be
2010 ;;; processed on behalf of that constant.
2012 ;;; It's not necessarily an error for this to happen. If we are
2013 ;;; processing the init form for some object that showed up *after*
2014 ;;; the original reference to this constant, then we just need to
2015 ;;; defer the processing of that init form. To detect this, we
2016 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
2017 ;;; constants created since the last time we started processing an
2018 ;;; init form. If the constant passed to emit-make-load-form shows up
2019 ;;; in this list, then there is a circular chain through creation
2020 ;;; forms, which is an error.
2022 ;;; If there is some intervening init form, then we blow out of
2023 ;;; processing it by throwing to the tag PENDING-INIT. The value we
2024 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
2025 ;;; offending init form can be tacked onto the init forms for the
2026 ;;; circular object.
2028 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
2029 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
2030 ;;; whether the creation form is the magic value
2031 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
2032 ;;; dumper will eventually get its hands on the object and use the
2033 ;;; normal structure dumping noise on it.
2035 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
2036 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
2037 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
2038 ;;; dumper to use that result instead whenever it sees this constant.
2040 ;;; Now we try to compile the init form. We bind
2041 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
2042 ;;; form (and any init forms that were added because of circularity
2043 ;;; detection). If this works, great. If not, we add the init forms to
2044 ;;; the init forms for the object that caused the problems and let it
2045 ;;; deal with it.
2046 (defvar *constants-being-created* nil)
2047 (defvar *constants-created-since-last-init* nil)
2048 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
2049 (defun emit-make-load-form (constant &optional (name nil namep)
2050 &aux (fasl *compile-object*))
2051 (aver (fasl-output-p fasl))
2052 (unless (or (fasl-constant-already-dumped-p constant fasl)
2053 ;; KLUDGE: This special hack is because I was too lazy
2054 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
2055 ;; function of LAYOUT returns nontrivial forms when
2056 ;; building the cross-compiler but :IGNORE-IT when
2057 ;; cross-compiling or running under the target Lisp. --
2058 ;; WHN 19990914
2059 #+sb-xc-host (typep constant 'layout))
2060 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
2061 (when circular-ref
2062 (when (find constant *constants-created-since-last-init* :test #'eq)
2063 (throw constant t))
2064 (throw 'pending-init circular-ref)))
2065 ;; If this is a global constant reference, we can call SYMBOL-GLOBAL-VALUE
2066 ;; during LOAD as a fasl op, and not compile a lambda.
2067 (when namep
2068 (fopcompile `(symbol-global-value ',name) nil t nil)
2069 (fasl-note-handle-for-constant constant (sb!fasl::dump-pop fasl) fasl)
2070 (return-from emit-make-load-form nil))
2071 (multiple-value-bind (creation-form init-form)
2072 (handler-case (sb!xc:make-load-form constant (make-null-lexenv))
2073 (error (condition) (compiler-error condition)))
2074 #-sb-xc-host
2075 (when (and (not namep)
2076 (listp creation-form) ; skip if already a magic keyword
2077 (typep constant 'structure-object)
2078 (sb!kernel::canonical-slot-saving-forms-p
2079 constant creation-form init-form))
2080 (setq creation-form :sb-just-dump-it-normally))
2081 (case creation-form
2082 (:sb-just-dump-it-normally
2083 (fasl-validate-structure constant fasl)
2085 (:ignore-it
2086 nil)
2088 (let* ((name (write-to-string constant :level 1 :length 2))
2089 (info (if init-form
2090 (list constant name init-form)
2091 (list constant))))
2092 (let ((*constants-being-created*
2093 (cons info *constants-being-created*))
2094 (*constants-created-since-last-init*
2095 (cons constant *constants-created-since-last-init*)))
2096 (when
2097 (catch constant
2098 (fasl-note-handle-for-constant
2099 constant
2100 (or (fopcompile-allocate-instance fasl creation-form)
2101 (compile-load-time-value creation-form))
2102 fasl)
2103 nil)
2104 (compiler-error "circular references in creation form for ~S"
2105 constant)))
2106 (when (cdr info)
2107 (let* ((*constants-created-since-last-init* nil)
2108 (circular-ref
2109 (catch 'pending-init
2110 (loop for (nil form) on (cdr info) by #'cddr
2111 collect form into forms
2112 finally (or (fopcompile-constant-init-forms fasl forms)
2113 (compile-make-load-form-init-forms forms)))
2114 nil)))
2115 (when circular-ref
2116 (setf (cdr circular-ref)
2117 (append (cdr circular-ref) (cdr info)))))))
2118 nil)))))
2121 ;;;; Host compile time definitions
2122 #+sb-xc-host
2123 (defun compile-in-lexenv (name lambda lexenv)
2124 (declare (ignore lexenv))
2125 (compile name lambda))
2127 #+sb-xc-host
2128 (defun eval-tlf (form index &optional lexenv)
2129 (declare (ignore index lexenv))
2130 (eval form))