0.pre7.86:
[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
74   ;; continuation, this is null even though the node that receives
75   ;; this continuation may not 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 physical environment analysis) this is
190   ;; computed for all nodes (after cleanup code has been emitted).
191   ;; Before then, a non-null value indicates that IR1 optimization has
192   ;; converted a 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   ;; CMU CL had a KILL slot here, documented as "set used by
276   ;; constraint propagation", which was used in constraint propagation
277   ;; as a list of LAMBDA-VARs killed, and in copy propagation as an
278   ;; SSET, representing I dunno what. I (WHN) found this confusing,
279   ;; and furthermore it caused type errors when I was trying to make
280   ;; the compiler produce fully general LAMBDA functions directly
281   ;; (instead of doing as CMU CL always did, producing extra little
282   ;; functions which return the LAMDBA you need) and therefore taking
283   ;; a new path through the compiler. So I split this into two:
284   ;;   KILL-LIST = list of LAMBDA-VARs killed, used in constraint propagation
285   ;;   KILL-SSET = an SSET value, used in copy propagation
286   (kill-list nil :type list)
287   (kill-sset nil :type (or sset null))
288   ;; other sets used in constraint propagation and/or copy propagation
289   (gen nil)
290   (in nil)
291   (out nil)
292   ;; the component this block is in, or NIL temporarily during IR1
293   ;; conversion and in deleted blocks
294   (component *current-component* :type (or component null))
295   ;; a flag used by various graph-walking code to determine whether
296   ;; this block has been processed already or what. We make this
297   ;; initially NIL so that FIND-INITIAL-DFO doesn't have to scan the
298   ;; entire initial component just to clear the flags.
299   (flag nil)
300   ;; some kind of info used by the back end
301   (info nil)
302   ;; If true, then constraints that hold in this block and its
303   ;; successors by merit of being tested by its IF predecessor.
304   (test-constraint nil :type (or sset null)))
305 (def!method print-object ((cblock cblock) stream)
306   (print-unreadable-object (cblock stream :type t :identity t)
307     (format stream ":START c~D" (cont-num (block-start cblock)))))
308
309 ;;; The BLOCK-ANNOTATION class is inherited (via :INCLUDE) by
310 ;;; different BLOCK-INFO annotation structures so that code
311 ;;; (specifically control analysis) can be shared.
312 (defstruct (block-annotation (:constructor nil)
313                              (:copier nil))
314   ;; The IR1 block that this block is in the INFO for.
315   (block (missing-arg) :type cblock)
316   ;; the next and previous block in emission order (not DFO). This
317   ;; determines which block we drop though to, and also used to chain
318   ;; together overflow blocks that result from splitting of IR2 blocks
319   ;; in lifetime analysis.
320   (next nil :type (or block-annotation null))
321   (prev nil :type (or block-annotation null)))
322
323 ;;; A COMPONENT structure provides a handle on a connected piece of
324 ;;; the flow graph. Most of the passes in the compiler operate on
325 ;;; COMPONENTs rather than on the entire flow graph.
326 (defstruct (component (:copier nil))
327   ;; the kind of component
328   ;;
329   ;; (The terminology here is left over from before
330   ;; sbcl-0.pre7.34.flaky5.2, when there was no such thing as
331   ;; FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P, so that Python was
332   ;; incapable of building standalone :EXTERNAL functions, but instead
333   ;; had to implement things like #'CL:COMPILE as FUNCALL of a little
334   ;; toplevel stub whose sole purpose was to return an :EXTERNAL
335   ;; function.)
336   ;;
337   ;; The possibilities are:
338   ;;   NIL
339   ;;     an ordinary component, containing non-top-level code
340   ;;   :TOPLEVEL
341   ;;     a component containing only load-time code
342   ;;   :COMPLEX-TOPLEVEL
343   ;;     In the old system, before FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P
344   ;;     was defined, this was necessarily a component containing both
345   ;;     top level and run-time code. Now this state is also used for
346   ;;     a component with HAS-EXTERNAL-REFERENCES-P functionals in it.
347   ;;   :INITIAL
348   ;;     the result of initial IR1 conversion, on which component
349   ;;     analysis has not been done
350   ;;   :DELETED
351   ;;     debris left over from component analysis
352   ;;
353   ;; See also COMPONENT-TOPLEVELISH-P.
354   (kind nil :type (member nil :toplevel :complex-toplevel :initial :deleted))
355   ;; the blocks that are the dummy head and tail of the DFO
356   ;;
357   ;; Entry/exit points have these blocks as their
358   ;; predecessors/successors. Null temporarily. The start and return
359   ;; from each non-deleted function is linked to the component head
360   ;; and tail. Until physical environment analysis links NLX entry
361   ;; stubs to the component head, every successor of the head is a
362   ;; function start (i.e. begins with a BIND node.)
363   (head nil :type (or null cblock))
364   (tail nil :type (or null cblock))
365   ;; This becomes a list of the CLAMBDA structures for all functions
366   ;; in this component. OPTIONAL-DISPATCHes are represented only by
367   ;; their XEP and other associated lambdas. This doesn't contain any
368   ;; deleted or LET lambdas.
369   ;;
370   ;; Note that logical associations between CLAMBDAs and COMPONENTs
371   ;; seem to exist for a while before this is initialized. In
372   ;; particular, I got burned by writing some code to use this value
373   ;; to decide which components need LOCAL-CALL-ANALYZE, when it turns
374   ;; out that LOCAL-CALL-ANALYZE had a role in initializing this value
375   ;; (and DFO stuff does too, maybe). Also, even after it's
376   ;; initialized, it might change as CLAMBDAs are deleted or merged.
377   ;; -- WHN 2001-09-30
378   (lambdas () :type list)
379   ;; a list of FUNCTIONAL structures for functions that are newly
380   ;; converted, and haven't been local-call analyzed yet. Initially
381   ;; functions are not in the LAMBDAS list. LOCAL-CALL-ANALYZE moves
382   ;; them there (possibly as LETs, or implicitly as XEPs if an
383   ;; OPTIONAL-DISPATCH.) Between runs of LOCAL-CALL-ANALYZE there may
384   ;; be some debris of converted or even deleted functions in this
385   ;; list.
386   (new-functions () :type list)
387   ;; If this is true, then there is stuff in this component that could
388   ;; benefit from further IR1 optimization.
389   (reoptimize t :type boolean)
390   ;; If this is true, then the control flow in this component was
391   ;; messed up by IR1 optimizations, so the DFO should be recomputed.
392   (reanalyze nil :type boolean)
393   ;; some sort of name for the code in this component
394   (name "<unknown>" :type simple-string)
395   ;; some kind of info used by the back end
396   (info nil)
397   ;; the SOURCE-INFO structure describing where this component was
398   ;; compiled from
399   (source-info *source-info* :type source-info)
400   ;; count of the number of inline expansions we have done while
401   ;; compiling this component, to detect infinite or exponential
402   ;; blowups
403   (inline-expansions 0 :type index)
404   ;; a map from combination nodes to things describing how an
405   ;; optimization of the node failed. The description is an alist
406   ;; (TRANSFORM . ARGS), where TRANSFORM is the structure describing
407   ;; the transform that failed, and ARGS is either a list of format
408   ;; arguments for the note, or the FUN-TYPE that would have
409   ;; enabled the transformation but failed to match.
410   (failed-optimizations (make-hash-table :test 'eq) :type hash-table)
411   ;; This is similar to NEW-FUNCTIONS, but is used when a function has
412   ;; already been analyzed, but new references have been added by
413   ;; inline expansion. Unlike NEW-FUNCTIONS, this is not disjoint from
414   ;; COMPONENT-LAMBDAS.
415   (reanalyze-functions nil :type list))
416 (defprinter (component :identity t)
417   name
418   (reanalyze :test reanalyze))
419
420 ;;; Before sbcl-0.7.0, there were :TOPLEVEL things which were magical
421 ;;; in multiple ways. That's since been refactored into the orthogonal
422 ;;; properties "optimized for locall with no arguments" and "externally
423 ;;; visible/referenced (so don't delete it)". The code <0.7.0 did a lot
424 ;;; of tests a la (EQ KIND :TOP_LEVEL) in the "don't delete it?" sense;
425 ;;; this function is a sort of literal translation of those tests into
426 ;;; the new world.
427 ;;;
428 ;;; FIXME: After things settle down, bare :TOPLEVEL might go away, at
429 ;;; which time it might be possible to replace the COMPONENT-KIND
430 ;;; :TOPLEVEL mess with a flag COMPONENT-HAS-EXTERNAL-REFERENCES-P
431 ;;; along the lines of FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P.
432 (defun lambda-toplevelish-p (clambda)
433   (or (eql (lambda-kind clambda) :toplevel)
434       (lambda-has-external-references-p clambda)))
435 (defun component-toplevelish-p (component)
436   (member (component-kind component)
437           '(:toplevel :complex-toplevel)))
438
439 ;;; A CLEANUP structure represents some dynamic binding action. Blocks
440 ;;; are annotated with the current CLEANUP so that dynamic bindings
441 ;;; can be removed when control is transferred out of the binding
442 ;;; environment. We arrange for changes in dynamic bindings to happen
443 ;;; at block boundaries, so that cleanup code may easily be inserted.
444 ;;; The "mess-up" action is explicitly represented by a funny function
445 ;;; call or ENTRY node.
446 ;;;
447 ;;; We guarantee that CLEANUPs only need to be done at block boundaries
448 ;;; by requiring that the exit continuations initially head their
449 ;;; blocks, and then by not merging blocks when there is a cleanup
450 ;;; change.
451 (defstruct (cleanup (:copier nil))
452   ;; the kind of thing that has to be cleaned up
453   (kind (missing-arg)
454         :type (member :special-bind :catch :unwind-protect :block :tagbody))
455   ;; the node that messes things up. This is the last node in the
456   ;; non-messed-up environment. Null only temporarily. This could be
457   ;; deleted due to unreachability.
458   (mess-up nil :type (or node null))
459   ;; a list of all the NLX-INFO structures whose NLX-INFO-CLEANUP is
460   ;; this cleanup. This is filled in by physical environment analysis.
461   (nlx-info nil :type list))
462 (defprinter (cleanup :identity t)
463   kind
464   mess-up
465   (nlx-info :test nlx-info))
466
467 ;;; A PHYSENV represents the result of physical environment analysis.
468 ;;;
469 ;;; As far as I can tell from reverse engineering, this IR1 structure
470 ;;; represents the physical environment (which is probably not the
471 ;;; standard Lispy term for this concept, but I dunno what is the
472 ;;; standard term): those things in the lexical environment which a
473 ;;; LAMBDA actually interacts with. Thus in
474 ;;;   (DEFUN FROB-THINGS (THINGS)
475 ;;;     (DOLIST (THING THINGS)
476 ;;;       (BLOCK FROBBING-ONE-THING
477 ;;;         (MAPCAR (LAMBDA (PATTERN)
478 ;;;                   (WHEN (FITS-P THING PATTERN)
479 ;;;                     (RETURN-FROM FROB-THINGS (LIST :FIT THING PATTERN))))
480 ;;;                 *PATTERNS*))))
481 ;;; the variables THINGS, THING, and PATTERN and the block names
482 ;;; FROB-THINGS and FROBBING-ONE-THING are all in the inner LAMBDA's
483 ;;; lexical environment, but of those only THING, PATTERN, and
484 ;;; FROB-THINGS are in its physical environment. In IR1, we largely
485 ;;; just collect the names of these things; in IR2 an IR2-PHYSENV
486 ;;; structure is attached to INFO and used to keep track of
487 ;;; associations between these names and less-abstract things (like
488 ;;; TNs, or eventually stack slots and registers). -- WHN 2001-09-29
489 (defstruct (physenv (:copier nil))
490   ;; the function that allocates this physical environment
491   (function (missing-arg) :type clambda)
492   #| ; seems not to be used as of sbcl-0.pre7.51
493   ;; a list of all the lambdas that allocate variables in this
494   ;; physical environment
495   (lambdas nil :type list)
496   |#
497   ;; This ultimately converges to a list of all the LAMBDA-VARs and
498   ;; NLX-INFOs needed from enclosing environments by code in this
499   ;; physical environment. In the meantime, it may be
500   ;;   * NIL at object creation time
501   ;;   * a superset of the correct result, generated somewhat later
502   ;;   * smaller and smaller sets converging to the correct result as
503   ;;     we notice and delete unused elements in the superset
504   (closure nil :type list)
505   ;; a list of NLX-INFO structures describing all the non-local exits
506   ;; into this physical environment
507   (nlx-info nil :type list)
508   ;; some kind of info used by the back end
509   (info nil))
510 (defprinter (physenv :identity t)
511   function
512   (closure :test closure)
513   (nlx-info :test nlx-info))
514
515 ;;; An TAIL-SET structure is used to accumulate information about
516 ;;; tail-recursive local calls. The "tail set" is effectively the
517 ;;; transitive closure of the "is called tail-recursively by"
518 ;;; relation.
519 ;;;
520 ;;; All functions in the same tail set share the same TAIL-SET
521 ;;; structure. Initially each function has its own TAIL-SET, but when
522 ;;; IR1-OPTIMIZE-RETURN notices a tail local call, it joins the tail
523 ;;; sets of the called function and the calling function.
524 ;;;
525 ;;; The tail set is somewhat approximate, because it is too early to
526 ;;; be sure which calls will be tail-recursive. Any call that *might*
527 ;;; end up tail-recursive causes TAIL-SET merging.
528 (defstruct (tail-set)
529   ;; a list of all the LAMBDAs in this tail set
530   (functions nil :type list)
531   ;; our current best guess of the type returned by these functions.
532   ;; This is the union across all the functions of the return node's
533   ;; RESULT-TYPE, excluding local calls.
534   (type *wild-type* :type ctype)
535   ;; some info used by the back end
536   (info nil))
537 (defprinter (tail-set :identity t)
538   functions
539   type
540   (info :test info))
541
542 ;;; The NLX-Info structure is used to collect various information
543 ;;; about non-local exits. This is effectively an annotation on the
544 ;;; CONTINUATION, although it is accessed by searching in the
545 ;;; PHYSENV-NLX-INFO.
546 (def!struct (nlx-info (:make-load-form-fun ignore-it))
547   ;; the cleanup associated with this exit. In a catch or
548   ;; unwind-protect, this is the :CATCH or :UNWIND-PROTECT cleanup,
549   ;; and not the cleanup for the escape block. The CLEANUP-KIND of
550   ;; this thus provides a good indication of what kind of exit is
551   ;; being done.
552   (cleanup (missing-arg) :type cleanup)
553   ;; the continuation exited to (the CONT of the EXIT nodes). If this
554   ;; exit is from an escape function (CATCH or UNWIND-PROTECT), then
555   ;; physical environment analysis deletes the escape function and
556   ;; instead has the %NLX-ENTRY use this continuation.
557   ;;
558   ;; This slot is primarily an indication of where this exit delivers
559   ;; its values to (if any), but it is also used as a sort of name to
560   ;; allow us to find the NLX-Info that corresponds to a given exit.
561   ;; For this purpose, the Entry must also be used to disambiguate,
562   ;; since exits to different places may deliver their result to the
563   ;; same continuation.
564   (continuation (missing-arg) :type continuation)
565   ;; the entry stub inserted by physical environment analysis. This is
566   ;; a block containing a call to the %NLX-Entry funny function that
567   ;; has the original exit destination as its successor. Null only
568   ;; temporarily.
569   (target nil :type (or cblock null))
570   ;; some kind of info used by the back end
571   info)
572 (defprinter (nlx-info :identity t)
573   continuation
574   target
575   info)
576 \f
577 ;;;; LEAF structures
578
579 ;;; Variables, constants and functions are all represented by LEAF
580 ;;; structures. A reference to a LEAF is indicated by a REF node. This
581 ;;; allows us to easily substitute one for the other without actually
582 ;;; hacking the flow graph.
583 (def!struct (leaf (:make-load-form-fun ignore-it)
584                   (:constructor nil))
585   ;; some name for this leaf. The exact significance of the name
586   ;; depends on what kind of leaf it is. In a LAMBDA-VAR or
587   ;; GLOBAL-VAR, this is the symbol name of the variable. In a
588   ;; functional that is from a DEFUN, this is the defined name. In
589   ;; other functionals, this is a descriptive string.
590   ;;
591   ;; KLUDGE: Note that at least for LAMBDA-VARs, this is important not
592   ;; just for debugging but for ordinary compilation as well. In
593   ;; particular, in RECOGNIZE-KNOWN-CALL function calls are compiled
594   ;; differently based on the LEAF-NAME.
595   (name nil :type t)
596   ;; the type which values of this leaf must have
597   (type *universal-type* :type ctype)
598   ;; where the TYPE information came from:
599   ;;  :DECLARED, from a declaration.
600   ;;  :ASSUMED, from uses of the object.
601   ;;  :DEFINED, from examination of the definition.
602   ;; FIXME: This should be a named type. (LEAF-WHERE-FROM? Or
603   ;; perhaps just WHERE-FROM, since it's not just used in LEAF,
604   ;; but also in various DEFINE-INFO-TYPEs in globaldb.lisp,
605   ;; and very likely elsewhere too.)
606   (where-from :assumed :type (member :declared :assumed :defined))
607   ;; list of the REF nodes for this leaf
608   (refs () :type list)
609   ;; true if there was ever a REF or SET node for this leaf. This may
610   ;; be true when REFS and SETS are null, since code can be deleted.
611   (ever-used nil :type boolean)
612   ;; some kind of info used by the back end
613   (info nil))
614
615 ;;; The CONSTANT structure is used to represent known constant values.
616 ;;; If NAME is not null, then it is the name of the named constant
617 ;;; which this leaf corresponds to, otherwise this is an anonymous
618 ;;; constant.
619 (def!struct (constant (:include leaf))
620   ;; the value of the constant
621   (value nil :type t))
622 (defprinter (constant :identity t)
623   (name :test name)
624   value)
625
626 ;;; The BASIC-VAR structure represents information common to all
627 ;;; variables which don't correspond to known local functions.
628 (def!struct (basic-var (:include leaf) (:constructor nil))
629   ;; Lists of the set nodes for this variable.
630   (sets () :type list))
631
632 ;;; The GLOBAL-VAR structure represents a value hung off of the symbol
633 ;;; NAME. We use a :CONSTANT VAR when we know that the thing is a
634 ;;; constant, but don't know what the value is at compile time.
635 (def!struct (global-var (:include basic-var))
636   ;; kind of variable described
637   (kind (missing-arg)
638         :type (member :special :global-function :global)))
639 (defprinter (global-var :identity t)
640   name
641   (type :test (not (eq type *universal-type*)))
642   (where-from :test (not (eq where-from :assumed)))
643   kind)
644
645 ;;; The SLOT-ACCESSOR structure represents slot accessor functions. It
646 ;;; is a subtype of GLOBAL-VAR to make it look more like a normal
647 ;;; function.
648 (def!struct (slot-accessor (:include global-var
649                                      (where-from :defined)
650                                      (kind :global-function)))
651   ;; The description of the structure that this is an accessor for.
652   (for (missing-arg) :type sb!xc:class)
653   ;; The slot description of the slot.
654   (slot (missing-arg)))
655 (defprinter (slot-accessor :identity t)
656   name
657   for
658   slot)
659
660 ;;; A DEFINED-FUN represents a function that is defined in the same
661 ;;; compilation block, or that has an inline expansion, or that has a
662 ;;; non-NIL INLINEP value. Whenever we change the INLINEP state (i.e.
663 ;;; an inline proclamation) we copy the structure so that former
664 ;;; INLINEP values are preserved.
665 (def!struct (defined-fun (:include global-var
666                                    (where-from :defined)
667                                    (kind :global-function)))
668   ;; The values of INLINEP and INLINE-EXPANSION initialized from the
669   ;; global environment.
670   (inlinep nil :type inlinep)
671   (inline-expansion nil :type (or cons null))
672   ;; the block-local definition of this function (either because it
673   ;; was semi-inline, or because it was defined in this block). If
674   ;; this function is not an entry point, then this may be deleted or
675   ;; LET-converted. Null if we haven't converted the expansion yet.
676   (functional nil :type (or functional null)))
677 (defprinter (defined-fun :identity t)
678   name
679   inlinep
680   (functional :test functional))
681 \f
682 ;;;; function stuff
683
684 ;;; We default the WHERE-FROM and TYPE slots to :DEFINED and FUNCTION.
685 ;;; We don't normally manipulate function types for defined functions,
686 ;;; but if someone wants to know, an approximation is there.
687 (def!struct (functional (:include leaf
688                                   (where-from :defined)
689                                   (type (specifier-type 'function))))
690   ;; some information about how this function is used. These values
691   ;; are meaningful:
692   ;;
693   ;;    NIL
694   ;;    an ordinary function, callable using local call
695   ;;
696   ;;    :LET
697   ;;    a lambda that is used in only one local call, and has in
698   ;;    effect been substituted directly inline. The return node is
699   ;;    deleted, and the result is computed with the actual result
700   ;;    continuation for the call.
701   ;;
702   ;;    :MV-LET
703   ;;    Similar to :LET, but the call is an MV-CALL.
704   ;;
705   ;;    :ASSIGNMENT
706   ;;    similar to a LET, but can have other than one call as long as
707   ;;    there is at most one non-tail call.
708   ;;
709   ;;    :OPTIONAL
710   ;;    a lambda that is an entry-point for an optional-dispatch.
711   ;;    Similar to NIL, but requires greater caution, since local call
712   ;;    analysis may create new references to this function. Also, the
713   ;;    function cannot be deleted even if it has *no* references. The
714   ;;    OPTIONAL-DISPATCH is in the LAMDBA-OPTIONAL-DISPATCH.
715   ;;
716   ;;    :EXTERNAL
717   ;;    an external entry point lambda. The function it is an entry
718   ;;    for is in the ENTRY-FUNCTION slot.
719   ;;
720   ;;    :TOPLEVEL
721   ;;    a top level lambda, holding a compiled top level form.
722   ;;    Compiled very much like NIL, but provides an indication of
723   ;;    top level context. A :TOPLEVEL lambda should have *no*
724   ;;    references. Its ENTRY-FUNCTION is a self-pointer.
725   ;;
726   ;;    :TOPLEVEL-XEP
727   ;;    After a component is compiled, we clobber any top level code
728   ;;    references to its non-closure XEPs with dummy FUNCTIONAL
729   ;;    structures having this kind. This prevents the retained
730   ;;    top level code from holding onto the IR for the code it
731   ;;    references.
732   ;;
733   ;;    :ESCAPE
734   ;;    :CLEANUP
735   ;;    special functions used internally by CATCH and UNWIND-PROTECT.
736   ;;    These are pretty much like a normal function (NIL), but are
737   ;;    treated specially by local call analysis and stuff. Neither
738   ;;    kind should ever be given an XEP even though they appear as
739   ;;    args to funny functions. An :ESCAPE function is never actually
740   ;;    called, and thus doesn't need to have code generated for it.
741   ;;
742   ;;    :DELETED
743   ;;    This function has been found to be uncallable, and has been
744   ;;    marked for deletion.
745   (kind nil :type (member nil :optional :deleted :external :toplevel
746                           :escape :cleanup :let :mv-let :assignment
747                           :toplevel-xep))
748   ;; Is this a function that some external entity (e.g. the fasl dumper)
749   ;; refers to, so that even when it appears to have no references, it
750   ;; shouldn't be deleted? In the old days (before
751   ;; sbcl-0.pre7.37.flaky5.2) this was sort of implicitly true when
752   ;; KIND was :TOPLEVEL. Now it must be set explicitly, both for
753   ;; :TOPLEVEL functions and for any other kind of functions that we
754   ;; want to dump or return from #'CL:COMPILE or whatever.
755   (has-external-references-p nil) 
756   ;; In a normal function, this is the external entry point (XEP)
757   ;; lambda for this function, if any. Each function that is used
758   ;; other than in a local call has an XEP, and all of the
759   ;; non-local-call references are replaced with references to the
760   ;; XEP.
761   ;;
762   ;; In an XEP lambda (indicated by the :EXTERNAL kind), this is the
763   ;; function that the XEP is an entry-point for. The body contains
764   ;; local calls to all the actual entry points in the function. In a
765   ;; :TOPLEVEL lambda (which is its own XEP) this is a self-pointer.
766   ;;
767   ;; With all other kinds, this is null.
768   (entry-function nil :type (or functional null))
769   ;; the value of any inline/notinline declaration for a local function
770   (inlinep nil :type inlinep)
771   ;; If we have a lambda that can be used as in inline expansion for
772   ;; this function, then this is it. If there is no source-level
773   ;; lambda corresponding to this function then this is Null (but then
774   ;; INLINEP will always be NIL as well.)
775   (inline-expansion nil :type list)
776   ;; the lexical environment that the inline-expansion should be converted in
777   (lexenv *lexenv* :type lexenv)
778   ;; the original function or macro lambda list, or :UNSPECIFIED if
779   ;; this is a compiler created function
780   (arg-documentation nil :type (or list (member :unspecified)))
781   ;; various rare miscellaneous info that drives code generation & stuff
782   (plist () :type list))
783 (defprinter (functional :identity t)
784   name)
785
786 ;;; The CLAMBDA only deals with required lexical arguments. Special,
787 ;;; optional, keyword and rest arguments are handled by transforming
788 ;;; into simpler stuff.
789 (def!struct (clambda (:include functional)
790                      (:conc-name lambda-)
791                      (:predicate lambda-p)
792                      (:constructor make-lambda)
793                      (:copier copy-lambda))
794   ;; list of LAMBDA-VAR descriptors for args
795   (vars nil :type list)
796   ;; If this function was ever a :OPTIONAL function (an entry-point
797   ;; for an OPTIONAL-DISPATCH), then this is that OPTIONAL-DISPATCH.
798   ;; The optional dispatch will be :DELETED if this function is no
799   ;; longer :OPTIONAL.
800   (optional-dispatch nil :type (or optional-dispatch null))
801   ;; the BIND node for this LAMBDA. This node marks the beginning of
802   ;; the lambda, and serves to explicitly represent the lambda binding
803   ;; semantics within the flow graph representation. This is null in
804   ;; deleted functions, and also in LETs where we deleted the call and
805   ;; bind (because there are no variables left), but have not yet
806   ;; actually deleted the LAMBDA yet.
807   (bind nil :type (or bind null))
808   ;; the RETURN node for this LAMBDA, or NIL if it has been deleted.
809   ;; This marks the end of the lambda, receiving the result of the
810   ;; body. In a LET, the return node is deleted, and the body delivers
811   ;; the value to the actual continuation. The return may also be
812   ;; deleted if it is unreachable.
813   (return nil :type (or creturn null))
814   ;; If this CLAMBDA is a LET, then this slot holds the LAMBDA whose
815   ;; LETS list we are in, otherwise it is a self-pointer.
816   (home nil :type (or clambda null))
817   ;; a list of all the all the lambdas that have been LET-substituted
818   ;; in this lambda. This is only non-null in lambdas that aren't
819   ;; LETs.
820   (lets () :type list)
821   ;; a list of all the ENTRY nodes in this function and its LETs, or
822   ;; null in a LET
823   (entries () :type list)
824   ;; a list of all the functions directly called from this function
825   ;; (or one of its LETs) using a non-LET local call. This may include
826   ;; deleted functions because nobody bothers to clear them out.
827   (calls () :type list)
828   ;; the TAIL-SET that this LAMBDA is in. This is null during creation.
829   ;;
830   ;; In CMU CL, and old SBCL, this was also NILed out when LET
831   ;; conversion happened. That caused some problems, so as of
832   ;; sbcl-0.pre7.37.flaky5.2 when I was trying to get the compiler to
833   ;; emit :EXTERNAL functions directly, and so now the value
834   ;; is no longer NILed out in LET conversion, but instead copied
835   ;; (so that any further optimizations on the rest of the tail
836   ;; set won't modify the value) if necessary.
837   (tail-set nil :type (or tail-set null))
838   ;; the structure which represents the phsical environment that this
839   ;; function's variables are allocated in. This is filled in by
840   ;; physical environment analysis. In a LET, this is EQ to our home's
841   ;; physical environment.
842   (physenv nil :type (or physenv null))
843   ;; In a LET, this is the NODE-LEXENV of the combination node. We
844   ;; retain it so that if the LET is deleted (due to a lack of vars),
845   ;; we will still have caller's lexenv to figure out which cleanup is
846   ;; in effect.
847   (call-lexenv nil :type (or lexenv null)))
848 (defprinter (clambda :conc-name lambda- :identity t)
849   name
850   (type :test (not (eq type *universal-type*)))
851   (where-from :test (not (eq where-from :assumed)))
852   (vars :prin1 (mapcar #'leaf-name vars)))
853
854 ;;; The OPTIONAL-DISPATCH leaf is used to represent hairy lambdas. It
855 ;;; is a FUNCTIONAL, like LAMBDA. Each legal number of arguments has a
856 ;;; function which is called when that number of arguments is passed.
857 ;;; The function is called with all the arguments actually passed. If
858 ;;; additional arguments are legal, then the LEXPR style MORE-ENTRY
859 ;;; handles them. The value returned by the function is the value
860 ;;; which results from calling the OPTIONAL-DISPATCH.
861 ;;;
862 ;;; The theory is that each entry-point function calls the next entry
863 ;;; point tail-recursively, passing all the arguments passed in and
864 ;;; the default for the argument the entry point is for. The last
865 ;;; entry point calls the real body of the function. In the presence
866 ;;; of SUPPLIED-P args and other hair, things are more complicated. In
867 ;;; general, there is a distinct internal function that takes the
868 ;;; SUPPLIED-P args as parameters. The preceding entry point calls
869 ;;; this function with NIL filled in for the SUPPLIED-P args, while
870 ;;; the current entry point calls it with T in the SUPPLIED-P
871 ;;; positions.
872 ;;;
873 ;;; Note that it is easy to turn a call with a known number of
874 ;;; arguments into a direct call to the appropriate entry-point
875 ;;; function, so functions that are compiled together can avoid doing
876 ;;; the dispatch.
877 (def!struct (optional-dispatch (:include functional))
878   ;; the original parsed argument list, for anyone who cares
879   (arglist nil :type list)
880   ;; true if &ALLOW-OTHER-KEYS was supplied
881   (allowp nil :type boolean)
882   ;; true if &KEY was specified (which doesn't necessarily mean that
883   ;; there are any &KEY arguments..)
884   (keyp nil :type boolean)
885   ;; the number of required arguments. This is the smallest legal
886   ;; number of arguments.
887   (min-args 0 :type unsigned-byte)
888   ;; the total number of required and optional arguments. Args at
889   ;; positions >= to this are &REST, &KEY or illegal args.
890   (max-args 0 :type unsigned-byte)
891   ;; list of the LAMBDAs which are the entry points for non-rest,
892   ;; non-key calls. The entry for MIN-ARGS is first, MIN-ARGS+1
893   ;; second, ... MAX-ARGS last. The last entry-point always calls the
894   ;; main entry; in simple cases it may be the main entry.
895   (entry-points nil :type list)
896   ;; an entry point which takes MAX-ARGS fixed arguments followed by
897   ;; an argument context pointer and an argument count. This entry
898   ;; point deals with listifying rest args and parsing keywords. This
899   ;; is null when extra arguments aren't legal.
900   (more-entry nil :type (or clambda null))
901   ;; the main entry-point into the function, which takes all arguments
902   ;; including keywords as fixed arguments. The format of the
903   ;; arguments must be determined by examining the arglist. This may
904   ;; be used by callers that supply at least MAX-ARGS arguments and
905   ;; know what they are doing.
906   (main-entry nil :type (or clambda null)))
907 (defprinter (optional-dispatch :identity t)
908   name
909   (type :test (not (eq type *universal-type*)))
910   (where-from :test (not (eq where-from :assumed)))
911   arglist
912   allowp
913   keyp
914   min-args
915   max-args
916   (entry-points :test entry-points)
917   (more-entry :test more-entry)
918   main-entry)
919
920 ;;; The ARG-INFO structure allows us to tack various information onto
921 ;;; LAMBDA-VARs during IR1 conversion. If we use one of these things,
922 ;;; then the var will have to be massaged a bit before it is simple
923 ;;; and lexical.
924 (def!struct arg-info
925   ;; true if this arg is to be specially bound
926   (specialp nil :type boolean)
927   ;; the kind of argument being described. Required args only have arg
928   ;; info structures if they are special.
929   (kind (missing-arg)
930         :type (member :required :optional :keyword :rest
931                       :more-context :more-count))
932   ;; If true, this is the VAR for SUPPLIED-P variable of a keyword or
933   ;; optional arg. This is true for keywords with non-constant
934   ;; defaults even when there is no user-specified supplied-p var.
935   (supplied-p nil :type (or lambda-var null))
936   ;; the default for a keyword or optional, represented as the
937   ;; original Lisp code. This is set to NIL in &KEY arguments that are
938   ;; defaulted using the SUPPLIED-P arg.
939   (default nil :type t)
940   ;; the actual key for a &KEY argument. Note that in ANSI CL this is not
941   ;; necessarily a keyword: (DEFUN FOO (&KEY ((BAR BAR))) ..).
942   (key nil :type symbol))
943 (defprinter (arg-info :identity t)
944   (specialp :test specialp)
945   kind
946   (supplied-p :test supplied-p)
947   (default :test default)
948   (key :test key))
949
950 ;;; The LAMBDA-VAR structure represents a lexical lambda variable.
951 ;;; This structure is also used during IR1 conversion to describe
952 ;;; lambda arguments which may ultimately turn out not to be simple
953 ;;; and lexical.
954 ;;;
955 ;;; LAMBDA-VARs with no REFs are considered to be deleted; physical
956 ;;; environment analysis isn't done on these variables, so the back
957 ;;; end must check for and ignore unreferenced variables. Note that a
958 ;;; deleted lambda-var may have sets; in this case the back end is
959 ;;; still responsible for propagating the Set-Value to the set's Cont.
960 (def!struct (lambda-var (:include basic-var))
961   ;; true if this variable has been declared IGNORE
962   (ignorep nil :type boolean)
963   ;; the CLAMBDA that this var belongs to. This may be null when we are
964   ;; building a lambda during IR1 conversion.
965   (home nil :type (or null clambda))
966   ;; This is set by physical environment analysis if it chooses an
967   ;; indirect (value cell) representation for this variable because it
968   ;; is both set and closed over.
969   (indirect nil :type boolean)
970   ;; The following two slots are only meaningful during IR1 conversion
971   ;; of hairy lambda vars:
972   ;;
973   ;; The ARG-INFO structure which holds information obtained from
974   ;; &keyword parsing.
975   (arg-info nil :type (or arg-info null))
976   ;; if true, the GLOBAL-VAR structure for the special variable which
977   ;; is to be bound to the value of this argument
978   (specvar nil :type (or global-var null))
979   ;; Set of the CONSTRAINTs on this variable. Used by constraint
980   ;; propagation. This is left null by the lambda pre-pass if it
981   ;; determine that this is a set closure variable, and is thus not a
982   ;; good subject for flow analysis.
983   (constraints nil :type (or sset null)))
984 (defprinter (lambda-var :identity t)
985   name
986   (type :test (not (eq type *universal-type*)))
987   (where-from :test (not (eq where-from :assumed)))
988   (ignorep :test ignorep)
989   (arg-info :test arg-info)
990   (specvar :test specvar))
991 \f
992 ;;;; basic node types
993
994 ;;; A REF represents a reference to a LEAF. REF-REOPTIMIZE is
995 ;;; initially (and forever) NIL, since REFs don't receive any values
996 ;;; and don't have any IR1 optimizer.
997 (defstruct (ref (:include node (:reoptimize nil))
998                 (:constructor make-ref (derived-type leaf))
999                 (:copier nil))
1000   ;; The leaf referenced.
1001   (leaf nil :type leaf))
1002 (defprinter (ref :identity t)
1003   leaf)
1004
1005 ;;; Naturally, the IF node always appears at the end of a block.
1006 ;;; NODE-CONT is a dummy continuation, and is there only to keep
1007 ;;; people happy.
1008 (defstruct (cif (:include node)
1009                 (:conc-name if-)
1010                 (:predicate if-p)
1011                 (:constructor make-if)
1012                 (:copier copy-if))
1013   ;; CONTINUATION for the predicate
1014   (test (missing-arg) :type continuation)
1015   ;; the blocks that we execute next in true and false case,
1016   ;; respectively (may be the same)
1017   (consequent (missing-arg) :type cblock)
1018   (alternative (missing-arg) :type cblock))
1019 (defprinter (cif :conc-name if- :identity t)
1020   (test :prin1 (continuation-use test))
1021   consequent
1022   alternative)
1023
1024 (defstruct (cset (:include node
1025                            (derived-type *universal-type*))
1026                  (:conc-name set-)
1027                  (:predicate set-p)
1028                  (:constructor make-set)
1029                  (:copier copy-set))
1030   ;; descriptor for the variable set
1031   (var (missing-arg) :type basic-var)
1032   ;; continuation for the value form
1033   (value (missing-arg) :type continuation))
1034 (defprinter (cset :conc-name set- :identity t)
1035   var
1036   (value :prin1 (continuation-use value)))
1037
1038 ;;; The BASIC-COMBINATION structure is used to represent both normal
1039 ;;; and multiple value combinations. In a local function call, this
1040 ;;; node appears at the end of its block and the body of the called
1041 ;;; function appears as the successor. The NODE-CONT remains the
1042 ;;; continuation which receives the value of the call.
1043 (defstruct (basic-combination (:include node)
1044                               (:constructor nil)
1045                               (:copier nil))
1046   ;; continuation for the function
1047   (fun (missing-arg) :type continuation)
1048   ;; list of CONTINUATIONs for the args. In a local call, an argument
1049   ;; continuation may be replaced with NIL to indicate that the
1050   ;; corresponding variable is unreferenced, and thus no argument
1051   ;; value need be passed.
1052   (args nil :type list)
1053   ;; the kind of function call being made. :LOCAL means that this is a
1054   ;; local call to a function in the same component, and that argument
1055   ;; syntax checking has been done, etc. Calls to known global
1056   ;; functions are represented by storing the FUNCTION-INFO for the
1057   ;; function in this slot. :FULL is a call to an (as yet) unknown
1058   ;; function. :ERROR is like :FULL, but means that we have discovered
1059   ;; that the call contains an error, and should not be reconsidered
1060   ;; for optimization.
1061   (kind :full :type (or (member :local :full :error) function-info))
1062   ;; some kind of information attached to this node by the back end
1063   (info nil))
1064
1065 ;;; The COMBINATION node represents all normal function calls,
1066 ;;; including FUNCALL. This is distinct from BASIC-COMBINATION so that
1067 ;;; an MV-COMBINATION isn't COMBINATION-P.
1068 (defstruct (combination (:include basic-combination)
1069                         (:constructor make-combination (fun))
1070                         (:copier nil)))
1071 (defprinter (combination :identity t)
1072   (fun :prin1 (continuation-use fun))
1073   (args :prin1 (mapcar (lambda (x)
1074                          (if x
1075                              (continuation-use x)
1076                              "<deleted>"))
1077                        args)))
1078
1079 ;;; An MV-COMBINATION is to MULTIPLE-VALUE-CALL as a COMBINATION is to
1080 ;;; FUNCALL. This is used to implement all the multiple-value
1081 ;;; receiving forms.
1082 (defstruct (mv-combination (:include basic-combination)
1083                            (:constructor make-mv-combination (fun))
1084                            (:copier nil)))
1085 (defprinter (mv-combination)
1086   (fun :prin1 (continuation-use fun))
1087   (args :prin1 (mapcar #'continuation-use args)))
1088
1089 ;;; The BIND node marks the beginning of a lambda body and represents
1090 ;;; the creation and initialization of the variables.
1091 (defstruct (bind (:include node)
1092                  (:copier nil))
1093   ;; the lambda we are binding variables for. Null when we are
1094   ;; creating the LAMBDA during IR1 translation.
1095   (lambda nil :type (or clambda null)))
1096 (defprinter (bind)
1097   lambda)
1098
1099 ;;; The RETURN node marks the end of a lambda body. It collects the
1100 ;;; return values and represents the control transfer on return. This
1101 ;;; is also where we stick information used for TAIL-SET type
1102 ;;; inference.
1103 (defstruct (creturn (:include node)
1104                     (:conc-name return-)
1105                     (:predicate return-p)
1106                     (:constructor make-return)
1107                     (:copier copy-return))
1108   ;; the lambda we are returning from. Null temporarily during
1109   ;; ir1tran.
1110   (lambda nil :type (or clambda null))
1111   ;; the continuation which yields the value of the lambda
1112   (result (missing-arg) :type continuation)
1113   ;; the union of the node-derived-type of all uses of the result
1114   ;; other than by a local call, intersected with the result's
1115   ;; asserted-type. If there are no non-call uses, this is
1116   ;; *EMPTY-TYPE*
1117   (result-type *wild-type* :type ctype))
1118 (defprinter (creturn :conc-name return- :identity t)
1119   lambda
1120   result-type)
1121 \f
1122 ;;;; non-local exit support
1123 ;;;;
1124 ;;;; In IR1, we insert special nodes to mark potentially non-local
1125 ;;;; lexical exits.
1126
1127 ;;; The ENTRY node serves to mark the start of the dynamic extent of a
1128 ;;; lexical exit. It is the mess-up node for the corresponding :Entry
1129 ;;; cleanup.
1130 (defstruct (entry (:include node)
1131                   (:copier nil))
1132   ;; All of the Exit nodes for potential non-local exits to this point.
1133   (exits nil :type list)
1134   ;; The cleanup for this entry. NULL only temporarily.
1135   (cleanup nil :type (or cleanup null)))
1136 (defprinter (entry :identity t))
1137
1138 ;;; The EXIT node marks the place at which exit code would be emitted,
1139 ;;; if necessary. This is interposed between the uses of the exit
1140 ;;; continuation and the exit continuation's DEST. Instead of using
1141 ;;; the returned value being delivered directly to the exit
1142 ;;; continuation, it is delivered to our VALUE continuation. The
1143 ;;; original exit continuation is the exit node's CONT.
1144 (defstruct (exit (:include node)
1145                  (:copier nil))
1146   ;; The Entry node that this is an exit for. If null, this is a
1147   ;; degenerate exit. A degenerate exit is used to "fill" an empty
1148   ;; block (which isn't allowed in IR1.) In a degenerate exit, Value
1149   ;; is always also null.
1150   (entry nil :type (or entry null))
1151   ;; The continuation yeilding the value we are to exit with. If NIL,
1152   ;; then no value is desired (as in GO).
1153   (value nil :type (or continuation null)))
1154 (defprinter (exit :identity t)
1155   (entry :test entry)
1156   (value :test value))
1157 \f
1158 ;;;; miscellaneous IR1 structures
1159
1160 (defstruct (undefined-warning
1161             #-no-ansi-print-object
1162             (:print-object (lambda (x s)
1163                              (print-unreadable-object (x s :type t)
1164                                (prin1 (undefined-warning-name x) s))))
1165             (:copier nil))
1166   ;; the name of the unknown thing
1167   (name nil :type (or symbol list))
1168   ;; the kind of reference to NAME
1169   (kind (missing-arg) :type (member :function :type :variable))
1170   ;; the number of times this thing was used
1171   (count 0 :type unsigned-byte)
1172   ;; a list of COMPILER-ERROR-CONTEXT structures describing places
1173   ;; where this thing was used. Note that we only record the first
1174   ;; *UNDEFINED-WARNING-LIMIT* calls.
1175   (warnings () :type list))
1176 \f
1177 ;;; a helper for the POLICY macro, defined late here so that the
1178 ;;; various type tests can be inlined
1179 (declaim (ftype (function ((or list lexenv node functional)) list)
1180                 %coerce-to-policy))
1181 (defun %coerce-to-policy (thing)
1182   (let ((result (etypecase thing
1183                   (list thing)
1184                   (lexenv (lexenv-policy thing))
1185                   (node (lexenv-policy (node-lexenv thing)))
1186                   (functional (lexenv-policy (functional-lexenv thing))))))
1187     ;; Test the first element of the list as a rudimentary sanity
1188     ;; that it really does look like a valid policy.
1189     (aver (or (null result) (policy-quality-name-p (caar result))))
1190     ;; Voila.
1191     result))
1192 \f
1193 ;;;; Freeze some structure types to speed type testing.
1194
1195 #!-sb-fluid
1196 (declaim (freeze-type node leaf lexenv continuation cblock component cleanup
1197                       physenv tail-set nlx-info))