1 ;;;; This file contains miscellaneous utilities used for manipulating
2 ;;;; the IR1 representation.
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
17 ;;; Return the innermost cleanup enclosing NODE, or NIL if there is
18 ;;; none in its function. If NODE has no cleanup, but is in a LET,
19 ;;; then we must still check the environment that the call is in.
20 (defun node-enclosing-cleanup (node)
21 (declare (type node node))
22 (do ((lexenv (node-lexenv node)
23 (lambda-call-lexenv (lexenv-lambda lexenv))))
25 (let ((cup (lexenv-cleanup lexenv)))
26 (when cup (return cup)))))
28 ;;; Convert the FORM in a block inserted between BLOCK1 and BLOCK2 as
29 ;;; an implicit MV-PROG1. The inserted block is returned. NODE is used
30 ;;; for IR1 context when converting the form. Note that the block is
31 ;;; not assigned a number, and is linked into the DFO at the
32 ;;; beginning. We indicate that we have trashed the DFO by setting
33 ;;; COMPONENT-REANALYZE. If CLEANUP is supplied, then convert with
35 (defun insert-cleanup-code (block1 block2 node form &optional cleanup)
36 (declare (type cblock block1 block2) (type node node)
37 (type (or cleanup null) cleanup))
38 (setf (component-reanalyze (block-component block1)) t)
39 (with-ir1-environment node
40 (let* ((start (make-continuation))
41 (block (continuation-starts-block start))
42 (cont (make-continuation))
44 (make-lexenv :cleanup cleanup)
46 (change-block-successor block1 block2 block)
47 (link-blocks block block2)
48 (ir1-convert start cont form)
49 (setf (block-last block) (continuation-use cont))
52 ;;;; continuation use hacking
54 ;;; Return a list of all the nodes which use Cont.
55 (declaim (ftype (function (continuation) list) find-uses))
56 (defun find-uses (cont)
57 (ecase (continuation-kind cont)
58 ((:block-start :deleted-block-start)
59 (block-start-uses (continuation-block cont)))
60 (:inside-block (list (continuation-use cont)))
64 ;;; Update continuation use information so that NODE is no longer a
65 ;;; use of its CONT. If the old continuation doesn't start its block,
66 ;;; then we don't update the BLOCK-START-USES, since it will be
67 ;;; deleted when we are done.
69 ;;; Note: if you call this function, you may have to do a
70 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
72 (declaim (ftype (function (node) (values)) delete-continuation-use))
73 (defun delete-continuation-use (node)
74 (let* ((cont (node-cont node))
75 (block (continuation-block cont)))
76 (ecase (continuation-kind cont)
78 ((:block-start :deleted-block-start)
79 (let ((uses (delete node (block-start-uses block))))
80 (setf (block-start-uses block) uses)
81 (setf (continuation-use cont)
82 (if (cdr uses) nil (car uses)))))
84 (setf (continuation-kind cont) :unused)
85 (setf (continuation-block cont) nil)
86 (setf (continuation-use cont) nil)
87 (setf (continuation-next cont) nil)))
88 (setf (node-cont node) nil))
91 ;;; Update continuation use information so that NODE uses CONT. If
92 ;;; CONT is :UNUSED, then we set its block to NODE's NODE-BLOCK (which
95 ;;; Note: if you call this function, you may have to do a
96 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
98 (declaim (ftype (function (node continuation) (values)) add-continuation-use))
99 (defun add-continuation-use (node cont)
100 (aver (not (node-cont node)))
101 (let ((block (continuation-block cont)))
102 (ecase (continuation-kind cont)
106 (let ((block (node-block node)))
108 (setf (continuation-block cont) block))
109 (setf (continuation-kind cont) :inside-block)
110 (setf (continuation-use cont) node))
111 ((:block-start :deleted-block-start)
112 (let ((uses (cons node (block-start-uses block))))
113 (setf (block-start-uses block) uses)
114 (setf (continuation-use cont)
115 (if (cdr uses) nil (car uses)))))))
116 (setf (node-cont node) cont)
119 ;;; Return true if CONT is the NODE-CONT for NODE and CONT is
120 ;;; transferred to immediately after the evaluation of NODE.
121 (defun immediately-used-p (cont node)
122 (declare (type continuation cont) (type node node))
123 (and (eq (node-cont node) cont)
124 (not (eq (continuation-kind cont) :deleted))
125 (let ((cblock (continuation-block cont))
126 (nblock (node-block node)))
127 (or (eq cblock nblock)
128 (let ((succ (block-succ nblock)))
129 (and (= (length succ) 1)
130 (eq (first succ) cblock)))))))
132 ;;;; continuation substitution
134 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
135 ;;; NIL. When we are done, we call FLUSH-DEST on OLD to clear its DEST
136 ;;; and to note potential optimization opportunities.
137 (defun substitute-continuation (new old)
138 (declare (type continuation old new))
139 (aver (not (continuation-dest new)))
140 (let ((dest (continuation-dest old)))
143 (cif (setf (if-test dest) new))
144 (cset (setf (set-value dest) new))
145 (creturn (setf (return-result dest) new))
146 (exit (setf (exit-value dest) new))
148 (if (eq old (basic-combination-fun dest))
149 (setf (basic-combination-fun dest) new)
150 (setf (basic-combination-args dest)
151 (nsubst new old (basic-combination-args dest))))))
154 (setf (continuation-dest new) dest))
157 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
158 ;;; arbitary number of uses. If NEW will end up with more than one
159 ;;; use, then we must arrange for it to start a block if it doesn't
161 (defun substitute-continuation-uses (new old)
162 (declare (type continuation old new))
163 (unless (and (eq (continuation-kind new) :unused)
164 (eq (continuation-kind old) :inside-block))
165 (ensure-block-start new))
168 (delete-continuation-use node)
169 (add-continuation-use node new))
170 (dolist (lexenv-use (continuation-lexenv-uses old))
171 (setf (cadr lexenv-use) new))
173 (reoptimize-continuation new)
176 ;;;; block starting/creation
178 ;;; Return the block that CONT is the start of, making a block if
179 ;;; necessary. This function is called by IR1 translators which may
180 ;;; cause a continuation to be used more than once. Every continuation
181 ;;; which may be used more than once must start a block by the time
182 ;;; that anyone does a USE-CONTINUATION on it.
184 ;;; We also throw the block into the next/prev list for the
185 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
187 (defun continuation-starts-block (cont)
188 (declare (type continuation cont))
189 (ecase (continuation-kind cont)
191 (aver (not (continuation-block cont)))
192 (let* ((head (component-head *current-component*))
193 (next (block-next head))
194 (new-block (make-block cont)))
195 (setf (block-next new-block) next)
196 (setf (block-prev new-block) head)
197 (setf (block-prev next) new-block)
198 (setf (block-next head) new-block)
199 (setf (continuation-block cont) new-block)
200 (setf (continuation-use cont) nil)
201 (setf (continuation-kind cont) :block-start)
204 (continuation-block cont))))
206 ;;; Ensure that CONT is the start of a block (or deleted) so that
207 ;;; the use set can be freely manipulated.
208 ;;; -- If the continuation is :UNUSED or is :INSIDE-BLOCK and the
209 ;;; CONT of LAST in its block, then we make it the start of a new
211 ;;; -- If the continuation is :INSIDE-BLOCK inside a block, then we
212 ;;; split the block using Node-Ends-Block, which makes the
213 ;;; continuation be a :BLOCK-START.
214 (defun ensure-block-start (cont)
215 (declare (type continuation cont))
216 (let ((kind (continuation-kind cont)))
218 ((:deleted :block-start :deleted-block-start))
219 ((:unused :inside-block)
220 (let ((block (continuation-block cont)))
221 (cond ((or (eq kind :unused)
222 (eq (node-cont (block-last block)) cont))
223 (setf (continuation-block cont)
224 (make-block-key :start cont
226 :start-uses (find-uses cont)))
227 (setf (continuation-kind cont) :deleted-block-start))
229 (node-ends-block (continuation-use cont))))))))
232 ;;;; miscellaneous shorthand functions
234 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
235 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
236 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
237 ;;; deleted, and then return its home.
238 (defun node-home-lambda (node)
239 (declare (type node node))
240 (do ((fun (lexenv-lambda (node-lexenv node))
241 (lexenv-lambda (lambda-call-lexenv fun))))
242 ((not (eq (functional-kind fun) :deleted))
244 (when (eq (lambda-home fun) fun)
247 (defun node-block (node)
248 (declare (type node node))
249 (the cblock (continuation-block (node-prev node))))
250 (defun node-physenv (node)
251 (declare (type node node))
252 (the physenv (lambda-physenv (node-home-lambda node))))
254 (defun lambda-block (clambda)
255 (declare (type clambda clambda))
256 (node-block (lambda-bind clambda)))
257 (defun lambda-component (clambda)
258 (block-component (lambda-block clambda)))
260 ;;; Return the enclosing cleanup for environment of the first or last
262 (defun block-start-cleanup (block)
263 (declare (type cblock block))
264 (node-enclosing-cleanup (continuation-next (block-start block))))
265 (defun block-end-cleanup (block)
266 (declare (type cblock block))
267 (node-enclosing-cleanup (block-last block)))
269 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
270 (defun block-home-lambda (block)
271 (declare (type cblock block))
272 (if (node-p (block-last block))
273 ;; This is the old CMU CL way of doing it.
274 (node-home-lambda (block-last block))
275 ;; The CMU CL approach sometimes fails, e.g. in IR1-CONVERT of
276 ;; one of the legs of an IF, now that SBCL uses this operation
277 ;; more aggressively than CMU CL did.
279 ;; In this case we reason that previous-in-target-execution-order
280 ;; blocks should be in the same lambda, and that they seem in
281 ;; practice to be previous-in-compilation-order blocks too,
282 ;; so we look back to find one which is sufficiently
283 ;; initialized to tell us what the home lambda is. We could
284 ;; get fancy about this, flooding the graph of all the
285 ;; previous blocks, but in practice it seems to work just
286 ;; to grab the first previous block and use it.
287 (node-home-lambda (block-last (first (block-pred block))))))
289 ;;; Return the IR1 physical environment for BLOCK.
290 (defun block-physenv (block)
291 (declare (type cblock block))
292 (lambda-physenv (block-home-lambda block)))
294 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
295 ;;; of its original source's top level form in its compilation unit.
296 (defun source-path-tlf-number (path)
297 (declare (list path))
300 ;;; Return the (reversed) list for the PATH in the original source
301 ;;; (with the Top Level Form number last).
302 (defun source-path-original-source (path)
303 (declare (list path) (inline member))
304 (cddr (member 'original-source-start path :test #'eq)))
306 ;;; Return the Form Number of PATH's original source inside the Top
307 ;;; Level Form that contains it. This is determined by the order that
308 ;;; we walk the subforms of the top level source form.
309 (defun source-path-form-number (path)
310 (declare (list path) (inline member))
311 (cadr (member 'original-source-start path :test #'eq)))
313 ;;; Return a list of all the enclosing forms not in the original
314 ;;; source that converted to get to this form, with the immediate
315 ;;; source for node at the start of the list.
316 (defun source-path-forms (path)
317 (subseq path 0 (position 'original-source-start path)))
319 ;;; Return the innermost source form for NODE.
320 (defun node-source-form (node)
321 (declare (type node node))
322 (let* ((path (node-source-path node))
323 (forms (source-path-forms path)))
326 (values (find-original-source path)))))
328 ;;; Return NODE-SOURCE-FORM, T if continuation has a single use,
329 ;;; otherwise NIL, NIL.
330 (defun continuation-source (cont)
331 (let ((use (continuation-use cont)))
333 (values (node-source-form use) t)
336 ;;; Return the LAMBDA that is CONT's home.
337 (defun continuation-home-lambda (cont)
338 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
339 ;; implementation might not be quite right, or might be uglier than
340 ;; necessary. It appears that the original Python never found a need
341 ;; to do this operation. The obvious things based on
342 ;; NODE-HOME-LAMBDA of CONTINUATION-USE usually works; then if that
343 ;; fails, BLOCK-HOME-LAMBDA of CONTINUATION-BLOCK works, given that
344 ;; generalize it enough to grovel harder when the simple CMU CL
345 ;; approach fails. -- WHN 2001-12-02
346 (cond ((continuation-use cont)
347 (node-home-lambda (continuation-use cont)))
348 ((continuation-block cont)
349 (block-home-lambda (continuation-block cont)))
351 (error "internal error: can't find home lambda for ~S"))))
353 ;;; Return a new LEXENV just like DEFAULT except for the specified
354 ;;; slot values. Values for the alist slots are NCONCed to the
355 ;;; beginning of the current value, rather than replacing it entirely.
356 (defun make-lexenv (&key (default *lexenv*)
357 functions variables blocks tags type-restrictions
359 (lambda (lexenv-lambda default))
360 (cleanup (lexenv-cleanup default))
361 (policy (lexenv-policy default)))
362 (macrolet ((frob (var slot)
363 `(let ((old (,slot default)))
367 (internal-make-lexenv
368 (frob functions lexenv-functions)
369 (frob variables lexenv-variables)
370 (frob blocks lexenv-blocks)
371 (frob tags lexenv-tags)
372 (frob type-restrictions lexenv-type-restrictions)
373 lambda cleanup policy
374 (frob options lexenv-options))))
376 ;;;; flow/DFO/component hackery
378 ;;; Join BLOCK1 and BLOCK2.
379 (defun link-blocks (block1 block2)
380 (declare (type cblock block1 block2))
381 (setf (block-succ block1)
382 (if (block-succ block1)
383 (%link-blocks block1 block2)
385 (push block1 (block-pred block2))
387 (defun %link-blocks (block1 block2)
388 (declare (type cblock block1 block2) (inline member))
389 (let ((succ1 (block-succ block1)))
390 (aver (not (member block2 succ1 :test #'eq)))
391 (cons block2 succ1)))
393 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
394 ;;; this leaves a successor with a single predecessor that ends in an
395 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
396 ;;; now be able to be propagated to the successor.
397 (defun unlink-blocks (block1 block2)
398 (declare (type cblock block1 block2))
399 (let ((succ1 (block-succ block1)))
400 (if (eq block2 (car succ1))
401 (setf (block-succ block1) (cdr succ1))
402 (do ((succ (cdr succ1) (cdr succ))
404 ((eq (car succ) block2)
405 (setf (cdr prev) (cdr succ)))
408 (let ((new-pred (delq block1 (block-pred block2))))
409 (setf (block-pred block2) new-pred)
410 (when (and new-pred (null (rest new-pred)))
411 (let ((pred-block (first new-pred)))
412 (when (if-p (block-last pred-block))
413 (setf (block-test-modified pred-block) t)))))
416 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
417 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
418 ;;; consequent/alternative blocks to point to NEW. We also set
419 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
420 ;;; the new successor.
421 (defun change-block-successor (block old new)
422 (declare (type cblock new old block) (inline member))
423 (unlink-blocks block old)
424 (let ((last (block-last block))
425 (comp (block-component block)))
426 (setf (component-reanalyze comp) t)
429 (setf (block-test-modified block) t)
430 (let* ((succ-left (block-succ block))
431 (new (if (and (eq new (component-tail comp))
435 (unless (member new succ-left :test #'eq)
436 (link-blocks block new))
437 (macrolet ((frob (slot)
438 `(when (eq (,slot last) old)
439 (setf (,slot last) new))))
441 (frob if-alternative))))
443 (unless (member new (block-succ block) :test #'eq)
444 (link-blocks block new)))))
448 ;;; Unlink a block from the next/prev chain. We also null out the
450 (declaim (ftype (function (cblock) (values)) remove-from-dfo))
451 (defun remove-from-dfo (block)
452 (let ((next (block-next block))
453 (prev (block-prev block)))
454 (setf (block-component block) nil)
455 (setf (block-next prev) next)
456 (setf (block-prev next) prev))
459 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
460 ;;; Component to be the same as for AFTER.
461 (defun add-to-dfo (block after)
462 (declare (type cblock block after))
463 (let ((next (block-next after))
464 (comp (block-component after)))
465 (aver (not (eq (component-kind comp) :deleted)))
466 (setf (block-component block) comp)
467 (setf (block-next after) block)
468 (setf (block-prev block) after)
469 (setf (block-next block) next)
470 (setf (block-prev next) block))
473 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
474 ;;; the head and tail which are set to T.
475 (declaim (ftype (function (component) (values)) clear-flags))
476 (defun clear-flags (component)
477 (let ((head (component-head component))
478 (tail (component-tail component)))
479 (setf (block-flag head) t)
480 (setf (block-flag tail) t)
481 (do-blocks (block component)
482 (setf (block-flag block) nil)))
485 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
486 ;;; true in the head and tail blocks.
487 (declaim (ftype (function nil component) make-empty-component))
488 (defun make-empty-component ()
489 (let* ((head (make-block-key :start nil :component nil))
490 (tail (make-block-key :start nil :component nil))
491 (res (make-component :head head :tail tail)))
492 (setf (block-flag head) t)
493 (setf (block-flag tail) t)
494 (setf (block-component head) res)
495 (setf (block-component tail) res)
496 (setf (block-next head) tail)
497 (setf (block-prev tail) head)
500 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
501 ;;; The new block is added to the DFO immediately following NODE's block.
502 (defun node-ends-block (node)
503 (declare (type node node))
504 (let* ((block (node-block node))
505 (start (node-cont node))
506 (last (block-last block))
507 (last-cont (node-cont last)))
508 (unless (eq last node)
509 (aver (and (eq (continuation-kind start) :inside-block)
510 (not (block-delete-p block))))
511 (let* ((succ (block-succ block))
513 (make-block-key :start start
514 :component (block-component block)
515 :start-uses (list (continuation-use start))
516 :succ succ :last last)))
517 (setf (continuation-kind start) :block-start)
520 (cons new-block (remove block (block-pred b)))))
521 (setf (block-succ block) ())
522 (setf (block-last block) node)
523 (link-blocks block new-block)
524 (add-to-dfo new-block block)
525 (setf (component-reanalyze (block-component block)) t)
527 (do ((cont start (node-cont (continuation-next cont))))
529 (when (eq (continuation-kind last-cont) :inside-block)
530 (setf (continuation-block last-cont) new-block)))
531 (setf (continuation-block cont) new-block))
533 (setf (block-type-asserted block) t)
534 (setf (block-test-modified block) t))))
540 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR. We
541 ;;; iterate over all local calls flushing the corresponding argument,
542 ;;; allowing the computation of the argument to be deleted. We also
543 ;;; mark the let for reoptimization, since it may be that we have
544 ;;; deleted the last variable.
546 ;;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
547 ;;; too much difficulty, since we can efficiently implement write-only
548 ;;; variables. We iterate over the sets, marking their blocks for dead
549 ;;; code flushing, since we can delete sets whose value is unused.
550 (defun delete-lambda-var (leaf)
551 (declare (type lambda-var leaf))
552 (let* ((fun (lambda-var-home leaf))
553 (n (position leaf (lambda-vars fun))))
554 (dolist (ref (leaf-refs fun))
555 (let* ((cont (node-cont ref))
556 (dest (continuation-dest cont)))
557 (when (and (combination-p dest)
558 (eq (basic-combination-fun dest) cont)
559 (eq (basic-combination-kind dest) :local))
560 (let* ((args (basic-combination-args dest))
562 (reoptimize-continuation arg)
564 (setf (elt args n) nil))))))
566 (dolist (set (lambda-var-sets leaf))
567 (setf (block-flush-p (node-block set)) t))
571 ;;; Note that something interesting has happened to VAR. We only deal
572 ;;; with LET variables, marking the corresponding initial value arg as
573 ;;; needing to be reoptimized.
574 (defun reoptimize-lambda-var (var)
575 (declare (type lambda-var var))
576 (let ((fun (lambda-var-home var)))
577 (when (and (eq (functional-kind fun) :let)
579 (do ((args (basic-combination-args
582 (first (leaf-refs fun)))))
584 (vars (lambda-vars fun) (cdr vars)))
586 (reoptimize-continuation (car args))))))
589 ;;; Delete a function that has no references. This need only be called
590 ;;; on functions that never had any references, since otherwise
591 ;;; DELETE-REF will handle the deletion.
592 (defun delete-functional (fun)
593 (aver (and (null (leaf-refs fun))
594 (not (functional-entry-fun fun))))
596 (optional-dispatch (delete-optional-dispatch fun))
597 (clambda (delete-lambda fun)))
600 ;;; Deal with deleting the last reference to a LAMBDA. Since there is
601 ;;; only one way into a LAMBDA, deleting the last reference to a
602 ;;; LAMBDA ensures that there is no way to reach any of the code in
603 ;;; it. So we just set the FUNCTIONAL-KIND for FUN and its LETs to
604 ;;; :DELETED, causing IR1 optimization to delete blocks in that
607 ;;; If the function isn't a LET, we unlink the function head and tail
608 ;;; from the component head and tail to indicate that the code is
609 ;;; unreachable. We also delete the function from COMPONENT-LAMBDAS
610 ;;; (it won't be there before local call analysis, but no matter.) If
611 ;;; the lambda was never referenced, we give a note.
613 ;;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
614 ;;; ENTRY-FUN so that people will know that it is not an entry point
616 (defun delete-lambda (leaf)
617 (declare (type clambda leaf))
618 (let ((kind (functional-kind leaf))
619 (bind (lambda-bind leaf)))
620 (aver (not (member kind '(:deleted :optional :toplevel))))
621 (aver (not (functional-has-external-references-p leaf)))
622 (setf (functional-kind leaf) :deleted)
623 (setf (lambda-bind leaf) nil)
624 (dolist (let (lambda-lets leaf))
625 (setf (lambda-bind let) nil)
626 (setf (functional-kind let) :deleted))
628 (if (member kind '(:let :mv-let :assignment))
629 (let ((home (lambda-home leaf)))
630 (setf (lambda-lets home) (delete leaf (lambda-lets home))))
631 (let* ((bind-block (node-block bind))
632 (component (block-component bind-block))
633 (return (lambda-return leaf)))
634 (aver (null (leaf-refs leaf)))
635 (unless (leaf-ever-used leaf)
636 (let ((*compiler-error-context* bind))
637 (compiler-note "deleting unused function~:[.~;~:*~% ~S~]"
638 (leaf-debug-name leaf))))
639 (unlink-blocks (component-head component) bind-block)
641 (unlink-blocks (node-block return) (component-tail component)))
642 (setf (component-reanalyze component) t)
643 (let ((tails (lambda-tail-set leaf)))
644 (setf (tail-set-funs tails)
645 (delete leaf (tail-set-funs tails)))
646 (setf (lambda-tail-set leaf) nil))
647 (setf (component-lambdas component)
648 (delete leaf (component-lambdas component)))))
650 (when (eq kind :external)
651 (let ((fun (functional-entry-fun leaf)))
652 (setf (functional-entry-fun fun) nil)
653 (when (optional-dispatch-p fun)
654 (delete-optional-dispatch fun)))))
658 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
659 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
660 ;;; is used both before and after local call analysis. Afterward, all
661 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
662 ;;; to the XEP, leaving it with no references at all. So we look at
663 ;;; the XEP to see whether an optional-dispatch is still really being
664 ;;; used. But before local call analysis, there are no XEPs, and all
665 ;;; references are direct.
667 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
668 ;;; entry-points, making them be normal lambdas, and then deleting the
669 ;;; ones with no references. This deletes any e-p lambdas that were
670 ;;; either never referenced, or couldn't be deleted when the last
671 ;;; deference was deleted (due to their :OPTIONAL kind.)
673 ;;; Note that the last optional ep may alias the main entry, so when
674 ;;; we process the main entry, its kind may have been changed to NIL
675 ;;; or even converted to a let.
676 (defun delete-optional-dispatch (leaf)
677 (declare (type optional-dispatch leaf))
678 (let ((entry (functional-entry-fun leaf)))
679 (unless (and entry (leaf-refs entry))
680 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
681 (setf (functional-kind leaf) :deleted)
684 (unless (eq (functional-kind fun) :deleted)
685 (aver (eq (functional-kind fun) :optional))
686 (setf (functional-kind fun) nil)
687 (let ((refs (leaf-refs fun)))
691 (or (maybe-let-convert fun)
692 (maybe-convert-to-assignment fun)))
694 (maybe-convert-to-assignment fun)))))))
696 (dolist (ep (optional-dispatch-entry-points leaf))
698 (when (optional-dispatch-more-entry leaf)
699 (frob (optional-dispatch-more-entry leaf)))
700 (let ((main (optional-dispatch-main-entry leaf)))
701 (when (eq (functional-kind main) :optional)
706 ;;; Do stuff to delete the semantic attachments of a REF node. When
707 ;;; this leaves zero or one reference, we do a type dispatch off of
708 ;;; the leaf to determine if a special action is appropriate.
709 (defun delete-ref (ref)
710 (declare (type ref ref))
711 (let* ((leaf (ref-leaf ref))
712 (refs (delete ref (leaf-refs leaf))))
713 (setf (leaf-refs leaf) refs)
718 (delete-lambda-var leaf))
720 (ecase (functional-kind leaf)
721 ((nil :let :mv-let :assignment :escape :cleanup)
722 (aver (not (functional-entry-fun leaf)))
723 (delete-lambda leaf))
725 (delete-lambda leaf))
726 ((:deleted :optional))))
728 (unless (eq (functional-kind leaf) :deleted)
729 (delete-optional-dispatch leaf)))))
732 (clambda (or (maybe-let-convert leaf)
733 (maybe-convert-to-assignment leaf)))
734 (lambda-var (reoptimize-lambda-var leaf))))
737 (clambda (maybe-convert-to-assignment leaf))))))
741 ;;; This function is called by people who delete nodes; it provides a
742 ;;; way to indicate that the value of a continuation is no longer
743 ;;; used. We null out the CONTINUATION-DEST, set FLUSH-P in the blocks
744 ;;; containing uses of CONT and set COMPONENT-REOPTIMIZE. If the PREV
745 ;;; of the use is deleted, then we blow off reoptimization.
747 ;;; If the continuation is :Deleted, then we don't do anything, since
748 ;;; all semantics have already been flushed. :DELETED-BLOCK-START
749 ;;; start continuations are treated just like :BLOCK-START; it is
750 ;;; possible that the continuation may be given a new dest (e.g. by
751 ;;; SUBSTITUTE-CONTINUATION), so we don't want to delete it.
752 (defun flush-dest (cont)
753 (declare (type continuation cont))
755 (unless (eq (continuation-kind cont) :deleted)
756 (aver (continuation-dest cont))
757 (setf (continuation-dest cont) nil)
759 (let ((prev (node-prev use)))
760 (unless (eq (continuation-kind prev) :deleted)
761 (let ((block (continuation-block prev)))
762 (setf (component-reoptimize (block-component block)) t)
763 (setf (block-attributep (block-flags block) flush-p type-asserted)
766 (setf (continuation-%type-check cont) nil)
770 ;;; Do a graph walk backward from BLOCK, marking all predecessor
771 ;;; blocks with the DELETE-P flag.
772 (defun mark-for-deletion (block)
773 (declare (type cblock block))
774 (unless (block-delete-p block)
775 (setf (block-delete-p block) t)
776 (setf (component-reanalyze (block-component block)) t)
777 (dolist (pred (block-pred block))
778 (mark-for-deletion pred)))
781 ;;; Delete CONT, eliminating both control and value semantics. We set
782 ;;; FLUSH-P and COMPONENT-REOPTIMIZE similarly to in FLUSH-DEST. Here
783 ;;; we must get the component from the use block, since the
784 ;;; continuation may be a :DELETED-BLOCK-START.
786 ;;; If CONT has DEST, then it must be the case that the DEST is
787 ;;; unreachable, since we can't compute the value desired. In this
788 ;;; case, we call MARK-FOR-DELETION to cause the DEST block and its
789 ;;; predecessors to tell people to ignore them, and to cause them to
790 ;;; be deleted eventually.
791 (defun delete-continuation (cont)
792 (declare (type continuation cont))
793 (aver (not (eq (continuation-kind cont) :deleted)))
796 (let ((prev (node-prev use)))
797 (unless (eq (continuation-kind prev) :deleted)
798 (let ((block (continuation-block prev)))
799 (setf (block-attributep (block-flags block) flush-p type-asserted) t)
800 (setf (component-reoptimize (block-component block)) t)))))
802 (let ((dest (continuation-dest cont)))
804 (let ((prev (node-prev dest)))
806 (not (eq (continuation-kind prev) :deleted)))
807 (let ((block (continuation-block prev)))
808 (unless (block-delete-p block)
809 (mark-for-deletion block)))))))
811 (setf (continuation-kind cont) :deleted)
812 (setf (continuation-dest cont) nil)
813 (setf (continuation-next cont) nil)
814 (setf (continuation-asserted-type cont) *empty-type*)
815 (setf (continuation-%derived-type cont) *empty-type*)
816 (setf (continuation-use cont) nil)
817 (setf (continuation-block cont) nil)
818 (setf (continuation-reoptimize cont) nil)
819 (setf (continuation-%type-check cont) nil)
820 (setf (continuation-info cont) nil)
824 ;;; This function does what is necessary to eliminate the code in it
825 ;;; from the IR1 representation. This involves unlinking it from its
826 ;;; predecessors and successors and deleting various node-specific
827 ;;; semantic information.
829 ;;; We mark the START as has having no next and remove the last node
830 ;;; from its CONT's uses. We also flush the DEST for all continuations
831 ;;; whose values are received by nodes in the block.
832 (defun delete-block (block)
833 (declare (type cblock block))
834 (aver (block-component block)) ; else block is already deleted!
835 (note-block-deletion block)
836 (setf (block-delete-p block) t)
838 (let* ((last (block-last block))
839 (cont (node-cont last)))
840 (delete-continuation-use last)
841 (if (eq (continuation-kind cont) :unused)
842 (delete-continuation cont)
843 (reoptimize-continuation cont)))
845 (dolist (b (block-pred block))
846 (unlink-blocks b block))
847 (dolist (b (block-succ block))
848 (unlink-blocks block b))
850 (do-nodes (node cont block)
852 (ref (delete-ref node))
854 (flush-dest (if-test node)))
855 ;; The next two cases serve to maintain the invariant that a LET
856 ;; always has a well-formed COMBINATION, REF and BIND. We delete
857 ;; the lambda whenever we delete any of these, but we must be
858 ;; careful that this LET has not already been partially deleted.
860 (when (and (eq (basic-combination-kind node) :local)
861 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
862 (continuation-use (basic-combination-fun node)))
863 (let ((fun (combination-lambda node)))
864 ;; If our REF was the 2'nd to last ref, and has been deleted, then
865 ;; Fun may be a LET for some other combination.
866 (when (and (member (functional-kind fun) '(:let :mv-let))
867 (eq (let-combination fun) node))
868 (delete-lambda fun))))
869 (flush-dest (basic-combination-fun node))
870 (dolist (arg (basic-combination-args node))
871 (when arg (flush-dest arg))))
873 (let ((lambda (bind-lambda node)))
874 (unless (eq (functional-kind lambda) :deleted)
875 (aver (member (functional-kind lambda) '(:let :mv-let :assignment)))
876 (delete-lambda lambda))))
878 (let ((value (exit-value node))
879 (entry (exit-entry node)))
883 (setf (entry-exits entry)
884 (delete node (entry-exits entry))))))
886 (flush-dest (return-result node))
887 (delete-return node))
889 (flush-dest (set-value node))
890 (let ((var (set-var node)))
891 (setf (basic-var-sets var)
892 (delete node (basic-var-sets var))))))
894 (delete-continuation (node-prev node)))
896 (remove-from-dfo block)
899 ;;; Do stuff to indicate that the return node Node is being deleted.
900 ;;; We set the RETURN to NIL.
901 (defun delete-return (node)
902 (declare (type creturn node))
903 (let ((fun (return-lambda node)))
904 (aver (lambda-return fun))
905 (setf (lambda-return fun) nil))
908 ;;; If any of the VARS in FUN was never referenced and was not
909 ;;; declared IGNORE, then complain.
910 (defun note-unreferenced-vars (fun)
911 (declare (type clambda fun))
912 (dolist (var (lambda-vars fun))
913 (unless (or (leaf-ever-used var)
914 (lambda-var-ignorep var))
915 (let ((*compiler-error-context* (lambda-bind fun)))
916 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
917 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
918 ;; requires this to be no more than a STYLE-WARNING.
919 (compiler-style-warning "The variable ~S is defined but never used."
920 (leaf-debug-name var)))
921 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
924 (defvar *deletion-ignored-objects* '(t nil))
926 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
927 ;;; our recursion so that we don't get lost in circular structures. We
928 ;;; ignore the car of forms if they are a symbol (to prevent confusing
929 ;;; function referencess with variables), and we also ignore anything
931 (defun present-in-form (obj form depth)
932 (declare (type (integer 0 20) depth))
933 (cond ((= depth 20) nil)
937 (let ((first (car form))
939 (if (member first '(quote function))
941 (or (and (not (symbolp first))
942 (present-in-form obj first depth))
943 (do ((l (cdr form) (cdr l))
945 ((or (atom l) (> n 100))
948 (when (present-in-form obj (car l) depth)
951 ;;; This function is called on a block immediately before we delete
952 ;;; it. We check to see whether any of the code about to die appeared
953 ;;; in the original source, and emit a note if so.
955 ;;; If the block was in a lambda is now deleted, then we ignore the
956 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
957 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
958 ;;; reasonable for a function to not return, and there is a different
959 ;;; note for that case anyway.
961 ;;; If the actual source is an atom, then we use a bunch of heuristics
962 ;;; to guess whether this reference really appeared in the original
964 ;;; -- If a symbol, it must be interned and not a keyword.
965 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
967 ;;; -- The atom must be "present" in the original source form, and
968 ;;; present in all intervening actual source forms.
969 (defun note-block-deletion (block)
970 (let ((home (block-home-lambda block)))
971 (unless (eq (functional-kind home) :deleted)
972 (do-nodes (node cont block)
973 (let* ((path (node-source-path node))
974 (first (first path)))
975 (when (or (eq first 'original-source-start)
977 (or (not (symbolp first))
978 (let ((pkg (symbol-package first)))
980 (not (eq pkg (symbol-package :end))))))
981 (not (member first *deletion-ignored-objects*))
982 (not (typep first '(or fixnum character)))
984 (present-in-form first x 0))
985 (source-path-forms path))
986 (present-in-form first (find-original-source path)
988 (unless (return-p node)
989 (let ((*compiler-error-context* node))
990 (compiler-note "deleting unreachable code")))
994 ;;; Delete a node from a block, deleting the block if there are no
995 ;;; nodes left. We remove the node from the uses of its CONT, but we
996 ;;; don't deal with cleaning up any type-specific semantic
997 ;;; attachments. If the CONT is :UNUSED after deleting this use, then
998 ;;; we delete CONT. (Note :UNUSED is not the same as no uses. A
999 ;;; continuation will only become :UNUSED if it was :INSIDE-BLOCK
1002 ;;; If the node is the last node, there must be exactly one successor.
1003 ;;; We link all of our precedessors to the successor and unlink the
1004 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1005 ;;; left, and the block is a successor of itself, then we replace the
1006 ;;; only node with a degenerate exit node. This provides a way to
1007 ;;; represent the bodyless infinite loop, given the prohibition on
1008 ;;; empty blocks in IR1.
1009 (defun unlink-node (node)
1010 (declare (type node node))
1011 (let* ((cont (node-cont node))
1012 (next (continuation-next cont))
1013 (prev (node-prev node))
1014 (block (continuation-block prev))
1015 (prev-kind (continuation-kind prev))
1016 (last (block-last block)))
1018 (unless (eq (continuation-kind cont) :deleted)
1019 (delete-continuation-use node)
1020 (when (eq (continuation-kind cont) :unused)
1021 (aver (not (continuation-dest cont)))
1022 (delete-continuation cont)))
1024 (setf (block-type-asserted block) t)
1025 (setf (block-test-modified block) t)
1027 (cond ((or (eq prev-kind :inside-block)
1028 (and (eq prev-kind :block-start)
1029 (not (eq node last))))
1030 (cond ((eq node last)
1031 (setf (block-last block) (continuation-use prev))
1032 (setf (continuation-next prev) nil))
1034 (setf (continuation-next prev) next)
1035 (setf (node-prev next) prev)))
1036 (setf (node-prev node) nil)
1039 (aver (eq prev-kind :block-start))
1040 (aver (eq node last))
1041 (let* ((succ (block-succ block))
1042 (next (first succ)))
1043 (aver (and succ (null (cdr succ))))
1045 ((member block succ)
1046 (with-ir1-environment node
1047 (let ((exit (make-exit))
1048 (dummy (make-continuation)))
1049 (setf (continuation-next prev) nil)
1050 (prev-link exit prev)
1051 (add-continuation-use exit dummy)
1052 (setf (block-last block) exit)))
1053 (setf (node-prev node) nil)
1056 (aver (eq (block-start-cleanup block)
1057 (block-end-cleanup block)))
1058 (unlink-blocks block next)
1059 (dolist (pred (block-pred block))
1060 (change-block-successor pred block next))
1061 (remove-from-dfo block)
1062 (cond ((continuation-dest prev)
1063 (setf (continuation-next prev) nil)
1064 (setf (continuation-kind prev) :deleted-block-start))
1066 (delete-continuation prev)))
1067 (setf (node-prev node) nil)
1070 ;;; Return true if NODE has been deleted, false if it is still a valid
1072 (defun node-deleted (node)
1073 (declare (type node node))
1074 (let ((prev (node-prev node)))
1076 (not (eq (continuation-kind prev) :deleted))
1077 (let ((block (continuation-block prev)))
1078 (and (block-component block)
1079 (not (block-delete-p block))))))))
1081 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1082 ;;; marking the blocks as delete-p to prevent weird stuff from being
1083 ;;; triggered by deletion.
1084 (defun delete-component (component)
1085 (declare (type component component))
1086 (aver (null (component-new-funs component)))
1087 (setf (component-kind component) :deleted)
1088 (do-blocks (block component)
1089 (setf (block-delete-p block) t))
1090 (dolist (fun (component-lambdas component))
1091 (setf (functional-kind fun) nil)
1092 (setf (functional-entry-fun fun) nil)
1093 (setf (leaf-refs fun) nil)
1094 (delete-functional fun))
1095 (do-blocks (block component)
1096 (delete-block block))
1099 ;;; Convert code of the form
1100 ;;; (FOO ... (FUN ...) ...)
1102 ;;; (FOO ... ... ...).
1103 ;;; In other words, replace the function combination FUN by its
1104 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1105 ;;; to blow out of whatever transform called this. Note, as the number
1106 ;;; of arguments changes, the transform must be prepared to return a
1107 ;;; lambda with a new lambda-list with the correct number of
1109 (defun extract-function-args (cont fun num-args)
1111 "If CONT is a call to FUN with NUM-ARGS args, change those arguments
1112 to feed directly to the continuation-dest of CONT, which must be
1114 (declare (type continuation cont)
1116 (type index num-args))
1117 (let ((outside (continuation-dest cont))
1118 (inside (continuation-use cont)))
1119 (aver (combination-p outside))
1120 (unless (combination-p inside)
1121 (give-up-ir1-transform))
1122 (let ((inside-fun (combination-fun inside)))
1123 (unless (eq (continuation-fun-name inside-fun) fun)
1124 (give-up-ir1-transform))
1125 (let ((inside-args (combination-args inside)))
1126 (unless (= (length inside-args) num-args)
1127 (give-up-ir1-transform))
1128 (let* ((outside-args (combination-args outside))
1129 (arg-position (position cont outside-args))
1130 (before-args (subseq outside-args 0 arg-position))
1131 (after-args (subseq outside-args (1+ arg-position))))
1132 (dolist (arg inside-args)
1133 (setf (continuation-dest arg) outside))
1134 (setf (combination-args inside) nil)
1135 (setf (combination-args outside)
1136 (append before-args inside-args after-args))
1137 (change-ref-leaf (continuation-use inside-fun)
1138 (find-free-function 'list "???"))
1139 (setf (combination-kind inside) :full)
1140 (setf (node-derived-type inside) *wild-type*)
1142 (setf (continuation-asserted-type cont) *wild-type*)
1147 ;;; Change the LEAF that a REF refers to.
1148 (defun change-ref-leaf (ref leaf)
1149 (declare (type ref ref) (type leaf leaf))
1150 (unless (eq (ref-leaf ref) leaf)
1151 (push ref (leaf-refs leaf))
1153 (setf (ref-leaf ref) leaf)
1154 (let ((ltype (leaf-type leaf)))
1155 (if (fun-type-p ltype)
1156 (setf (node-derived-type ref) ltype)
1157 (derive-node-type ref ltype)))
1158 (reoptimize-continuation (node-cont ref)))
1161 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1162 (defun substitute-leaf (new-leaf old-leaf)
1163 (declare (type leaf new-leaf old-leaf))
1164 (dolist (ref (leaf-refs old-leaf))
1165 (change-ref-leaf ref new-leaf))
1168 ;;; Like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1169 ;;; whether to substitute.
1170 (defun substitute-leaf-if (test new-leaf old-leaf)
1171 (declare (type leaf new-leaf old-leaf) (type function test))
1172 (dolist (ref (leaf-refs old-leaf))
1173 (when (funcall test ref)
1174 (change-ref-leaf ref new-leaf)))
1177 ;;; Return a LEAF which represents the specified constant object. If
1178 ;;; the object is not in *CONSTANTS*, then we create a new constant
1179 ;;; LEAF and enter it.
1180 (defun find-constant (object)
1182 ;; FIXME: What is the significance of this test? ("things
1183 ;; that are worth uniquifying"?)
1184 '(or symbol number character instance))
1185 (or (gethash object *constants*)
1186 (setf (gethash object *constants*)
1187 (make-constant :value object
1188 :%source-name '.anonymous.
1189 :type (ctype-of object)
1190 :where-from :defined)))
1191 (make-constant :value object
1192 :%source-name '.anonymous.
1193 :type (ctype-of object)
1194 :where-from :defined)))
1196 ;;; If there is a non-local exit noted in ENTRY's environment that
1197 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1198 (defun find-nlx-info (entry cont)
1199 (declare (type entry entry) (type continuation cont))
1200 (let ((entry-cleanup (entry-cleanup entry)))
1201 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1202 (when (and (eq (nlx-info-continuation nlx) cont)
1203 (eq (nlx-info-cleanup nlx) entry-cleanup))
1206 ;;;; functional hackery
1208 (declaim (ftype (function (functional) clambda) main-entry))
1209 (defun main-entry (functional)
1210 (etypecase functional
1211 (clambda functional)
1213 (optional-dispatch-main-entry functional))))
1215 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1216 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1217 ;;; optional with null default and no SUPPLIED-P. There must be a
1218 ;;; &REST arg with no references.
1219 (declaim (ftype (function (functional) boolean) looks-like-an-mv-bind))
1220 (defun looks-like-an-mv-bind (functional)
1221 (and (optional-dispatch-p functional)
1222 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1224 (let ((info (lambda-var-arg-info (car arg))))
1225 (unless info (return nil))
1226 (case (arg-info-kind info)
1228 (when (or (arg-info-supplied-p info) (arg-info-default info))
1231 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1235 ;;; Return true if function is an XEP. This is true of normal XEPs
1236 ;;; (:EXTERNAL kind) and top level lambdas (:TOPLEVEL kind.)
1237 (defun external-entry-point-p (fun)
1238 (declare (type functional fun))
1239 (not (null (member (functional-kind fun) '(:external :toplevel)))))
1241 ;;; If CONT's only use is a non-notinline global function reference,
1242 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1243 ;;; is true, then we don't care if the leaf is NOTINLINE.
1244 (defun continuation-fun-name (cont &optional notinline-ok)
1245 (declare (type continuation cont))
1246 (let ((use (continuation-use cont)))
1248 (let ((leaf (ref-leaf use)))
1249 (if (and (global-var-p leaf)
1250 (eq (global-var-kind leaf) :global-function)
1251 (or (not (defined-fun-p leaf))
1252 (not (eq (defined-fun-inlinep leaf) :notinline))
1254 (leaf-source-name leaf)
1258 ;;; Return the COMBINATION node that is the call to the LET FUN.
1259 (defun let-combination (fun)
1260 (declare (type clambda fun))
1261 (aver (member (functional-kind fun) '(:let :mv-let)))
1262 (continuation-dest (node-cont (first (leaf-refs fun)))))
1264 ;;; Return the initial value continuation for a LET variable, or NIL
1265 ;;; if there is none.
1266 (defun let-var-initial-value (var)
1267 (declare (type lambda-var var))
1268 (let ((fun (lambda-var-home var)))
1269 (elt (combination-args (let-combination fun))
1270 (position-or-lose var (lambda-vars fun)))))
1272 ;;; Return the LAMBDA that is called by the local CALL.
1273 (defun combination-lambda (call)
1274 (declare (type basic-combination call))
1275 (aver (eq (basic-combination-kind call) :local))
1276 (ref-leaf (continuation-use (basic-combination-fun call))))
1278 (defvar *inline-expansion-limit* 200
1280 "an upper limit on the number of inline function calls that will be expanded
1281 in any given code object (single function or block compilation)")
1283 ;;; Check whether NODE's component has exceeded its inline expansion
1284 ;;; limit, and warn if so, returning NIL.
1285 (defun inline-expansion-ok (node)
1286 (let ((expanded (incf (component-inline-expansions
1288 (node-block node))))))
1289 (cond ((> expanded *inline-expansion-limit*) nil)
1290 ((= expanded *inline-expansion-limit*)
1291 ;; FIXME: If the objective is to stop the recursive
1292 ;; expansion of inline functions, wouldn't it be more
1293 ;; correct to look back through surrounding expansions
1294 ;; (which are, I think, stored in the *CURRENT-PATH*, and
1295 ;; possibly stored elsewhere too) and suppress expansion
1296 ;; and print this warning when the function being proposed
1297 ;; for inline expansion is found there? (I don't like the
1298 ;; arbitrary numerical limit in principle, and I think
1299 ;; it'll be a nuisance in practice if we ever want the
1300 ;; compiler to be able to use WITH-COMPILATION-UNIT on
1301 ;; arbitrarily huge blocks of code. -- WHN)
1302 (let ((*compiler-error-context* node))
1303 (compiler-note "*INLINE-EXPANSION-LIMIT* (~D) was exceeded, ~
1304 probably trying to~% ~
1305 inline a recursive function."
1306 *inline-expansion-limit*))
1312 ;;; Apply a function to some arguments, returning a list of the values
1313 ;;; resulting of the evaluation. If an error is signalled during the
1314 ;;; application, then we print a warning message and return NIL as our
1315 ;;; second value to indicate this. Node is used as the error context
1316 ;;; for any error message, and Context is a string that is spliced
1317 ;;; into the warning.
1318 (declaim (ftype (function ((or symbol function) list node string)
1319 (values list boolean))
1321 (defun careful-call (function args node context)
1323 (multiple-value-list
1324 (handler-case (apply function args)
1326 (let ((*compiler-error-context* node))
1327 (compiler-warning "Lisp error during ~A:~%~A" context condition)
1328 (return-from careful-call (values nil nil))))))
1331 ;;;; utilities used at run-time for parsing &KEY args in IR1
1333 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1334 ;;; the continuation for the value of the &KEY argument KEY in the
1335 ;;; list of continuations ARGS. It returns the continuation if the
1336 ;;; keyword is present, or NIL otherwise. The legality and
1337 ;;; constantness of the keywords should already have been checked.
1338 (declaim (ftype (function (list keyword) (or continuation null))
1339 find-keyword-continuation))
1340 (defun find-keyword-continuation (args key)
1341 (do ((arg args (cddr arg)))
1343 (when (eq (continuation-value (first arg)) key)
1344 (return (second arg)))))
1346 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1347 ;;; verify that alternating continuations in ARGS are constant and
1348 ;;; that there is an even number of args.
1349 (declaim (ftype (function (list) boolean) check-key-args-constant))
1350 (defun check-key-args-constant (args)
1351 (do ((arg args (cddr arg)))
1353 (unless (and (rest arg)
1354 (constant-continuation-p (first arg)))
1357 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1358 ;;; verify that the list of continuations ARGS is a well-formed &KEY
1359 ;;; arglist and that only keywords present in the list KEYS are
1361 (declaim (ftype (function (list list) boolean) check-transform-keys))
1362 (defun check-transform-keys (args keys)
1363 (and (check-key-args-constant args)
1364 (do ((arg args (cddr arg)))
1366 (unless (member (continuation-value (first arg)) keys)
1371 ;;; Called by the expansion of the EVENT macro.
1372 (declaim (ftype (function (event-info (or node null)) *) %event))
1373 (defun %event (info node)
1374 (incf (event-info-count info))
1375 (when (and (>= (event-info-level info) *event-note-threshold*)
1376 (policy (or node *lexenv*)
1377 (= inhibit-warnings 0)))
1378 (let ((*compiler-error-context* node))
1379 (compiler-note (event-info-description info))))
1381 (let ((action (event-info-action info)))
1382 (when action (funcall action node))))