1 ;;;; This file contains miscellaneous utilities used for manipulating
2 ;;;; the IR1 representation.
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
17 ;;; Return the innermost cleanup enclosing NODE, or NIL if there is
18 ;;; none in its function. If NODE has no cleanup, but is in a LET,
19 ;;; then we must still check the environment that the call is in.
20 (defun node-enclosing-cleanup (node)
21 (declare (type node node))
22 (do ((lexenv (node-lexenv node)
23 (lambda-call-lexenv (lexenv-lambda lexenv))))
25 (let ((cup (lexenv-cleanup lexenv)))
26 (when cup (return cup)))))
28 ;;; Convert the FORM in a block inserted between BLOCK1 and BLOCK2 as
29 ;;; an implicit MV-PROG1. The inserted block is returned. NODE is used
30 ;;; for IR1 context when converting the form. Note that the block is
31 ;;; not assigned a number, and is linked into the DFO at the
32 ;;; beginning. We indicate that we have trashed the DFO by setting
33 ;;; COMPONENT-REANALYZE. If CLEANUP is supplied, then convert with
35 (defun insert-cleanup-code (block1 block2 node form &optional cleanup)
36 (declare (type cblock block1 block2) (type node node)
37 (type (or cleanup null) cleanup))
38 (setf (component-reanalyze (block-component block1)) t)
39 (with-ir1-environment node
40 (let* ((start (make-continuation))
41 (block (continuation-starts-block start))
42 (cont (make-continuation))
44 (make-lexenv :cleanup cleanup)
46 (change-block-successor block1 block2 block)
47 (link-blocks block block2)
48 (ir1-convert start cont form)
49 (setf (block-last block) (continuation-use cont))
52 ;;;; continuation use hacking
54 ;;; Return a list of all the nodes which use Cont.
55 (declaim (ftype (function (continuation) list) find-uses))
56 (defun find-uses (cont)
57 (ecase (continuation-kind cont)
58 ((:block-start :deleted-block-start)
59 (block-start-uses (continuation-block cont)))
60 (:inside-block (list (continuation-use cont)))
64 ;;; Update continuation use information so that NODE is no longer a
65 ;;; use of its CONT. If the old continuation doesn't start its block,
66 ;;; then we don't update the BLOCK-START-USES, since it will be
67 ;;; deleted when we are done.
69 ;;; Note: if you call this function, you may have to do a
70 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
72 (declaim (ftype (function (node) (values)) delete-continuation-use))
73 (defun delete-continuation-use (node)
74 (let* ((cont (node-cont node))
75 (block (continuation-block cont)))
76 (ecase (continuation-kind cont)
78 ((:block-start :deleted-block-start)
79 (let ((uses (delete node (block-start-uses block))))
80 (setf (block-start-uses block) uses)
81 (setf (continuation-use cont)
82 (if (cdr uses) nil (car uses)))))
84 (setf (continuation-kind cont) :unused)
85 (setf (continuation-block cont) nil)
86 (setf (continuation-use cont) nil)
87 (setf (continuation-next cont) nil)))
88 (setf (node-cont node) nil))
91 ;;; Update continuation use information so that NODE uses CONT. If
92 ;;; CONT is :UNUSED, then we set its block to NODE's NODE-BLOCK (which
95 ;;; Note: if you call this function, you may have to do a
96 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
98 (declaim (ftype (function (node continuation) (values)) add-continuation-use))
99 (defun add-continuation-use (node cont)
100 (aver (not (node-cont node)))
101 (let ((block (continuation-block cont)))
102 (ecase (continuation-kind cont)
106 (let ((block (node-block node)))
108 (setf (continuation-block cont) block))
109 (setf (continuation-kind cont) :inside-block)
110 (setf (continuation-use cont) node))
111 ((:block-start :deleted-block-start)
112 (let ((uses (cons node (block-start-uses block))))
113 (setf (block-start-uses block) uses)
114 (setf (continuation-use cont)
115 (if (cdr uses) nil (car uses)))))))
116 (setf (node-cont node) cont)
119 ;;; Return true if CONT is the NODE-CONT for NODE and CONT is
120 ;;; transferred to immediately after the evaluation of NODE.
121 (defun immediately-used-p (cont node)
122 (declare (type continuation cont) (type node node))
123 (and (eq (node-cont node) cont)
124 (not (eq (continuation-kind cont) :deleted))
125 (let ((cblock (continuation-block cont))
126 (nblock (node-block node)))
127 (or (eq cblock nblock)
128 (let ((succ (block-succ nblock)))
129 (and (= (length succ) 1)
130 (eq (first succ) cblock)))))))
132 ;;;; continuation substitution
134 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
135 ;;; NIL. When we are done, we call FLUSH-DEST on OLD to clear its DEST
136 ;;; and to note potential optimization opportunities.
137 (defun substitute-continuation (new old)
138 (declare (type continuation old new))
139 (aver (not (continuation-dest new)))
140 (let ((dest (continuation-dest old)))
143 (cif (setf (if-test dest) new))
144 (cset (setf (set-value dest) new))
145 (creturn (setf (return-result dest) new))
146 (exit (setf (exit-value dest) new))
148 (if (eq old (basic-combination-fun dest))
149 (setf (basic-combination-fun dest) new)
150 (setf (basic-combination-args dest)
151 (nsubst new old (basic-combination-args dest))))))
154 (setf (continuation-dest new) dest))
157 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
158 ;;; arbitary number of uses. If NEW will end up with more than one
159 ;;; use, then we must arrange for it to start a block if it doesn't
161 (defun substitute-continuation-uses (new old)
162 (declare (type continuation old new))
163 (unless (and (eq (continuation-kind new) :unused)
164 (eq (continuation-kind old) :inside-block))
165 (ensure-block-start new))
168 (delete-continuation-use node)
169 (add-continuation-use node new))
170 (dolist (lexenv-use (continuation-lexenv-uses old))
171 (setf (cadr lexenv-use) new))
173 (reoptimize-continuation new)
176 ;;;; block starting/creation
178 ;;; Return the block that CONT is the start of, making a block if
179 ;;; necessary. This function is called by IR1 translators which may
180 ;;; cause a continuation to be used more than once. Every continuation
181 ;;; which may be used more than once must start a block by the time
182 ;;; that anyone does a USE-CONTINUATION on it.
184 ;;; We also throw the block into the next/prev list for the
185 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
187 (defun continuation-starts-block (cont)
188 (declare (type continuation cont))
189 (ecase (continuation-kind cont)
191 (aver (not (continuation-block cont)))
192 (let* ((head (component-head *current-component*))
193 (next (block-next head))
194 (new-block (make-block cont)))
195 (setf (block-next new-block) next)
196 (setf (block-prev new-block) head)
197 (setf (block-prev next) new-block)
198 (setf (block-next head) new-block)
199 (setf (continuation-block cont) new-block)
200 (setf (continuation-use cont) nil)
201 (setf (continuation-kind cont) :block-start)
204 (continuation-block cont))))
206 ;;; Ensure that Cont is the start of a block (or deleted) so that the use
207 ;;; set can be freely manipulated.
208 ;;; -- If the continuation is :Unused or is :Inside-Block and the Cont of Last
209 ;;; in its block, then we make it the start of a new deleted block.
210 ;;; -- If the continuation is :Inside-Block inside a block, then we split the
211 ;;; block using Node-Ends-Block, which makes the continuation be a
213 (defun ensure-block-start (cont)
214 (declare (type continuation cont))
215 (let ((kind (continuation-kind cont)))
217 ((:deleted :block-start :deleted-block-start))
218 ((:unused :inside-block)
219 (let ((block (continuation-block cont)))
220 (cond ((or (eq kind :unused)
221 (eq (node-cont (block-last block)) cont))
222 (setf (continuation-block cont)
223 (make-block-key :start cont
225 :start-uses (find-uses cont)))
226 (setf (continuation-kind cont) :deleted-block-start))
228 (node-ends-block (continuation-use cont))))))))
231 ;;;; miscellaneous shorthand functions
233 ;;; Return the home (i.e. enclosing non-let) lambda for Node. Since the
234 ;;; LEXENV-LAMBDA may be deleted, we must chain up the LAMBDA-CALL-LEXENV
235 ;;; thread until we find a lambda that isn't deleted, and then return its home.
236 (declaim (maybe-inline node-home-lambda))
237 (defun node-home-lambda (node)
238 (declare (type node node))
239 (do ((fun (lexenv-lambda (node-lexenv node))
240 (lexenv-lambda (lambda-call-lexenv fun))))
241 ((not (eq (functional-kind fun) :deleted))
243 (when (eq (lambda-home fun) fun)
246 #!-sb-fluid (declaim (inline node-block node-tlf-number))
247 (declaim (maybe-inline node-environment))
248 (defun node-block (node)
249 (declare (type node node))
250 (the cblock (continuation-block (node-prev node))))
251 (defun node-environment (node)
252 (declare (type node node))
253 #!-sb-fluid (declare (inline node-home-lambda))
254 (the environment (lambda-environment (node-home-lambda node))))
256 ;;; Return the enclosing cleanup for environment of the first or last node
258 (defun block-start-cleanup (block)
259 (declare (type cblock block))
260 (node-enclosing-cleanup (continuation-next (block-start block))))
261 (defun block-end-cleanup (block)
262 (declare (type cblock block))
263 (node-enclosing-cleanup (block-last block)))
265 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
266 (defun block-home-lambda (block)
267 (declare (type cblock block))
268 #!-sb-fluid (declare (inline node-home-lambda))
269 (node-home-lambda (block-last block)))
271 ;;; Return the IR1 environment for BLOCK.
272 (defun block-environment (block)
273 (declare (type cblock block))
274 #!-sb-fluid (declare (inline node-home-lambda))
275 (lambda-environment (node-home-lambda (block-last block))))
277 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
278 ;;; of its original source's top-level form in its compilation unit.
279 (defun source-path-tlf-number (path)
280 (declare (list path))
283 ;;; Return the (reversed) list for the PATH in the original source
284 ;;; (with the Top Level Form number last).
285 (defun source-path-original-source (path)
286 (declare (list path) (inline member))
287 (cddr (member 'original-source-start path :test #'eq)))
289 ;;; Return the Form Number of PATH's original source inside the Top
290 ;;; Level Form that contains it. This is determined by the order that
291 ;;; we walk the subforms of the top level source form.
292 (defun source-path-form-number (path)
293 (declare (list path) (inline member))
294 (cadr (member 'original-source-start path :test #'eq)))
296 ;;; Return a list of all the enclosing forms not in the original
297 ;;; source that converted to get to this form, with the immediate
298 ;;; source for node at the start of the list.
299 (defun source-path-forms (path)
300 (subseq path 0 (position 'original-source-start path)))
302 ;;; Return the innermost source form for NODE.
303 (defun node-source-form (node)
304 (declare (type node node))
305 (let* ((path (node-source-path node))
306 (forms (source-path-forms path)))
309 (values (find-original-source path)))))
311 ;;; Return NODE-SOURCE-FORM, T if continuation has a single use,
312 ;;; otherwise NIL, NIL.
313 (defun continuation-source (cont)
314 (let ((use (continuation-use cont)))
316 (values (node-source-form use) t)
319 ;;; Return a new LEXENV just like DEFAULT except for the specified
320 ;;; slot values. Values for the alist slots are NCONCed to the
321 ;;; beginning of the current value, rather than replacing it entirely.
322 (defun make-lexenv (&key (default *lexenv*)
323 functions variables blocks tags type-restrictions
325 (lambda (lexenv-lambda default))
326 (cleanup (lexenv-cleanup default))
327 (policy (lexenv-policy default)))
328 (macrolet ((frob (var slot)
329 `(let ((old (,slot default)))
333 (internal-make-lexenv
334 (frob functions lexenv-functions)
335 (frob variables lexenv-variables)
336 (frob blocks lexenv-blocks)
337 (frob tags lexenv-tags)
338 (frob type-restrictions lexenv-type-restrictions)
339 lambda cleanup policy
340 (frob options lexenv-options))))
342 ;;;; flow/DFO/component hackery
344 ;;; Join BLOCK1 and BLOCK2.
345 #!-sb-fluid (declaim (inline link-blocks))
346 (defun link-blocks (block1 block2)
347 (declare (type cblock block1 block2))
348 (setf (block-succ block1)
349 (if (block-succ block1)
350 (%link-blocks block1 block2)
352 (push block1 (block-pred block2))
354 (defun %link-blocks (block1 block2)
355 (declare (type cblock block1 block2) (inline member))
356 (let ((succ1 (block-succ block1)))
357 (aver (not (member block2 succ1 :test #'eq)))
358 (cons block2 succ1)))
360 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
361 ;;; this leaves a successor with a single predecessor that ends in an
362 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
363 ;;; now be able to be propagated to the successor.
364 (defun unlink-blocks (block1 block2)
365 (declare (type cblock block1 block2))
366 (let ((succ1 (block-succ block1)))
367 (if (eq block2 (car succ1))
368 (setf (block-succ block1) (cdr succ1))
369 (do ((succ (cdr succ1) (cdr succ))
371 ((eq (car succ) block2)
372 (setf (cdr prev) (cdr succ)))
375 (let ((new-pred (delq block1 (block-pred block2))))
376 (setf (block-pred block2) new-pred)
377 (when (and new-pred (null (rest new-pred)))
378 (let ((pred-block (first new-pred)))
379 (when (if-p (block-last pred-block))
380 (setf (block-test-modified pred-block) t)))))
383 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
384 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
385 ;;; consequent/alternative blocks to point to NEW. We also set
386 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
387 ;;; the new successor.
388 (defun change-block-successor (block old new)
389 (declare (type cblock new old block) (inline member))
390 (unlink-blocks block old)
391 (let ((last (block-last block))
392 (comp (block-component block)))
393 (setf (component-reanalyze comp) t)
396 (setf (block-test-modified block) t)
397 (let* ((succ-left (block-succ block))
398 (new (if (and (eq new (component-tail comp))
402 (unless (member new succ-left :test #'eq)
403 (link-blocks block new))
404 (macrolet ((frob (slot)
405 `(when (eq (,slot last) old)
406 (setf (,slot last) new))))
408 (frob if-alternative))))
410 (unless (member new (block-succ block) :test #'eq)
411 (link-blocks block new)))))
415 ;;; Unlink a block from the next/prev chain. We also null out the
417 (declaim (ftype (function (cblock) (values)) remove-from-dfo))
418 (defun remove-from-dfo (block)
419 (let ((next (block-next block))
420 (prev (block-prev block)))
421 (setf (block-component block) nil)
422 (setf (block-next prev) next)
423 (setf (block-prev next) prev))
426 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
427 ;;; Component to be the same as for AFTER.
428 (defun add-to-dfo (block after)
429 (declare (type cblock block after))
430 (let ((next (block-next after))
431 (comp (block-component after)))
432 (aver (not (eq (component-kind comp) :deleted)))
433 (setf (block-component block) comp)
434 (setf (block-next after) block)
435 (setf (block-prev block) after)
436 (setf (block-next block) next)
437 (setf (block-prev next) block))
440 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
441 ;;; the head and tail which are set to T.
442 (declaim (ftype (function (component) (values)) clear-flags))
443 (defun clear-flags (component)
444 (let ((head (component-head component))
445 (tail (component-tail component)))
446 (setf (block-flag head) t)
447 (setf (block-flag tail) t)
448 (do-blocks (block component)
449 (setf (block-flag block) nil)))
452 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
453 ;;; true in the head and tail blocks.
454 (declaim (ftype (function nil component) make-empty-component))
455 (defun make-empty-component ()
456 (let* ((head (make-block-key :start nil :component nil))
457 (tail (make-block-key :start nil :component nil))
458 (res (make-component :head head :tail tail)))
459 (setf (block-flag head) t)
460 (setf (block-flag tail) t)
461 (setf (block-component head) res)
462 (setf (block-component tail) res)
463 (setf (block-next head) tail)
464 (setf (block-prev tail) head)
467 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
468 ;;; The new block is added to the DFO immediately following NODE's block.
469 (defun node-ends-block (node)
470 (declare (type node node))
471 (let* ((block (node-block node))
472 (start (node-cont node))
473 (last (block-last block))
474 (last-cont (node-cont last)))
475 (unless (eq last node)
476 (aver (and (eq (continuation-kind start) :inside-block)
477 (not (block-delete-p block))))
478 (let* ((succ (block-succ block))
480 (make-block-key :start start
481 :component (block-component block)
482 :start-uses (list (continuation-use start))
483 :succ succ :last last)))
484 (setf (continuation-kind start) :block-start)
487 (cons new-block (remove block (block-pred b)))))
488 (setf (block-succ block) ())
489 (setf (block-last block) node)
490 (link-blocks block new-block)
491 (add-to-dfo new-block block)
492 (setf (component-reanalyze (block-component block)) t)
494 (do ((cont start (node-cont (continuation-next cont))))
496 (when (eq (continuation-kind last-cont) :inside-block)
497 (setf (continuation-block last-cont) new-block)))
498 (setf (continuation-block cont) new-block))
500 (setf (block-type-asserted block) t)
501 (setf (block-test-modified block) t))))
507 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR. We
508 ;;; iterate over all local calls flushing the corresponding argument,
509 ;;; allowing the computation of the argument to be deleted. We also
510 ;;; mark the let for reoptimization, since it may be that we have
511 ;;; deleted the last variable.
513 ;;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
514 ;;; too much difficulty, since we can efficiently implement write-only
515 ;;; variables. We iterate over the sets, marking their blocks for dead
516 ;;; code flushing, since we can delete sets whose value is unused.
517 (defun delete-lambda-var (leaf)
518 (declare (type lambda-var leaf))
519 (let* ((fun (lambda-var-home leaf))
520 (n (position leaf (lambda-vars fun))))
521 (dolist (ref (leaf-refs fun))
522 (let* ((cont (node-cont ref))
523 (dest (continuation-dest cont)))
524 (when (and (combination-p dest)
525 (eq (basic-combination-fun dest) cont)
526 (eq (basic-combination-kind dest) :local))
527 (let* ((args (basic-combination-args dest))
529 (reoptimize-continuation arg)
531 (setf (elt args n) nil))))))
533 (dolist (set (lambda-var-sets leaf))
534 (setf (block-flush-p (node-block set)) t))
538 ;;; Note that something interesting has happened to VAR. We only deal
539 ;;; with LET variables, marking the corresponding initial value arg as
540 ;;; needing to be reoptimized.
541 (defun reoptimize-lambda-var (var)
542 (declare (type lambda-var var))
543 (let ((fun (lambda-var-home var)))
544 (when (and (eq (functional-kind fun) :let)
546 (do ((args (basic-combination-args
549 (first (leaf-refs fun)))))
551 (vars (lambda-vars fun) (cdr vars)))
553 (reoptimize-continuation (car args))))))
556 ;;; Delete a function that has no references. This need only be called
557 ;;; on functions that never had any references, since otherwise
558 ;;; DELETE-REF will handle the deletion.
559 (defun delete-functional (fun)
560 (aver (and (null (leaf-refs fun))
561 (not (functional-entry-function fun))))
563 (optional-dispatch (delete-optional-dispatch fun))
564 (clambda (delete-lambda fun)))
567 ;;; Deal with deleting the last reference to a LAMBDA. Since there is
568 ;;; only one way into a LAMBDA, deleting the last reference to a
569 ;;; LAMBDA ensures that there is no way to reach any of the code in
570 ;;; it. So we just set the FUNCTIONAL-KIND for FUN and its LETs to
571 ;;; :DELETED, causing IR1 optimization to delete blocks in that
574 ;;; If the function isn't a LET, we unlink the function head and tail
575 ;;; from the component head and tail to indicate that the code is
576 ;;; unreachable. We also delete the function from COMPONENT-LAMBDAS
577 ;;; (it won't be there before local call analysis, but no matter.) If
578 ;;; the lambda was never referenced, we give a note.
580 ;;; If the lambda is an XEP, then we null out the ENTRY-FUNCTION in its
581 ;;; ENTRY-FUNCTION so that people will know that it is not an entry point
583 (defun delete-lambda (leaf)
584 (declare (type clambda leaf))
585 (let ((kind (functional-kind leaf))
586 (bind (lambda-bind leaf)))
587 (aver (not (member kind '(:deleted :optional :top-level))))
588 (aver (not (functional-has-external-references-p leaf)))
589 (setf (functional-kind leaf) :deleted)
590 (setf (lambda-bind leaf) nil)
591 (dolist (let (lambda-lets leaf))
592 (setf (lambda-bind let) nil)
593 (setf (functional-kind let) :deleted))
595 (if (member kind '(:let :mv-let :assignment))
596 (let ((home (lambda-home leaf)))
597 (setf (lambda-lets home) (delete leaf (lambda-lets home))))
598 (let* ((bind-block (node-block bind))
599 (component (block-component bind-block))
600 (return (lambda-return leaf)))
601 (aver (null (leaf-refs leaf)))
602 (unless (leaf-ever-used leaf)
603 (let ((*compiler-error-context* bind))
604 (compiler-note "deleting unused function~:[.~;~:*~% ~S~]"
606 (unlink-blocks (component-head component) bind-block)
608 (unlink-blocks (node-block return) (component-tail component)))
609 (setf (component-reanalyze component) t)
610 (let ((tails (lambda-tail-set leaf)))
611 (setf (tail-set-functions tails)
612 (delete leaf (tail-set-functions tails)))
613 (setf (lambda-tail-set leaf) nil))
614 (setf (component-lambdas component)
615 (delete leaf (component-lambdas component)))))
617 (when (eq kind :external)
618 (let ((fun (functional-entry-function leaf)))
619 (setf (functional-entry-function fun) nil)
620 (when (optional-dispatch-p fun)
621 (delete-optional-dispatch fun)))))
625 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
626 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
627 ;;; is used both before and after local call analysis. Afterward, all
628 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
629 ;;; to the XEP, leaving it with no references at all. So we look at
630 ;;; the XEP to see whether an optional-dispatch is still really being
631 ;;; used. But before local call analysis, there are no XEPs, and all
632 ;;; references are direct.
634 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
635 ;;; entry-points, making them be normal lambdas, and then deleting the
636 ;;; ones with no references. This deletes any e-p lambdas that were
637 ;;; either never referenced, or couldn't be deleted when the last
638 ;;; deference was deleted (due to their :OPTIONAL kind.)
640 ;;; Note that the last optional ep may alias the main entry, so when
641 ;;; we process the main entry, its kind may have been changed to NIL
642 ;;; or even converted to a let.
643 (defun delete-optional-dispatch (leaf)
644 (declare (type optional-dispatch leaf))
645 (let ((entry (functional-entry-function leaf)))
646 (unless (and entry (leaf-refs entry))
647 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
648 (setf (functional-kind leaf) :deleted)
651 (unless (eq (functional-kind fun) :deleted)
652 (aver (eq (functional-kind fun) :optional))
653 (setf (functional-kind fun) nil)
654 (let ((refs (leaf-refs fun)))
658 (or (maybe-let-convert fun)
659 (maybe-convert-to-assignment fun)))
661 (maybe-convert-to-assignment fun)))))))
663 (dolist (ep (optional-dispatch-entry-points leaf))
665 (when (optional-dispatch-more-entry leaf)
666 (frob (optional-dispatch-more-entry leaf)))
667 (let ((main (optional-dispatch-main-entry leaf)))
668 (when (eq (functional-kind main) :optional)
673 ;;; Do stuff to delete the semantic attachments of a REF node. When
674 ;;; this leaves zero or one reference, we do a type dispatch off of
675 ;;; the leaf to determine if a special action is appropriate.
676 (defun delete-ref (ref)
677 (declare (type ref ref))
678 (let* ((leaf (ref-leaf ref))
679 (refs (delete ref (leaf-refs leaf))))
680 (setf (leaf-refs leaf) refs)
684 (lambda-var (delete-lambda-var leaf))
686 (ecase (functional-kind leaf)
687 ((nil :let :mv-let :assignment :escape :cleanup)
688 (aver (not (functional-entry-function leaf)))
689 (delete-lambda leaf))
691 (delete-lambda leaf))
692 ((:deleted :optional))))
694 (unless (eq (functional-kind leaf) :deleted)
695 (delete-optional-dispatch leaf)))))
698 (clambda (or (maybe-let-convert leaf)
699 (maybe-convert-to-assignment leaf)))
700 (lambda-var (reoptimize-lambda-var leaf))))
703 (clambda (maybe-convert-to-assignment leaf))))))
707 ;;; This function is called by people who delete nodes; it provides a
708 ;;; way to indicate that the value of a continuation is no longer
709 ;;; used. We null out the CONTINUATION-DEST, set FLUSH-P in the blocks
710 ;;; containing uses of CONT and set COMPONENT-REOPTIMIZE. If the PREV
711 ;;; of the use is deleted, then we blow off reoptimization.
713 ;;; If the continuation is :Deleted, then we don't do anything, since
714 ;;; all semantics have already been flushed. :DELETED-BLOCK-START
715 ;;; start continuations are treated just like :BLOCK-START; it is
716 ;;; possible that the continuation may be given a new dest (e.g. by
717 ;;; SUBSTITUTE-CONTINUATION), so we don't want to delete it.
718 (defun flush-dest (cont)
719 (declare (type continuation cont))
721 (unless (eq (continuation-kind cont) :deleted)
722 (aver (continuation-dest cont))
723 (setf (continuation-dest cont) nil)
725 (let ((prev (node-prev use)))
726 (unless (eq (continuation-kind prev) :deleted)
727 (let ((block (continuation-block prev)))
728 (setf (component-reoptimize (block-component block)) t)
729 (setf (block-attributep (block-flags block) flush-p type-asserted)
732 (setf (continuation-%type-check cont) nil)
736 ;;; Do a graph walk backward from BLOCK, marking all predecessor
737 ;;; blocks with the DELETE-P flag.
738 (defun mark-for-deletion (block)
739 (declare (type cblock block))
740 (unless (block-delete-p block)
741 (setf (block-delete-p block) t)
742 (setf (component-reanalyze (block-component block)) t)
743 (dolist (pred (block-pred block))
744 (mark-for-deletion pred)))
747 ;;; Delete CONT, eliminating both control and value semantics. We set
748 ;;; FLUSH-P and COMPONENT-REOPTIMIZE similarly to in FLUSH-DEST. Here
749 ;;; we must get the component from the use block, since the
750 ;;; continuation may be a :DELETED-BLOCK-START.
752 ;;; If CONT has DEST, then it must be the case that the DEST is
753 ;;; unreachable, since we can't compute the value desired. In this
754 ;;; case, we call MARK-FOR-DELETION to cause the DEST block and its
755 ;;; predecessors to tell people to ignore them, and to cause them to
756 ;;; be deleted eventually.
757 (defun delete-continuation (cont)
758 (declare (type continuation cont))
759 (aver (not (eq (continuation-kind cont) :deleted)))
762 (let ((prev (node-prev use)))
763 (unless (eq (continuation-kind prev) :deleted)
764 (let ((block (continuation-block prev)))
765 (setf (block-attributep (block-flags block) flush-p type-asserted) t)
766 (setf (component-reoptimize (block-component block)) t)))))
768 (let ((dest (continuation-dest cont)))
770 (let ((prev (node-prev dest)))
772 (not (eq (continuation-kind prev) :deleted)))
773 (let ((block (continuation-block prev)))
774 (unless (block-delete-p block)
775 (mark-for-deletion block)))))))
777 (setf (continuation-kind cont) :deleted)
778 (setf (continuation-dest cont) nil)
779 (setf (continuation-next cont) nil)
780 (setf (continuation-asserted-type cont) *empty-type*)
781 (setf (continuation-%derived-type cont) *empty-type*)
782 (setf (continuation-use cont) nil)
783 (setf (continuation-block cont) nil)
784 (setf (continuation-reoptimize cont) nil)
785 (setf (continuation-%type-check cont) nil)
786 (setf (continuation-info cont) nil)
790 ;;; This function does what is necessary to eliminate the code in it
791 ;;; from the IR1 representation. This involves unlinking it from its
792 ;;; predecessors and successors and deleting various node-specific
793 ;;; semantic information.
795 ;;; We mark the START as has having no next and remove the last node
796 ;;; from its CONT's uses. We also flush the DEST for all continuations
797 ;;; whose values are received by nodes in the block.
798 (defun delete-block (block)
799 (declare (type cblock block))
800 (aver (block-component block)) ; else block is already deleted!
801 (note-block-deletion block)
802 (setf (block-delete-p block) t)
804 (let* ((last (block-last block))
805 (cont (node-cont last)))
806 (delete-continuation-use last)
807 (if (eq (continuation-kind cont) :unused)
808 (delete-continuation cont)
809 (reoptimize-continuation cont)))
811 (dolist (b (block-pred block))
812 (unlink-blocks b block))
813 (dolist (b (block-succ block))
814 (unlink-blocks block b))
816 (do-nodes (node cont block)
818 (ref (delete-ref node))
820 (flush-dest (if-test node)))
821 ;; The next two cases serve to maintain the invariant that a LET
822 ;; always has a well-formed COMBINATION, REF and BIND. We delete
823 ;; the lambda whenever we delete any of these, but we must be
824 ;; careful that this LET has not already been partially deleted.
826 (when (and (eq (basic-combination-kind node) :local)
827 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
828 (continuation-use (basic-combination-fun node)))
829 (let ((fun (combination-lambda node)))
830 ;; If our REF was the 2'nd to last ref, and has been deleted, then
831 ;; Fun may be a LET for some other combination.
832 (when (and (member (functional-kind fun) '(:let :mv-let))
833 (eq (let-combination fun) node))
834 (delete-lambda fun))))
835 (flush-dest (basic-combination-fun node))
836 (dolist (arg (basic-combination-args node))
837 (when arg (flush-dest arg))))
839 (let ((lambda (bind-lambda node)))
840 (unless (eq (functional-kind lambda) :deleted)
841 (aver (member (functional-kind lambda) '(:let :mv-let :assignment)))
842 (delete-lambda lambda))))
844 (let ((value (exit-value node))
845 (entry (exit-entry node)))
849 (setf (entry-exits entry)
850 (delete node (entry-exits entry))))))
852 (flush-dest (return-result node))
853 (delete-return node))
855 (flush-dest (set-value node))
856 (let ((var (set-var node)))
857 (setf (basic-var-sets var)
858 (delete node (basic-var-sets var))))))
860 (delete-continuation (node-prev node)))
862 (remove-from-dfo block)
865 ;;; Do stuff to indicate that the return node Node is being deleted.
866 ;;; We set the RETURN to NIL.
867 (defun delete-return (node)
868 (declare (type creturn node))
869 (let ((fun (return-lambda node)))
870 (aver (lambda-return fun))
871 (setf (lambda-return fun) nil))
874 ;;; If any of the VARS in FUN was never referenced and was not
875 ;;; declared IGNORE, then complain.
876 (defun note-unreferenced-vars (fun)
877 (declare (type clambda fun))
878 (dolist (var (lambda-vars fun))
879 (unless (or (leaf-ever-used var)
880 (lambda-var-ignorep var))
881 (let ((*compiler-error-context* (lambda-bind fun)))
882 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
883 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
884 ;; requires this to be a STYLE-WARNING.
885 (compiler-style-warning "The variable ~S is defined but never used."
887 (setf (leaf-ever-used var) t))))
890 (defvar *deletion-ignored-objects* '(t nil))
892 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
893 ;;; our recursion so that we don't get lost in circular structures. We
894 ;;; ignore the car of forms if they are a symbol (to prevent confusing
895 ;;; function referencess with variables), and we also ignore anything
897 (defun present-in-form (obj form depth)
898 (declare (type (integer 0 20) depth))
899 (cond ((= depth 20) nil)
903 (let ((first (car form))
905 (if (member first '(quote function))
907 (or (and (not (symbolp first))
908 (present-in-form obj first depth))
909 (do ((l (cdr form) (cdr l))
911 ((or (atom l) (> n 100))
914 (when (present-in-form obj (car l) depth)
917 ;;; This function is called on a block immediately before we delete
918 ;;; it. We check to see whether any of the code about to die appeared
919 ;;; in the original source, and emit a note if so.
921 ;;; If the block was in a lambda is now deleted, then we ignore the
922 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
923 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
924 ;;; reasonable for a function to not return, and there is a different
925 ;;; note for that case anyway.
927 ;;; If the actual source is an atom, then we use a bunch of heuristics
928 ;;; to guess whether this reference really appeared in the original
930 ;;; -- If a symbol, it must be interned and not a keyword.
931 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
933 ;;; -- The atom must be "present" in the original source form, and
934 ;;; present in all intervening actual source forms.
935 (defun note-block-deletion (block)
936 (let ((home (block-home-lambda block)))
937 (unless (eq (functional-kind home) :deleted)
938 (do-nodes (node cont block)
939 (let* ((path (node-source-path node))
940 (first (first path)))
941 (when (or (eq first 'original-source-start)
943 (or (not (symbolp first))
944 (let ((pkg (symbol-package first)))
946 (not (eq pkg (symbol-package :end))))))
947 (not (member first *deletion-ignored-objects*))
948 (not (typep first '(or fixnum character)))
950 (present-in-form first x 0))
951 (source-path-forms path))
952 (present-in-form first (find-original-source path)
954 (unless (return-p node)
955 (let ((*compiler-error-context* node))
956 (compiler-note "deleting unreachable code")))
960 ;;; Delete a node from a block, deleting the block if there are no
961 ;;; nodes left. We remove the node from the uses of its CONT, but we
962 ;;; don't deal with cleaning up any type-specific semantic
963 ;;; attachments. If the CONT is :UNUSED after deleting this use, then
964 ;;; we delete CONT. (Note :UNUSED is not the same as no uses. A
965 ;;; continuation will only become :UNUSED if it was :INSIDE-BLOCK
968 ;;; If the node is the last node, there must be exactly one successor.
969 ;;; We link all of our precedessors to the successor and unlink the
970 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
971 ;;; left, and the block is a successor of itself, then we replace the
972 ;;; only node with a degenerate exit node. This provides a way to
973 ;;; represent the bodyless infinite loop, given the prohibition on
974 ;;; empty blocks in IR1.
975 (defun unlink-node (node)
976 (declare (type node node))
977 (let* ((cont (node-cont node))
978 (next (continuation-next cont))
979 (prev (node-prev node))
980 (block (continuation-block prev))
981 (prev-kind (continuation-kind prev))
982 (last (block-last block)))
984 (unless (eq (continuation-kind cont) :deleted)
985 (delete-continuation-use node)
986 (when (eq (continuation-kind cont) :unused)
987 (aver (not (continuation-dest cont)))
988 (delete-continuation cont)))
990 (setf (block-type-asserted block) t)
991 (setf (block-test-modified block) t)
993 (cond ((or (eq prev-kind :inside-block)
994 (and (eq prev-kind :block-start)
995 (not (eq node last))))
996 (cond ((eq node last)
997 (setf (block-last block) (continuation-use prev))
998 (setf (continuation-next prev) nil))
1000 (setf (continuation-next prev) next)
1001 (setf (node-prev next) prev)))
1002 (setf (node-prev node) nil)
1005 (aver (eq prev-kind :block-start))
1006 (aver (eq node last))
1007 (let* ((succ (block-succ block))
1008 (next (first succ)))
1009 (aver (and succ (null (cdr succ))))
1011 ((member block succ)
1012 (with-ir1-environment node
1013 (let ((exit (make-exit))
1014 (dummy (make-continuation)))
1015 (setf (continuation-next prev) nil)
1016 (prev-link exit prev)
1017 (add-continuation-use exit dummy)
1018 (setf (block-last block) exit)))
1019 (setf (node-prev node) nil)
1022 (aver (eq (block-start-cleanup block)
1023 (block-end-cleanup block)))
1024 (unlink-blocks block next)
1025 (dolist (pred (block-pred block))
1026 (change-block-successor pred block next))
1027 (remove-from-dfo block)
1028 (cond ((continuation-dest prev)
1029 (setf (continuation-next prev) nil)
1030 (setf (continuation-kind prev) :deleted-block-start))
1032 (delete-continuation prev)))
1033 (setf (node-prev node) nil)
1036 ;;; Return true if NODE has been deleted, false if it is still a valid
1038 (defun node-deleted (node)
1039 (declare (type node node))
1040 (let ((prev (node-prev node)))
1042 (not (eq (continuation-kind prev) :deleted))
1043 (let ((block (continuation-block prev)))
1044 (and (block-component block)
1045 (not (block-delete-p block))))))))
1047 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1048 ;;; marking the blocks as delete-p to prevent weird stuff from being
1049 ;;; triggered by deletion.
1050 (defun delete-component (component)
1051 (declare (type component component))
1052 (aver (null (component-new-functions component)))
1053 (setf (component-kind component) :deleted)
1054 (do-blocks (block component)
1055 (setf (block-delete-p block) t))
1056 (dolist (fun (component-lambdas component))
1057 (setf (functional-kind fun) nil)
1058 (setf (functional-entry-function fun) nil)
1059 (setf (leaf-refs fun) nil)
1060 (delete-functional fun))
1061 (do-blocks (block component)
1062 (delete-block block))
1065 ;;; Convert code of the form
1066 ;;; (FOO ... (FUN ...) ...)
1068 ;;; (FOO ... ... ...).
1069 ;;; In other words, replace the function combination FUN by its
1070 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1071 ;;; to blow out of whatever transform called this. Note, as the number
1072 ;;; of arguments changes, the transform must be prepared to return a
1073 ;;; lambda with a new lambda-list with the correct number of
1075 (defun extract-function-args (cont fun num-args)
1077 "If CONT is a call to FUN with NUM-ARGS args, change those arguments
1078 to feed directly to the continuation-dest of CONT, which must be
1080 (declare (type continuation cont)
1082 (type index num-args))
1083 (let ((outside (continuation-dest cont))
1084 (inside (continuation-use cont)))
1085 (aver (combination-p outside))
1086 (unless (combination-p inside)
1087 (give-up-ir1-transform))
1088 (let ((inside-fun (combination-fun inside)))
1089 (unless (eq (continuation-function-name inside-fun) fun)
1090 (give-up-ir1-transform))
1091 (let ((inside-args (combination-args inside)))
1092 (unless (= (length inside-args) num-args)
1093 (give-up-ir1-transform))
1094 (let* ((outside-args (combination-args outside))
1095 (arg-position (position cont outside-args))
1096 (before-args (subseq outside-args 0 arg-position))
1097 (after-args (subseq outside-args (1+ arg-position))))
1098 (dolist (arg inside-args)
1099 (setf (continuation-dest arg) outside))
1100 (setf (combination-args inside) nil)
1101 (setf (combination-args outside)
1102 (append before-args inside-args after-args))
1103 (change-ref-leaf (continuation-use inside-fun)
1104 (find-free-function 'list "???"))
1105 (setf (combination-kind inside) :full)
1106 (setf (node-derived-type inside) *wild-type*)
1108 (setf (continuation-asserted-type cont) *wild-type*)
1113 ;;; Change the Leaf that a Ref refers to.
1114 (defun change-ref-leaf (ref leaf)
1115 (declare (type ref ref) (type leaf leaf))
1116 (unless (eq (ref-leaf ref) leaf)
1117 (push ref (leaf-refs leaf))
1119 (setf (ref-leaf ref) leaf)
1120 (let ((ltype (leaf-type leaf)))
1121 (if (function-type-p ltype)
1122 (setf (node-derived-type ref) ltype)
1123 (derive-node-type ref ltype)))
1124 (reoptimize-continuation (node-cont ref)))
1127 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1128 (defun substitute-leaf (new-leaf old-leaf)
1129 (declare (type leaf new-leaf old-leaf))
1130 (dolist (ref (leaf-refs old-leaf))
1131 (change-ref-leaf ref new-leaf))
1134 ;;; Like SUBSITUTE-LEAF, only there is a predicate on the Ref to tell
1135 ;;; whether to substitute.
1136 (defun substitute-leaf-if (test new-leaf old-leaf)
1137 (declare (type leaf new-leaf old-leaf) (type function test))
1138 (dolist (ref (leaf-refs old-leaf))
1139 (when (funcall test ref)
1140 (change-ref-leaf ref new-leaf)))
1143 ;;; Return a LEAF which represents the specified constant object. If
1144 ;;; the object is not in *CONSTANTS*, then we create a new constant
1145 ;;; LEAF and enter it.
1146 #!-sb-fluid (declaim (maybe-inline find-constant))
1147 (defun find-constant (object)
1148 (if (typep object '(or symbol number character instance))
1149 (or (gethash object *constants*)
1150 (setf (gethash object *constants*)
1151 (make-constant :value object
1153 :type (ctype-of object)
1154 :where-from :defined)))
1155 (make-constant :value object
1157 :type (ctype-of object)
1158 :where-from :defined)))
1160 ;;; If there is a non-local exit noted in ENTRY's environment that
1161 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1162 (defun find-nlx-info (entry cont)
1163 (declare (type entry entry) (type continuation cont))
1164 (let ((entry-cleanup (entry-cleanup entry)))
1165 (dolist (nlx (environment-nlx-info (node-environment entry)) nil)
1166 (when (and (eq (nlx-info-continuation nlx) cont)
1167 (eq (nlx-info-cleanup nlx) entry-cleanup))
1170 ;;;; functional hackery
1172 (declaim (ftype (function (functional) clambda) main-entry))
1173 (defun main-entry (functional)
1174 (etypecase functional
1175 (clambda functional)
1177 (optional-dispatch-main-entry functional))))
1179 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1180 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1181 ;;; optional with null default and no SUPPLIED-P. There must be a
1182 ;;; &REST arg with no references.
1183 (declaim (ftype (function (functional) boolean) looks-like-an-mv-bind))
1184 (defun looks-like-an-mv-bind (functional)
1185 (and (optional-dispatch-p functional)
1186 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1188 (let ((info (lambda-var-arg-info (car arg))))
1189 (unless info (return nil))
1190 (case (arg-info-kind info)
1192 (when (or (arg-info-supplied-p info) (arg-info-default info))
1195 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1199 ;;; Return true if function is an XEP. This is true of normal XEPs
1200 ;;; (:EXTERNAL kind) and top-level lambdas (:TOP-LEVEL kind.)
1201 (defun external-entry-point-p (fun)
1202 (declare (type functional fun))
1203 (not (null (member (functional-kind fun) '(:external :top-level)))))
1205 ;;; If CONT's only use is a non-notinline global function reference,
1206 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1207 ;;; is true, then we don't care if the leaf is NOTINLINE.
1208 (defun continuation-function-name (cont &optional notinline-ok)
1209 (declare (type continuation cont))
1210 (let ((use (continuation-use cont)))
1212 (let ((leaf (ref-leaf use)))
1213 (if (and (global-var-p leaf)
1214 (eq (global-var-kind leaf) :global-function)
1215 (or (not (defined-function-p leaf))
1216 (not (eq (defined-function-inlinep leaf) :notinline))
1222 ;;; Return the COMBINATION node that is the call to the LET FUN.
1223 (defun let-combination (fun)
1224 (declare (type clambda fun))
1225 (aver (member (functional-kind fun) '(:let :mv-let)))
1226 (continuation-dest (node-cont (first (leaf-refs fun)))))
1228 ;;; Return the initial value continuation for a LET variable, or NIL
1229 ;;; if there is none.
1230 (defun let-var-initial-value (var)
1231 (declare (type lambda-var var))
1232 (let ((fun (lambda-var-home var)))
1233 (elt (combination-args (let-combination fun))
1234 (position-or-lose var (lambda-vars fun)))))
1236 ;;; Return the LAMBDA that is called by the local Call.
1237 #!-sb-fluid (declaim (inline combination-lambda))
1238 (defun combination-lambda (call)
1239 (declare (type basic-combination call))
1240 (aver (eq (basic-combination-kind call) :local))
1241 (ref-leaf (continuation-use (basic-combination-fun call))))
1243 (defvar *inline-expansion-limit* 200
1245 "an upper limit on the number of inline function calls that will be expanded
1246 in any given code object (single function or block compilation)")
1248 ;;; Check whether NODE's component has exceeded its inline expansion
1249 ;;; limit, and warn if so, returning NIL.
1250 (defun inline-expansion-ok (node)
1251 (let ((expanded (incf (component-inline-expansions
1253 (node-block node))))))
1254 (cond ((> expanded *inline-expansion-limit*) nil)
1255 ((= expanded *inline-expansion-limit*)
1256 ;; FIXME: If the objective is to stop the recursive
1257 ;; expansion of inline functions, wouldn't it be more
1258 ;; correct to look back through surrounding expansions
1259 ;; (which are, I think, stored in the *CURRENT-PATH*, and
1260 ;; possibly stored elsewhere too) and suppress expansion
1261 ;; and print this warning when the function being proposed
1262 ;; for inline expansion is found there? (I don't like the
1263 ;; arbitrary numerical limit in principle, and I think
1264 ;; it'll be a nuisance in practice if we ever want the
1265 ;; compiler to be able to use WITH-COMPILATION-UNIT on
1266 ;; arbitrarily huge blocks of code. -- WHN)
1267 (let ((*compiler-error-context* node))
1268 (compiler-note "*INLINE-EXPANSION-LIMIT* (~D) was exceeded, ~
1269 probably trying to~% ~
1270 inline a recursive function."
1271 *inline-expansion-limit*))
1275 ;;;; compiler error context determination
1277 (declaim (special *current-path*))
1279 ;;; We bind print level and length when printing out messages so that
1280 ;;; we don't dump huge amounts of garbage.
1282 ;;; FIXME: It's not possible to get the defaults right for everyone.
1283 ;;; So: Should these variables be in the SB-EXT package? Or should we
1284 ;;; just get rid of them completely and just use the bare
1285 ;;; CL:*PRINT-FOO* variables instead?
1286 (declaim (type (or unsigned-byte null)
1287 *compiler-error-print-level*
1288 *compiler-error-print-length*
1289 *compiler-error-print-lines*))
1290 (defvar *compiler-error-print-level* 5
1292 "the value for *PRINT-LEVEL* when printing compiler error messages")
1293 (defvar *compiler-error-print-length* 10
1295 "the value for *PRINT-LENGTH* when printing compiler error messages")
1296 (defvar *compiler-error-print-lines* 12
1298 "the value for *PRINT-LINES* when printing compiler error messages")
1300 (defvar *enclosing-source-cutoff* 1
1302 "The maximum number of enclosing non-original source forms (i.e. from
1303 macroexpansion) that we print in full. For additional enclosing forms, we
1304 print only the CAR.")
1305 (declaim (type unsigned-byte *enclosing-source-cutoff*))
1307 ;;; We separate the determination of compiler error contexts from the
1308 ;;; actual signalling of those errors by objectifying the error
1309 ;;; context. This allows postponement of the determination of how (and
1310 ;;; if) to signal the error.
1312 ;;; We take care not to reference any of the IR1 so that pending
1313 ;;; potential error messages won't prevent the IR1 from being GC'd. To
1314 ;;; this end, we convert source forms to strings so that source forms
1315 ;;; that contain IR1 references (e.g. %DEFUN) don't hold onto the IR.
1316 (defstruct (compiler-error-context
1317 #-no-ansi-print-object
1318 (:print-object (lambda (x stream)
1319 (print-unreadable-object (x stream :type t))))
1321 ;; a list of the stringified CARs of the enclosing non-original source forms
1322 ;; exceeding the *enclosing-source-cutoff*
1323 (enclosing-source nil :type list)
1324 ;; a list of stringified enclosing non-original source forms
1325 (source nil :type list)
1326 ;; the stringified form in the original source that expanded into SOURCE
1327 (original-source (required-argument) :type simple-string)
1328 ;; a list of prefixes of "interesting" forms that enclose original-source
1329 (context nil :type list)
1330 ;; the FILE-INFO-NAME for the relevant FILE-INFO
1331 (file-name (required-argument)
1332 :type (or pathname (member :lisp :stream)))
1333 ;; the file position at which the top-level form starts, if applicable
1334 (file-position nil :type (or index null))
1335 ;; the original source part of the source path
1336 (original-source-path nil :type list))
1338 ;;; If true, this is the node which is used as context in compiler warning
1340 (declaim (type (or null compiler-error-context node) *compiler-error-context*))
1341 (defvar *compiler-error-context* nil)
1343 ;;; a hashtable mapping macro names to source context parsers. Each parser
1344 ;;; function returns the source-context list for that form.
1345 (defvar *source-context-methods* (make-hash-table))
1347 ;;; documentation originally from cmu-user.tex:
1348 ;;; This macro defines how to extract an abbreviated source context from
1349 ;;; the \var{name}d form when it appears in the compiler input.
1350 ;;; \var{lambda-list} is a \code{defmacro} style lambda-list used to
1351 ;;; parse the arguments. The \var{body} should return a list of
1352 ;;; subforms that can be printed on about one line. There are
1353 ;;; predefined methods for \code{defstruct}, \code{defmethod}, etc. If
1354 ;;; no method is defined, then the first two subforms are returned.
1355 ;;; Note that this facility implicitly determines the string name
1356 ;;; associated with anonymous functions.
1357 ;;; So even though SBCL itself only uses this macro within this file,
1358 ;;; it's a reasonable thing to put in SB-EXT in case some dedicated
1359 ;;; user wants to do some heavy tweaking to make SBCL give more
1360 ;;; informative output about his code.
1361 (defmacro def-source-context (name lambda-list &body body)
1363 "DEF-SOURCE-CONTEXT Name Lambda-List Form*
1364 This macro defines how to extract an abbreviated source context from the
1365 Named form when it appears in the compiler input. Lambda-List is a DEFMACRO
1366 style lambda-list used to parse the arguments. The Body should return a
1367 list of subforms suitable for a \"~{~S ~}\" format string."
1368 (let ((n-whole (gensym)))
1369 `(setf (gethash ',name *source-context-methods*)
1370 #'(lambda (,n-whole)
1371 (destructuring-bind ,lambda-list ,n-whole ,@body)))))
1373 (def-source-context defstruct (name-or-options &rest slots)
1374 (declare (ignore slots))
1375 `(defstruct ,(if (consp name-or-options)
1376 (car name-or-options)
1379 (def-source-context function (thing)
1380 (if (and (consp thing) (eq (first thing) 'lambda) (consp (rest thing)))
1381 `(lambda ,(second thing))
1382 `(function ,thing)))
1384 ;;; Return the first two elements of FORM if FORM is a list. Take the
1385 ;;; CAR of the second form if appropriate.
1386 (defun source-form-context (form)
1387 (cond ((atom form) nil)
1388 ((>= (length form) 2)
1389 (funcall (gethash (first form) *source-context-methods*
1391 (declare (ignore x))
1392 (list (first form) (second form))))
1397 ;;; Given a source path, return the original source form and a
1398 ;;; description of the interesting aspects of the context in which it
1399 ;;; appeared. The context is a list of lists, one sublist per context
1400 ;;; form. The sublist is a list of some of the initial subforms of the
1403 ;;; For now, we use the first two subforms of each interesting form. A
1404 ;;; form is interesting if the first element is a symbol beginning
1405 ;;; with "DEF" and it is not the source form. If there is no
1406 ;;; DEF-mumble, then we use the outermost containing form. If the
1407 ;;; second subform is a list, then in some cases we return the CAR of
1408 ;;; that form rather than the whole form (i.e. don't show DEFSTRUCT
1410 (defun find-original-source (path)
1411 (declare (list path))
1412 (let* ((rpath (reverse (source-path-original-source path)))
1414 (root (find-source-root tlf *source-info*)))
1415 (collect ((context))
1417 (current (rest rpath)))
1420 (aver (null current))
1422 (let ((head (first form)))
1423 (when (symbolp head)
1424 (let ((name (symbol-name head)))
1425 (when (and (>= (length name) 3) (string= name "DEF" :end1 3))
1426 (context (source-form-context form))))))
1427 (when (null current) (return))
1428 (setq form (nth (pop current) form)))
1431 (values form (context)))
1433 (let ((c (source-form-context root)))
1434 (values form (if c (list c) nil))))
1436 (values '(unable to locate source)
1437 '((some strange place)))))))))
1439 ;;; Convert a source form to a string, suitably formatted for use in
1440 ;;; compiler warnings.
1441 (defun stringify-form (form &optional (pretty t))
1442 (let ((*print-level* *compiler-error-print-level*)
1443 (*print-length* *compiler-error-print-length*)
1444 (*print-lines* *compiler-error-print-lines*)
1445 (*print-pretty* pretty))
1447 (format nil "~<~@; ~S~:>" (list form))
1448 (prin1-to-string form))))
1450 ;;; Return a COMPILER-ERROR-CONTEXT structure describing the current
1451 ;;; error context, or NIL if we can't figure anything out. ARGS is a
1452 ;;; list of things that are going to be printed out in the error
1453 ;;; message, and can thus be blown off when they appear in the source
1455 (defun find-error-context (args)
1456 (let ((context *compiler-error-context*))
1457 (if (compiler-error-context-p context)
1459 (let ((path (or (and (boundp '*current-path*) *current-path*)
1461 (node-source-path context)
1463 (when (and *source-info* path)
1464 (multiple-value-bind (form src-context) (find-original-source path)
1465 (collect ((full nil cons)
1467 (let ((forms (source-path-forms path))
1469 (dolist (src (if (member (first forms) args)
1472 (if (>= n *enclosing-source-cutoff*)
1473 (short (stringify-form (if (consp src)
1477 (full (stringify-form src)))
1480 (let* ((tlf (source-path-tlf-number path))
1481 (file-info (source-info-file-info *source-info*)))
1482 (make-compiler-error-context
1483 :enclosing-source (short)
1485 :original-source (stringify-form form)
1486 :context src-context
1487 :file-name (file-info-name file-info)
1489 (multiple-value-bind (ignore pos)
1490 (find-source-root tlf *source-info*)
1491 (declare (ignore ignore))
1493 :original-source-path
1494 (source-path-original-source path))))))))))
1496 ;;;; printing error messages
1498 ;;; We save the context information that we printed out most recently
1499 ;;; so that we don't print it out redundantly.
1501 ;;; The last COMPILER-ERROR-CONTEXT that we printed.
1502 (defvar *last-error-context* nil)
1503 (declaim (type (or compiler-error-context null) *last-error-context*))
1505 ;;; The format string and args for the last error we printed.
1506 (defvar *last-format-string* nil)
1507 (defvar *last-format-args* nil)
1508 (declaim (type (or string null) *last-format-string*))
1509 (declaim (type list *last-format-args*))
1511 ;;; The number of times that the last error message has been emitted,
1512 ;;; so that we can compress duplicate error messages.
1513 (defvar *last-message-count* 0)
1514 (declaim (type index *last-message-count*))
1516 ;;; If the last message was given more than once, then print out an
1517 ;;; indication of how many times it was repeated. We reset the message
1518 ;;; count when we are done.
1519 (defun note-message-repeats (&optional (terpri t))
1520 (cond ((= *last-message-count* 1)
1521 (when terpri (terpri *error-output*)))
1522 ((> *last-message-count* 1)
1523 (format *error-output* "~&; [Last message occurs ~D times.]~2%"
1524 *last-message-count*)))
1525 (setq *last-message-count* 0))
1527 ;;; Print out the message, with appropriate context if we can find it.
1528 ;;; If the context is different from the context of the last message
1529 ;;; we printed, then we print the context. If the original source is
1530 ;;; different from the source we are working on, then we print the
1531 ;;; current source in addition to the original source.
1533 ;;; We suppress printing of messages identical to the previous, but
1534 ;;; record the number of times that the message is repeated.
1535 (defun print-compiler-message (format-string format-args)
1537 (declare (type simple-string format-string))
1538 (declare (type list format-args))
1540 (let ((stream *error-output*)
1541 (context (find-error-context format-args)))
1544 (let ((file (compiler-error-context-file-name context))
1545 (in (compiler-error-context-context context))
1546 (form (compiler-error-context-original-source context))
1547 (enclosing (compiler-error-context-enclosing-source context))
1548 (source (compiler-error-context-source context))
1549 (last *last-error-context*))
1552 (equal file (compiler-error-context-file-name last)))
1553 (when (pathnamep file)
1554 (note-message-repeats)
1556 (format stream "~2&; file: ~A~%" (namestring file))))
1559 (equal in (compiler-error-context-context last)))
1560 (note-message-repeats)
1562 (format stream "~&")
1563 (pprint-logical-block (stream nil :per-line-prefix "; ")
1564 (format stream "in:~{~<~% ~4:;~{ ~S~}~>~^ =>~}" in))
1565 (format stream "~%"))
1570 (compiler-error-context-original-source last)))
1571 (note-message-repeats)
1573 (format stream "~&")
1574 (pprint-logical-block (stream nil :per-line-prefix "; ")
1575 (format stream " ~A" form))
1576 (format stream "~&"))
1580 (compiler-error-context-enclosing-source last)))
1582 (note-message-repeats)
1584 (format stream "~&; --> ~{~<~%; --> ~1:;~A~> ~}~%" enclosing)))
1587 (equal source (compiler-error-context-source last)))
1588 (setq *last-format-string* nil)
1590 (note-message-repeats)
1591 (dolist (src source)
1592 (format stream "~&")
1593 (write-string "; ==>" stream)
1594 (format stream "~&")
1595 (pprint-logical-block (stream nil :per-line-prefix "; ")
1596 (write-string src stream)))))))
1598 (format stream "~&")
1599 (note-message-repeats)
1600 (setq *last-format-string* nil)
1601 (format stream "~&")))
1603 (setq *last-error-context* context)
1605 (unless (and (equal format-string *last-format-string*)
1606 (tree-equal format-args *last-format-args*))
1607 (note-message-repeats nil)
1608 (setq *last-format-string* format-string)
1609 (setq *last-format-args* format-args)
1610 (let ((*print-level* *compiler-error-print-level*)
1611 (*print-length* *compiler-error-print-length*)
1612 (*print-lines* *compiler-error-print-lines*))
1613 (format stream "~&")
1614 (pprint-logical-block (stream nil :per-line-prefix "; ")
1615 (format stream "~&~?" format-string format-args))
1616 (format stream "~&"))))
1618 (incf *last-message-count*)
1621 (defun print-compiler-condition (condition)
1622 (declare (type condition condition))
1623 (let (;; These different classes of conditions have different
1624 ;; effects on the return codes of COMPILE-FILE, so it's nice
1625 ;; for users to be able to pick them out by lexical search
1626 ;; through the output.
1627 (what (etypecase condition
1628 (style-warning 'style-warning)
1631 (multiple-value-bind (format-string format-args)
1632 (if (typep condition 'simple-condition)
1633 (values (simple-condition-format-control condition)
1634 (simple-condition-format-arguments condition))
1636 (list (with-output-to-string (s)
1637 (princ condition s)))))
1638 (print-compiler-message (format nil
1645 ;;; COMPILER-NOTE is vaguely like COMPILER-ERROR and the other
1646 ;;; condition-signalling functions, but it just writes some output
1647 ;;; instead of signalling. (In CMU CL, it did signal a condition, but
1648 ;;; this didn't seem to work all that well; it was weird to have
1649 ;;; COMPILE-FILE return with WARNINGS-P set when the only problem was
1650 ;;; that the compiler couldn't figure out how to compile something as
1651 ;;; efficiently as it liked.)
1652 (defun compiler-note (format-string &rest format-args)
1653 (unless (if *compiler-error-context*
1654 (policy *compiler-error-context* (= inhibit-warnings 3))
1655 (policy *lexenv* (= inhibit-warnings 3)))
1656 (incf *compiler-note-count*)
1657 (print-compiler-message (format nil "note: ~A" format-string)
1661 ;;; Issue a note when we might or might not be in the compiler.
1662 (defun maybe-compiler-note (&rest rest)
1663 (if (boundp '*lexenv*) ; if we're in the compiler
1664 (apply #'compiler-note rest)
1665 (let ((stream *error-output*))
1666 (pprint-logical-block (stream nil :per-line-prefix ";")
1668 (format stream " note: ~3I~_")
1669 (pprint-logical-block (stream nil)
1670 (apply #'format stream rest)))
1671 (fresh-line stream)))) ; (outside logical block, no per-line-prefix)
1673 ;;; The politically correct way to print out progress messages and
1674 ;;; such like. We clear the current error context so that we know that
1675 ;;; it needs to be reprinted, and we also Force-Output so that the
1676 ;;; message gets seen right away.
1677 (declaim (ftype (function (string &rest t) (values)) compiler-mumble))
1678 (defun compiler-mumble (format-string &rest format-args)
1679 (note-message-repeats)
1680 (setq *last-error-context* nil)
1681 (apply #'format *error-output* format-string format-args)
1682 (force-output *error-output*)
1685 ;;; Return a string that somehow names the code in COMPONENT. We use
1686 ;;; the source path for the bind node for an arbitrary entry point to
1687 ;;; find the source context, then return that as a string.
1688 (declaim (ftype (function (component) simple-string) find-component-name))
1689 (defun find-component-name (component)
1690 (let ((ep (first (block-succ (component-head component)))))
1691 (aver ep) ; else no entry points??
1692 (multiple-value-bind (form context)
1693 (find-original-source
1694 (node-source-path (continuation-next (block-start ep))))
1695 (declare (ignore form))
1696 (let ((*print-level* 2)
1697 (*print-pretty* nil))
1698 (format nil "~{~{~S~^ ~}~^ => ~}" context)))))
1700 ;;;; condition system interface
1702 ;;; Keep track of how many times each kind of condition happens.
1703 (defvar *compiler-error-count*)
1704 (defvar *compiler-warning-count*)
1705 (defvar *compiler-style-warning-count*)
1706 (defvar *compiler-note-count*)
1708 ;;; Keep track of whether any surrounding COMPILE or COMPILE-FILE call
1709 ;;; should return WARNINGS-P or FAILURE-P.
1710 (defvar *failure-p*)
1711 (defvar *warnings-p*)
1713 ;;; condition handlers established by the compiler. We re-signal the
1714 ;;; condition, then if it isn't handled, we increment our warning
1715 ;;; counter and print the error message.
1716 (defun compiler-error-handler (condition)
1718 (incf *compiler-error-count*)
1719 (setf *warnings-p* t
1721 (print-compiler-condition condition)
1722 (continue condition))
1723 (defun compiler-warning-handler (condition)
1725 (incf *compiler-warning-count*)
1726 (setf *warnings-p* t
1728 (print-compiler-condition condition)
1729 (muffle-warning condition))
1730 (defun compiler-style-warning-handler (condition)
1732 (incf *compiler-style-warning-count*)
1733 (setf *warnings-p* t)
1734 (print-compiler-condition condition)
1735 (muffle-warning condition))
1737 ;;;; undefined warnings
1739 (defvar *undefined-warning-limit* 3
1741 "If non-null, then an upper limit on the number of unknown function or type
1742 warnings that the compiler will print for any given name in a single
1743 compilation. This prevents excessive amounts of output when the real
1744 problem is a missing definition (as opposed to a typo in the use.)")
1746 ;;; Make an entry in the *UNDEFINED-WARNINGS* describing a reference
1747 ;;; to NAME of the specified KIND. If we have exceeded the warning
1748 ;;; limit, then just increment the count, otherwise note the current
1751 ;;; Undefined types are noted by a condition handler in
1752 ;;; WITH-COMPILATION-UNIT, which can potentially be invoked outside
1753 ;;; the compiler, hence the BOUNDP check.
1754 (defun note-undefined-reference (name kind)
1756 ;; Check for boundness so we don't blow up if we're called
1757 ;; when IR1 conversion isn't going on.
1759 ;; FIXME: I'm pretty sure the INHIBIT-WARNINGS test below
1760 ;; isn't a good idea; we should have INHIBIT-WARNINGS
1761 ;; affect compiler notes, not STYLE-WARNINGs. And I'm not
1762 ;; sure what the BOUNDP '*LEXENV* test above is for; it's
1763 ;; likely a good idea, but it probably deserves an
1764 ;; explanatory comment.
1765 (policy *lexenv* (= inhibit-warnings 3)))
1766 (let* ((found (dolist (warning *undefined-warnings* nil)
1767 (when (and (equal (undefined-warning-name warning) name)
1768 (eq (undefined-warning-kind warning) kind))
1771 (make-undefined-warning :name name :kind kind))))
1772 (unless found (push res *undefined-warnings*))
1773 (when (or (not *undefined-warning-limit*)
1774 (< (undefined-warning-count res) *undefined-warning-limit*))
1775 (push (find-error-context (list name))
1776 (undefined-warning-warnings res)))
1777 (incf (undefined-warning-count res))))
1782 ;;; Apply a function to some arguments, returning a list of the values
1783 ;;; resulting of the evaluation. If an error is signalled during the
1784 ;;; application, then we print a warning message and return NIL as our
1785 ;;; second value to indicate this. Node is used as the error context
1786 ;;; for any error message, and Context is a string that is spliced
1787 ;;; into the warning.
1788 (declaim (ftype (function ((or symbol function) list node string)
1789 (values list boolean))
1791 (defun careful-call (function args node context)
1793 (multiple-value-list
1794 (handler-case (apply function args)
1796 (let ((*compiler-error-context* node))
1797 (compiler-warning "Lisp error during ~A:~%~A" context condition)
1798 (return-from careful-call (values nil nil))))))
1801 ;;;; utilities used at run-time for parsing &KEY args in IR1
1803 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1804 ;;; the continuation for the value of the &KEY argument KEY in the
1805 ;;; list of continuations ARGS. It returns the continuation if the
1806 ;;; keyword is present, or NIL otherwise. The legality and
1807 ;;; constantness of the keywords should already have been checked.
1808 (declaim (ftype (function (list keyword) (or continuation null))
1809 find-keyword-continuation))
1810 (defun find-keyword-continuation (args key)
1811 (do ((arg args (cddr arg)))
1813 (when (eq (continuation-value (first arg)) key)
1814 (return (second arg)))))
1816 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1817 ;;; verify that alternating continuations in ARGS are constant and
1818 ;;; that there is an even number of args.
1819 (declaim (ftype (function (list) boolean) check-key-args-constant))
1820 (defun check-key-args-constant (args)
1821 (do ((arg args (cddr arg)))
1823 (unless (and (rest arg)
1824 (constant-continuation-p (first arg)))
1827 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1828 ;;; verify that the list of continuations ARGS is a well-formed &KEY
1829 ;;; arglist and that only keywords present in the list KEYS are
1831 (declaim (ftype (function (list list) boolean) check-transform-keys))
1832 (defun check-transform-keys (args keys)
1833 (and (check-key-args-constant args)
1834 (do ((arg args (cddr arg)))
1836 (unless (member (continuation-value (first arg)) keys)
1841 ;;; Called by the expansion of the EVENT macro.
1842 (declaim (ftype (function (event-info (or node null)) *) %event))
1843 (defun %event (info node)
1844 (incf (event-info-count info))
1845 (when (and (>= (event-info-level info) *event-note-threshold*)
1846 (policy (or node *lexenv*)
1847 (= inhibit-warnings 0)))
1848 (let ((*compiler-error-context* node))
1849 (compiler-note (event-info-description info))))
1851 (let ((action (event-info-action info)))
1852 (when action (funcall action node))))