0.6.12.49:
[sbcl.git] / src / compiler / node.lisp
1 ;;;; structures for the first intermediate representation in the
2 ;;;; compiler, IR1
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!C")
14
15 ;;; The front-end data structure (IR1) is composed of nodes and
16 ;;; continuations. The general idea is that continuations contain
17 ;;; top-down information and nodes contain bottom-up, derived
18 ;;; information. A continuation represents a place in the code, while
19 ;;; a node represents code that does something.
20 ;;;
21 ;;; This representation is more of a flow-graph than an augmented
22 ;;; syntax tree. The evaluation order is explicitly represented in the
23 ;;; linkage by continuations, rather than being implicit in the nodes
24 ;;; which receive the the results of evaluation. This allows us to
25 ;;; decouple the flow of results from the flow of control. A
26 ;;; continuation represents both, but the continuation can represent
27 ;;; the case of a discarded result by having no DEST.
28
29 (def!struct (continuation
30              (:make-load-form-fun ignore-it)
31              (:constructor make-continuation (&optional dest)))
32   ;; An indication of the way that this continuation is currently used:
33   ;;
34   ;; :UNUSED
35   ;;    A continuation for which all control-related slots have the
36   ;;    default values. A continuation is unused during IR1 conversion
37   ;;    until it is assigned a block, and may be also be temporarily
38   ;;    unused during later manipulations of IR1. In a consistent
39   ;;    state there should never be any mention of :UNUSED
40   ;;    continuations. Next can have a non-null value if the next node
41   ;;    has already been determined.
42   ;;
43   ;; :DELETED
44   ;;    A continuation that has been deleted from IR1. Any pointers into
45   ;;    IR1 are cleared. There are two conditions under which a deleted
46   ;;    continuation may appear in code:
47   ;;     -- The CONT of the LAST node in a block may be a deleted
48   ;;        continuation when the original receiver of the continuation's
49   ;;        value was deleted. Note that DEST in a deleted continuation is
50   ;;        null, so it is easy to know not to attempt delivering any
51   ;;        values to the continuation.
52   ;;     -- Unreachable code that hasn't been deleted yet may receive
53   ;;        deleted continuations. All such code will be in blocks that
54   ;;        have DELETE-P set. All unreachable code is deleted by control
55   ;;        optimization, so the backend doesn't have to worry about this.
56   ;;
57   ;; :BLOCK-START
58   ;;    The continuation that is the START of BLOCK. This is the only kind
59   ;;    of continuation that can have more than one use. The BLOCK's
60   ;;    START-USES is a list of all the uses.
61   ;;
62   ;; :DELETED-BLOCK-START
63   ;;    Like :BLOCK-START, but BLOCK has been deleted. A block
64   ;;    starting continuation is made into a deleted block start when
65   ;;    the block is deleted, but the continuation still may have
66   ;;    value semantics. Since there isn't any code left, next is
67   ;;    null.
68   ;;
69   ;; :INSIDE-BLOCK
70   ;;    A continuation that is the CONT of some node in BLOCK.
71   (kind :unused :type (member :unused :deleted :inside-block :block-start
72                               :deleted-block-start))
73   ;; The node which receives this value, if any. In a deleted continuation,
74   ;; this is null even though the node that receives this continuation may not
75   ;; yet be deleted.
76   (dest nil :type (or node null))
77   ;; If this is a NODE, then it is the node which is to be evaluated
78   ;; next. This is always null in :DELETED and :UNUSED continuations,
79   ;; and will be null in a :INSIDE-BLOCK continuation when this is the
80   ;; CONT of the LAST.
81   (next nil :type (or node null))
82   ;; an assertion on the type of this continuation's value
83   (asserted-type *wild-type* :type ctype)
84   ;; cached type of this continuation's value. If NIL, then this must
85   ;; be recomputed: see CONTINUATION-DERIVED-TYPE.
86   (%derived-type nil :type (or ctype null))
87   ;; Node where this continuation is used, if unique. This is always
88   ;; null in :DELETED and :UNUSED continuations, and is never null in
89   ;; :INSIDE-BLOCK continuations. In a :BLOCK-START continuation, the
90   ;; Block's START-USES indicate whether NIL means no uses or more
91   ;; than one use.
92   (use nil :type (or node null))
93   ;; the basic block this continuation is in. This is null only in
94   ;; :DELETED and :UNUSED continuations. Note that blocks that are
95   ;; unreachable but still in the DFO may receive deleted
96   ;; continuations, so it isn't o.k. to assume that any continuation
97   ;; that you pick up out of its DEST node has a BLOCK.
98   (block nil :type (or cblock null))
99   ;; set to true when something about this continuation's value has
100   ;; changed. See REOPTIMIZE-CONTINUATION. This provides a way for IR1
101   ;; optimize to determine which operands to a node have changed. If
102   ;; the optimizer for this node type doesn't care, it can elect not
103   ;; to clear this flag.
104   (reoptimize t :type boolean)
105   ;; an indication of what we have proven about how this contination's
106   ;; type assertion is satisfied:
107   ;;
108   ;; NIL
109   ;;    No type check is necessary (proven type is a subtype of the assertion.)
110   ;;
111   ;; T
112   ;;    A type check is needed.
113   ;;
114   ;; :DELETED
115   ;;    Don't do a type check, but believe (intersect) the assertion.
116   ;;    A T check can be changed to :DELETED if we somehow prove the
117   ;;    check is unnecessary, or if we eliminate it through a policy
118   ;;    decision.
119   ;;
120   ;; :NO-CHECK
121   ;;    Type check generation sets the slot to this if a check is
122   ;;    called for, but it believes it has proven that the check won't
123   ;;    be done for policy reasons or because a safe implementation
124   ;;    will be used. In the latter case, LTN must ensure that a safe
125   ;;    implementation *is* used.
126   ;;
127   ;; :ERROR
128   ;;    There is a compile-time type error in some use of this
129   ;;    continuation. A type check should still be generated, but be
130   ;;    careful.
131   ;;
132   ;; This is computed lazily by CONTINUATION-DERIVED-TYPE, so use
133   ;; CONTINUATION-TYPE-CHECK instead of the %'ed slot accessor.
134   (%type-check t :type (member t nil :deleted :no-check :error))
135   ;; something or other that the back end annotates this continuation with
136   (info nil)
137   ;; uses of this continuation in the lexical environment. They are
138   ;; recorded so that when one continuation is substituted for another
139   ;; the environment may be updated properly.
140   (lexenv-uses nil :type list))
141
142 (def!method print-object ((x continuation) stream)
143   (print-unreadable-object (x stream :type t :identity t)))
144
145 (defstruct (node (:constructor nil)
146                  (:copier nil))
147   ;; the bottom-up derived type for this node. This does not take into
148   ;; consideration output type assertions on this node (actually on its CONT).
149   (derived-type *wild-type* :type ctype)
150   ;; True if this node needs to be optimized. This is set to true
151   ;; whenever something changes about the value of a continuation
152   ;; whose DEST is this node.
153   (reoptimize t :type boolean)
154   ;; the continuation which receives the value of this node. This also
155   ;; indicates what we do controlwise after evaluating this node. This
156   ;; may be null during IR1 conversion.
157   (cont nil :type (or continuation null))
158   ;; the continuation that this node is the next of. This is null
159   ;; during IR1 conversion when we haven't linked the node in yet or
160   ;; in nodes that have been deleted from the IR1 by UNLINK-NODE.
161   (prev nil :type (or continuation null))
162   ;; the lexical environment this node was converted in
163   (lexenv *lexenv* :type lexenv)
164   ;; a representation of the source code responsible for generating
165   ;; this node
166   ;;
167   ;; For a form introduced by compilation (does not appear in the
168   ;; original source), the path begins with a list of all the
169   ;; enclosing introduced forms. This list is from the inside out,
170   ;; with the form immediately responsible for this node at the head
171   ;; of the list.
172   ;;
173   ;; Following the introduced forms is a representation of the
174   ;; location of the enclosing original source form. This transition
175   ;; is indicated by the magic ORIGINAL-SOURCE-START marker. The first
176   ;; element of the original source is the "form number", which is the
177   ;; ordinal number of this form in a depth-first, left-to-right walk
178   ;; of the truly top-level form in which this appears.
179   ;;
180   ;; Following is a list of integers describing the path taken through
181   ;; the source to get to this point:
182   ;;     (K L M ...) => (NTH K (NTH L (NTH M ...)))
183   ;;
184   ;; The last element in the list is the top-level form number, which
185   ;; is the ordinal number (in this call to the compiler) of the truly
186   ;; top-level form containing the original source.
187   (source-path *current-path* :type list)
188   ;; If this node is in a tail-recursive position, then this is set to
189   ;; T. At the end of IR1 (in environment analysis) this is computed
190   ;; for all nodes (after cleanup code has been emitted). Before then,
191   ;; a non-null value indicates that IR1 optimization has converted a
192   ;; tail local call to a direct transfer.
193   ;;
194   ;; If the back-end breaks tail-recursion for some reason, then it
195   ;; can null out this slot.
196   (tail-p nil :type boolean))
197
198 ;;; Flags that are used to indicate various things about a block, such
199 ;;; as what optimizations need to be done on it:
200 ;;; -- REOPTIMIZE is set when something interesting happens the uses of a
201 ;;;    continuation whose Dest is in this block. This indicates that the
202 ;;;    value-driven (forward) IR1 optimizations should be done on this block.
203 ;;; -- FLUSH-P is set when code in this block becomes potentially flushable,
204 ;;;    usually due to a continuation's DEST becoming null.
205 ;;; -- TYPE-CHECK is true when the type check phase should be run on this
206 ;;;    block. IR1 optimize can introduce new blocks after type check has
207 ;;;    already run. We need to check these blocks, but there is no point in
208 ;;;    checking blocks we have already checked.
209 ;;; -- DELETE-P is true when this block is used to indicate that this block
210 ;;;    has been determined to be unreachable and should be deleted. IR1
211 ;;;    phases should not attempt to  examine or modify blocks with DELETE-P
212 ;;;    set, since they may:
213 ;;;     - be in the process of being deleted, or
214 ;;;     - have no successors, or
215 ;;;     - receive :DELETED continuations.
216 ;;; -- TYPE-ASSERTED, TEST-MODIFIED
217 ;;;    These flags are used to indicate that something in this block
218 ;;;    might be of interest to constraint propagation. TYPE-ASSERTED
219 ;;;    is set when a continuation type assertion is strengthened.
220 ;;;    TEST-MODIFIED is set whenever the test for the ending IF has
221 ;;;    changed (may be true when there is no IF.)
222 (def-boolean-attribute block
223   reoptimize flush-p type-check delete-p type-asserted test-modified)
224
225 (macrolet ((frob (slot)
226              `(defmacro ,(symbolicate "BLOCK-" slot) (block)
227                 `(block-attributep (block-flags ,block) ,',slot))))
228   (frob reoptimize)
229   (frob flush-p)
230   (frob type-check)
231   (frob delete-p)
232   (frob type-asserted)
233   (frob test-modified))
234
235 ;;; The CBLOCK structure represents a basic block. We include
236 ;;; SSET-ELEMENT so that we can have sets of blocks. Initially the
237 ;;; SSET-ELEMENT-NUMBER is null, DFO analysis numbers in reverse DFO.
238 ;;; During IR2 conversion, IR1 blocks are re-numbered in forward emit
239 ;;; order. This latter numbering also forms the basis of the block
240 ;;; numbering in the debug-info (though that is relative to the start
241 ;;; of the function.)
242 (defstruct (cblock (:include sset-element)
243                    (:constructor make-block (start))
244                    (:constructor make-block-key)
245                    (:conc-name block-)
246                    (:predicate block-p)
247                    (:copier copy-block))
248   ;; a list of all the blocks that are predecessors/successors of this
249   ;; block. In well-formed IR1, most blocks will have one successor.
250   ;; The only exceptions are:
251   ;;  1. component head blocks (any number)
252   ;;  2. blocks ending in an IF (1 or 2)
253   ;;  3. blocks with DELETE-P set (zero)
254   (pred nil :type list)
255   (succ nil :type list)
256   ;; the continuation which heads this block (either a :BLOCK-START or
257   ;; :DELETED-BLOCK-START), or NIL when we haven't made the start
258   ;; continuation yet (and in the dummy component head and tail
259   ;; blocks)
260   (start nil :type (or continuation null))
261   ;; a list of all the nodes that have START as their CONT
262   (start-uses nil :type list)
263   ;; the last node in this block. This is NIL when we are in the
264   ;; process of building a block (and in the dummy component head and
265   ;; tail blocks.)
266   (last nil :type (or node null))
267   ;; the forward and backward links in the depth-first ordering of the
268   ;; blocks. These slots are NIL at beginning/end.
269   (next nil :type (or null cblock))
270   (prev nil :type (or null cblock))
271   ;; This block's attributes: see above.
272   (flags (block-attributes reoptimize flush-p type-check type-asserted
273                            test-modified)
274          :type attributes)
275   ;; Some sets used by constraint propagation.
276   (kill nil)
277   (gen nil)
278   (in nil)
279   (out nil)
280   ;; the component this block is in, or NIL temporarily during IR1
281   ;; conversion and in deleted blocks
282   (component *current-component* :type (or component null))
283   ;; a flag used by various graph-walking code to determine whether
284   ;; this block has been processed already or what. We make this
285   ;; initially NIL so that FIND-INITIAL-DFO doesn't have to scan the
286   ;; entire initial component just to clear the flags.
287   (flag nil)
288   ;; Some kind of info used by the back end.
289   (info nil)
290   ;; If true, then constraints that hold in this block and its
291   ;; successors by merit of being tested by its IF predecessor.
292   (test-constraint nil :type (or sset null)))
293 (def!method print-object ((cblock cblock) stream)
294   (print-unreadable-object (cblock stream :type t :identity t)
295     (format stream ":START c~D" (cont-num (block-start cblock)))))
296
297 ;;; The Block-Annotation structure is shared (via :INCLUDE) by
298 ;;; different block-info annotation structures so that code
299 ;;; (specifically control analysis) can be shared.
300 (defstruct (block-annotation (:constructor nil)
301                              (:copier nil))
302   ;; The IR1 block that this block is in the INFO for.
303   (block (required-argument) :type cblock)
304   ;; the next and previous block in emission order (not DFO). This
305   ;; determines which block we drop though to, and also used to chain
306   ;; together overflow blocks that result from splitting of IR2 blocks
307   ;; in lifetime analysis.
308   (next nil :type (or block-annotation null))
309   (prev nil :type (or block-annotation null)))
310
311 ;;; The Component structure provides a handle on a connected piece of
312 ;;; the flow graph. Most of the passes in the compiler operate on
313 ;;; components rather than on the entire flow graph.
314 (defstruct (component (:copier nil))
315   ;; The kind of component:
316   ;;
317   ;; NIL
318   ;;     An ordinary component, containing non-top-level code.
319   ;;
320   ;; :Top-Level
321   ;;     A component containing only load-time code.
322   ;;
323   ;; :Complex-Top-Level
324   ;;     A component containing both top-level and run-time code.
325   ;;
326   ;; :Initial
327   ;;     The result of initial IR1 conversion, on which component
328   ;;     analysis has not been done.
329   ;;
330   ;; :Deleted
331   ;;     Debris left over from component analysis.
332   (kind nil :type (member nil :top-level :complex-top-level :initial :deleted))
333   ;; The blocks that are the dummy head and tail of the DFO.
334   ;; Entry/exit points have these blocks as their
335   ;; predecessors/successors. Null temporarily. The start and return
336   ;; from each non-deleted function is linked to the component head
337   ;; and tail. Until environment analysis links NLX entry stubs to the
338   ;; component head, every successor of the head is a function start
339   ;; (i.e. begins with a Bind node.)
340   (head nil :type (or null cblock))
341   (tail nil :type (or null cblock))
342   ;; A list of the CLambda structures for all functions in this
343   ;; component. Optional-Dispatches are represented only by their XEP
344   ;; and other associated lambdas. This doesn't contain any deleted or
345   ;; let lambdas.
346   (lambdas () :type list)
347   ;; A list of Functional structures for functions that are newly
348   ;; converted, and haven't been local-call analyzed yet. Initially
349   ;; functions are not in the Lambdas list. LOCAL-CALL-ANALYZE moves
350   ;; them there (possibly as LETs, or implicitly as XEPs if an
351   ;; OPTIONAL-DISPATCH.) Between runs of LOCAL-CALL-ANALYZE there may
352   ;; be some debris of converted or even deleted functions in this
353   ;; list.
354   (new-functions () :type list)
355   ;; If true, then there is stuff in this component that could benefit
356   ;; from further IR1 optimization.
357   (reoptimize t :type boolean)
358   ;; If true, then the control flow in this component was messed up by
359   ;; IR1 optimizations. The DFO should be recomputed.
360   (reanalyze nil :type boolean)
361   ;; String that is some sort of name for the code in this component.
362   (name "<unknown>" :type simple-string)
363   ;; Some kind of info used by the back end.
364   (info nil)
365   ;; The Source-Info structure describing where this component was
366   ;; compiled from.
367   (source-info *source-info* :type source-info)
368   ;; Count of the number of inline expansions we have done while
369   ;; compiling this component, to detect infinite or exponential
370   ;; blowups.
371   (inline-expansions 0 :type index)
372   ;; A hashtable from combination nodes to things describing how an
373   ;; optimization of the node failed. The value is an alist (Transform
374   ;; . Args), where Transform is the structure describing the
375   ;; transform that failed, and Args is either a list of format
376   ;; arguments for the note, or the FUNCTION-TYPE that would have
377   ;; enabled the transformation but failed to match.
378   (failed-optimizations (make-hash-table :test 'eq) :type hash-table)
379   ;; Similar to NEW-FUNCTIONS, but is used when a function has already
380   ;; been analyzed, but new references have been added by inline
381   ;; expansion. Unlike NEW-FUNCTIONS, this is not disjoint from
382   ;; COMPONENT-LAMBDAS.
383   (reanalyze-functions nil :type list))
384 (defprinter (component)
385   name
386   (reanalyze :test reanalyze))
387
388 ;;; The CLEANUP structure represents some dynamic binding action.
389 ;;; Blocks are annotated with the current cleanup so that dynamic
390 ;;; bindings can be removed when control is transferred out of the
391 ;;; binding environment. We arrange for changes in dynamic bindings to
392 ;;; happen at block boundaries, so that cleanup code may easily be
393 ;;; inserted. The "mess-up" action is explicitly represented by a
394 ;;; funny function call or Entry node.
395 ;;;
396 ;;; We guarantee that cleanups only need to be done at block boundaries
397 ;;; by requiring that the exit continuations initially head their
398 ;;; blocks, and then by not merging blocks when there is a cleanup
399 ;;; change.
400 (defstruct (cleanup (:copier nil))
401   ;; The kind of thing that has to be cleaned up.
402   (kind (required-argument)
403         :type (member :special-bind :catch :unwind-protect :block :tagbody))
404   ;; The node that messes things up. This is the last node in the
405   ;; non-messed-up environment. Null only temporarily. This could be
406   ;; deleted due to unreachability.
407   (mess-up nil :type (or node null))
408   ;; A list of all the NLX-Info structures whose NLX-Info-Cleanup is
409   ;; this cleanup. This is filled in by environment analysis.
410   (nlx-info nil :type list))
411 (defprinter (cleanup)
412   kind
413   mess-up
414   (nlx-info :test nlx-info))
415
416 ;;; The ENVIRONMENT structure represents the result of environment analysis.
417 (defstruct (environment (:copier nil))
418   ;; the function that allocates this environment
419   (function (required-argument) :type clambda)
420   ;; a list of all the lambdas that allocate variables in this environment
421   (lambdas nil :type list)
422   ;; a list of all the lambda-vars and NLX-Infos needed from enclosing
423   ;; environments by code in this environment
424   (closure nil :type list)
425   ;; a list of NLX-Info structures describing all the non-local exits
426   ;; into this environment
427   (nlx-info nil :type list)
428   ;; some kind of info used by the back end
429   (info nil))
430 (defprinter (environment)
431   function
432   (closure :test closure)
433   (nlx-info :test nlx-info))
434
435 ;;; The TAIL-SET structure is used to accumulate information about
436 ;;; tail-recursive local calls. The "tail set" is effectively the
437 ;;; transitive closure of the "is called tail-recursively by"
438 ;;; relation.
439 ;;;
440 ;;; All functions in the same tail set share the same TAIL-SET
441 ;;; structure. Initially each function has its own TAIL-SET, but when
442 ;;; IR1-OPTIMIZE-RETURN notices a tail local call, it joins the tail
443 ;;; sets of the called function and the calling function.
444 ;;;
445 ;;; The tail set is somewhat approximate, because it is too early to
446 ;;; be sure which calls will be TR. Any call that *might* end up TR
447 ;;; causes tail-set merging.
448 (defstruct (tail-set (:copier nil))
449   ;; a list of all the lambdas in this tail set
450   (functions nil :type list)
451   ;; our current best guess of the type returned by these functions.
452   ;; This is the union across all the functions of the return node's
453   ;; RESULT-TYPE. excluding local calls.
454   (type *wild-type* :type ctype)
455   ;; some info used by the back end
456   (info nil))
457 (defprinter (tail-set)
458   functions
459   type
460   (info :test info))
461
462 ;;; The NLX-Info structure is used to collect various information
463 ;;; about non-local exits. This is effectively an annotation on the
464 ;;; CONTINUATION, although it is accessed by searching in the
465 ;;; ENVIRONMENT-NLX-INFO.
466 (def!struct (nlx-info (:make-load-form-fun ignore-it))
467   ;; the cleanup associated with this exit. In a catch or
468   ;; unwind-protect, this is the :CATCH or :UNWIND-PROTECT cleanup,
469   ;; and not the cleanup for the escape block. The CLEANUP-KIND of
470   ;; this thus provides a good indication of what kind of exit is
471   ;; being done.
472   (cleanup (required-argument) :type cleanup)
473   ;; the continuation exited to (the CONT of the EXIT nodes). If this
474   ;; exit is from an escape function (CATCH or UNWIND-PROTECT), then
475   ;; environment analysis deletes the escape function and instead has
476   ;; the %NLX-ENTRY use this continuation.
477   ;;
478   ;; This slot is primarily an indication of where this exit delivers
479   ;; its values to (if any), but it is also used as a sort of name to
480   ;; allow us to find the NLX-Info that corresponds to a given exit.
481   ;; For this purpose, the Entry must also be used to disambiguate,
482   ;; since exits to different places may deliver their result to the
483   ;; same continuation.
484   (continuation (required-argument) :type continuation)
485   ;; the entry stub inserted by environment analysis. This is a block
486   ;; containing a call to the %NLX-Entry funny function that has the
487   ;; original exit destination as its successor. Null only
488   ;; temporarily.
489   (target nil :type (or cblock null))
490   ;; some kind of info used by the back end
491   info)
492 (defprinter (nlx-info)
493   continuation
494   target
495   info)
496 \f
497 ;;;; LEAF structures
498
499 ;;; Variables, constants and functions are all represented by LEAF
500 ;;; structures. A reference to a LEAF is indicated by a REF node. This
501 ;;; allows us to easily substitute one for the other without actually
502 ;;; hacking the flow graph.
503 (def!struct (leaf (:make-load-form-fun ignore-it)
504                   (:constructor nil))
505   ;; some name for this leaf. The exact significance of the name
506   ;; depends on what kind of leaf it is. In a LAMBDA-VAR or
507   ;; GLOBAL-VAR, this is the symbol name of the variable. In a
508   ;; functional that is from a DEFUN, this is the defined name. In
509   ;; other functionals, this is a descriptive string.
510   (name nil :type t)
511   ;; the type which values of this leaf must have
512   (type *universal-type* :type ctype)
513   ;; where the TYPE information came from:
514   ;;  :DECLARED, from a declaration.
515   ;;  :ASSUMED, from uses of the object.
516   ;;  :DEFINED, from examination of the definition.
517   ;; FIXME: This should be a named type. (LEAF-WHERE-FROM? Or
518   ;; perhaps just WHERE-FROM, since it's not just used in LEAF,
519   ;; but also in various DEFINE-INFO-TYPEs in globaldb.lisp,
520   ;; and very likely elsewhere too.)
521   (where-from :assumed :type (member :declared :assumed :defined))
522   ;; list of the REF nodes for this leaf
523   (refs () :type list)
524   ;; true if there was ever a REF or SET node for this leaf. This may
525   ;; be true when REFS and SETS are null, since code can be deleted.
526   (ever-used nil :type boolean)
527   ;; some kind of info used by the back end
528   (info nil))
529
530 ;;; The CONSTANT structure is used to represent known constant values.
531 ;;; If NAME is not null, then it is the name of the named constant
532 ;;; which this leaf corresponds to, otherwise this is an anonymous
533 ;;; constant.
534 (def!struct (constant (:include leaf))
535   ;; the value of the constant
536   (value nil :type t))
537 (defprinter (constant)
538   (name :test name)
539   value)
540
541 ;;; The BASIC-VAR structure represents information common to all
542 ;;; variables which don't correspond to known local functions.
543 (def!struct (basic-var (:include leaf) (:constructor nil))
544   ;; Lists of the set nodes for this variable.
545   (sets () :type list))
546
547 ;;; The GLOBAL-VAR structure represents a value hung off of the symbol
548 ;;; NAME. We use a :CONSTANT VAR when we know that the thing is a
549 ;;; constant, but don't know what the value is at compile time.
550 (def!struct (global-var (:include basic-var))
551   ;; kind of variable described
552   (kind (required-argument)
553         :type (member :special :global-function :constant :global)))
554 (defprinter (global-var)
555   name
556   (type :test (not (eq type *universal-type*)))
557   (where-from :test (not (eq where-from :assumed)))
558   kind)
559
560 ;;; The SLOT-ACCESSOR structure represents slot accessor functions. It
561 ;;; is a subtype of GLOBAL-VAR to make it look more like a normal
562 ;;; function.
563 (def!struct (slot-accessor (:include global-var
564                                      (where-from :defined)
565                                      (kind :global-function)))
566   ;; The description of the structure that this is an accessor for.
567   (for (required-argument) :type sb!xc:class)
568   ;; The slot description of the slot.
569   (slot (required-argument)))
570 (defprinter (slot-accessor)
571   name
572   for
573   slot)
574
575 ;;; The DEFINED-FUNCTION structure represents functions that are
576 ;;; defined in the same compilation block, or that have inline
577 ;;; expansions, or have a non-NIL INLINEP value. Whenever we change
578 ;;; the INLINEP state (i.e. an inline proclamation) we copy the
579 ;;; structure so that former INLINEP values are preserved.
580 (def!struct (defined-function (:include global-var
581                                         (where-from :defined)
582                                         (kind :global-function)))
583   ;; The values of INLINEP and INLINE-EXPANSION initialized from the
584   ;; global environment.
585   (inlinep nil :type inlinep)
586   (inline-expansion nil :type (or cons null))
587   ;; The block-local definition of this function (either because it
588   ;; was semi-inline, or because it was defined in this block.) If
589   ;; this function is not an entry point, then this may be deleted or
590   ;; let-converted. Null if we haven't converted the expansion yet.
591   (functional nil :type (or functional null)))
592 (defprinter (defined-function)
593   name
594   inlinep
595   (functional :test functional))
596 \f
597 ;;;; function stuff
598
599 ;;; We default the WHERE-FROM and TYPE slots to :DEFINED and FUNCTION.
600 ;;; We don't normally manipulate function types for defined functions,
601 ;;; but if someone wants to know, an approximation is there.
602 (def!struct (functional (:include leaf
603                                   (where-from :defined)
604                                   (type (specifier-type 'function))))
605   ;; Some information about how this function is used. These values are
606   ;; meaningful:
607   ;;
608   ;;    NIL
609   ;;    an ordinary function, callable using local call
610   ;;
611   ;;    :LET
612   ;;    a lambda that is used in only one local call, and has in
613   ;;    effect been substituted directly inline. The return node is
614   ;;    deleted, and the result is computed with the actual result
615   ;;    continuation for the call.
616   ;;
617   ;;    :MV-LET
618   ;;    Similar to :LET, but the call is an MV-CALL.
619   ;;
620   ;;    :ASSIGNMENT
621   ;;    similar to a LET, but can have other than one call as long as
622   ;;    there is at most one non-tail call.
623   ;;
624   ;;    :OPTIONAL
625   ;;    a lambda that is an entry-point for an optional-dispatch.
626   ;;    Similar to NIL, but requires greater caution, since local call
627   ;;    analysis may create new references to this function. Also, the
628   ;;    function cannot be deleted even if it has *no* references. The
629   ;;    Optional-Dispatch is in the LAMDBA-OPTIONAL-DISPATCH.
630   ;;
631   ;;    :EXTERNAL
632   ;;    an external entry point lambda. The function it is an entry
633   ;;    for is in the Entry-Function.
634   ;;
635   ;;    :TOP-LEVEL
636   ;;    a top-level lambda, holding a compiled top-level form.
637   ;;    Compiled very much like NIL, but provides an indication of
638   ;;    top-level context. A top-level lambda should have *no*
639   ;;    references. Its Entry-Function is a self-pointer.
640   ;;
641   ;;    :TOP-LEVEL-XEP
642   ;;    After a component is compiled, we clobber any top-level code
643   ;;    references to its non-closure XEPs with dummy FUNCTIONAL
644   ;;    structures having this kind. This prevents the retained
645   ;;    top-level code from holding onto the IR for the code it
646   ;;    references.
647   ;;
648   ;;    :ESCAPE
649   ;;    :CLEANUP
650   ;;    special functions used internally by CATCH and UNWIND-PROTECT.
651   ;;    These are pretty much like a normal function (NIL), but are
652   ;;    treated specially by local call analysis and stuff. Neither
653   ;;    kind should ever be given an XEP even though they appear as
654   ;;    args to funny functions. An :ESCAPE function is never actually
655   ;;    called, and thus doesn't need to have code generated for it.
656   ;;
657   ;;    :DELETED
658   ;;    This function has been found to be uncallable, and has been
659   ;;    marked for deletion.
660   (kind nil :type (member nil :optional :deleted :external :top-level :escape
661                           :cleanup :let :mv-let :assignment
662                           :top-level-xep))
663   ;; In a normal function, this is the external entry point (XEP)
664   ;; lambda for this function, if any. Each function that is used
665   ;; other than in a local call has an XEP, and all of the
666   ;; non-local-call references are replaced with references to the
667   ;; XEP.
668   ;;
669   ;; In an XEP lambda (indicated by the :External kind), this is the
670   ;; function that the XEP is an entry-point for. The body contains
671   ;; local calls to all the actual entry points in the function. In a
672   ;; :Top-Level lambda (which is its own XEP) this is a self-pointer.
673   ;;
674   ;; With all other kinds, this is null.
675   (entry-function nil :type (or functional null))
676   ;; the value of any inline/notinline declaration for a local function
677   (inlinep nil :type inlinep)
678   ;; If we have a lambda that can be used as in inline expansion for
679   ;; this function, then this is it. If there is no source-level
680   ;; lambda corresponding to this function then this is Null (but then
681   ;; INLINEP will always be NIL as well.)
682   (inline-expansion nil :type list)
683   ;; the lexical environment that the inline-expansion should be converted in
684   (lexenv *lexenv* :type lexenv)
685   ;; the original function or macro lambda list, or :UNSPECIFIED if
686   ;; this is a compiler created function
687   (arg-documentation nil :type (or list (member :unspecified)))
688   ;; various rare miscellaneous info that drives code generation & stuff
689   (plist () :type list))
690 (defprinter (functional)
691   name)
692
693 ;;; The CLAMBDA only deals with required lexical arguments. Special,
694 ;;; optional, keyword and rest arguments are handled by transforming
695 ;;; into simpler stuff.
696 (def!struct (clambda (:include functional)
697                      (:conc-name lambda-)
698                      (:predicate lambda-p)
699                      (:constructor make-lambda)
700                      (:copier copy-lambda))
701   ;; List of lambda-var descriptors for args.
702   (vars nil :type list)
703   ;; If this function was ever a :OPTIONAL function (an entry-point
704   ;; for an optional-dispatch), then this is that optional-dispatch.
705   ;; The optional dispatch will be :DELETED if this function is no
706   ;; longer :OPTIONAL.
707   (optional-dispatch nil :type (or optional-dispatch null))
708   ;; The Bind node for this Lambda. This node marks the beginning of
709   ;; the lambda, and serves to explicitly represent the lambda binding
710   ;; semantics within the flow graph representation. Null in deleted
711   ;; functions, and also in LETs where we deleted the call & bind
712   ;; (because there are no variables left), but have not yet actually
713   ;; deleted the lambda yet.
714   (bind nil :type (or bind null))
715   ;; The Return node for this Lambda, or NIL if it has been deleted.
716   ;; This marks the end of the lambda, receiving the result of the
717   ;; body. In a let, the return node is deleted, and the body delivers
718   ;; the value to the actual continuation. The return may also be
719   ;; deleted if it is unreachable.
720   (return nil :type (or creturn null))
721   ;; If this is a let, then the Lambda whose Lets list we are in,
722   ;; otherwise this is a self-pointer.
723   (home nil :type (or clambda null))
724   ;; A list of all the all the lambdas that have been let-substituted
725   ;; in this lambda. This is only non-null in lambdas that aren't
726   ;; lets.
727   (lets () :type list)
728   ;; A list of all the Entry nodes in this function and its lets. Null
729   ;; an a let.
730   (entries () :type list)
731   ;; A list of all the functions directly called from this function
732   ;; (or one of its lets) using a non-let local call. May include
733   ;; deleted functions because nobody bothers to clear them out.
734   (calls () :type list)
735   ;; The Tail-Set that this lambda is in. Null during creation and in
736   ;; let lambdas.
737   (tail-set nil :type (or tail-set null))
738   ;; The structure which represents the environment that this
739   ;; Function's variables are allocated in. This is filled in by
740   ;; environment analysis. In a let, this is EQ to our home's
741   ;; environment.
742   (environment nil :type (or environment null))
743   ;; In a LET, this is the NODE-LEXENV of the combination node. We
744   ;; retain it so that if the let is deleted (due to a lack of vars),
745   ;; we will still have caller's lexenv to figure out which cleanup is
746   ;; in effect.
747   (call-lexenv nil :type (or lexenv null)))
748 (defprinter (clambda :conc-name lambda-)
749   name
750   (type :test (not (eq type *universal-type*)))
751   (where-from :test (not (eq where-from :assumed)))
752   (vars :prin1 (mapcar #'leaf-name vars)))
753
754 ;;; The OPTIONAL-DISPATCH leaf is used to represent hairy lambdas. It
755 ;;; is a FUNCTIONAL, like LAMBDA. Each legal number of arguments has a
756 ;;; function which is called when that number of arguments is passed.
757 ;;; The function is called with all the arguments actually passed. If
758 ;;; additional arguments are legal, then the LEXPR style MORE-ENTRY
759 ;;; handles them. The value returned by the function is the value
760 ;;; which results from calling the OPTIONAL-DISPATCH.
761 ;;;
762 ;;; The theory is that each entry-point function calls the next entry
763 ;;; point tail-recursively, passing all the arguments passed in and
764 ;;; the default for the argument the entry point is for. The last
765 ;;; entry point calls the real body of the function. In the presence
766 ;;; of supplied-p args and other hair, things are more complicated. In
767 ;;; general, there is a distinct internal function that takes the
768 ;;; supplied-p args as parameters. The preceding entry point calls
769 ;;; this function with NIL filled in for the supplied-p args, while
770 ;;; the current entry point calls it with T in the supplied-p
771 ;;; positions.
772 ;;;
773 ;;; Note that it is easy to turn a call with a known number of
774 ;;; arguments into a direct call to the appropriate entry-point
775 ;;; function, so functions that are compiled together can avoid doing
776 ;;; the dispatch.
777 (def!struct (optional-dispatch (:include functional))
778   ;; the original parsed argument list, for anyone who cares
779   (arglist nil :type list)
780   ;; true if &ALLOW-OTHER-KEYS was supplied
781   (allowp nil :type boolean)
782   ;; true if &KEY was specified (which doesn't necessarily mean that
783   ;; there are any &KEY arguments..)
784   (keyp nil :type boolean)
785   ;; the number of required arguments. This is the smallest legal
786   ;; number of arguments.
787   (min-args 0 :type unsigned-byte)
788   ;; the total number of required and optional arguments. Args at
789   ;; positions >= to this are &REST, &KEY or illegal args.
790   (max-args 0 :type unsigned-byte)
791   ;; list of the LAMBDAs which are the entry points for non-rest,
792   ;; non-key calls. The entry for MIN-ARGS is first, MIN-ARGS+1
793   ;; second, ... MAX-ARGS last. The last entry-point always calls the
794   ;; main entry; in simple cases it may be the main entry.
795   (entry-points nil :type list)
796   ;; An entry point which takes MAX-ARGS fixed arguments followed by
797   ;; an argument context pointer and an argument count. This entry
798   ;; point deals with listifying rest args and parsing keywords. This
799   ;; is null when extra arguments aren't legal.
800   (more-entry nil :type (or clambda null))
801   ;; The main entry-point into the function, which takes all arguments
802   ;; including keywords as fixed arguments. The format of the
803   ;; arguments must be determined by examining the arglist. This may
804   ;; be used by callers that supply at least Max-Args arguments and
805   ;; know what they are doing.
806   (main-entry nil :type (or clambda null)))
807 (defprinter (optional-dispatch)
808   name
809   (type :test (not (eq type *universal-type*)))
810   (where-from :test (not (eq where-from :assumed)))
811   arglist
812   allowp
813   keyp
814   min-args
815   max-args
816   (entry-points :test entry-points)
817   (more-entry :test more-entry)
818   main-entry)
819
820 ;;; The ARG-INFO structure allows us to tack various information onto
821 ;;; LAMBDA-VARs during IR1 conversion. If we use one of these things,
822 ;;; then the var will have to be massaged a bit before it is simple
823 ;;; and lexical.
824 (def!struct arg-info
825   ;; true if this arg is to be specially bound
826   (specialp nil :type boolean)
827   ;; the kind of argument being described. Required args only have arg
828   ;; info structures if they are special.
829   (kind (required-argument) :type (member :required :optional :keyword :rest
830                                           :more-context :more-count))
831   ;; If true, this is the VAR for SUPPLIED-P variable of a keyword or
832   ;; optional arg. This is true for keywords with non-constant
833   ;; defaults even when there is no user-specified supplied-p var.
834   (supplied-p nil :type (or lambda-var null))
835   ;; the default for a keyword or optional, represented as the
836   ;; original Lisp code. This is set to NIL in &KEY arguments that are
837   ;; defaulted using the SUPPLIED-P arg.
838   (default nil :type t)
839   ;; the actual key for a &KEY argument. Note that in ANSI CL this is not
840   ;; necessarily a keyword: (DEFUN FOO (&KEY ((BAR BAR))) ..).
841   (key nil :type symbol))
842 (defprinter (arg-info)
843   (specialp :test specialp)
844   kind
845   (supplied-p :test supplied-p)
846   (default :test default)
847   (key :test key))
848
849 ;;; The LAMBDA-VAR structure represents a lexical lambda variable.
850 ;;; This structure is also used during IR1 conversion to describe
851 ;;; lambda arguments which may ultimately turn out not to be simple
852 ;;; and lexical.
853 ;;;
854 ;;; LAMBDA-VARs with no REFs are considered to be deleted; environment
855 ;;; analysis isn't done on these variables, so the back end must check
856 ;;; for and ignore unreferenced variables. Note that a deleted
857 ;;; lambda-var may have sets; in this case the back end is still
858 ;;; responsible for propagating the Set-Value to the set's Cont.
859 (def!struct (lambda-var (:include basic-var))
860   ;; true if this variable has been declared IGNORE
861   (ignorep nil :type boolean)
862   ;; the CLAMBDA that this var belongs to. This may be null when we are
863   ;; building a lambda during IR1 conversion.
864   (home nil :type (or null clambda))
865   ;; This is set by environment analysis if it chooses an indirect
866   ;; (value cell) representation for this variable because it is both
867   ;; set and closed over.
868   (indirect nil :type boolean)
869   ;; The following two slots are only meaningful during IR1 conversion
870   ;; of hairy lambda vars:
871   ;;
872   ;; The ARG-INFO structure which holds information obtained from
873   ;; &keyword parsing.
874   (arg-info nil :type (or arg-info null))
875   ;; if true, the GLOBAL-VAR structure for the special variable which
876   ;; is to be bound to the value of this argument
877   (specvar nil :type (or global-var null))
878   ;; Set of the CONSTRAINTs on this variable. Used by constraint
879   ;; propagation. This is left null by the lambda pre-pass if it
880   ;; determine that this is a set closure variable, and is thus not a
881   ;; good subject for flow analysis.
882   (constraints nil :type (or sset null)))
883 (defprinter (lambda-var)
884   name
885   (type :test (not (eq type *universal-type*)))
886   (where-from :test (not (eq where-from :assumed)))
887   (ignorep :test ignorep)
888   (arg-info :test arg-info)
889   (specvar :test specvar))
890 \f
891 ;;;; basic node types
892
893 ;;; A REF represents a reference to a LEAF. REF-REOPTIMIZE is
894 ;;; initially (and forever) NIL, since REFs don't receive any values
895 ;;; and don't have any IR1 optimizer.
896 (defstruct (ref (:include node (:reoptimize nil))
897                 (:constructor make-ref (derived-type leaf))
898                 (:copier nil))
899   ;; The leaf referenced.
900   (leaf nil :type leaf))
901 (defprinter (ref)
902   leaf)
903
904 ;;; Naturally, the IF node always appears at the end of a block.
905 ;;; NODE-CONT is a dummy continuation, and is there only to keep
906 ;;; people happy.
907 (defstruct (cif (:include node)
908                 (:conc-name if-)
909                 (:predicate if-p)
910                 (:constructor make-if)
911                 (:copier copy-if))
912   ;; CONTINUATION for the predicate
913   (test (required-argument) :type continuation)
914   ;; the blocks that we execute next in true and false case,
915   ;; respectively (may be the same)
916   (consequent (required-argument) :type cblock)
917   (alternative (required-argument) :type cblock))
918 (defprinter (cif :conc-name if-)
919   (test :prin1 (continuation-use test))
920   consequent
921   alternative)
922
923 (defstruct (cset (:include node
924                            (derived-type *universal-type*))
925                  (:conc-name set-)
926                  (:predicate set-p)
927                  (:constructor make-set)
928                  (:copier copy-set))
929   ;; descriptor for the variable set
930   (var (required-argument) :type basic-var)
931   ;; continuation for the value form
932   (value (required-argument) :type continuation))
933 (defprinter (cset :conc-name set-)
934   var
935   (value :prin1 (continuation-use value)))
936
937 ;;; The BASIC-COMBINATION structure is used to represent both normal
938 ;;; and multiple value combinations. In a local function call, this
939 ;;; node appears at the end of its block and the body of the called
940 ;;; function appears as the successor. The NODE-CONT remains the
941 ;;; continuation which receives the value of the call.
942 (defstruct (basic-combination (:include node)
943                               (:constructor nil)
944                               (:copier nil))
945   ;; continuation for the function
946   (fun (required-argument) :type continuation)
947   ;; list of CONTINUATIONs for the args. In a local call, an argument
948   ;; continuation may be replaced with NIL to indicate that the
949   ;; corresponding variable is unreferenced, and thus no argument
950   ;; value need be passed.
951   (args nil :type list)
952   ;; the kind of function call being made. :LOCAL means that this is a
953   ;; local call to a function in the same component, and that argument
954   ;; syntax checking has been done, etc. Calls to known global
955   ;; functions are represented by storing the FUNCTION-INFO for the
956   ;; function in this slot. :FULL is a call to an (as yet) unknown
957   ;; function. :ERROR is like :FULL, but means that we have discovered
958   ;; that the call contains an error, and should not be reconsidered
959   ;; for optimization.
960   (kind :full :type (or (member :local :full :error) function-info))
961   ;; some kind of information attached to this node by the back end
962   (info nil))
963
964 ;;; The COMBINATION node represents all normal function calls,
965 ;;; including FUNCALL. This is distinct from BASIC-COMBINATION so that
966 ;;; an MV-COMBINATION isn't COMBINATION-P.
967 (defstruct (combination (:include basic-combination)
968                         (:constructor make-combination (fun))
969                         (:copier nil)))
970 (defprinter (combination)
971   (fun :prin1 (continuation-use fun))
972   (args :prin1 (mapcar (lambda (x)
973                          (if x
974                              (continuation-use x)
975                              "<deleted>"))
976                        args)))
977
978 ;;; An MV-COMBINATION is to MULTIPLE-VALUE-CALL as a COMBINATION is to
979 ;;; FUNCALL. This is used to implement all the multiple-value
980 ;;; receiving forms.
981 (defstruct (mv-combination (:include basic-combination)
982                            (:constructor make-mv-combination (fun))
983                            (:copier nil)))
984 (defprinter (mv-combination)
985   (fun :prin1 (continuation-use fun))
986   (args :prin1 (mapcar #'continuation-use args)))
987
988 ;;; The BIND node marks the beginning of a lambda body and represents
989 ;;; the creation and initialization of the variables.
990 (defstruct (bind (:include node)
991                  (:copier nil))
992   ;; the lambda we are binding variables for. Null when we are
993   ;; creating the LAMBDA during IR1 translation.
994   (lambda nil :type (or clambda null)))
995 (defprinter (bind)
996   lambda)
997
998 ;;; The RETURN node marks the end of a lambda body. It collects the
999 ;;; return values and represents the control transfer on return. This
1000 ;;; is also where we stick information used for TAIL-SET type
1001 ;;; inference.
1002 (defstruct (creturn (:include node)
1003                     (:conc-name return-)
1004                     (:predicate return-p)
1005                     (:constructor make-return)
1006                     (:copier copy-return))
1007   ;; the lambda we are returning from. Null temporarily during
1008   ;; ir1tran.
1009   (lambda nil :type (or clambda null))
1010   ;; the continuation which yields the value of the lambda
1011   (result (required-argument) :type continuation)
1012   ;; the union of the node-derived-type of all uses of the result
1013   ;; other than by a local call, intersected with the result's
1014   ;; asserted-type. If there are no non-call uses, this is
1015   ;; *EMPTY-TYPE*
1016   (result-type *wild-type* :type ctype))
1017 (defprinter (creturn :conc-name return-)
1018   lambda
1019   result-type)
1020 \f
1021 ;;;; non-local exit support
1022 ;;;;
1023 ;;;; In IR1, we insert special nodes to mark potentially non-local
1024 ;;;; lexical exits.
1025
1026 ;;; The ENTRY node serves to mark the start of the dynamic extent of a
1027 ;;; lexical exit. It is the mess-up node for the corresponding :Entry
1028 ;;; cleanup.
1029 (defstruct (entry (:include node)
1030                   (:copier nil))
1031   ;; All of the Exit nodes for potential non-local exits to this point.
1032   (exits nil :type list)
1033   ;; The cleanup for this entry. NULL only temporarily.
1034   (cleanup nil :type (or cleanup null)))
1035 (defprinter (entry))
1036
1037 ;;; The EXIT node marks the place at which exit code would be emitted,
1038 ;;; if necessary. This is interposed between the uses of the exit
1039 ;;; continuation and the exit continuation's DEST. Instead of using
1040 ;;; the returned value being delivered directly to the exit
1041 ;;; continuation, it is delivered to our VALUE continuation. The
1042 ;;; original exit continuation is the exit node's CONT.
1043 (defstruct (exit (:include node)
1044                  (:copier nil))
1045   ;; The Entry node that this is an exit for. If null, this is a
1046   ;; degenerate exit. A degenerate exit is used to "fill" an empty
1047   ;; block (which isn't allowed in IR1.) In a degenerate exit, Value
1048   ;; is always also null.
1049   (entry nil :type (or entry null))
1050   ;; The continuation yeilding the value we are to exit with. If NIL,
1051   ;; then no value is desired (as in GO).
1052   (value nil :type (or continuation null)))
1053 (defprinter (exit)
1054   (entry :test entry)
1055   (value :test value))
1056 \f
1057 ;;;; miscellaneous IR1 structures
1058
1059 (defstruct (undefined-warning
1060             #-no-ansi-print-object
1061             (:print-object (lambda (x s)
1062                              (print-unreadable-object (x s :type t)
1063                                (prin1 (undefined-warning-name x) s))))
1064             (:copier nil))
1065   ;; the name of the unknown thing
1066   (name nil :type (or symbol list))
1067   ;; the kind of reference to NAME
1068   (kind (required-argument) :type (member :function :type :variable))
1069   ;; the number of times this thing was used
1070   (count 0 :type unsigned-byte)
1071   ;; a list of COMPILER-ERROR-CONTEXT structures describing places
1072   ;; where this thing was used. Note that we only record the first
1073   ;; *UNDEFINED-WARNING-LIMIT* calls.
1074   (warnings () :type list))
1075 \f
1076 ;;; a helper for the POLICY macro, defined late here so that the
1077 ;;; various type tests can be inlined
1078 (declaim (ftype (function ((or list lexenv node functional)) list)
1079                 %coerce-to-policy))
1080 (defun %coerce-to-policy (thing)
1081   (let ((result (etypecase thing
1082                   (list thing)
1083                   (lexenv (lexenv-policy thing))
1084                   (node (lexenv-policy (node-lexenv thing)))
1085                   (functional (lexenv-policy (functional-lexenv thing))))))
1086     ;; Test the first element of the list as a rudimentary sanity
1087     ;; that it really does look like a valid policy.
1088     (aver (or (null result) (policy-quality-name-p (caar result))))
1089     ;; Voila.
1090     result))
1091 \f
1092 ;;;; Freeze some structure types to speed type testing.
1093
1094 #!-sb-fluid
1095 (declaim (freeze-type node leaf lexenv continuation cblock component cleanup
1096                       environment tail-set nlx-info))