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 none in
18 ;;; its function. If Node has no cleanup, but is in a let, then we must still
19 ;;; 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 an
29 ;;; implicit MV-Prog1. The inserted block is returned. Node is used for IR1
30 ;;; context when converting the form. Note that the block is not assigned a
31 ;;; number, and is linked into the DFO at the beginning. We indicate that we
32 ;;; have trashed the DFO by setting Component-Reanalyze. If Cleanup is
33 ;;; supplied, then convert with that cleanup.
34 (defun insert-cleanup-code (block1 block2 node form &optional cleanup)
35 (declare (type cblock block1 block2) (type node node)
36 (type (or cleanup null) cleanup))
37 (setf (component-reanalyze (block-component block1)) t)
38 (with-ir1-environment node
39 (let* ((start (make-continuation))
40 (block (continuation-starts-block start))
41 (cont (make-continuation))
43 (make-lexenv :cleanup cleanup)
45 (change-block-successor block1 block2 block)
46 (link-blocks block block2)
47 (ir1-convert start cont form)
48 (setf (block-last block) (continuation-use cont))
51 ;;;; continuation use hacking
53 ;;; Return a list of all the nodes which use Cont.
54 (declaim (ftype (function (continuation) list) find-uses))
55 (defun find-uses (cont)
56 (ecase (continuation-kind cont)
57 ((:block-start :deleted-block-start)
58 (block-start-uses (continuation-block cont)))
59 (:inside-block (list (continuation-use cont)))
63 ;;; Update continuation use information so that Node is no longer a
64 ;;; use of its Cont. If the old continuation doesn't start its block,
65 ;;; then we don't update the Block-Start-Uses, since it will be
66 ;;; deleted when we are done.
68 ;;; Note: if you call this function, you may have to do a
69 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
71 (declaim (ftype (function (node) (values)) delete-continuation-use))
72 (defun delete-continuation-use (node)
73 (let* ((cont (node-cont node))
74 (block (continuation-block cont)))
75 (ecase (continuation-kind cont)
77 ((:block-start :deleted-block-start)
78 (let ((uses (delete node (block-start-uses block))))
79 (setf (block-start-uses block) uses)
80 (setf (continuation-use cont)
81 (if (cdr uses) nil (car uses)))))
83 (setf (continuation-kind cont) :unused)
84 (setf (continuation-block cont) nil)
85 (setf (continuation-use cont) nil)
86 (setf (continuation-next cont) nil)))
87 (setf (node-cont node) nil))
90 ;;; Update continuation use information so that Node uses Cont. If
91 ;;; Cont is :Unused, then we set its block to Node's Node-Block (which
94 ;;; Note: if you call this function, you may have to do a
95 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
97 (declaim (ftype (function (node continuation) (values)) add-continuation-use))
98 (defun add-continuation-use (node cont)
99 (aver (not (node-cont node)))
100 (let ((block (continuation-block cont)))
101 (ecase (continuation-kind cont)
105 (let ((block (node-block node)))
107 (setf (continuation-block cont) block))
108 (setf (continuation-kind cont) :inside-block)
109 (setf (continuation-use cont) node))
110 ((:block-start :deleted-block-start)
111 (let ((uses (cons node (block-start-uses block))))
112 (setf (block-start-uses block) uses)
113 (setf (continuation-use cont)
114 (if (cdr uses) nil (car uses)))))))
115 (setf (node-cont node) cont)
118 ;;; Return true if Cont is the Node-Cont for Node and Cont is transferred to
119 ;;; immediately after the evaluation of Node.
120 (defun immediately-used-p (cont node)
121 (declare (type continuation cont) (type node node))
122 (and (eq (node-cont node) cont)
123 (not (eq (continuation-kind cont) :deleted))
124 (let ((cblock (continuation-block cont))
125 (nblock (node-block node)))
126 (or (eq cblock nblock)
127 (let ((succ (block-succ nblock)))
128 (and (= (length succ) 1)
129 (eq (first succ) cblock)))))))
131 ;;;; continuation substitution
133 ;;; In Old's Dest, replace Old with New. New's Dest must initially be NIL.
134 ;;; When we are done, we call Flush-Dest on Old to clear its Dest and to note
135 ;;; potential optimization opportunities.
136 (defun substitute-continuation (new old)
137 (declare (type continuation old new))
138 (aver (not (continuation-dest new)))
139 (let ((dest (continuation-dest old)))
142 (cif (setf (if-test dest) new))
143 (cset (setf (set-value dest) new))
144 (creturn (setf (return-result dest) new))
145 (exit (setf (exit-value dest) new))
147 (if (eq old (basic-combination-fun dest))
148 (setf (basic-combination-fun dest) new)
149 (setf (basic-combination-args dest)
150 (nsubst new old (basic-combination-args dest))))))
153 (setf (continuation-dest new) dest))
156 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
157 ;;; arbitary number of uses. If NEW will end up with more than one
158 ;;; use, then we must arrange for it to start a block if it doesn't
160 (defun substitute-continuation-uses (new old)
161 (declare (type continuation old new))
162 (unless (and (eq (continuation-kind new) :unused)
163 (eq (continuation-kind old) :inside-block))
164 (ensure-block-start new))
167 (delete-continuation-use node)
168 (add-continuation-use node new))
169 (dolist (lexenv-use (continuation-lexenv-uses old))
170 (setf (cadr lexenv-use) new))
172 (reoptimize-continuation new)
175 ;;;; block starting/creation
177 ;;; Return the block that CONT is the start of, making a block if
178 ;;; necessary. This function is called by IR1 translators which may
179 ;;; cause a continuation to be used more than once. Every continuation
180 ;;; which may be used more than once must start a block by the time
181 ;;; that anyone does a USE-CONTINUATION on it.
183 ;;; We also throw the block into the next/prev list for the
184 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
186 (defun continuation-starts-block (cont)
187 (declare (type continuation cont))
188 (ecase (continuation-kind cont)
190 (aver (not (continuation-block cont)))
191 (let* ((head (component-head *current-component*))
192 (next (block-next head))
193 (new-block (make-block cont)))
194 (setf (block-next new-block) next)
195 (setf (block-prev new-block) head)
196 (setf (block-prev next) new-block)
197 (setf (block-next head) new-block)
198 (setf (continuation-block cont) new-block)
199 (setf (continuation-use cont) nil)
200 (setf (continuation-kind cont) :block-start)
203 (continuation-block cont))))
205 ;;; Ensure that Cont is the start of a block (or deleted) so that the use
206 ;;; set can be freely manipulated.
207 ;;; -- If the continuation is :Unused or is :Inside-Block and the Cont of Last
208 ;;; in its block, then we make it the start of a new deleted block.
209 ;;; -- If the continuation is :Inside-Block inside a block, then we split the
210 ;;; block using Node-Ends-Block, which makes the continuation be a
212 (defun ensure-block-start (cont)
213 (declare (type continuation cont))
214 (let ((kind (continuation-kind cont)))
216 ((:deleted :block-start :deleted-block-start))
217 ((:unused :inside-block)
218 (let ((block (continuation-block cont)))
219 (cond ((or (eq kind :unused)
220 (eq (node-cont (block-last block)) cont))
221 (setf (continuation-block cont)
222 (make-block-key :start cont
224 :start-uses (find-uses cont)))
225 (setf (continuation-kind cont) :deleted-block-start))
227 (node-ends-block (continuation-use cont))))))))
230 ;;;; miscellaneous shorthand functions
232 ;;; Return the home (i.e. enclosing non-let) lambda for Node. Since the
233 ;;; LEXENV-LAMBDA may be deleted, we must chain up the LAMBDA-CALL-LEXENV
234 ;;; thread until we find a lambda that isn't deleted, and then return its home.
235 (declaim (maybe-inline node-home-lambda))
236 (defun node-home-lambda (node)
237 (declare (type node node))
238 (do ((fun (lexenv-lambda (node-lexenv node))
239 (lexenv-lambda (lambda-call-lexenv fun))))
240 ((not (eq (functional-kind fun) :deleted))
242 (when (eq (lambda-home fun) fun)
245 #!-sb-fluid (declaim (inline node-block node-tlf-number))
246 (declaim (maybe-inline node-environment))
247 (defun node-block (node)
248 (declare (type node node))
249 (the cblock (continuation-block (node-prev node))))
250 (defun node-environment (node)
251 (declare (type node node))
252 #!-sb-fluid (declare (inline node-home-lambda))
253 (the environment (lambda-environment (node-home-lambda node))))
255 ;;; Return the enclosing cleanup for environment of the first or last node
257 (defun block-start-cleanup (block)
258 (declare (type cblock block))
259 (node-enclosing-cleanup (continuation-next (block-start block))))
260 (defun block-end-cleanup (block)
261 (declare (type cblock block))
262 (node-enclosing-cleanup (block-last block)))
264 ;;; Return the non-let lambda that holds Block's code.
265 (defun block-home-lambda (block)
266 (declare (type cblock block))
267 #!-sb-fluid (declare (inline node-home-lambda))
268 (node-home-lambda (block-last block)))
270 ;;; Return the IR1 environment for Block.
271 (defun block-environment (block)
272 (declare (type cblock block))
273 #!-sb-fluid (declare (inline node-home-lambda))
274 (lambda-environment (node-home-lambda (block-last block))))
276 ;;; Return the Top Level Form number of path, i.e. the ordinal number
277 ;;; of its original source's top-level form in its compilation unit.
278 (defun source-path-tlf-number (path)
279 (declare (list path))
282 ;;; Return the (reversed) list for the path in the original source
283 ;;; (with the Top Level Form number last).
284 (defun source-path-original-source (path)
285 (declare (list path) (inline member))
286 (cddr (member 'original-source-start path :test #'eq)))
288 ;;; Return the Form Number of Path's original source inside the Top
289 ;;; Level Form that contains it. This is determined by the order that
290 ;;; we walk the subforms of the top level source form.
291 (defun source-path-form-number (path)
292 (declare (list path) (inline member))
293 (cadr (member 'original-source-start path :test #'eq)))
295 ;;; Return a list of all the enclosing forms not in the original
296 ;;; source that converted to get to this form, with the immediate
297 ;;; source for node at the start of the list.
298 (defun source-path-forms (path)
299 (subseq path 0 (position 'original-source-start path)))
301 ;;; Return the innermost source form for Node.
302 (defun node-source-form (node)
303 (declare (type node node))
304 (let* ((path (node-source-path node))
305 (forms (source-path-forms path)))
308 (values (find-original-source path)))))
310 ;;; Return NODE-SOURCE-FORM, T if continuation has a single use,
311 ;;; otherwise NIL, NIL.
312 (defun continuation-source (cont)
313 (let ((use (continuation-use cont)))
315 (values (node-source-form use) t)
318 ;;; Return a new LEXENV just like DEFAULT except for the specified
319 ;;; slot values. Values for the alist slots are NCONCed to the
320 ;;; beginning of the current value, rather than replacing it entirely.
321 (defun make-lexenv (&key (default *lexenv*)
322 functions variables blocks tags type-restrictions
324 (lambda (lexenv-lambda default))
325 (cleanup (lexenv-cleanup default))
326 (policy (lexenv-policy default)))
327 (macrolet ((frob (var slot)
328 `(let ((old (,slot default)))
332 (internal-make-lexenv
333 (frob functions lexenv-functions)
334 (frob variables lexenv-variables)
335 (frob blocks lexenv-blocks)
336 (frob tags lexenv-tags)
337 (frob type-restrictions lexenv-type-restrictions)
338 lambda cleanup policy
339 (frob options lexenv-options))))
341 ;;;; flow/DFO/component hackery
343 ;;; Join BLOCK1 and BLOCK2.
344 #!-sb-fluid (declaim (inline link-blocks))
345 (defun link-blocks (block1 block2)
346 (declare (type cblock block1 block2))
347 (setf (block-succ block1)
348 (if (block-succ block1)
349 (%link-blocks block1 block2)
351 (push block1 (block-pred block2))
353 (defun %link-blocks (block1 block2)
354 (declare (type cblock block1 block2) (inline member))
355 (let ((succ1 (block-succ block1)))
356 (aver (not (member block2 succ1 :test #'eq)))
357 (cons block2 succ1)))
359 ;;; Like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If this leaves a
360 ;;; successor with a single predecessor that ends in an IF, then set
361 ;;; BLOCK-TEST-MODIFIED so that any test constraint will now be able to be
362 ;;; propagated to the successor.
363 (defun unlink-blocks (block1 block2)
364 (declare (type cblock block1 block2))
365 (let ((succ1 (block-succ block1)))
366 (if (eq block2 (car succ1))
367 (setf (block-succ block1) (cdr succ1))
368 (do ((succ (cdr succ1) (cdr succ))
370 ((eq (car succ) block2)
371 (setf (cdr prev) (cdr succ)))
374 (let ((new-pred (delq block1 (block-pred block2))))
375 (setf (block-pred block2) new-pred)
376 (when (and new-pred (null (rest new-pred)))
377 (let ((pred-block (first new-pred)))
378 (when (if-p (block-last pred-block))
379 (setf (block-test-modified pred-block) t)))))
382 ;;; Swing the succ/pred link between Block and Old to be between Block and
383 ;;; New. If Block ends in an IF, then we have to fix up the
384 ;;; consequent/alternative blocks to point to New. We also set
385 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to the new
387 (defun change-block-successor (block old new)
388 (declare (type cblock new old block) (inline member))
389 (unlink-blocks block old)
390 (let ((last (block-last block))
391 (comp (block-component block)))
392 (setf (component-reanalyze comp) t)
395 (setf (block-test-modified block) t)
396 (let* ((succ-left (block-succ block))
397 (new (if (and (eq new (component-tail comp))
401 (unless (member new succ-left :test #'eq)
402 (link-blocks block new))
403 (macrolet ((frob (slot)
404 `(when (eq (,slot last) old)
405 (setf (,slot last) new))))
407 (frob if-alternative))))
409 (unless (member new (block-succ block) :test #'eq)
410 (link-blocks block new)))))
414 ;;; Unlink a block from the next/prev chain. We also null out the
416 (declaim (ftype (function (cblock) (values)) remove-from-dfo))
417 #!-sb-fluid (declaim (inline 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 #!-sb-fluid (declaim (inline add-to-dfo))
429 (defun add-to-dfo (block after)
430 (declare (type cblock block after))
431 (let ((next (block-next after))
432 (comp (block-component after)))
433 (aver (not (eq (component-kind comp) :deleted)))
434 (setf (block-component block) comp)
435 (setf (block-next after) block)
436 (setf (block-prev block) after)
437 (setf (block-next block) next)
438 (setf (block-prev next) block))
441 ;;; Set the Flag for all the blocks in Component to NIL, except for the head
442 ;;; and tail which are set to T.
443 (declaim (ftype (function (component) (values)) clear-flags))
444 (defun clear-flags (component)
445 (let ((head (component-head component))
446 (tail (component-tail component)))
447 (setf (block-flag head) t)
448 (setf (block-flag tail) t)
449 (do-blocks (block component)
450 (setf (block-flag block) nil)))
453 ;;; Make a component with no blocks in it. The Block-Flag is initially
454 ;;; true in the head and tail blocks.
455 (declaim (ftype (function nil component) make-empty-component))
456 (defun make-empty-component ()
457 (let* ((head (make-block-key :start nil :component nil))
458 (tail (make-block-key :start nil :component nil))
459 (res (make-component :head head :tail tail)))
460 (setf (block-flag head) t)
461 (setf (block-flag tail) t)
462 (setf (block-component head) res)
463 (setf (block-component tail) res)
464 (setf (block-next head) tail)
465 (setf (block-prev tail) head)
468 ;;; Makes Node the Last node in its block, splitting the block if necessary.
469 ;;; The new block is added to the DFO immediately following Node's block.
470 (defun node-ends-block (node)
471 (declare (type node node))
472 (let* ((block (node-block node))
473 (start (node-cont node))
474 (last (block-last block))
475 (last-cont (node-cont last)))
476 (unless (eq last node)
477 (aver (and (eq (continuation-kind start) :inside-block)
478 (not (block-delete-p block))))
479 (let* ((succ (block-succ block))
481 (make-block-key :start start
482 :component (block-component block)
483 :start-uses (list (continuation-use start))
484 :succ succ :last last)))
485 (setf (continuation-kind start) :block-start)
488 (cons new-block (remove block (block-pred b)))))
489 (setf (block-succ block) ())
490 (setf (block-last block) node)
491 (link-blocks block new-block)
492 (add-to-dfo new-block block)
493 (setf (component-reanalyze (block-component block)) t)
495 (do ((cont start (node-cont (continuation-next cont))))
497 (when (eq (continuation-kind last-cont) :inside-block)
498 (setf (continuation-block last-cont) new-block)))
499 (setf (continuation-block cont) new-block))
501 (setf (block-type-asserted block) t)
502 (setf (block-test-modified block) t))))
508 ;;; Deal with deleting the last (read) reference to a lambda-var. We
509 ;;; iterate over all local calls flushing the corresponding argument, allowing
510 ;;; the computation of the argument to be deleted. We also mark the let for
511 ;;; reoptimization, since it may be that we have deleted the last variable.
513 ;;; The lambda-var may still have some sets, but this doesn't cause too much
514 ;;; difficulty, since we can efficiently implement write-only variables. We
515 ;;; iterate over the sets, marking their blocks for dead code flushing, since
516 ;;; 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 with
539 ;;; LET variables, marking the corresponding initial value arg as needing to be
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 ;;; This function deletes functions that have no references. This need only
557 ;;; be called 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 only
568 ;;; one way into a lambda, deleting the last reference to a lambda ensures that
569 ;;; there is no way to reach any of the code in it. So we just set the
570 ;;; Functional-Kind for Fun and its Lets to :Deleted, causing IR1 optimization
571 ;;; to delete blocks in that lambda.
573 ;;; If the function isn't a Let, we unlink the function head and tail from
574 ;;; the component head and tail to indicate that the code is unreachable. We
575 ;;; also delete the function from Component-Lambdas (it won't be there before
576 ;;; local call analysis, but no matter.) If the lambda was never referenced,
579 ;;; If the lambda is an XEP, then we null out the Entry-Function in its
580 ;;; Entry-Function so that people will know that it is not an entry point
582 (defun delete-lambda (leaf)
583 (declare (type clambda leaf))
584 (let ((kind (functional-kind leaf))
585 (bind (lambda-bind leaf)))
586 (aver (not (member kind '(:deleted :optional :top-level))))
587 (setf (functional-kind leaf) :deleted)
588 (setf (lambda-bind leaf) nil)
589 (dolist (let (lambda-lets leaf))
590 (setf (lambda-bind let) nil)
591 (setf (functional-kind let) :deleted))
593 (if (member kind '(:let :mv-let :assignment))
594 (let ((home (lambda-home leaf)))
595 (setf (lambda-lets home) (delete leaf (lambda-lets home))))
596 (let* ((bind-block (node-block bind))
597 (component (block-component bind-block))
598 (return (lambda-return leaf)))
599 (aver (null (leaf-refs leaf)))
600 (unless (leaf-ever-used leaf)
601 (let ((*compiler-error-context* bind))
602 (compiler-note "deleting unused function~:[.~;~:*~% ~S~]"
604 (unlink-blocks (component-head component) bind-block)
606 (unlink-blocks (node-block return) (component-tail component)))
607 (setf (component-reanalyze component) t)
608 (let ((tails (lambda-tail-set leaf)))
609 (setf (tail-set-functions tails)
610 (delete leaf (tail-set-functions tails)))
611 (setf (lambda-tail-set leaf) nil))
612 (setf (component-lambdas component)
613 (delete leaf (component-lambdas component)))))
615 (when (eq kind :external)
616 (let ((fun (functional-entry-function leaf)))
617 (setf (functional-entry-function fun) nil)
618 (when (optional-dispatch-p fun)
619 (delete-optional-dispatch fun)))))
623 ;;; Deal with deleting the last reference to an Optional-Dispatch. We have
624 ;;; to be a bit more careful than with lambdas, since Delete-Ref is used both
625 ;;; before and after local call analysis. Afterward, all references to
626 ;;; still-existing optional-dispatches have been moved to the XEP, leaving it
627 ;;; with no references at all. So we look at the XEP to see whether an
628 ;;; optional-dispatch is still really being used. But before local call
629 ;;; analysis, there are no XEPs, and all references are direct.
631 ;;; When we do delete the optional-dispatch, we grovel all of its
632 ;;; entry-points, making them be normal lambdas, and then deleting the ones
633 ;;; with no references. This deletes any e-p lambdas that were either never
634 ;;; referenced, or couldn't be deleted when the last deference was deleted (due
635 ;;; to their :OPTIONAL kind.)
637 ;;; Note that the last optional ep may alias the main entry, so when we process
638 ;;; the main entry, its kind may have been changed to NIL or even converted to
640 (defun delete-optional-dispatch (leaf)
641 (declare (type optional-dispatch leaf))
642 (let ((entry (functional-entry-function leaf)))
643 (unless (and entry (leaf-refs entry))
644 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
645 (setf (functional-kind leaf) :deleted)
648 (unless (eq (functional-kind fun) :deleted)
649 (aver (eq (functional-kind fun) :optional))
650 (setf (functional-kind fun) nil)
651 (let ((refs (leaf-refs fun)))
655 (or (maybe-let-convert fun)
656 (maybe-convert-to-assignment fun)))
658 (maybe-convert-to-assignment fun)))))))
660 (dolist (ep (optional-dispatch-entry-points leaf))
662 (when (optional-dispatch-more-entry leaf)
663 (frob (optional-dispatch-more-entry leaf)))
664 (let ((main (optional-dispatch-main-entry leaf)))
665 (when (eq (functional-kind main) :optional)
670 ;;; Do stuff to delete the semantic attachments of a Ref node. When this
671 ;;; leaves zero or one reference, we do a type dispatch off of the leaf to
672 ;;; determine if a special action is appropriate.
673 (defun delete-ref (ref)
674 (declare (type ref ref))
675 (let* ((leaf (ref-leaf ref))
676 (refs (delete ref (leaf-refs leaf))))
677 (setf (leaf-refs leaf) refs)
681 (lambda-var (delete-lambda-var leaf))
683 (ecase (functional-kind leaf)
684 ((nil :let :mv-let :assignment :escape :cleanup)
685 (aver (not (functional-entry-function leaf)))
686 (delete-lambda leaf))
688 (delete-lambda leaf))
689 ((:deleted :optional))))
691 (unless (eq (functional-kind leaf) :deleted)
692 (delete-optional-dispatch leaf)))))
695 (clambda (or (maybe-let-convert leaf)
696 (maybe-convert-to-assignment leaf)))
697 (lambda-var (reoptimize-lambda-var leaf))))
700 (clambda (maybe-convert-to-assignment leaf))))))
704 ;;; This function is called by people who delete nodes; it provides a way to
705 ;;; indicate that the value of a continuation is no longer used. We null out
706 ;;; the Continuation-Dest, set Flush-P in the blocks containing uses of Cont
707 ;;; and set Component-Reoptimize. If the Prev of the use is deleted, then we
708 ;;; blow off reoptimization.
710 ;;; If the continuation is :Deleted, then we don't do anything, since all
711 ;;; semantics have already been flushed. :Deleted-Block-Start start
712 ;;; continuations are treated just like :Block-Start; it is possible that the
713 ;;; continuation may be given a new dest (e.g. by SUBSTITUTE-CONTINUATION), so
714 ;;; we don't want to delete it.
715 (defun flush-dest (cont)
716 (declare (type continuation cont))
718 (unless (eq (continuation-kind cont) :deleted)
719 (aver (continuation-dest cont))
720 (setf (continuation-dest cont) nil)
722 (let ((prev (node-prev use)))
723 (unless (eq (continuation-kind prev) :deleted)
724 (let ((block (continuation-block prev)))
725 (setf (component-reoptimize (block-component block)) t)
726 (setf (block-attributep (block-flags block) flush-p type-asserted)
729 (setf (continuation-%type-check cont) nil)
733 ;;; Do a graph walk backward from Block, marking all predecessor blocks with
734 ;;; the DELETE-P flag.
735 (defun mark-for-deletion (block)
736 (declare (type cblock block))
737 (unless (block-delete-p block)
738 (setf (block-delete-p block) t)
739 (setf (component-reanalyze (block-component block)) t)
740 (dolist (pred (block-pred block))
741 (mark-for-deletion pred)))
744 ;;; Delete Cont, eliminating both control and value semantics. We set
745 ;;; FLUSH-P and COMPONENT-REOPTIMIZE similarly to in FLUSH-DEST. Here we must
746 ;;; get the component from the use block, since the continuation may be a
747 ;;; :DELETED-BLOCK-START.
749 ;;; If Cont has DEST, then it must be the case that the DEST is unreachable,
750 ;;; since we can't compute the value desired. In this case, we call
751 ;;; MARK-FOR-DELETION to cause the DEST block and its predecessors to tell
752 ;;; people to ignore them, and to cause them to be deleted eventually.
753 (defun delete-continuation (cont)
754 (declare (type continuation cont))
755 (aver (not (eq (continuation-kind cont) :deleted)))
758 (let ((prev (node-prev use)))
759 (unless (eq (continuation-kind prev) :deleted)
760 (let ((block (continuation-block prev)))
761 (setf (block-attributep (block-flags block) flush-p type-asserted) t)
762 (setf (component-reoptimize (block-component block)) t)))))
764 (let ((dest (continuation-dest cont)))
766 (let ((prev (node-prev dest)))
768 (not (eq (continuation-kind prev) :deleted)))
769 (let ((block (continuation-block prev)))
770 (unless (block-delete-p block)
771 (mark-for-deletion block)))))))
773 (setf (continuation-kind cont) :deleted)
774 (setf (continuation-dest cont) nil)
775 (setf (continuation-next cont) nil)
776 (setf (continuation-asserted-type cont) *empty-type*)
777 (setf (continuation-%derived-type cont) *empty-type*)
778 (setf (continuation-use cont) nil)
779 (setf (continuation-block cont) nil)
780 (setf (continuation-reoptimize cont) nil)
781 (setf (continuation-%type-check cont) nil)
782 (setf (continuation-info cont) nil)
786 ;;; This function does what is necessary to eliminate the code in it
787 ;;; from the IR1 representation. This involves unlinking it from its
788 ;;; predecessors and successors and deleting various node-specific
789 ;;; semantic information.
791 ;;; We mark the START as has having no next and remove the last node
792 ;;; from its CONT's uses. We also flush the DEST for all continuations
793 ;;; whose values are received by nodes in the block.
794 (defun delete-block (block)
795 (declare (type cblock block))
796 (aver (block-component block)) ; else block is already deleted!
797 (note-block-deletion block)
798 (setf (block-delete-p block) t)
800 (let* ((last (block-last block))
801 (cont (node-cont last)))
802 (delete-continuation-use last)
803 (if (eq (continuation-kind cont) :unused)
804 (delete-continuation cont)
805 (reoptimize-continuation cont)))
807 (dolist (b (block-pred block))
808 (unlink-blocks b block))
809 (dolist (b (block-succ block))
810 (unlink-blocks block b))
812 (do-nodes (node cont block)
814 (ref (delete-ref node))
816 (flush-dest (if-test node)))
817 ;; The next two cases serve to maintain the invariant that a LET always
818 ;; has a well-formed COMBINATION, REF and BIND. We delete the lambda
819 ;; whenever we delete any of these, but we must be careful that this LET
820 ;; has not already been partially deleted.
822 (when (and (eq (basic-combination-kind node) :local)
823 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
824 (continuation-use (basic-combination-fun node)))
825 (let ((fun (combination-lambda node)))
826 ;; If our REF was the 2'nd to last ref, and has been deleted, then
827 ;; Fun may be a LET for some other combination.
828 (when (and (member (functional-kind fun) '(:let :mv-let))
829 (eq (let-combination fun) node))
830 (delete-lambda fun))))
831 (flush-dest (basic-combination-fun node))
832 (dolist (arg (basic-combination-args node))
833 (when arg (flush-dest arg))))
835 (let ((lambda (bind-lambda node)))
836 (unless (eq (functional-kind lambda) :deleted)
837 (aver (member (functional-kind lambda) '(:let :mv-let :assignment)))
838 (delete-lambda lambda))))
840 (let ((value (exit-value node))
841 (entry (exit-entry node)))
845 (setf (entry-exits entry)
846 (delete node (entry-exits entry))))))
848 (flush-dest (return-result node))
849 (delete-return node))
851 (flush-dest (set-value node))
852 (let ((var (set-var node)))
853 (setf (basic-var-sets var)
854 (delete node (basic-var-sets var))))))
856 (delete-continuation (node-prev node)))
858 (remove-from-dfo block)
861 ;;; Do stuff to indicate that the return node Node is being deleted. We set
862 ;;; the RETURN to NIL.
863 (defun delete-return (node)
864 (declare (type creturn node))
865 (let ((fun (return-lambda node)))
866 (aver (lambda-return fun))
867 (setf (lambda-return fun) nil))
870 ;;; If any of the Vars in fun were never referenced and was not declared
871 ;;; IGNORE, then complain.
872 (defun note-unreferenced-vars (fun)
873 (declare (type clambda fun))
874 (dolist (var (lambda-vars fun))
875 (unless (or (leaf-ever-used var)
876 (lambda-var-ignorep var))
877 (let ((*compiler-error-context* (lambda-bind fun)))
878 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
879 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
880 ;; requires this to be a STYLE-WARNING.
881 (compiler-style-warning "The variable ~S is defined but never used."
883 (setf (leaf-ever-used var) t))))
886 (defvar *deletion-ignored-objects* '(t nil))
888 ;;; Return true if we can find Obj in Form, NIL otherwise. We bound our
889 ;;; recursion so that we don't get lost in circular structures. We ignore the
890 ;;; car of forms if they are a symbol (to prevent confusing function
891 ;;; referencess with variables), and we also ignore anything inside ' or #'.
892 (defun present-in-form (obj form depth)
893 (declare (type (integer 0 20) depth))
894 (cond ((= depth 20) nil)
898 (let ((first (car form))
900 (if (member first '(quote function))
902 (or (and (not (symbolp first))
903 (present-in-form obj first depth))
904 (do ((l (cdr form) (cdr l))
906 ((or (atom l) (> n 100))
909 (when (present-in-form obj (car l) depth)
912 ;;; This function is called on a block immediately before we delete it. We
913 ;;; check to see whether any of the code about to die appeared in the original
914 ;;; source, and emit a note if so.
916 ;;; If the block was in a lambda is now deleted, then we ignore the whole
917 ;;; block, since this case is picked off in DELETE-LAMBDA. We also ignore
918 ;;; the deletion of CRETURN nodes, since it is somewhat reasonable for a
919 ;;; function to not return, and there is a different note for that case anyway.
921 ;;; If the actual source is an atom, then we use a bunch of heuristics to
922 ;;; guess whether this reference really appeared in the original source:
923 ;;; -- If a symbol, it must be interned and not a keyword.
924 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum or a
926 ;;; -- The atom must be "present" in the original source form, and present in
927 ;;; all intervening actual source forms.
928 (defun note-block-deletion (block)
929 (let ((home (block-home-lambda block)))
930 (unless (eq (functional-kind home) :deleted)
931 (do-nodes (node cont block)
932 (let* ((path (node-source-path node))
933 (first (first path)))
934 (when (or (eq first 'original-source-start)
936 (or (not (symbolp first))
937 (let ((pkg (symbol-package first)))
939 (not (eq pkg (symbol-package :end))))))
940 (not (member first *deletion-ignored-objects*))
941 (not (typep first '(or fixnum character)))
943 (present-in-form first x 0))
944 (source-path-forms path))
945 (present-in-form first (find-original-source path)
947 (unless (return-p node)
948 (let ((*compiler-error-context* node))
949 (compiler-note "deleting unreachable code")))
953 ;;; Delete a node from a block, deleting the block if there are no nodes
954 ;;; left. We remove the node from the uses of its CONT, but we don't deal with
955 ;;; cleaning up any type-specific semantic attachments. If the CONT is :UNUSED
956 ;;; after deleting this use, then we delete CONT. (Note :UNUSED is not the
957 ;;; same as no uses. A continuation will only become :UNUSED if it was
958 ;;; :INSIDE-BLOCK before.)
960 ;;; If the node is the last node, there must be exactly one successor. We
961 ;;; link all of our precedessors to the successor and unlink the block. In
962 ;;; this case, we return T, otherwise NIL. If no nodes are left, and the block
963 ;;; is a successor of itself, then we replace the only node with a degenerate
964 ;;; exit node. This provides a way to represent the bodyless infinite loop,
965 ;;; given the prohibition on empty blocks in IR1.
966 (defun unlink-node (node)
967 (declare (type node node))
968 (let* ((cont (node-cont node))
969 (next (continuation-next cont))
970 (prev (node-prev node))
971 (block (continuation-block prev))
972 (prev-kind (continuation-kind prev))
973 (last (block-last block)))
975 (unless (eq (continuation-kind cont) :deleted)
976 (delete-continuation-use node)
977 (when (eq (continuation-kind cont) :unused)
978 (aver (not (continuation-dest cont)))
979 (delete-continuation cont)))
981 (setf (block-type-asserted block) t)
982 (setf (block-test-modified block) t)
984 (cond ((or (eq prev-kind :inside-block)
985 (and (eq prev-kind :block-start)
986 (not (eq node last))))
987 (cond ((eq node last)
988 (setf (block-last block) (continuation-use prev))
989 (setf (continuation-next prev) nil))
991 (setf (continuation-next prev) next)
992 (setf (node-prev next) prev)))
993 (setf (node-prev node) nil)
996 (aver (eq prev-kind :block-start))
997 (aver (eq node last))
998 (let* ((succ (block-succ block))
1000 (aver (and succ (null (cdr succ))))
1002 ((member block succ)
1003 (with-ir1-environment node
1004 (let ((exit (make-exit))
1005 (dummy (make-continuation)))
1006 (setf (continuation-next prev) nil)
1007 (prev-link exit prev)
1008 (add-continuation-use exit dummy)
1009 (setf (block-last block) exit)))
1010 (setf (node-prev node) nil)
1013 (aver (eq (block-start-cleanup block)
1014 (block-end-cleanup block)))
1015 (unlink-blocks block next)
1016 (dolist (pred (block-pred block))
1017 (change-block-successor pred block next))
1018 (remove-from-dfo block)
1019 (cond ((continuation-dest prev)
1020 (setf (continuation-next prev) nil)
1021 (setf (continuation-kind prev) :deleted-block-start))
1023 (delete-continuation prev)))
1024 (setf (node-prev node) nil)
1027 ;;; Return true if NODE has been deleted, false if it is still a valid part
1029 (defun node-deleted (node)
1030 (declare (type node node))
1031 (let ((prev (node-prev node)))
1033 (not (eq (continuation-kind prev) :deleted))
1034 (let ((block (continuation-block prev)))
1035 (and (block-component block)
1036 (not (block-delete-p block))))))))
1038 ;;; Delete all the blocks and functions in Component. We scan first marking
1039 ;;; the blocks as delete-p to prevent weird stuff from being triggered by
1041 (defun delete-component (component)
1042 (declare (type component component))
1043 (aver (null (component-new-functions component)))
1044 (setf (component-kind component) :deleted)
1045 (do-blocks (block component)
1046 (setf (block-delete-p block) t))
1047 (dolist (fun (component-lambdas component))
1048 (setf (functional-kind fun) nil)
1049 (setf (functional-entry-function fun) nil)
1050 (setf (leaf-refs fun) nil)
1051 (delete-functional fun))
1052 (do-blocks (block component)
1053 (delete-block block))
1056 ;;; Convert code of the form
1057 ;;; (FOO ... (FUN ...) ...)
1059 ;;; (FOO ... ... ...).
1060 ;;; In other words, replace the function combination FUN by its
1061 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1062 ;;; to blow out of whatever transform called this. Note, as the number
1063 ;;; of arguments changes, the transform must be prepared to return a
1064 ;;; lambda with a new lambda-list with the correct number of
1066 (defun extract-function-args (cont fun num-args)
1068 "If CONT is a call to FUN with NUM-ARGS args, change those arguments
1069 to feed directly to the continuation-dest of CONT, which must be
1071 (declare (type continuation cont)
1073 (type index num-args))
1074 (let ((outside (continuation-dest cont))
1075 (inside (continuation-use cont)))
1076 (aver (combination-p outside))
1077 (unless (combination-p inside)
1078 (give-up-ir1-transform))
1079 (let ((inside-fun (combination-fun inside)))
1080 (unless (eq (continuation-function-name inside-fun) fun)
1081 (give-up-ir1-transform))
1082 (let ((inside-args (combination-args inside)))
1083 (unless (= (length inside-args) num-args)
1084 (give-up-ir1-transform))
1085 (let* ((outside-args (combination-args outside))
1086 (arg-position (position cont outside-args))
1087 (before-args (subseq outside-args 0 arg-position))
1088 (after-args (subseq outside-args (1+ arg-position))))
1089 (dolist (arg inside-args)
1090 (setf (continuation-dest arg) outside))
1091 (setf (combination-args inside) nil)
1092 (setf (combination-args outside)
1093 (append before-args inside-args after-args))
1094 (change-ref-leaf (continuation-use inside-fun)
1095 (find-free-function 'list "???"))
1096 (setf (combination-kind inside) :full)
1097 (setf (node-derived-type inside) *wild-type*)
1099 (setf (continuation-asserted-type cont) *wild-type*)
1104 ;;; Change the Leaf that a Ref refers to.
1105 (defun change-ref-leaf (ref leaf)
1106 (declare (type ref ref) (type leaf leaf))
1107 (unless (eq (ref-leaf ref) leaf)
1108 (push ref (leaf-refs leaf))
1110 (setf (ref-leaf ref) leaf)
1111 (let ((ltype (leaf-type leaf)))
1112 (if (function-type-p ltype)
1113 (setf (node-derived-type ref) ltype)
1114 (derive-node-type ref ltype)))
1115 (reoptimize-continuation (node-cont ref)))
1118 ;;; Change all Refs for Old-Leaf to New-Leaf.
1119 (defun substitute-leaf (new-leaf old-leaf)
1120 (declare (type leaf new-leaf old-leaf))
1121 (dolist (ref (leaf-refs old-leaf))
1122 (change-ref-leaf ref new-leaf))
1125 ;;; Like SUBSITIUTE-LEAF, only there is a predicate on the Ref to tell
1126 ;;; whether to substitute.
1127 (defun substitute-leaf-if (test new-leaf old-leaf)
1128 (declare (type leaf new-leaf old-leaf) (type function test))
1129 (dolist (ref (leaf-refs old-leaf))
1130 (when (funcall test ref)
1131 (change-ref-leaf ref new-leaf)))
1134 ;;; Return a LEAF which represents the specified constant object. If the
1135 ;;; object is not in *CONSTANTS*, then we create a new constant LEAF and
1137 #!-sb-fluid (declaim (maybe-inline find-constant))
1138 (defun find-constant (object)
1139 (if (typep object '(or symbol number character instance))
1140 (or (gethash object *constants*)
1141 (setf (gethash object *constants*)
1142 (make-constant :value object
1144 :type (ctype-of object)
1145 :where-from :defined)))
1146 (make-constant :value object
1148 :type (ctype-of object)
1149 :where-from :defined)))
1151 ;;; If there is a non-local exit noted in Entry's environment that exits to
1152 ;;; Cont in that entry, then return it, otherwise return NIL.
1153 (defun find-nlx-info (entry cont)
1154 (declare (type entry entry) (type continuation cont))
1155 (let ((entry-cleanup (entry-cleanup entry)))
1156 (dolist (nlx (environment-nlx-info (node-environment entry)) nil)
1157 (when (and (eq (nlx-info-continuation nlx) cont)
1158 (eq (nlx-info-cleanup nlx) entry-cleanup))
1161 ;;;; functional hackery
1163 ;;; If Functional is a Lambda, just return it; if it is an
1164 ;;; optional-dispatch, return the main-entry.
1165 (declaim (ftype (function (functional) clambda) main-entry))
1166 (defun main-entry (functional)
1167 (etypecase functional
1168 (clambda functional)
1170 (optional-dispatch-main-entry functional))))
1172 ;;; Returns true if Functional is a thing that can be treated like
1173 ;;; MV-Bind when it appears in an MV-Call. All fixed arguments must be
1174 ;;; optional with null default and no supplied-p. There must be a rest
1175 ;;; arg with no references.
1176 (declaim (ftype (function (functional) boolean) looks-like-an-mv-bind))
1177 (defun looks-like-an-mv-bind (functional)
1178 (and (optional-dispatch-p functional)
1179 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1181 (let ((info (lambda-var-arg-info (car arg))))
1182 (unless info (return nil))
1183 (case (arg-info-kind info)
1185 (when (or (arg-info-supplied-p info) (arg-info-default info))
1188 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1192 ;;; Return true if function is an XEP. This is true of normal XEPs
1193 ;;; (:External kind) and top-level lambdas (:Top-Level kind.)
1194 #!-sb-fluid (declaim (inline external-entry-point-p))
1195 (defun external-entry-point-p (fun)
1196 (declare (type functional fun))
1197 (not (null (member (functional-kind fun) '(:external :top-level)))))
1199 ;;; If Cont's only use is a non-notinline global function reference, then
1200 ;;; return the referenced symbol, otherwise NIL. If Notinline-OK is true, then
1201 ;;; we don't care if the leaf is notinline.
1202 (defun continuation-function-name (cont &optional notinline-ok)
1203 (declare (type continuation cont))
1204 (let ((use (continuation-use cont)))
1206 (let ((leaf (ref-leaf use)))
1207 (if (and (global-var-p leaf)
1208 (eq (global-var-kind leaf) :global-function)
1209 (or (not (defined-function-p leaf))
1210 (not (eq (defined-function-inlinep leaf) :notinline))
1216 ;;; Return the COMBINATION node that is the call to the let Fun.
1217 (defun let-combination (fun)
1218 (declare (type clambda fun))
1219 (aver (member (functional-kind fun) '(:let :mv-let)))
1220 (continuation-dest (node-cont (first (leaf-refs fun)))))
1222 ;;; Return the initial value continuation for a let variable or NIL if none.
1223 (defun let-var-initial-value (var)
1224 (declare (type lambda-var var))
1225 (let ((fun (lambda-var-home var)))
1226 (elt (combination-args (let-combination fun))
1227 (position-or-lose var (lambda-vars fun)))))
1229 ;;; Return the LAMBDA that is called by the local Call.
1230 #!-sb-fluid (declaim (inline combination-lambda))
1231 (defun combination-lambda (call)
1232 (declare (type basic-combination call))
1233 (aver (eq (basic-combination-kind call) :local))
1234 (ref-leaf (continuation-use (basic-combination-fun call))))
1236 (defvar *inline-expansion-limit* 200
1238 "An upper limit on the number of inline function calls that will be expanded
1239 in any given code object (single function or block compilation.)")
1241 ;;; Check whether Node's component has exceeded its inline expansion
1242 ;;; limit, and warn if so, returning NIL.
1243 (defun inline-expansion-ok (node)
1244 (let ((expanded (incf (component-inline-expansions
1246 (node-block node))))))
1247 (cond ((> expanded *inline-expansion-limit*) nil)
1248 ((= expanded *inline-expansion-limit*)
1249 (let ((*compiler-error-context* node))
1250 (compiler-note "*INLINE-EXPANSION-LIMIT* (~D) was exceeded, ~
1251 probably trying to~% ~
1252 inline a recursive function."
1253 *inline-expansion-limit*))
1257 ;;;; compiler error context determination
1259 (declaim (special *current-path*))
1261 ;;; We bind print level and length when printing out messages so that
1262 ;;; we don't dump huge amounts of garbage.
1264 ;;; FIXME: It's not possible to get the defaults right for everyone.
1265 ;;; So: Should these variables be in the SB-EXT package? Or should we
1266 ;;; just get rid of them completely and just use the bare
1267 ;;; CL:*PRINT-FOO* variables instead?
1268 (declaim (type (or unsigned-byte null)
1269 *compiler-error-print-level*
1270 *compiler-error-print-length*
1271 *compiler-error-print-lines*))
1272 (defvar *compiler-error-print-level* 5
1274 "the value for *PRINT-LEVEL* when printing compiler error messages")
1275 (defvar *compiler-error-print-length* 10
1277 "the value for *PRINT-LENGTH* when printing compiler error messages")
1278 (defvar *compiler-error-print-lines* 12
1280 "the value for *PRINT-LINES* when printing compiler error messages")
1282 (defvar *enclosing-source-cutoff* 1
1284 "The maximum number of enclosing non-original source forms (i.e. from
1285 macroexpansion) that we print in full. For additional enclosing forms, we
1286 print only the CAR.")
1287 (declaim (type unsigned-byte *enclosing-source-cutoff*))
1289 ;;; We separate the determination of compiler error contexts from the actual
1290 ;;; signalling of those errors by objectifying the error context. This allows
1291 ;;; postponement of the determination of how (and if) to signal the error.
1293 ;;; We take care not to reference any of the IR1 so that pending potential
1294 ;;; error messages won't prevent the IR1 from being GC'd. To this end, we
1295 ;;; convert source forms to strings so that source forms that contain IR1
1296 ;;; references (e.g. %DEFUN) don't hold onto the IR.
1297 (defstruct (compiler-error-context
1298 #-no-ansi-print-object
1299 (:print-object (lambda (x stream)
1300 (print-unreadable-object (x stream :type t))))
1302 ;; A list of the stringified CARs of the enclosing non-original source forms
1303 ;; exceeding the *enclosing-source-cutoff*.
1304 (enclosing-source nil :type list)
1305 ;; A list of stringified enclosing non-original source forms.
1306 (source nil :type list)
1307 ;; The stringified form in the original source that expanded into Source.
1308 (original-source (required-argument) :type simple-string)
1309 ;; A list of prefixes of "interesting" forms that enclose original-source.
1310 (context nil :type list)
1311 ;; The FILE-INFO-NAME for the relevant FILE-INFO.
1312 (file-name (required-argument)
1313 :type (or pathname (member :lisp :stream)))
1314 ;; The file position at which the top-level form starts, if applicable.
1315 (file-position nil :type (or index null))
1316 ;; The original source part of the source path.
1317 (original-source-path nil :type list))
1319 ;;; If true, this is the node which is used as context in compiler warning
1321 (declaim (type (or null compiler-error-context node) *compiler-error-context*))
1322 (defvar *compiler-error-context* nil)
1324 ;;; a hashtable mapping macro names to source context parsers. Each parser
1325 ;;; function returns the source-context list for that form.
1326 (defvar *source-context-methods* (make-hash-table))
1328 ;;; documentation originally from cmu-user.tex:
1329 ;;; This macro defines how to extract an abbreviated source context from
1330 ;;; the \var{name}d form when it appears in the compiler input.
1331 ;;; \var{lambda-list} is a \code{defmacro} style lambda-list used to
1332 ;;; parse the arguments. The \var{body} should return a list of
1333 ;;; subforms that can be printed on about one line. There are
1334 ;;; predefined methods for \code{defstruct}, \code{defmethod}, etc. If
1335 ;;; no method is defined, then the first two subforms are returned.
1336 ;;; Note that this facility implicitly determines the string name
1337 ;;; associated with anonymous functions.
1338 ;;; So even though SBCL itself only uses this macro within this file, it's a
1339 ;;; reasonable thing to put in SB-EXT in case some dedicated user wants to do
1340 ;;; some heavy tweaking to make SBCL give more informative output about his
1342 (defmacro def-source-context (name lambda-list &body body)
1344 "DEF-SOURCE-CONTEXT Name Lambda-List Form*
1345 This macro defines how to extract an abbreviated source context from the
1346 Named form when it appears in the compiler input. Lambda-List is a DEFMACRO
1347 style lambda-list used to parse the arguments. The Body should return a
1348 list of subforms suitable for a \"~{~S ~}\" format string."
1349 (let ((n-whole (gensym)))
1350 `(setf (gethash ',name *source-context-methods*)
1351 #'(lambda (,n-whole)
1352 (destructuring-bind ,lambda-list ,n-whole ,@body)))))
1354 (def-source-context defstruct (name-or-options &rest slots)
1355 (declare (ignore slots))
1356 `(defstruct ,(if (consp name-or-options)
1357 (car name-or-options)
1360 (def-source-context function (thing)
1361 (if (and (consp thing) (eq (first thing) 'lambda) (consp (rest thing)))
1362 `(lambda ,(second thing))
1363 `(function ,thing)))
1365 ;;; Return the first two elements of FORM if FORM is a list. Take the
1366 ;;; CAR of the second form if appropriate.
1367 (defun source-form-context (form)
1368 (cond ((atom form) nil)
1369 ((>= (length form) 2)
1370 (funcall (gethash (first form) *source-context-methods*
1372 (declare (ignore x))
1373 (list (first form) (second form))))
1378 ;;; Given a source path, return the original source form and a description
1379 ;;; of the interesting aspects of the context in which it appeared. The
1380 ;;; context is a list of lists, one sublist per context form. The sublist is a
1381 ;;; list of some of the initial subforms of the context form.
1383 ;;; For now, we use the first two subforms of each interesting form. A form is
1384 ;;; interesting if the first element is a symbol beginning with "DEF" and it is
1385 ;;; not the source form. If there is no DEF-mumble, then we use the outermost
1386 ;;; containing form. If the second subform is a list, then in some cases we
1387 ;;; return the car of that form rather than the whole form (i.e. don't show
1388 ;;; defstruct options, etc.)
1389 (defun find-original-source (path)
1390 (declare (list path))
1391 (let* ((rpath (reverse (source-path-original-source path)))
1393 (root (find-source-root tlf *source-info*)))
1394 (collect ((context))
1396 (current (rest rpath)))
1399 (aver (null current))
1401 (let ((head (first form)))
1402 (when (symbolp head)
1403 (let ((name (symbol-name head)))
1404 (when (and (>= (length name) 3) (string= name "DEF" :end1 3))
1405 (context (source-form-context form))))))
1406 (when (null current) (return))
1407 (setq form (nth (pop current) form)))
1410 (values form (context)))
1412 (let ((c (source-form-context root)))
1413 (values form (if c (list c) nil))))
1415 (values '(unable to locate source)
1416 '((some strange place)))))))))
1418 ;;; Convert a source form to a string, suitably formatted for use in
1419 ;;; compiler warnings.
1420 (defun stringify-form (form &optional (pretty t))
1421 (let ((*print-level* *compiler-error-print-level*)
1422 (*print-length* *compiler-error-print-length*)
1423 (*print-lines* *compiler-error-print-lines*)
1424 (*print-pretty* pretty))
1426 (format nil "~<~@; ~S~:>" (list form))
1427 (prin1-to-string form))))
1429 ;;; Return a COMPILER-ERROR-CONTEXT structure describing the current
1430 ;;; error context, or NIL if we can't figure anything out. ARGS is a
1431 ;;; list of things that are going to be printed out in the error
1432 ;;; message, and can thus be blown off when they appear in the source
1434 (defun find-error-context (args)
1435 (let ((context *compiler-error-context*))
1436 (if (compiler-error-context-p context)
1438 (let ((path (or *current-path*
1440 (node-source-path context)
1442 (when (and *source-info* path)
1443 (multiple-value-bind (form src-context) (find-original-source path)
1444 (collect ((full nil cons)
1446 (let ((forms (source-path-forms path))
1448 (dolist (src (if (member (first forms) args)
1451 (if (>= n *enclosing-source-cutoff*)
1452 (short (stringify-form (if (consp src)
1456 (full (stringify-form src)))
1459 (let* ((tlf (source-path-tlf-number path))
1460 (file-info (source-info-file-info *source-info*)))
1461 (make-compiler-error-context
1462 :enclosing-source (short)
1464 :original-source (stringify-form form)
1465 :context src-context
1466 :file-name (file-info-name file-info)
1468 (multiple-value-bind (ignore pos)
1469 (find-source-root tlf *source-info*)
1470 (declare (ignore ignore))
1472 :original-source-path
1473 (source-path-original-source path))))))))))
1475 ;;;; printing error messages
1477 ;;; We save the context information that we printed out most recently
1478 ;;; so that we don't print it out redundantly.
1480 ;;; The last COMPILER-ERROR-CONTEXT that we printed.
1481 (defvar *last-error-context* nil)
1482 (declaim (type (or compiler-error-context null) *last-error-context*))
1484 ;;; The format string and args for the last error we printed.
1485 (defvar *last-format-string* nil)
1486 (defvar *last-format-args* nil)
1487 (declaim (type (or string null) *last-format-string*))
1488 (declaim (type list *last-format-args*))
1490 ;;; The number of times that the last error message has been emitted,
1491 ;;; so that we can compress duplicate error messages.
1492 (defvar *last-message-count* 0)
1493 (declaim (type index *last-message-count*))
1495 ;;; If the last message was given more than once, then print out an
1496 ;;; indication of how many times it was repeated. We reset the message count
1497 ;;; when we are done.
1498 (defun note-message-repeats (&optional (terpri t))
1499 (cond ((= *last-message-count* 1)
1500 (when terpri (terpri *error-output*)))
1501 ((> *last-message-count* 1)
1502 (format *error-output* "~&; [Last message occurs ~D times.]~2%"
1503 *last-message-count*)))
1504 (setq *last-message-count* 0))
1506 ;;; Print out the message, with appropriate context if we can find it.
1507 ;;; If the context is different from the context of the last message
1508 ;;; we printed, then we print the context. If the original source is
1509 ;;; different from the source we are working on, then we print the
1510 ;;; current source in addition to the original source.
1512 ;;; We suppress printing of messages identical to the previous, but
1513 ;;; record the number of times that the message is repeated.
1514 (defun print-compiler-message (format-string format-args)
1516 (declare (type simple-string format-string))
1517 (declare (type list format-args))
1519 (let ((stream *error-output*)
1520 (context (find-error-context format-args)))
1523 (let ((file (compiler-error-context-file-name context))
1524 (in (compiler-error-context-context context))
1525 (form (compiler-error-context-original-source context))
1526 (enclosing (compiler-error-context-enclosing-source context))
1527 (source (compiler-error-context-source context))
1528 (last *last-error-context*))
1531 (equal file (compiler-error-context-file-name last)))
1532 (when (pathnamep file)
1533 (note-message-repeats)
1535 (format stream "~2&; file: ~A~%" (namestring file))))
1538 (equal in (compiler-error-context-context last)))
1539 (note-message-repeats)
1541 (format stream "~&")
1542 (pprint-logical-block (stream nil :per-line-prefix "; ")
1543 (format stream "in:~{~<~% ~4:;~{ ~S~}~>~^ =>~}" in))
1544 (format stream "~%"))
1549 (compiler-error-context-original-source last)))
1550 (note-message-repeats)
1552 (format stream "~&")
1553 (pprint-logical-block (stream nil :per-line-prefix "; ")
1554 (format stream " ~A" form))
1555 (format stream "~&"))
1559 (compiler-error-context-enclosing-source last)))
1561 (note-message-repeats)
1563 (format stream "~&; --> ~{~<~%; --> ~1:;~A~> ~}~%" enclosing)))
1566 (equal source (compiler-error-context-source last)))
1567 (setq *last-format-string* nil)
1569 (note-message-repeats)
1570 (dolist (src source)
1571 (format stream "~&")
1572 (write-string "; ==>" stream)
1573 (format stream "~&")
1574 (pprint-logical-block (stream nil :per-line-prefix "; ")
1575 (write-string src stream)))))))
1577 (format stream "~&")
1578 (note-message-repeats)
1579 (setq *last-format-string* nil)
1580 (format stream "~&")))
1582 (setq *last-error-context* context)
1584 (unless (and (equal format-string *last-format-string*)
1585 (tree-equal format-args *last-format-args*))
1586 (note-message-repeats nil)
1587 (setq *last-format-string* format-string)
1588 (setq *last-format-args* format-args)
1589 (let ((*print-level* *compiler-error-print-level*)
1590 (*print-length* *compiler-error-print-length*)
1591 (*print-lines* *compiler-error-print-lines*))
1592 (format stream "~&")
1593 (pprint-logical-block (stream nil :per-line-prefix "; ")
1594 (format stream "~&~?" format-string format-args))
1595 (format stream "~&"))))
1597 (incf *last-message-count*)
1600 (defun print-compiler-condition (condition)
1601 (declare (type condition condition))
1602 (let (;; These different classes of conditions have different
1603 ;; effects on the return codes of COMPILE-FILE, so it's nice
1604 ;; for users to be able to pick them out by lexical search
1605 ;; through the output.
1606 (what (etypecase condition
1607 (style-warning 'style-warning)
1610 (multiple-value-bind (format-string format-args)
1611 (if (typep condition 'simple-condition)
1612 (values (simple-condition-format-control condition)
1613 (simple-condition-format-arguments condition))
1615 (list (with-output-to-string (s)
1616 (princ condition s)))))
1617 (print-compiler-message (format nil
1624 ;;; COMPILER-NOTE is vaguely like COMPILER-ERROR and the other
1625 ;;; condition-signalling functions, but it just writes some output instead of
1626 ;;; signalling. (In CMU CL, it did signal a condition, but this didn't seem to
1627 ;;; work all that well; it was weird to have COMPILE-FILE return with
1628 ;;; WARNINGS-P set when the only problem was that the compiler couldn't figure
1629 ;;; out how to compile something as efficiently as it liked.)
1630 (defun compiler-note (format-string &rest format-args)
1631 (unless (if *compiler-error-context*
1632 (policy *compiler-error-context* (= inhibit-warnings 3))
1633 (policy *lexenv* (= inhibit-warnings 3)))
1634 (incf *compiler-note-count*)
1635 (print-compiler-message (format nil "note: ~A" format-string)
1639 ;;; Issue a note when we might or might not be in the compiler.
1640 (defun maybe-compiler-note (&rest rest)
1641 (if (boundp '*lexenv*) ; if we're in the compiler
1642 (apply #'compiler-note rest)
1643 (let ((stream *error-output*))
1644 (pprint-logical-block (stream nil :per-line-prefix ";")
1646 (format stream " note: ~3I~_")
1647 (pprint-logical-block (stream nil)
1648 (apply #'format stream rest)))
1649 (fresh-line stream)))) ; (outside logical block, no per-line-prefix)
1651 ;;; The politically correct way to print out progress messages and
1652 ;;; such like. We clear the current error context so that we know that
1653 ;;; it needs to be reprinted, and we also Force-Output so that the
1654 ;;; message gets seen right away.
1655 (declaim (ftype (function (string &rest t) (values)) compiler-mumble))
1656 (defun compiler-mumble (format-string &rest format-args)
1657 (note-message-repeats)
1658 (setq *last-error-context* nil)
1659 (apply #'format *error-output* format-string format-args)
1660 (force-output *error-output*)
1663 ;;; Return a string that somehow names the code in COMPONENT. We use
1664 ;;; the source path for the bind node for an arbitrary entry point to
1665 ;;; find the source context, then return that as a string.
1666 (declaim (ftype (function (component) simple-string) find-component-name))
1667 (defun find-component-name (component)
1668 (let ((ep (first (block-succ (component-head component)))))
1669 (aver ep) ; else no entry points??
1670 (multiple-value-bind (form context)
1671 (find-original-source
1672 (node-source-path (continuation-next (block-start ep))))
1673 (declare (ignore form))
1674 (let ((*print-level* 2)
1675 (*print-pretty* nil))
1676 (format nil "~{~{~S~^ ~}~^ => ~}" context)))))
1678 ;;;; condition system interface
1680 ;;; Keep track of how many times each kind of condition happens.
1681 (defvar *compiler-error-count*)
1682 (defvar *compiler-warning-count*)
1683 (defvar *compiler-style-warning-count*)
1684 (defvar *compiler-note-count*)
1686 ;;; Keep track of whether any surrounding COMPILE or COMPILE-FILE call
1687 ;;; should return WARNINGS-P or FAILURE-P.
1688 (defvar *failure-p*)
1689 (defvar *warnings-p*)
1691 ;;; condition handlers established by the compiler. We re-signal the
1692 ;;; condition, then if it isn't handled, we increment our warning
1693 ;;; counter and print the error message.
1694 (defun compiler-error-handler (condition)
1696 (incf *compiler-error-count*)
1697 (setf *warnings-p* t
1699 (print-compiler-condition condition)
1700 (continue condition))
1701 (defun compiler-warning-handler (condition)
1703 (incf *compiler-warning-count*)
1704 (setf *warnings-p* t
1706 (print-compiler-condition condition)
1707 (muffle-warning condition))
1708 (defun compiler-style-warning-handler (condition)
1710 (incf *compiler-style-warning-count*)
1711 (setf *warnings-p* t)
1712 (print-compiler-condition condition)
1713 (muffle-warning condition))
1715 ;;;; undefined warnings
1717 (defvar *undefined-warning-limit* 3
1719 "If non-null, then an upper limit on the number of unknown function or type
1720 warnings that the compiler will print for any given name in a single
1721 compilation. This prevents excessive amounts of output when the real
1722 problem is a missing definition (as opposed to a typo in the use.)")
1724 ;;; Make an entry in the *UNDEFINED-WARNINGS* describing a reference
1725 ;;; to NAME of the specified KIND. If we have exceeded the warning
1726 ;;; limit, then just increment the count, otherwise note the current
1729 ;;; Undefined types are noted by a condition handler in
1730 ;;; WITH-COMPILATION-UNIT, which can potentially be invoked outside
1731 ;;; the compiler, hence the BOUNDP check.
1732 (defun note-undefined-reference (name kind)
1734 ;; Check for boundness so we don't blow up if we're called
1735 ;; when IR1 conversion isn't going on.
1737 ;; FIXME: I'm pretty sure the INHIBIT-WARNINGS test below
1738 ;; isn't a good idea; we should have INHIBIT-WARNINGS
1739 ;; affect compiler notes, not STYLE-WARNINGs. And I'm not
1740 ;; sure what the BOUNDP '*LEXENV* test above is for; it's
1741 ;; likely a good idea, but it probably deserves an
1742 ;; explanatory comment.
1743 (policy *lexenv* (= inhibit-warnings 3)))
1744 (let* ((found (dolist (warning *undefined-warnings* nil)
1745 (when (and (equal (undefined-warning-name warning) name)
1746 (eq (undefined-warning-kind warning) kind))
1749 (make-undefined-warning :name name :kind kind))))
1750 (unless found (push res *undefined-warnings*))
1751 (when (or (not *undefined-warning-limit*)
1752 (< (undefined-warning-count res) *undefined-warning-limit*))
1753 (push (find-error-context (list name))
1754 (undefined-warning-warnings res)))
1755 (incf (undefined-warning-count res))))
1760 ;;; Apply a function to some arguments, returning a list of the values
1761 ;;; resulting of the evaluation. If an error is signalled during the
1762 ;;; application, then we print a warning message and return NIL as our
1763 ;;; second value to indicate this. Node is used as the error context
1764 ;;; for any error message, and Context is a string that is spliced
1765 ;;; into the warning.
1766 (declaim (ftype (function ((or symbol function) list node string)
1767 (values list boolean))
1769 (defun careful-call (function args node context)
1771 (multiple-value-list
1772 (handler-case (apply function args)
1774 (let ((*compiler-error-context* node))
1775 (compiler-warning "Lisp error during ~A:~%~A" context condition)
1776 (return-from careful-call (values nil nil))))))
1779 ;;;; utilities used at run-time for parsing &KEY args in IR1
1781 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1782 ;;; the continuation for the value of the &KEY argument KEY in the
1783 ;;; list of continuations ARGS. It returns the continuation if the
1784 ;;; keyword is present, or NIL otherwise. The legality and
1785 ;;; constantness of the keywords should already have been checked.
1786 (declaim (ftype (function (list keyword) (or continuation null))
1787 find-keyword-continuation))
1788 (defun find-keyword-continuation (args key)
1789 (do ((arg args (cddr arg)))
1791 (when (eq (continuation-value (first arg)) key)
1792 (return (second arg)))))
1794 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1795 ;;; verify that alternating continuations in ARGS are constant and
1796 ;;; that there is an even number of args.
1797 (declaim (ftype (function (list) boolean) check-key-args-constant))
1798 (defun check-key-args-constant (args)
1799 (do ((arg args (cddr arg)))
1801 (unless (and (rest arg)
1802 (constant-continuation-p (first arg)))
1805 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1806 ;;; verify that the list of continuations ARGS is a well-formed &KEY
1807 ;;; arglist and that only keywords present in the list KEYS are
1809 (declaim (ftype (function (list list) boolean) check-transform-keys))
1810 (defun check-transform-keys (args keys)
1811 (and (check-key-args-constant args)
1812 (do ((arg args (cddr arg)))
1814 (unless (member (continuation-value (first arg)) keys)
1819 ;;; Called by the expansion of the EVENT macro.
1820 (declaim (ftype (function (event-info (or node null)) *) %event))
1821 (defun %event (info node)
1822 (incf (event-info-count info))
1823 (when (and (>= (event-info-level info) *event-note-threshold*)
1824 (policy (or node *lexenv*)
1825 (= inhibit-warnings 0)))
1826 (let ((*compiler-error-context* node))
1827 (compiler-note (event-info-description info))))
1829 (let ((action (event-info-action info)))
1830 (when action (funcall action node))))