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 (assert (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 (assert (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 (assert (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 of
277 ;;; its orignal 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 orignal source (with the
283 ;;; TLF 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 orignal source inside the Top Level
289 ;;; Form that contains it. This is determined by the order that we walk the
290 ;;; 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 source that
296 ;;; converted to get to this form, with the immediate source for node at the
297 ;;; 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, otherwise
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 slot
319 ;;; values. Values for the alist slots are NCONC'ed to the beginning of the
320 ;;; 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 (cookie (lexenv-cookie default))
327 (interface-cookie (lexenv-interface-cookie 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 cookie interface-cookie
340 (frob options lexenv-options))))
342 ;;; Return a cookie that defaults any unsupplied optimize qualities in the
343 ;;; Interface-Cookie with the corresponding ones from the Cookie.
344 (defun make-interface-cookie (lexenv)
345 (declare (type lexenv lexenv))
346 (let ((icookie (lexenv-interface-cookie lexenv))
347 (cookie (lexenv-cookie lexenv)))
349 :speed (or (cookie-speed icookie) (cookie-speed cookie))
350 :space (or (cookie-space icookie) (cookie-space cookie))
351 :safety (or (cookie-safety icookie) (cookie-safety cookie))
352 :cspeed (or (cookie-cspeed icookie) (cookie-cspeed cookie))
353 :brevity (or (cookie-brevity icookie) (cookie-brevity cookie))
354 :debug (or (cookie-debug icookie) (cookie-debug cookie)))))
356 ;;;; flow/DFO/component hackery
358 ;;; Join Block1 and Block2.
359 #!-sb-fluid (declaim (inline link-blocks))
360 (defun link-blocks (block1 block2)
361 (declare (type cblock block1 block2))
362 (setf (block-succ block1)
363 (if (block-succ block1)
364 (%link-blocks block1 block2)
366 (push block1 (block-pred block2))
368 (defun %link-blocks (block1 block2)
369 (declare (type cblock block1 block2) (inline member))
370 (let ((succ1 (block-succ block1)))
371 (assert (not (member block2 succ1 :test #'eq)))
372 (cons block2 succ1)))
374 ;;; Like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If this leaves a
375 ;;; successor with a single predecessor that ends in an IF, then set
376 ;;; BLOCK-TEST-MODIFIED so that any test constraint will now be able to be
377 ;;; propagated to the successor.
378 (defun unlink-blocks (block1 block2)
379 (declare (type cblock block1 block2))
380 (let ((succ1 (block-succ block1)))
381 (if (eq block2 (car succ1))
382 (setf (block-succ block1) (cdr succ1))
383 (do ((succ (cdr succ1) (cdr succ))
385 ((eq (car succ) block2)
386 (setf (cdr prev) (cdr succ)))
389 (let ((new-pred (delq block1 (block-pred block2))))
390 (setf (block-pred block2) new-pred)
391 (when (and new-pred (null (rest new-pred)))
392 (let ((pred-block (first new-pred)))
393 (when (if-p (block-last pred-block))
394 (setf (block-test-modified pred-block) t)))))
397 ;;; Swing the succ/pred link between Block and Old to be between Block and
398 ;;; New. If Block ends in an IF, then we have to fix up the
399 ;;; consequent/alternative blocks to point to New. We also set
400 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to the new
402 (defun change-block-successor (block old new)
403 (declare (type cblock new old block) (inline member))
404 (unlink-blocks block old)
405 (let ((last (block-last block))
406 (comp (block-component block)))
407 (setf (component-reanalyze comp) t)
410 (setf (block-test-modified block) t)
411 (let* ((succ-left (block-succ block))
412 (new (if (and (eq new (component-tail comp))
416 (unless (member new succ-left :test #'eq)
417 (link-blocks block new))
418 (macrolet ((frob (slot)
419 `(when (eq (,slot last) old)
420 (setf (,slot last) new))))
422 (frob if-alternative))))
424 (unless (member new (block-succ block) :test #'eq)
425 (link-blocks block new)))))
429 ;;; Unlink a block from the next/prev chain. We also null out the
431 (declaim (ftype (function (cblock) (values)) remove-from-dfo))
432 #!-sb-fluid (declaim (inline remove-from-dfo))
433 (defun remove-from-dfo (block)
434 (let ((next (block-next block))
435 (prev (block-prev block)))
436 (setf (block-component block) nil)
437 (setf (block-next prev) next)
438 (setf (block-prev next) prev))
441 ;;; Add Block to the next/prev chain following After. We also set the
442 ;;; Component to be the same as for After.
443 #!-sb-fluid (declaim (inline add-to-dfo))
444 (defun add-to-dfo (block after)
445 (declare (type cblock block after))
446 (let ((next (block-next after))
447 (comp (block-component after)))
448 (assert (not (eq (component-kind comp) :deleted)))
449 (setf (block-component block) comp)
450 (setf (block-next after) block)
451 (setf (block-prev block) after)
452 (setf (block-next block) next)
453 (setf (block-prev next) block))
456 ;;; Set the Flag for all the blocks in Component to NIL, except for the head
457 ;;; and tail which are set to T.
458 (declaim (ftype (function (component) (values)) clear-flags))
459 (defun clear-flags (component)
460 (let ((head (component-head component))
461 (tail (component-tail component)))
462 (setf (block-flag head) t)
463 (setf (block-flag tail) t)
464 (do-blocks (block component)
465 (setf (block-flag block) nil)))
468 ;;; Make a component with no blocks in it. The Block-Flag is initially
469 ;;; true in the head and tail blocks.
470 (declaim (ftype (function nil component) make-empty-component))
471 (defun make-empty-component ()
472 (let* ((head (make-block-key :start nil :component nil))
473 (tail (make-block-key :start nil :component nil))
474 (res (make-component :head head :tail tail)))
475 (setf (block-flag head) t)
476 (setf (block-flag tail) t)
477 (setf (block-component head) res)
478 (setf (block-component tail) res)
479 (setf (block-next head) tail)
480 (setf (block-prev tail) head)
483 ;;; Makes Node the Last node in its block, splitting the block if necessary.
484 ;;; The new block is added to the DFO immediately following Node's block.
485 (defun node-ends-block (node)
486 (declare (type node node))
487 (let* ((block (node-block node))
488 (start (node-cont node))
489 (last (block-last block))
490 (last-cont (node-cont last)))
491 (unless (eq last node)
492 (assert (and (eq (continuation-kind start) :inside-block)
493 (not (block-delete-p block))))
494 (let* ((succ (block-succ block))
496 (make-block-key :start start
497 :component (block-component block)
498 :start-uses (list (continuation-use start))
499 :succ succ :last last)))
500 (setf (continuation-kind start) :block-start)
503 (cons new-block (remove block (block-pred b)))))
504 (setf (block-succ block) ())
505 (setf (block-last block) node)
506 (link-blocks block new-block)
507 (add-to-dfo new-block block)
508 (setf (component-reanalyze (block-component block)) t)
510 (do ((cont start (node-cont (continuation-next cont))))
512 (when (eq (continuation-kind last-cont) :inside-block)
513 (setf (continuation-block last-cont) new-block)))
514 (setf (continuation-block cont) new-block))
516 (setf (block-type-asserted block) t)
517 (setf (block-test-modified block) t))))
523 ;;; Deal with deleting the last (read) reference to a lambda-var. We
524 ;;; iterate over all local calls flushing the corresponding argument, allowing
525 ;;; the computation of the argument to be deleted. We also mark the let for
526 ;;; reoptimization, since it may be that we have deleted the last variable.
528 ;;; The lambda-var may still have some sets, but this doesn't cause too much
529 ;;; difficulty, since we can efficiently implement write-only variables. We
530 ;;; iterate over the sets, marking their blocks for dead code flushing, since
531 ;;; we can delete sets whose value is unused.
532 (defun delete-lambda-var (leaf)
533 (declare (type lambda-var leaf))
534 (let* ((fun (lambda-var-home leaf))
535 (n (position leaf (lambda-vars fun))))
536 (dolist (ref (leaf-refs fun))
537 (let* ((cont (node-cont ref))
538 (dest (continuation-dest cont)))
539 (when (and (combination-p dest)
540 (eq (basic-combination-fun dest) cont)
541 (eq (basic-combination-kind dest) :local))
542 (let* ((args (basic-combination-args dest))
544 (reoptimize-continuation arg)
546 (setf (elt args n) nil))))))
548 (dolist (set (lambda-var-sets leaf))
549 (setf (block-flush-p (node-block set)) t))
553 ;;; Note that something interesting has happened to Var. We only deal with
554 ;;; LET variables, marking the corresponding initial value arg as needing to be
556 (defun reoptimize-lambda-var (var)
557 (declare (type lambda-var var))
558 (let ((fun (lambda-var-home var)))
559 (when (and (eq (functional-kind fun) :let)
561 (do ((args (basic-combination-args
564 (first (leaf-refs fun)))))
566 (vars (lambda-vars fun) (cdr vars)))
568 (reoptimize-continuation (car args))))))
571 ;;; This function deletes functions that have no references. This need only
572 ;;; be called on functions that never had any references, since otherwise
573 ;;; DELETE-REF will handle the deletion.
574 (defun delete-functional (fun)
575 (assert (and (null (leaf-refs fun))
576 (not (functional-entry-function fun))))
578 (optional-dispatch (delete-optional-dispatch fun))
579 (clambda (delete-lambda fun)))
582 ;;; Deal with deleting the last reference to a lambda. Since there is only
583 ;;; one way into a lambda, deleting the last reference to a lambda ensures that
584 ;;; there is no way to reach any of the code in it. So we just set the
585 ;;; Functional-Kind for Fun and its Lets to :Deleted, causing IR1 optimization
586 ;;; to delete blocks in that lambda.
588 ;;; If the function isn't a Let, we unlink the function head and tail from
589 ;;; the component head and tail to indicate that the code is unreachable. We
590 ;;; also delete the function from Component-Lambdas (it won't be there before
591 ;;; local call analysis, but no matter.) If the lambda was never referenced,
594 ;;; If the lambda is an XEP, then we null out the Entry-Function in its
595 ;;; Entry-Function so that people will know that it is not an entry point
597 (defun delete-lambda (leaf)
598 (declare (type clambda leaf))
599 (let ((kind (functional-kind leaf))
600 (bind (lambda-bind leaf)))
601 (assert (not (member kind '(:deleted :optional :top-level))))
602 (setf (functional-kind leaf) :deleted)
603 (setf (lambda-bind leaf) nil)
604 (dolist (let (lambda-lets leaf))
605 (setf (lambda-bind let) nil)
606 (setf (functional-kind let) :deleted))
608 (if (member kind '(:let :mv-let :assignment))
609 (let ((home (lambda-home leaf)))
610 (setf (lambda-lets home) (delete leaf (lambda-lets home))))
611 (let* ((bind-block (node-block bind))
612 (component (block-component bind-block))
613 (return (lambda-return leaf)))
614 (assert (null (leaf-refs leaf)))
615 (unless (leaf-ever-used leaf)
616 (let ((*compiler-error-context* bind))
617 (compiler-note "deleting unused function~:[.~;~:*~% ~S~]"
619 (unlink-blocks (component-head component) bind-block)
621 (unlink-blocks (node-block return) (component-tail component)))
622 (setf (component-reanalyze component) t)
623 (let ((tails (lambda-tail-set leaf)))
624 (setf (tail-set-functions tails)
625 (delete leaf (tail-set-functions tails)))
626 (setf (lambda-tail-set leaf) nil))
627 (setf (component-lambdas component)
628 (delete leaf (component-lambdas component)))))
630 (when (eq kind :external)
631 (let ((fun (functional-entry-function leaf)))
632 (setf (functional-entry-function fun) nil)
633 (when (optional-dispatch-p fun)
634 (delete-optional-dispatch fun)))))
638 ;;; Deal with deleting the last reference to an Optional-Dispatch. We have
639 ;;; to be a bit more careful than with lambdas, since Delete-Ref is used both
640 ;;; before and after local call analysis. Afterward, all references to
641 ;;; still-existing optional-dispatches have been moved to the XEP, leaving it
642 ;;; with no references at all. So we look at the XEP to see whether an
643 ;;; optional-dispatch is still really being used. But before local call
644 ;;; analysis, there are no XEPs, and all references are direct.
646 ;;; When we do delete the optional-dispatch, we grovel all of its
647 ;;; entry-points, making them be normal lambdas, and then deleting the ones
648 ;;; with no references. This deletes any e-p lambdas that were either never
649 ;;; referenced, or couldn't be deleted when the last deference was deleted (due
650 ;;; to their :OPTIONAL kind.)
652 ;;; Note that the last optional ep may alias the main entry, so when we process
653 ;;; the main entry, its kind may have been changed to NIL or even converted to
655 (defun delete-optional-dispatch (leaf)
656 (declare (type optional-dispatch leaf))
657 (let ((entry (functional-entry-function leaf)))
658 (unless (and entry (leaf-refs entry))
659 (assert (or (not entry) (eq (functional-kind entry) :deleted)))
660 (setf (functional-kind leaf) :deleted)
663 (unless (eq (functional-kind fun) :deleted)
664 (assert (eq (functional-kind fun) :optional))
665 (setf (functional-kind fun) nil)
666 (let ((refs (leaf-refs fun)))
670 (or (maybe-let-convert fun)
671 (maybe-convert-to-assignment fun)))
673 (maybe-convert-to-assignment fun)))))))
675 (dolist (ep (optional-dispatch-entry-points leaf))
677 (when (optional-dispatch-more-entry leaf)
678 (frob (optional-dispatch-more-entry leaf)))
679 (let ((main (optional-dispatch-main-entry leaf)))
680 (when (eq (functional-kind main) :optional)
685 ;;; Do stuff to delete the semantic attachments of a Ref node. When this
686 ;;; leaves zero or one reference, we do a type dispatch off of the leaf to
687 ;;; determine if a special action is appropriate.
688 (defun delete-ref (ref)
689 (declare (type ref ref))
690 (let* ((leaf (ref-leaf ref))
691 (refs (delete ref (leaf-refs leaf))))
692 (setf (leaf-refs leaf) refs)
696 (lambda-var (delete-lambda-var leaf))
698 (ecase (functional-kind leaf)
699 ((nil :let :mv-let :assignment :escape :cleanup)
700 (assert (not (functional-entry-function leaf)))
701 (delete-lambda leaf))
703 (delete-lambda leaf))
704 ((:deleted :optional))))
706 (unless (eq (functional-kind leaf) :deleted)
707 (delete-optional-dispatch leaf)))))
710 (clambda (or (maybe-let-convert leaf)
711 (maybe-convert-to-assignment leaf)))
712 (lambda-var (reoptimize-lambda-var leaf))))
715 (clambda (maybe-convert-to-assignment leaf))))))
719 ;;; This function is called by people who delete nodes; it provides a way to
720 ;;; indicate that the value of a continuation is no longer used. We null out
721 ;;; the Continuation-Dest, set Flush-P in the blocks containing uses of Cont
722 ;;; and set Component-Reoptimize. If the Prev of the use is deleted, then we
723 ;;; blow off reoptimization.
725 ;;; If the continuation is :Deleted, then we don't do anything, since all
726 ;;; semantics have already been flushed. :Deleted-Block-Start start
727 ;;; continuations are treated just like :Block-Start; it is possible that the
728 ;;; continuation may be given a new dest (e.g. by SUBSTITUTE-CONTINUATION), so
729 ;;; we don't want to delete it.
730 (defun flush-dest (cont)
731 (declare (type continuation cont))
733 (unless (eq (continuation-kind cont) :deleted)
734 (assert (continuation-dest cont))
735 (setf (continuation-dest cont) nil)
737 (let ((prev (node-prev use)))
738 (unless (eq (continuation-kind prev) :deleted)
739 (let ((block (continuation-block prev)))
740 (setf (component-reoptimize (block-component block)) t)
741 (setf (block-attributep (block-flags block) flush-p type-asserted)
744 (setf (continuation-%type-check cont) nil)
748 ;;; Do a graph walk backward from Block, marking all predecessor blocks with
749 ;;; the DELETE-P flag.
750 (defun mark-for-deletion (block)
751 (declare (type cblock block))
752 (unless (block-delete-p block)
753 (setf (block-delete-p block) t)
754 (setf (component-reanalyze (block-component block)) t)
755 (dolist (pred (block-pred block))
756 (mark-for-deletion pred)))
759 ;;; Delete Cont, eliminating both control and value semantics. We set
760 ;;; FLUSH-P and COMPONENT-REOPTIMIZE similarly to in FLUSH-DEST. Here we must
761 ;;; get the component from the use block, since the continuation may be a
762 ;;; :DELETED-BLOCK-START.
764 ;;; If Cont has DEST, then it must be the case that the DEST is unreachable,
765 ;;; since we can't compute the value desired. In this case, we call
766 ;;; MARK-FOR-DELETION to cause the DEST block and its predecessors to tell
767 ;;; people to ignore them, and to cause them to be deleted eventually.
768 (defun delete-continuation (cont)
769 (declare (type continuation cont))
770 (assert (not (eq (continuation-kind cont) :deleted)))
773 (let ((prev (node-prev use)))
774 (unless (eq (continuation-kind prev) :deleted)
775 (let ((block (continuation-block prev)))
776 (setf (block-attributep (block-flags block) flush-p type-asserted) t)
777 (setf (component-reoptimize (block-component block)) t)))))
779 (let ((dest (continuation-dest cont)))
781 (let ((prev (node-prev dest)))
783 (not (eq (continuation-kind prev) :deleted)))
784 (let ((block (continuation-block prev)))
785 (unless (block-delete-p block)
786 (mark-for-deletion block)))))))
788 (setf (continuation-kind cont) :deleted)
789 (setf (continuation-dest cont) nil)
790 (setf (continuation-next cont) nil)
791 (setf (continuation-asserted-type cont) *empty-type*)
792 (setf (continuation-%derived-type cont) *empty-type*)
793 (setf (continuation-use cont) nil)
794 (setf (continuation-block cont) nil)
795 (setf (continuation-reoptimize cont) nil)
796 (setf (continuation-%type-check cont) nil)
797 (setf (continuation-info cont) nil)
801 ;;; This function does what is necessary to eliminate the code in it from
802 ;;; the IR1 representation. This involves unlinking it from its predecessors
803 ;;; and successors and deleting various node-specific semantic information.
805 ;;; We mark the Start as has having no next and remove the last node from
806 ;;; its Cont's uses. We also flush the DEST for all continuations whose values
807 ;;; are received by nodes in the block.
808 (defun delete-block (block)
809 (declare (type cblock block))
810 (assert (block-component block) () "Block is already deleted.")
811 (note-block-deletion block)
812 (setf (block-delete-p block) t)
814 (let* ((last (block-last block))
815 (cont (node-cont last)))
816 (delete-continuation-use last)
817 (if (eq (continuation-kind cont) :unused)
818 (delete-continuation cont)
819 (reoptimize-continuation cont)))
821 (dolist (b (block-pred block))
822 (unlink-blocks b block))
823 (dolist (b (block-succ block))
824 (unlink-blocks block b))
826 (do-nodes (node cont block)
828 (ref (delete-ref node))
830 (flush-dest (if-test node)))
831 ;; The next two cases serve to maintain the invariant that a LET always
832 ;; has a well-formed COMBINATION, REF and BIND. We delete the lambda
833 ;; whenever we delete any of these, but we must be careful that this LET
834 ;; has not already been partially deleted.
836 (when (and (eq (basic-combination-kind node) :local)
837 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
838 (continuation-use (basic-combination-fun node)))
839 (let ((fun (combination-lambda node)))
840 ;; If our REF was the 2'nd to last ref, and has been deleted, then
841 ;; Fun may be a LET for some other combination.
842 (when (and (member (functional-kind fun) '(:let :mv-let))
843 (eq (let-combination fun) node))
844 (delete-lambda fun))))
845 (flush-dest (basic-combination-fun node))
846 (dolist (arg (basic-combination-args node))
847 (when arg (flush-dest arg))))
849 (let ((lambda (bind-lambda node)))
850 (unless (eq (functional-kind lambda) :deleted)
851 (assert (member (functional-kind lambda)
852 '(:let :mv-let :assignment)))
853 (delete-lambda lambda))))
855 (let ((value (exit-value node))
856 (entry (exit-entry node)))
860 (setf (entry-exits entry)
861 (delete node (entry-exits entry))))))
863 (flush-dest (return-result node))
864 (delete-return node))
866 (flush-dest (set-value node))
867 (let ((var (set-var node)))
868 (setf (basic-var-sets var)
869 (delete node (basic-var-sets var))))))
871 (delete-continuation (node-prev node)))
873 (remove-from-dfo block)
876 ;;; Do stuff to indicate that the return node Node is being deleted. We set
877 ;;; the RETURN to NIL.
878 (defun delete-return (node)
879 (declare (type creturn node))
880 (let ((fun (return-lambda node)))
881 (assert (lambda-return fun))
882 (setf (lambda-return fun) nil))
885 ;;; If any of the Vars in fun were never referenced and was not declared
886 ;;; IGNORE, then complain.
887 (defun note-unreferenced-vars (fun)
888 (declare (type clambda fun))
889 (dolist (var (lambda-vars fun))
890 (unless (or (leaf-ever-used var)
891 (lambda-var-ignorep var))
892 (let ((*compiler-error-context* (lambda-bind fun)))
893 (unless (policy *compiler-error-context* (= brevity 3))
894 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
895 ;; requires this to be a STYLE-WARNING.
896 (compiler-style-warning "The variable ~S is defined but never used."
898 (setf (leaf-ever-used var) t))))
901 (defvar *deletion-ignored-objects* '(t nil))
903 ;;; Return true if we can find Obj in Form, NIL otherwise. We bound our
904 ;;; recursion so that we don't get lost in circular structures. We ignore the
905 ;;; car of forms if they are a symbol (to prevent confusing function
906 ;;; referencess with variables), and we also ignore anything inside ' or #'.
907 (defun present-in-form (obj form depth)
908 (declare (type (integer 0 20) depth))
909 (cond ((= depth 20) nil)
913 (let ((first (car form))
915 (if (member first '(quote function))
917 (or (and (not (symbolp first))
918 (present-in-form obj first depth))
919 (do ((l (cdr form) (cdr l))
921 ((or (atom l) (> n 100))
924 (when (present-in-form obj (car l) depth)
927 ;;; This function is called on a block immediately before we delete it. We
928 ;;; check to see whether any of the code about to die appeared in the original
929 ;;; source, and emit a note if so.
931 ;;; If the block was in a lambda is now deleted, then we ignore the whole
932 ;;; block, since this case is picked off in DELETE-LAMBDA. We also ignore
933 ;;; the deletion of CRETURN nodes, since it is somewhat reasonable for a
934 ;;; function to not return, and there is a different note for that case anyway.
936 ;;; If the actual source is an atom, then we use a bunch of heuristics to
937 ;;; guess whether this reference really appeared in the original source:
938 ;;; -- If a symbol, it must be interned and not a keyword.
939 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum or a
941 ;;; -- The atom must be "present" in the original source form, and present in
942 ;;; all intervening actual source forms.
943 (defun note-block-deletion (block)
944 (let ((home (block-home-lambda block)))
945 (unless (eq (functional-kind home) :deleted)
946 (do-nodes (node cont block)
947 (let* ((path (node-source-path node))
948 (first (first path)))
949 (when (or (eq first 'original-source-start)
951 (or (not (symbolp first))
952 (let ((pkg (symbol-package first)))
954 (not (eq pkg (symbol-package :end))))))
955 (not (member first *deletion-ignored-objects*))
956 (not (typep first '(or fixnum character)))
958 (present-in-form first x 0))
959 (source-path-forms path))
960 (present-in-form first (find-original-source path)
962 (unless (return-p node)
963 (let ((*compiler-error-context* node))
964 (compiler-note "deleting unreachable code")))
968 ;;; Delete a node from a block, deleting the block if there are no nodes
969 ;;; left. We remove the node from the uses of its CONT, but we don't deal with
970 ;;; cleaning up any type-specific semantic attachments. If the CONT is :UNUSED
971 ;;; after deleting this use, then we delete CONT. (Note :UNUSED is not the
972 ;;; same as no uses. A continuation will only become :UNUSED if it was
973 ;;; :INSIDE-BLOCK before.)
975 ;;; If the node is the last node, there must be exactly one successor. We
976 ;;; link all of our precedessors to the successor and unlink the block. In
977 ;;; this case, we return T, otherwise NIL. If no nodes are left, and the block
978 ;;; is a successor of itself, then we replace the only node with a degenerate
979 ;;; exit node. This provides a way to represent the bodyless infinite loop,
980 ;;; given the prohibition on empty blocks in IR1.
981 (defun unlink-node (node)
982 (declare (type node node))
983 (let* ((cont (node-cont node))
984 (next (continuation-next cont))
985 (prev (node-prev node))
986 (block (continuation-block prev))
987 (prev-kind (continuation-kind prev))
988 (last (block-last block)))
990 (unless (eq (continuation-kind cont) :deleted)
991 (delete-continuation-use node)
992 (when (eq (continuation-kind cont) :unused)
993 (assert (not (continuation-dest cont)))
994 (delete-continuation cont)))
996 (setf (block-type-asserted block) t)
997 (setf (block-test-modified block) t)
999 (cond ((or (eq prev-kind :inside-block)
1000 (and (eq prev-kind :block-start)
1001 (not (eq node last))))
1002 (cond ((eq node last)
1003 (setf (block-last block) (continuation-use prev))
1004 (setf (continuation-next prev) nil))
1006 (setf (continuation-next prev) next)
1007 (setf (node-prev next) prev)))
1008 (setf (node-prev node) nil)
1011 (assert (eq prev-kind :block-start))
1012 (assert (eq node last))
1013 (let* ((succ (block-succ block))
1014 (next (first succ)))
1015 (assert (and succ (null (cdr succ))))
1017 ((member block succ)
1018 (with-ir1-environment node
1019 (let ((exit (make-exit))
1020 (dummy (make-continuation)))
1021 (setf (continuation-next prev) nil)
1022 (prev-link exit prev)
1023 (add-continuation-use exit dummy)
1024 (setf (block-last block) exit)))
1025 (setf (node-prev node) nil)
1028 (assert (eq (block-start-cleanup block)
1029 (block-end-cleanup block)))
1030 (unlink-blocks block next)
1031 (dolist (pred (block-pred block))
1032 (change-block-successor pred block next))
1033 (remove-from-dfo block)
1034 (cond ((continuation-dest prev)
1035 (setf (continuation-next prev) nil)
1036 (setf (continuation-kind prev) :deleted-block-start))
1038 (delete-continuation prev)))
1039 (setf (node-prev node) nil)
1042 ;;; Return true if NODE has been deleted, false if it is still a valid part
1044 (defun node-deleted (node)
1045 (declare (type node node))
1046 (let ((prev (node-prev node)))
1048 (not (eq (continuation-kind prev) :deleted))
1049 (let ((block (continuation-block prev)))
1050 (and (block-component block)
1051 (not (block-delete-p block))))))))
1053 ;;; Delete all the blocks and functions in Component. We scan first marking
1054 ;;; the blocks as delete-p to prevent weird stuff from being triggered by
1056 (defun delete-component (component)
1057 (declare (type component component))
1058 (assert (null (component-new-functions component)))
1059 (setf (component-kind component) :deleted)
1060 (do-blocks (block component)
1061 (setf (block-delete-p block) t))
1062 (dolist (fun (component-lambdas component))
1063 (setf (functional-kind fun) nil)
1064 (setf (functional-entry-function fun) nil)
1065 (setf (leaf-refs fun) nil)
1066 (delete-functional fun))
1067 (do-blocks (block component)
1068 (delete-block block))
1071 ;;; Convert code of the form
1072 ;;; (FOO ... (FUN ...) ...)
1074 ;;; (FOO ... ... ...).
1075 ;;; In other words, replace the function combination FUN by its
1076 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1077 ;;; to blow out of whatever transform called this. Note, as the number
1078 ;;; of arguments changes, the transform must be prepared to return a
1079 ;;; lambda with a new lambda-list with the correct number of
1081 (defun extract-function-args (cont fun num-args)
1083 "If CONT is a call to FUN with NUM-ARGS args, change those arguments
1084 to feed directly to the continuation-dest of CONT, which must be
1086 (declare (type continuation cont)
1088 (type index num-args))
1089 (let ((outside (continuation-dest cont))
1090 (inside (continuation-use cont)))
1091 (assert (combination-p outside))
1092 (unless (combination-p inside)
1093 (give-up-ir1-transform))
1094 (let ((inside-fun (combination-fun inside)))
1095 (unless (eq (continuation-function-name inside-fun) fun)
1096 (give-up-ir1-transform))
1097 (let ((inside-args (combination-args inside)))
1098 (unless (= (length inside-args) num-args)
1099 (give-up-ir1-transform))
1100 (let* ((outside-args (combination-args outside))
1101 (arg-position (position cont outside-args))
1102 (before-args (subseq outside-args 0 arg-position))
1103 (after-args (subseq outside-args (1+ arg-position))))
1104 (dolist (arg inside-args)
1105 (setf (continuation-dest arg) outside))
1106 (setf (combination-args inside) nil)
1107 (setf (combination-args outside)
1108 (append before-args inside-args after-args))
1109 (change-ref-leaf (continuation-use inside-fun)
1110 (find-free-function 'list "???"))
1111 (setf (combination-kind inside) :full)
1112 (setf (node-derived-type inside) *wild-type*)
1114 (setf (continuation-asserted-type cont) *wild-type*)
1119 ;;; Change the Leaf that a Ref refers to.
1120 (defun change-ref-leaf (ref leaf)
1121 (declare (type ref ref) (type leaf leaf))
1122 (unless (eq (ref-leaf ref) leaf)
1123 (push ref (leaf-refs leaf))
1125 (setf (ref-leaf ref) leaf)
1126 (let ((ltype (leaf-type leaf)))
1127 (if (function-type-p ltype)
1128 (setf (node-derived-type ref) ltype)
1129 (derive-node-type ref ltype)))
1130 (reoptimize-continuation (node-cont ref)))
1133 ;;; Change all Refs for Old-Leaf to New-Leaf.
1134 (defun substitute-leaf (new-leaf old-leaf)
1135 (declare (type leaf new-leaf old-leaf))
1136 (dolist (ref (leaf-refs old-leaf))
1137 (change-ref-leaf ref new-leaf))
1140 ;;; Like SUBSITIUTE-LEAF, only there is a predicate on the Ref to tell
1141 ;;; whether to substitute.
1142 (defun substitute-leaf-if (test new-leaf old-leaf)
1143 (declare (type leaf new-leaf old-leaf) (type function test))
1144 (dolist (ref (leaf-refs old-leaf))
1145 (when (funcall test ref)
1146 (change-ref-leaf ref new-leaf)))
1149 ;;; Return a LEAF which represents the specified constant object. If the
1150 ;;; object is not in *CONSTANTS*, then we create a new constant LEAF and
1152 #!-sb-fluid (declaim (maybe-inline find-constant))
1153 (defun find-constant (object)
1154 (if (typep object '(or symbol number character instance))
1155 (or (gethash object *constants*)
1156 (setf (gethash object *constants*)
1157 (make-constant :value object
1159 :type (ctype-of object)
1160 :where-from :defined)))
1161 (make-constant :value object
1163 :type (ctype-of object)
1164 :where-from :defined)))
1166 ;;; If there is a non-local exit noted in Entry's environment that exits to
1167 ;;; Cont in that entry, then return it, otherwise return NIL.
1168 (defun find-nlx-info (entry cont)
1169 (declare (type entry entry) (type continuation cont))
1170 (let ((entry-cleanup (entry-cleanup entry)))
1171 (dolist (nlx (environment-nlx-info (node-environment entry)) nil)
1172 (when (and (eq (nlx-info-continuation nlx) cont)
1173 (eq (nlx-info-cleanup nlx) entry-cleanup))
1176 ;;;; functional hackery
1178 ;;; If Functional is a Lambda, just return it; if it is an
1179 ;;; optional-dispatch, return the main-entry.
1180 (declaim (ftype (function (functional) clambda) main-entry))
1181 (defun main-entry (functional)
1182 (etypecase functional
1183 (clambda functional)
1185 (optional-dispatch-main-entry functional))))
1187 ;;; Returns true if Functional is a thing that can be treated like
1188 ;;; MV-Bind when it appears in an MV-Call. All fixed arguments must be
1189 ;;; optional with null default and no supplied-p. There must be a rest
1190 ;;; arg with no references.
1191 (declaim (ftype (function (functional) boolean) looks-like-an-mv-bind))
1192 (defun looks-like-an-mv-bind (functional)
1193 (and (optional-dispatch-p functional)
1194 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1196 (let ((info (lambda-var-arg-info (car arg))))
1197 (unless info (return nil))
1198 (case (arg-info-kind info)
1200 (when (or (arg-info-supplied-p info) (arg-info-default info))
1203 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1207 ;;; Return true if function is an XEP. This is true of normal XEPs
1208 ;;; (:External kind) and top-level lambdas (:Top-Level kind.)
1209 #!-sb-fluid (declaim (inline external-entry-point-p))
1210 (defun external-entry-point-p (fun)
1211 (declare (type functional fun))
1212 (not (null (member (functional-kind fun) '(:external :top-level)))))
1214 ;;; If Cont's only use is a non-notinline global function reference, then
1215 ;;; return the referenced symbol, otherwise NIL. If Notinline-OK is true, then
1216 ;;; we don't care if the leaf is notinline.
1217 (defun continuation-function-name (cont &optional notinline-ok)
1218 (declare (type continuation cont))
1219 (let ((use (continuation-use cont)))
1221 (let ((leaf (ref-leaf use)))
1222 (if (and (global-var-p leaf)
1223 (eq (global-var-kind leaf) :global-function)
1224 (or (not (defined-function-p leaf))
1225 (not (eq (defined-function-inlinep leaf) :notinline))
1231 ;;; Return the COMBINATION node that is the call to the let Fun.
1232 (defun let-combination (fun)
1233 (declare (type clambda fun))
1234 (assert (member (functional-kind fun) '(:let :mv-let)))
1235 (continuation-dest (node-cont (first (leaf-refs fun)))))
1237 ;;; Return the initial value continuation for a let variable or NIL if none.
1238 (defun let-var-initial-value (var)
1239 (declare (type lambda-var var))
1240 (let ((fun (lambda-var-home var)))
1241 (elt (combination-args (let-combination fun))
1242 (position-or-lose var (lambda-vars fun)))))
1244 ;;; Return the LAMBDA that is called by the local Call.
1245 #!-sb-fluid (declaim (inline combination-lambda))
1246 (defun combination-lambda (call)
1247 (declare (type basic-combination call))
1248 (assert (eq (basic-combination-kind call) :local))
1249 (ref-leaf (continuation-use (basic-combination-fun call))))
1251 (defvar *inline-expansion-limit* 200
1253 "An upper limit on the number of inline function calls that will be expanded
1254 in any given code object (single function or block compilation.)")
1256 ;;; Check whether Node's component has exceeded its inline expansion
1257 ;;; limit, and warn if so, returning NIL.
1258 (defun inline-expansion-ok (node)
1259 (let ((expanded (incf (component-inline-expansions
1261 (node-block node))))))
1262 (cond ((> expanded *inline-expansion-limit*) nil)
1263 ((= expanded *inline-expansion-limit*)
1264 (let ((*compiler-error-context* node))
1265 (compiler-note "*INLINE-EXPANSION-LIMIT* (~D) was exceeded, ~
1266 probably trying to~% ~
1267 inline a recursive function."
1268 *inline-expansion-limit*))
1272 ;;;; compiler error context determination
1274 (declaim (special *current-path*))
1276 ;;; We bind print level and length when printing out messages so that we don't
1277 ;;; dump huge amounts of garbage.
1278 (declaim (type (or unsigned-byte null)
1279 *compiler-error-print-level*
1280 *compiler-error-print-length*
1281 *compiler-error-print-lines*))
1282 (defvar *compiler-error-print-level* 3
1284 "The value for *PRINT-LEVEL* when printing compiler error messages.")
1285 (defvar *compiler-error-print-length* 5
1287 "The value for *PRINT-LENGTH* when printing compiler error messages.")
1288 (defvar *compiler-error-print-lines* 5
1290 "The value for *PRINT-LINES* when printing compiler error messages.")
1292 (defvar *enclosing-source-cutoff* 1
1294 "The maximum number of enclosing non-original source forms (i.e. from
1295 macroexpansion) that we print in full. For additional enclosing forms, we
1296 print only the CAR.")
1297 (declaim (type unsigned-byte *enclosing-source-cutoff*))
1299 ;;; We separate the determination of compiler error contexts from the actual
1300 ;;; signalling of those errors by objectifying the error context. This allows
1301 ;;; postponement of the determination of how (and if) to signal the error.
1303 ;;; We take care not to reference any of the IR1 so that pending potential
1304 ;;; error messages won't prevent the IR1 from being GC'd. To this end, we
1305 ;;; convert source forms to strings so that source forms that contain IR1
1306 ;;; references (e.g. %DEFUN) don't hold onto the IR.
1307 (defstruct (compiler-error-context
1308 #-no-ansi-print-object
1309 (:print-object (lambda (x stream)
1310 (print-unreadable-object (x stream :type t)))))
1311 ;; A list of the stringified CARs of the enclosing non-original source forms
1312 ;; exceeding the *enclosing-source-cutoff*.
1313 (enclosing-source nil :type list)
1314 ;; A list of stringified enclosing non-original source forms.
1315 (source nil :type list)
1316 ;; The stringified form in the original source that expanded into Source.
1317 (original-source (required-argument) :type simple-string)
1318 ;; A list of prefixes of "interesting" forms that enclose original-source.
1319 (context nil :type list)
1320 ;; The FILE-INFO-NAME for the relevant FILE-INFO.
1321 (file-name (required-argument)
1322 :type (or pathname (member :lisp :stream)))
1323 ;; The file position at which the top-level form starts, if applicable.
1324 (file-position nil :type (or index null))
1325 ;; The original source part of the source path.
1326 (original-source-path nil :type list))
1328 ;;; If true, this is the node which is used as context in compiler warning
1330 (declaim (type (or null compiler-error-context node) *compiler-error-context*))
1331 (defvar *compiler-error-context* nil)
1333 ;;; a hashtable mapping macro names to source context parsers. Each parser
1334 ;;; function returns the source-context list for that form.
1335 (defvar *source-context-methods* (make-hash-table))
1337 ;;; documentation originally from cmu-user.tex:
1338 ;;; This macro defines how to extract an abbreviated source context from
1339 ;;; the \var{name}d form when it appears in the compiler input.
1340 ;;; \var{lambda-list} is a \code{defmacro} style lambda-list used to
1341 ;;; parse the arguments. The \var{body} should return a list of
1342 ;;; subforms that can be printed on about one line. There are
1343 ;;; predefined methods for \code{defstruct}, \code{defmethod}, etc. If
1344 ;;; no method is defined, then the first two subforms are returned.
1345 ;;; Note that this facility implicitly determines the string name
1346 ;;; associated with anonymous functions.
1347 ;;; So even though SBCL itself only uses this macro within this file, it's a
1348 ;;; reasonable thing to put in SB-EXT in case some dedicated user wants to do
1349 ;;; some heavy tweaking to make SBCL give more informative output about his
1351 (defmacro def-source-context (name lambda-list &body body)
1353 "DEF-SOURCE-CONTEXT Name Lambda-List Form*
1354 This macro defines how to extract an abbreviated source context from the
1355 Named form when it appears in the compiler input. Lambda-List is a DEFMACRO
1356 style lambda-list used to parse the arguments. The Body should return a
1357 list of subforms suitable for a \"~{~S ~}\" format string."
1358 (let ((n-whole (gensym)))
1359 `(setf (gethash ',name *source-context-methods*)
1360 #'(lambda (,n-whole)
1361 (destructuring-bind ,lambda-list ,n-whole ,@body)))))
1363 (def-source-context defstruct (name-or-options &rest slots)
1364 (declare (ignore slots))
1365 `(defstruct ,(if (consp name-or-options)
1366 (car name-or-options)
1369 (def-source-context function (thing)
1370 (if (and (consp thing) (eq (first thing) 'lambda) (consp (rest thing)))
1371 `(lambda ,(second thing))
1372 `(function ,thing)))
1374 ;;; Return the first two elements of FORM if FORM is a list. Take the
1375 ;;; CAR of the second form if appropriate.
1376 (defun source-form-context (form)
1377 (cond ((atom form) nil)
1378 ((>= (length form) 2)
1379 (funcall (gethash (first form) *source-context-methods*
1381 (declare (ignore x))
1382 (list (first form) (second form))))
1387 ;;; Given a source path, return the original source form and a description
1388 ;;; of the interesting aspects of the context in which it appeared. The
1389 ;;; context is a list of lists, one sublist per context form. The sublist is a
1390 ;;; list of some of the initial subforms of the context form.
1392 ;;; For now, we use the first two subforms of each interesting form. A form is
1393 ;;; interesting if the first element is a symbol beginning with "DEF" and it is
1394 ;;; not the source form. If there is no DEF-mumble, then we use the outermost
1395 ;;; containing form. If the second subform is a list, then in some cases we
1396 ;;; return the car of that form rather than the whole form (i.e. don't show
1397 ;;; defstruct options, etc.)
1398 (defun find-original-source (path)
1399 (declare (list path))
1400 (let* ((rpath (reverse (source-path-original-source path)))
1402 (root (find-source-root tlf *source-info*)))
1403 (collect ((context))
1405 (current (rest rpath)))
1408 (assert (null current))
1410 (let ((head (first form)))
1411 (when (symbolp head)
1412 (let ((name (symbol-name head)))
1413 (when (and (>= (length name) 3) (string= name "DEF" :end1 3))
1414 (context (source-form-context form))))))
1415 (when (null current) (return))
1416 (setq form (nth (pop current) form)))
1419 (values form (context)))
1421 (let ((c (source-form-context root)))
1422 (values form (if c (list c) nil))))
1424 (values '(unable to locate source)
1425 '((some strange place)))))))))
1427 ;;; Convert a source form to a string, suitably formatted for use in
1428 ;;; compiler warnings.
1429 (defun stringify-form (form &optional (pretty t))
1430 (let ((*print-level* *compiler-error-print-level*)
1431 (*print-length* *compiler-error-print-length*)
1432 (*print-lines* *compiler-error-print-lines*)
1433 (*print-pretty* pretty))
1435 (format nil "~<~@; ~S~:>" (list form))
1436 (prin1-to-string form))))
1438 ;;; Return a COMPILER-ERROR-CONTEXT structure describing the current
1439 ;;; error context, or NIL if we can't figure anything out. ARGS is a
1440 ;;; list of things that are going to be printed out in the error
1441 ;;; message, and can thus be blown off when they appear in the source
1443 (defun find-error-context (args)
1444 (let ((context *compiler-error-context*))
1445 (if (compiler-error-context-p context)
1447 (let ((path (or *current-path*
1449 (node-source-path context)
1451 (when (and *source-info* path)
1452 (multiple-value-bind (form src-context) (find-original-source path)
1453 (collect ((full nil cons)
1455 (let ((forms (source-path-forms path))
1457 (dolist (src (if (member (first forms) args)
1460 (if (>= n *enclosing-source-cutoff*)
1461 (short (stringify-form (if (consp src)
1465 (full (stringify-form src)))
1468 (let* ((tlf (source-path-tlf-number path))
1469 (file (find-file-info tlf *source-info*)))
1470 (make-compiler-error-context
1471 :enclosing-source (short)
1473 :original-source (stringify-form form)
1474 :context src-context
1475 :file-name (file-info-name file)
1477 (multiple-value-bind (ignore pos)
1478 (find-source-root tlf *source-info*)
1479 (declare (ignore ignore))
1481 :original-source-path
1482 (source-path-original-source path))))))))))
1484 ;;;; printing error messages
1486 ;;; We save the context information that we printed out most recently
1487 ;;; so that we don't print it out redundantly.
1489 ;;; The last COMPILER-ERROR-CONTEXT that we printed.
1490 (defvar *last-error-context* nil)
1491 (declaim (type (or compiler-error-context null) *last-error-context*))
1493 ;;; The format string and args for the last error we printed.
1494 (defvar *last-format-string* nil)
1495 (defvar *last-format-args* nil)
1496 (declaim (type (or string null) *last-format-string*))
1497 (declaim (type list *last-format-args*))
1499 ;;; The number of times that the last error message has been emitted,
1500 ;;; so that we can compress duplicate error messages.
1501 (defvar *last-message-count* 0)
1502 (declaim (type index *last-message-count*))
1504 ;;; If the last message was given more than once, then print out an
1505 ;;; indication of how many times it was repeated. We reset the message count
1506 ;;; when we are done.
1507 (defun note-message-repeats (&optional (terpri t))
1508 (cond ((= *last-message-count* 1)
1509 (when terpri (terpri *error-output*)))
1510 ((> *last-message-count* 1)
1511 (format *error-output* "~&; [Last message occurs ~D times.]~2%"
1512 *last-message-count*)))
1513 (setq *last-message-count* 0))
1515 ;;; Print out the message, with appropriate context if we can find it.
1516 ;;; If If the context is different from the context of the last
1517 ;;; message we printed, then we print the context. If the original
1518 ;;; source is different from the source we are working on, then we
1519 ;;; print the current source in addition to the original source.
1521 ;;; We suppress printing of messages identical to the previous, but
1522 ;;; record the number of times that the message is repeated.
1523 (defun print-compiler-message (format-string format-args)
1525 (declare (type simple-string format-string))
1526 (declare (type list format-args))
1528 (let ((stream *error-output*)
1529 (context (find-error-context format-args)))
1532 (let ((file (compiler-error-context-file-name context))
1533 (in (compiler-error-context-context context))
1534 (form (compiler-error-context-original-source context))
1535 (enclosing (compiler-error-context-enclosing-source context))
1536 (source (compiler-error-context-source context))
1537 (last *last-error-context*))
1540 (equal file (compiler-error-context-file-name last)))
1541 (when (pathnamep file)
1542 (note-message-repeats)
1544 (format stream "~2&; file: ~A~%" (namestring file))))
1547 (equal in (compiler-error-context-context last)))
1548 (note-message-repeats)
1550 (format stream "~&")
1551 (pprint-logical-block (stream nil :per-line-prefix "; ")
1552 (format stream "in:~{~<~% ~4:;~{ ~S~}~>~^ =>~}" in))
1553 (format stream "~%"))
1558 (compiler-error-context-original-source last)))
1559 (note-message-repeats)
1561 (format stream "~&")
1562 (pprint-logical-block (stream nil :per-line-prefix "; ")
1563 (format stream " ~A" form))
1564 (format stream "~&"))
1568 (compiler-error-context-enclosing-source last)))
1570 (note-message-repeats)
1572 (format stream "~&; --> ~{~<~%; --> ~1:;~A~> ~}~%" enclosing)))
1575 (equal source (compiler-error-context-source last)))
1576 (setq *last-format-string* nil)
1578 (note-message-repeats)
1579 (dolist (src source)
1580 (format stream "~&")
1581 (write-string "; ==>" stream)
1582 (format stream "~&")
1583 (pprint-logical-block (stream nil :per-line-prefix "; ")
1584 (write-string src stream)))))))
1586 (format stream "~&")
1587 (note-message-repeats)
1588 (setq *last-format-string* nil)
1589 (format stream "~&")))
1591 (setq *last-error-context* context)
1593 (unless (and (equal format-string *last-format-string*)
1594 (tree-equal format-args *last-format-args*))
1595 (note-message-repeats nil)
1596 (setq *last-format-string* format-string)
1597 (setq *last-format-args* format-args)
1598 (let ((*print-level* *compiler-error-print-level*)
1599 (*print-length* *compiler-error-print-length*)
1600 (*print-lines* *compiler-error-print-lines*))
1601 (format stream "~&")
1602 (pprint-logical-block (stream nil :per-line-prefix "; ")
1603 (format stream "~&~?" format-string format-args))
1604 (format stream "~&"))))
1606 (incf *last-message-count*)
1609 (defun print-compiler-condition (condition)
1610 (declare (type condition condition))
1611 (let (;; These different classes of conditions have different effects
1612 ;; on the return codes of COMPILE-FILE, so it's nice for users to be
1613 ;; able to pick them out by lexical search through the output.
1614 (what (etypecase condition
1615 (style-warning 'style-warning)
1618 (multiple-value-bind (format-string format-args)
1619 (if (typep condition 'simple-condition)
1620 (values (simple-condition-format-control condition)
1621 (simple-condition-format-arguments condition))
1623 (list (with-output-to-string (s)
1624 (princ condition s)))))
1625 (print-compiler-message (format nil
1632 ;;; COMPILER-NOTE is vaguely like COMPILER-ERROR and the other
1633 ;;; condition-signalling functions, but it just writes some output instead of
1634 ;;; signalling. (In CMU CL, it did signal a condition, but this didn't seem to
1635 ;;; work all that well; it was weird to have COMPILE-FILE return with
1636 ;;; WARNINGS-P set when the only problem was that the compiler couldn't figure
1637 ;;; out how to compile something as efficiently as it liked.)
1638 (defun compiler-note (format-string &rest format-args)
1639 (unless (if *compiler-error-context*
1640 (policy *compiler-error-context* (= brevity 3))
1641 (policy nil (= brevity 3)))
1642 (incf *compiler-note-count*)
1643 (print-compiler-message (format nil "note: ~A" format-string)
1647 ;;; Issue a note when we might or might not be in the compiler.
1648 (defun maybe-compiler-note (&rest rest)
1649 (if (boundp '*lexenv*) ; if we're in the compiler
1650 (apply #'compiler-note rest)
1651 (let ((stream *error-output*))
1652 (pprint-logical-block (stream nil :per-line-prefix ";")
1654 (format stream " note: ~3I~_")
1655 (pprint-logical-block (stream nil)
1656 (apply #'format stream rest)))
1657 (fresh-line stream)))) ; (outside logical block, no per-line-prefix)
1659 ;;; The politically correct way to print out progress messages and
1660 ;;; such like. We clear the current error context so that we know that
1661 ;;; it needs to be reprinted, and we also Force-Output so that the
1662 ;;; message gets seen right away.
1663 (declaim (ftype (function (string &rest t) (values)) compiler-mumble))
1664 (defun compiler-mumble (format-string &rest format-args)
1665 (note-message-repeats)
1666 (setq *last-error-context* nil)
1667 (apply #'format *error-output* format-string format-args)
1668 (force-output *error-output*)
1671 ;;; Return a string that somehow names the code in Component. We use
1672 ;;; the source path for the bind node for an arbitrary entry point to
1673 ;;; find the source context, then return that as a string.
1674 (declaim (ftype (function (component) simple-string) find-component-name))
1675 (defun find-component-name (component)
1676 (let ((ep (first (block-succ (component-head component)))))
1677 (assert ep () "no entry points?")
1678 (multiple-value-bind (form context)
1679 (find-original-source
1680 (node-source-path (continuation-next (block-start ep))))
1681 (declare (ignore form))
1682 (let ((*print-level* 2)
1683 (*print-pretty* nil))
1684 (format nil "~{~{~S~^ ~}~^ => ~}" context)))))
1686 ;;;; condition system interface
1688 ;;; Keep track of how many times each kind of condition happens.
1689 (defvar *compiler-error-count*)
1690 (defvar *compiler-warning-count*)
1691 (defvar *compiler-style-warning-count*)
1692 (defvar *compiler-note-count*)
1694 ;;; Keep track of whether any surrounding COMPILE or COMPILE-FILE call
1695 ;;; should return WARNINGS-P or FAILURE-P.
1696 (defvar *failure-p*)
1697 (defvar *warnings-p*)
1699 ;;; condition handlers established by the compiler. We re-signal the
1700 ;;; condition, if it is not handled, we increment our warning counter
1701 ;;; and print the error message.
1702 (defun compiler-error-handler (condition)
1704 (incf *compiler-error-count*)
1705 (setf *warnings-p* t
1707 (print-compiler-condition condition)
1708 (continue condition))
1709 (defun compiler-warning-handler (condition)
1711 (incf *compiler-warning-count*)
1712 (setf *warnings-p* t
1714 (print-compiler-condition condition)
1715 (muffle-warning condition))
1716 (defun compiler-style-warning-handler (condition)
1718 (incf *compiler-style-warning-count*)
1719 (setf *warnings-p* t)
1720 (print-compiler-condition condition)
1721 (muffle-warning condition))
1723 ;;;; undefined warnings
1725 (defvar *undefined-warning-limit* 3
1727 "If non-null, then an upper limit on the number of unknown function or type
1728 warnings that the compiler will print for any given name in a single
1729 compilation. This prevents excessive amounts of output when the real
1730 problem is a missing definition (as opposed to a typo in the use.)")
1732 ;;; Make an entry in the *UNDEFINED-WARNINGS* describing a reference
1733 ;;; to Name of the specified Kind. If we have exceeded the warning
1734 ;;; limit, then just increment the count, otherwise note the current
1737 ;;; Undefined types are noted by a condition handler in
1738 ;;; WITH-COMPILATION-UNIT, which can potentially be invoked outside
1739 ;;; the compiler, hence the BOUNDP check.
1740 (defun note-undefined-reference (name kind)
1741 (unless (and (boundp '*lexenv*)
1742 ;; FIXME: I'm pretty sure the BREVITY test below isn't
1743 ;; a good idea; we should have BREVITY affect compiler
1744 ;; notes, not STYLE-WARNINGs. And I'm not sure what the
1745 ;; BOUNDP '*LEXENV* test above is for; it's likely
1746 ;; a good idea, but it probably deserves an explanatory
1748 (policy nil (= brevity 3)))
1749 (let* ((found (dolist (warning *undefined-warnings* nil)
1750 (when (and (equal (undefined-warning-name warning) name)
1751 (eq (undefined-warning-kind warning) kind))
1754 (make-undefined-warning :name name :kind kind))))
1755 (unless found (push res *undefined-warnings*))
1756 (when (or (not *undefined-warning-limit*)
1757 (< (undefined-warning-count res) *undefined-warning-limit*))
1758 (push (find-error-context (list name))
1759 (undefined-warning-warnings res)))
1760 (incf (undefined-warning-count res))))
1765 ;;; Apply a function to some arguments, returning a list of the values
1766 ;;; resulting of the evaluation. If an error is signalled during the
1767 ;;; application, then we print a warning message and return NIL as our
1768 ;;; second value to indicate this. Node is used as the error context
1769 ;;; for any error message, and Context is a string that is spliced
1770 ;;; into the warning.
1771 (declaim (ftype (function ((or symbol function) list node string)
1772 (values list boolean))
1774 (defun careful-call (function args node context)
1776 (multiple-value-list
1777 (handler-case (apply function args)
1779 (let ((*compiler-error-context* node))
1780 (compiler-warning "Lisp error during ~A:~%~A" context condition)
1781 (return-from careful-call (values nil nil))))))
1784 ;;;; utilities used at run-time for parsing keyword args in IR1
1786 ;;; This function is used by the result of Parse-Deftransform to find
1787 ;;; the continuation for the value of the keyword argument Key in the
1788 ;;; list of continuations Args. It returns the continuation if the
1789 ;;; keyword is present, or NIL otherwise. The legality and
1790 ;;; constantness of the keywords should already have been checked.
1791 (declaim (ftype (function (list keyword) (or continuation null))
1792 find-keyword-continuation))
1793 (defun find-keyword-continuation (args key)
1794 (do ((arg args (cddr arg)))
1796 (when (eq (continuation-value (first arg)) key)
1797 (return (second arg)))))
1799 ;;; This function is used by the result of Parse-Deftransform to
1800 ;;; verify that alternating continuations in Args are constant and
1801 ;;; that there is an even number of args.
1802 (declaim (ftype (function (list) boolean) check-keywords-constant))
1803 (defun check-keywords-constant (args)
1804 (do ((arg args (cddr arg)))
1806 (unless (and (rest arg)
1807 (constant-continuation-p (first arg)))
1810 ;;; This function is used by the result of Parse-Deftransform to
1811 ;;; verify that the list of continuations Args is a well-formed
1812 ;;; keyword arglist and that only keywords present in the list Keys
1814 (declaim (ftype (function (list list) boolean) check-transform-keys))
1815 (defun check-transform-keys (args keys)
1816 (and (check-keywords-constant args)
1817 (do ((arg args (cddr arg)))
1819 (unless (member (continuation-value (first arg)) keys)
1824 ;;; Called by the expansion of the EVENT macro.
1825 (declaim (ftype (function (event-info (or node null)) *) %event))
1826 (defun %event (info node)
1827 (incf (event-info-count info))
1828 (when (and (>= (event-info-level info) *event-note-threshold*)
1830 (policy node (= brevity 0))
1831 (policy nil (= brevity 0))))
1832 (let ((*compiler-error-context* node))
1833 (compiler-note (event-info-description info))))
1835 (let ((action (event-info-action info)))
1836 (when action (funcall action node))))