8b1d32b42433c48da7c0b736d01b533ad221261c
[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   ;; the 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   ;; This is computed lazily by CONTINUATION-DERIVED-TYPE, so use
128   ;; CONTINUATION-TYPE-CHECK instead of the %'ed slot accessor.
129   (%type-check t :type (member t nil :deleted :no-check))
130   ;; Asserted type, weakend according to policies
131   (type-to-check *wild-type* :type ctype)
132   ;; Cached type which is checked by DEST. If NIL, then this must be
133   ;; recomputed: see CONTINUATION-EXTERNALLY-CHECKABLE-TYPE.
134   (%externally-checkable-type nil :type (or null ctype))
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   ;; unique ID for debugging
148   #!+sb-show (id (new-object-id) :read-only t)
149   ;; the bottom-up derived type for this node. This does not take into
150   ;; consideration output type assertions on this node (actually on its CONT).
151   (derived-type *wild-type* :type ctype)
152   ;; True if this node needs to be optimized. This is set to true
153   ;; whenever something changes about the value of a continuation
154   ;; whose DEST is this node.
155   (reoptimize t :type boolean)
156   ;; the continuation which receives the value of this node. This also
157   ;; indicates what we do controlwise after evaluating this node. This
158   ;; may be null during IR1 conversion.
159   (cont nil :type (or continuation null))
160   ;; the continuation that this node is the next of. This is null
161   ;; during IR1 conversion when we haven't linked the node in yet or
162   ;; in nodes that have been deleted from the IR1 by UNLINK-NODE.
163   (prev nil :type (or continuation null))
164   ;; the lexical environment this node was converted in
165   (lexenv *lexenv* :type lexenv)
166   ;; a representation of the source code responsible for generating
167   ;; this node
168   ;;
169   ;; For a form introduced by compilation (does not appear in the
170   ;; original source), the path begins with a list of all the
171   ;; enclosing introduced forms. This list is from the inside out,
172   ;; with the form immediately responsible for this node at the head
173   ;; of the list.
174   ;;
175   ;; Following the introduced forms is a representation of the
176   ;; location of the enclosing original source form. This transition
177   ;; is indicated by the magic ORIGINAL-SOURCE-START marker. The first
178   ;; element of the original source is the "form number", which is the
179   ;; ordinal number of this form in a depth-first, left-to-right walk
180   ;; of the truly-top-level form in which this appears.
181   ;;
182   ;; Following is a list of integers describing the path taken through
183   ;; the source to get to this point:
184   ;;     (K L M ...) => (NTH K (NTH L (NTH M ...)))
185   ;;
186   ;; The last element in the list is the top level form number, which
187   ;; is the ordinal number (in this call to the compiler) of the truly
188   ;; top level form containing the original source.
189   (source-path *current-path* :type list)
190   ;; If this node is in a tail-recursive position, then this is set to
191   ;; T. At the end of IR1 (in physical environment analysis) this is
192   ;; computed for all nodes (after cleanup code has been emitted).
193   ;; Before then, a non-null value indicates that IR1 optimization has
194   ;; converted a tail local call to a direct transfer.
195   ;;
196   ;; If the back-end breaks tail-recursion for some reason, then it
197   ;; can null out this slot.
198   (tail-p nil :type boolean))
199
200 ;;; Flags that are used to indicate various things about a block, such
201 ;;; as what optimizations need to be done on it:
202 ;;; -- REOPTIMIZE is set when something interesting happens the uses of a
203 ;;;    continuation whose DEST is in this block. This indicates that the
204 ;;;    value-driven (forward) IR1 optimizations should be done on this block.
205 ;;; -- FLUSH-P is set when code in this block becomes potentially flushable,
206 ;;;    usually due to a continuation's DEST becoming null.
207 ;;; -- TYPE-CHECK is true when the type check phase should be run on this
208 ;;;    block. IR1 optimize can introduce new blocks after type check has
209 ;;;    already run. We need to check these blocks, but there is no point in
210 ;;;    checking blocks we have already checked.
211 ;;; -- DELETE-P is true when this block is used to indicate that this block
212 ;;;    has been determined to be unreachable and should be deleted. IR1
213 ;;;    phases should not attempt to examine or modify blocks with DELETE-P
214 ;;;    set, since they may:
215 ;;;     - be in the process of being deleted, or
216 ;;;     - have no successors, or
217 ;;;     - receive :DELETED continuations.
218 ;;; -- TYPE-ASSERTED, TEST-MODIFIED
219 ;;;    These flags are used to indicate that something in this block
220 ;;;    might be of interest to constraint propagation. TYPE-ASSERTED
221 ;;;    is set when a continuation type assertion is strengthened.
222 ;;;    TEST-MODIFIED is set whenever the test for the ending IF has
223 ;;;    changed (may be true when there is no IF.)
224 (!def-boolean-attribute block
225   reoptimize flush-p type-check delete-p type-asserted test-modified)
226
227 ;;; FIXME: Tweak so that definitions of e.g. BLOCK-DELETE-P is
228 ;;; findable by grep for 'def.*block-delete-p'.
229 (macrolet ((frob (slot)
230              `(defmacro ,(symbolicate "BLOCK-" slot) (block)
231                 `(block-attributep (block-flags ,block) ,',slot))))
232   (frob reoptimize)
233   (frob flush-p)
234   (frob type-check)
235   (frob delete-p)
236   (frob type-asserted)
237   (frob test-modified))
238
239 ;;; The CBLOCK structure represents a basic block. We include
240 ;;; SSET-ELEMENT so that we can have sets of blocks. Initially the
241 ;;; SSET-ELEMENT-NUMBER is null, DFO analysis numbers in reverse DFO.
242 ;;; During IR2 conversion, IR1 blocks are re-numbered in forward emit
243 ;;; order. This latter numbering also forms the basis of the block
244 ;;; numbering in the debug-info (though that is relative to the start
245 ;;; of the function.)
246 (defstruct (cblock (:include sset-element)
247                    (:constructor make-block (start))
248                    (:constructor make-block-key)
249                    (:conc-name block-)
250                    (:predicate block-p)
251                    (:copier copy-block))
252   ;; a list of all the blocks that are predecessors/successors of this
253   ;; block. In well-formed IR1, most blocks will have one successor.
254   ;; The only exceptions are:
255   ;;  1. component head blocks (any number)
256   ;;  2. blocks ending in an IF (1 or 2)
257   ;;  3. blocks with DELETE-P set (zero)
258   (pred nil :type list)
259   (succ nil :type list)
260   ;; the continuation which heads this block (either a :BLOCK-START or
261   ;; :DELETED-BLOCK-START), or NIL when we haven't made the start
262   ;; continuation yet (and in the dummy component head and tail
263   ;; blocks)
264   (start nil :type (or continuation null))
265   ;; a list of all the nodes that have START as their CONT
266   (start-uses nil :type list)
267   ;; the last node in this block. This is NIL when we are in the
268   ;; process of building a block (and in the dummy component head and
269   ;; tail blocks.)
270   (last nil :type (or node null))
271   ;; the forward and backward links in the depth-first ordering of the
272   ;; blocks. These slots are NIL at beginning/end.
273   (next nil :type (or null cblock))
274   (prev nil :type (or null cblock))
275   ;; This block's attributes: see above.
276   (flags (block-attributes reoptimize flush-p type-check type-asserted
277                            test-modified)
278          :type attributes)
279   ;; CMU CL had a KILL slot here, documented as "set used by
280   ;; constraint propagation", which was used in constraint propagation
281   ;; as a list of LAMBDA-VARs killed, and in copy propagation as an
282   ;; SSET, representing I dunno what. I (WHN) found this confusing,
283   ;; and furthermore it caused type errors when I was trying to make
284   ;; the compiler produce fully general LAMBDA functions directly
285   ;; (instead of doing as CMU CL always did, producing extra little
286   ;; functions which return the LAMDBA you need) and therefore taking
287   ;; a new path through the compiler. So I split this into two:
288   ;;   KILL-LIST = list of LAMBDA-VARs killed, used in constraint propagation
289   ;;   KILL-SSET = an SSET value, used in copy propagation
290   (kill-list nil :type list)
291   (kill-sset nil :type (or sset null))
292   ;; other sets used in constraint propagation and/or copy propagation
293   (gen nil)
294   (in nil)
295   (out nil)
296   ;; the component this block is in, or NIL temporarily during IR1
297   ;; conversion and in deleted blocks
298   (component (progn
299                (aver-live-component *current-component*)
300                *current-component*)
301              :type (or component null))
302   ;; a flag used by various graph-walking code to determine whether
303   ;; this block has been processed already or what. We make this
304   ;; initially NIL so that FIND-INITIAL-DFO doesn't have to scan the
305   ;; entire initial component just to clear the flags.
306   (flag nil)
307   ;; some kind of info used by the back end
308   (info nil)
309   ;; If true, then constraints that hold in this block and its
310   ;; successors by merit of being tested by its IF predecessor.
311   (test-constraint nil :type (or sset null)))
312 (def!method print-object ((cblock cblock) stream)
313   (print-unreadable-object (cblock stream :type t :identity t)
314     (format stream "~W :START c~W"
315             (block-number cblock)
316             (cont-num (block-start cblock)))))
317
318 ;;; The BLOCK-ANNOTATION class is inherited (via :INCLUDE) by
319 ;;; different BLOCK-INFO annotation structures so that code
320 ;;; (specifically control analysis) can be shared.
321 (defstruct (block-annotation (:constructor nil)
322                              (:copier nil))
323   ;; The IR1 block that this block is in the INFO for.
324   (block (missing-arg) :type cblock)
325   ;; the next and previous block in emission order (not DFO). This
326   ;; determines which block we drop though to, and is also used to
327   ;; chain together overflow blocks that result from splitting of IR2
328   ;; blocks in lifetime analysis.
329   (next nil :type (or block-annotation null))
330   (prev nil :type (or block-annotation null)))
331
332 ;;; A COMPONENT structure provides a handle on a connected piece of
333 ;;; the flow graph. Most of the passes in the compiler operate on
334 ;;; COMPONENTs rather than on the entire flow graph.
335 ;;;
336 ;;; According to the CMU CL internals/front.tex, the reason for
337 ;;; separating compilation into COMPONENTs is
338 ;;;   to increase the efficiency of large block compilations. In
339 ;;;   addition to improving locality of reference and reducing the
340 ;;;   size of flow analysis problems, this allows back-end data
341 ;;;   structures to be reclaimed after the compilation of each
342 ;;;   component.
343 (defstruct (component (:copier nil))
344   ;; unique ID for debugging
345   #!+sb-show (id (new-object-id) :read-only t)
346   ;; the kind of component
347   ;;
348   ;; (The terminology here is left over from before
349   ;; sbcl-0.pre7.34.flaky5.2, when there was no such thing as
350   ;; FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P, so that Python was
351   ;; incapable of building standalone :EXTERNAL functions, but instead
352   ;; had to implement things like #'CL:COMPILE as FUNCALL of a little
353   ;; toplevel stub whose sole purpose was to return an :EXTERNAL
354   ;; function.)
355   ;;
356   ;; The possibilities are:
357   ;;   NIL
358   ;;     an ordinary component, containing non-top-level code
359   ;;   :TOPLEVEL
360   ;;     a component containing only load-time code
361   ;;   :COMPLEX-TOPLEVEL
362   ;;     In the old system, before FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P
363   ;;     was defined, this was necessarily a component containing both
364   ;;     top level and run-time code. Now this state is also used for
365   ;;     a component with HAS-EXTERNAL-REFERENCES-P functionals in it.
366   ;;   :INITIAL
367   ;;     the result of initial IR1 conversion, on which component
368   ;;     analysis has not been done
369   ;;   :DELETED
370   ;;     debris left over from component analysis
371   ;;
372   ;; See also COMPONENT-TOPLEVELISH-P.
373   (kind nil :type (member nil :toplevel :complex-toplevel :initial :deleted))
374   ;; the blocks that are the dummy head and tail of the DFO
375   ;;
376   ;; Entry/exit points have these blocks as their
377   ;; predecessors/successors. Null temporarily. The start and return
378   ;; from each non-deleted function is linked to the component head
379   ;; and tail. Until physical environment analysis links NLX entry
380   ;; stubs to the component head, every successor of the head is a
381   ;; function start (i.e. begins with a BIND node.)
382   (head nil :type (or null cblock))
383   (tail nil :type (or null cblock))
384   ;; This becomes a list of the CLAMBDA structures for all functions
385   ;; in this component. OPTIONAL-DISPATCHes are represented only by
386   ;; their XEP and other associated lambdas. This doesn't contain any
387   ;; deleted or LET lambdas.
388   ;;
389   ;; Note that logical associations between CLAMBDAs and COMPONENTs
390   ;; seem to exist for a while before this is initialized. See e.g.
391   ;; the NEW-FUNCTIONALS slot. In particular, I got burned by writing
392   ;; some code to use this value to decide which components need
393   ;; LOCALL-ANALYZE-COMPONENT, when it turns out that
394   ;; LOCALL-ANALYZE-COMPONENT had a role in initializing this value
395   ;; (and DFO stuff does too, maybe). Also, even after it's
396   ;; initialized, it might change as CLAMBDAs are deleted or merged.
397   ;; -- WHN 2001-09-30
398   (lambdas () :type list)
399   ;; a list of FUNCTIONALs for functions that are newly converted, and
400   ;; haven't been local-call analyzed yet. Initially functions are not
401   ;; in the LAMBDAS list. Local call analysis moves them there
402   ;; (possibly as LETs, or implicitly as XEPs if an OPTIONAL-DISPATCH.)
403   ;; Between runs of local call analysis there may be some debris of
404   ;; converted or even deleted functions in this list.
405   (new-functionals () :type list)
406   ;; If this is true, then there is stuff in this component that could
407   ;; benefit from further IR1 optimization.
408   (reoptimize t :type boolean)
409   ;; If this is true, then the control flow in this component was
410   ;; messed up by IR1 optimizations, so the DFO should be recomputed.
411   (reanalyze nil :type boolean)
412   ;; some sort of name for the code in this component
413   (name "<unknown>" :type simple-string)
414   ;; When I am a child, this is :NO-IR2-YET.
415   ;; In my adulthood, IR2 stores notes to itself here.
416   ;; After I have left the great wheel and am staring into the GC, this
417   ;;   is set to :DEAD to indicate that it's a gruesome error to operate
418   ;;   on me (e.g. by using me as *CURRENT-COMPONENT*, or by pushing
419   ;;   LAMBDAs onto my NEW-FUNCTIONALS, as in sbcl-0.pre7.115).
420   (info :no-ir2-yet :type (or ir2-component (member :no-ir2-yet :dead)))
421   ;; the SOURCE-INFO structure describing where this component was
422   ;; compiled from
423   (source-info *source-info* :type source-info)
424   ;; count of the number of inline expansions we have done while
425   ;; compiling this component, to detect infinite or exponential
426   ;; blowups
427   (inline-expansions 0 :type index)
428   ;; a map from combination nodes to things describing how an
429   ;; optimization of the node failed. The description is an alist
430   ;; (TRANSFORM . ARGS), where TRANSFORM is the structure describing
431   ;; the transform that failed, and ARGS is either a list of format
432   ;; arguments for the note, or the FUN-TYPE that would have
433   ;; enabled the transformation but failed to match.
434   (failed-optimizations (make-hash-table :test 'eq) :type hash-table)
435   ;; This is similar to NEW-FUNCTIONALS, but is used when a function
436   ;; has already been analyzed, but new references have been added by
437   ;; inline expansion. Unlike NEW-FUNCTIONALS, this is not disjoint
438   ;; from COMPONENT-LAMBDAS.
439   (reanalyze-functionals nil :type list))
440 (defprinter (component :identity t)
441   name
442   #!+sb-show id
443   (reanalyze :test reanalyze))
444
445 ;;; Check that COMPONENT is suitable for roles which involve adding
446 ;;; new code. (gotta love imperative programming with lotso in-place
447 ;;; side effects...)
448 (defun aver-live-component (component)
449   ;; FIXME: As of sbcl-0.pre7.115, we're asserting that
450   ;; COMPILE-COMPONENT hasn't happened yet. Might it be even better
451   ;; (certainly stricter, possibly also correct...) to assert that
452   ;; IR1-FINALIZE hasn't happened yet?
453   (aver (not (eql (component-info component) :dead))))
454
455 ;;; Before sbcl-0.7.0, there were :TOPLEVEL things which were magical
456 ;;; in multiple ways. That's since been refactored into the orthogonal
457 ;;; properties "optimized for locall with no arguments" and "externally
458 ;;; visible/referenced (so don't delete it)". The code <0.7.0 did a lot
459 ;;; of tests a la (EQ KIND :TOP_LEVEL) in the "don't delete it?" sense;
460 ;;; this function is a sort of literal translation of those tests into
461 ;;; the new world.
462 ;;;
463 ;;; FIXME: After things settle down, bare :TOPLEVEL might go away, at
464 ;;; which time it might be possible to replace the COMPONENT-KIND
465 ;;; :TOPLEVEL mess with a flag COMPONENT-HAS-EXTERNAL-REFERENCES-P
466 ;;; along the lines of FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P.
467 (defun lambda-toplevelish-p (clambda)
468   (or (eql (lambda-kind clambda) :toplevel)
469       (lambda-has-external-references-p clambda)))
470 (defun component-toplevelish-p (component)
471   (member (component-kind component)
472           '(:toplevel :complex-toplevel)))
473
474 ;;; A CLEANUP structure represents some dynamic binding action. Blocks
475 ;;; are annotated with the current CLEANUP so that dynamic bindings
476 ;;; can be removed when control is transferred out of the binding
477 ;;; environment. We arrange for changes in dynamic bindings to happen
478 ;;; at block boundaries, so that cleanup code may easily be inserted.
479 ;;; The "mess-up" action is explicitly represented by a funny function
480 ;;; call or ENTRY node.
481 ;;;
482 ;;; We guarantee that CLEANUPs only need to be done at block boundaries
483 ;;; by requiring that the exit continuations initially head their
484 ;;; blocks, and then by not merging blocks when there is a cleanup
485 ;;; change.
486 (defstruct (cleanup (:copier nil))
487   ;; the kind of thing that has to be cleaned up
488   (kind (missing-arg)
489         :type (member :special-bind :catch :unwind-protect :block :tagbody))
490   ;; the node that messes things up. This is the last node in the
491   ;; non-messed-up environment. Null only temporarily. This could be
492   ;; deleted due to unreachability.
493   (mess-up nil :type (or node null))
494   ;; a list of all the NLX-INFO structures whose NLX-INFO-CLEANUP is
495   ;; this cleanup. This is filled in by physical environment analysis.
496   (nlx-info nil :type list))
497 (defprinter (cleanup :identity t)
498   kind
499   mess-up
500   (nlx-info :test nlx-info))
501
502 ;;; A PHYSENV represents the result of physical environment analysis.
503 ;;;
504 ;;; As far as I can tell from reverse engineering, this IR1 structure
505 ;;; represents the physical environment (which is probably not the
506 ;;; standard Lispy term for this concept, but I dunno what is the
507 ;;; standard term): those things in the lexical environment which a
508 ;;; LAMBDA actually interacts with. Thus in
509 ;;;   (DEFUN FROB-THINGS (THINGS)
510 ;;;     (DOLIST (THING THINGS)
511 ;;;       (BLOCK FROBBING-ONE-THING
512 ;;;         (MAPCAR (LAMBDA (PATTERN)
513 ;;;                   (WHEN (FITS-P THING PATTERN)
514 ;;;                     (RETURN-FROM FROB-THINGS (LIST :FIT THING PATTERN))))
515 ;;;                 *PATTERNS*))))
516 ;;; the variables THINGS, THING, and PATTERN and the block names
517 ;;; FROB-THINGS and FROBBING-ONE-THING are all in the inner LAMBDA's
518 ;;; lexical environment, but of those only THING, PATTERN, and
519 ;;; FROB-THINGS are in its physical environment. In IR1, we largely
520 ;;; just collect the names of these things; in IR2 an IR2-PHYSENV
521 ;;; structure is attached to INFO and used to keep track of
522 ;;; associations between these names and less-abstract things (like
523 ;;; TNs, or eventually stack slots and registers). -- WHN 2001-09-29
524 (defstruct (physenv (:copier nil))
525   ;; the function that allocates this physical environment
526   (lambda (missing-arg) :type clambda :read-only t)
527   #| ; seems not to be used as of sbcl-0.pre7.51
528   ;; a list of all the lambdas that allocate variables in this
529   ;; physical environment
530   (lambdas nil :type list)
531   |#
532   ;; This ultimately converges to a list of all the LAMBDA-VARs and
533   ;; NLX-INFOs needed from enclosing environments by code in this
534   ;; physical environment. In the meantime, it may be
535   ;;   * NIL at object creation time
536   ;;   * a superset of the correct result, generated somewhat later
537   ;;   * smaller and smaller sets converging to the correct result as
538   ;;     we notice and delete unused elements in the superset
539   (closure nil :type list)
540   ;; a list of NLX-INFO structures describing all the non-local exits
541   ;; into this physical environment
542   (nlx-info nil :type list)
543   ;; some kind of info used by the back end
544   (info nil))
545 (defprinter (physenv :identity t)
546   lambda
547   (closure :test closure)
548   (nlx-info :test nlx-info))
549
550 ;;; An TAIL-SET structure is used to accumulate information about
551 ;;; tail-recursive local calls. The "tail set" is effectively the
552 ;;; transitive closure of the "is called tail-recursively by"
553 ;;; relation.
554 ;;;
555 ;;; All functions in the same tail set share the same TAIL-SET
556 ;;; structure. Initially each function has its own TAIL-SET, but when
557 ;;; IR1-OPTIMIZE-RETURN notices a tail local call, it joins the tail
558 ;;; sets of the called function and the calling function.
559 ;;;
560 ;;; The tail set is somewhat approximate, because it is too early to
561 ;;; be sure which calls will be tail-recursive. Any call that *might*
562 ;;; end up tail-recursive causes TAIL-SET merging.
563 (defstruct (tail-set)
564   ;; a list of all the LAMBDAs in this tail set
565   (funs nil :type list)
566   ;; our current best guess of the type returned by these functions.
567   ;; This is the union across all the functions of the return node's
568   ;; RESULT-TYPE, excluding local calls.
569   (type *wild-type* :type ctype)
570   ;; some info used by the back end
571   (info nil))
572 (defprinter (tail-set :identity t)
573   funs
574   type
575   (info :test info))
576
577 ;;; An NLX-INFO structure is used to collect various information about
578 ;;; non-local exits. This is effectively an annotation on the
579 ;;; CONTINUATION, although it is accessed by searching in the
580 ;;; PHYSENV-NLX-INFO.
581 (def!struct (nlx-info (:make-load-form-fun ignore-it))
582   ;; the cleanup associated with this exit. In a catch or
583   ;; unwind-protect, this is the :CATCH or :UNWIND-PROTECT cleanup,
584   ;; and not the cleanup for the escape block. The CLEANUP-KIND of
585   ;; this thus provides a good indication of what kind of exit is
586   ;; being done.
587   (cleanup (missing-arg) :type cleanup)
588   ;; the continuation exited to (the CONT of the EXIT nodes). If this
589   ;; exit is from an escape function (CATCH or UNWIND-PROTECT), then
590   ;; physical environment analysis deletes the escape function and
591   ;; instead has the %NLX-ENTRY use this continuation.
592   ;;
593   ;; This slot is primarily an indication of where this exit delivers
594   ;; its values to (if any), but it is also used as a sort of name to
595   ;; allow us to find the NLX-INFO that corresponds to a given exit.
596   ;; For this purpose, the ENTRY must also be used to disambiguate,
597   ;; since exits to different places may deliver their result to the
598   ;; same continuation.
599   (continuation (missing-arg) :type continuation)
600   ;; the entry stub inserted by physical environment analysis. This is
601   ;; a block containing a call to the %NLX-ENTRY funny function that
602   ;; has the original exit destination as its successor. Null only
603   ;; temporarily.
604   (target nil :type (or cblock null))
605   ;; some kind of info used by the back end
606   info)
607 (defprinter (nlx-info :identity t)
608   continuation
609   target
610   info)
611 \f
612 ;;;; LEAF structures
613
614 ;;; Variables, constants and functions are all represented by LEAF
615 ;;; structures. A reference to a LEAF is indicated by a REF node. This
616 ;;; allows us to easily substitute one for the other without actually
617 ;;; hacking the flow graph.
618 (def!struct (leaf (:make-load-form-fun ignore-it)
619                   (:constructor nil))
620   ;; unique ID for debugging
621   #!+sb-show (id (new-object-id) :read-only t)
622   ;; (For public access to this slot, use LEAF-SOURCE-NAME.)
623   ;;
624   ;; the name of LEAF as it appears in the source, e.g. 'FOO or '(SETF
625   ;; FOO) or 'N or '*Z*, or the special .ANONYMOUS. value if there's
626   ;; no name for this thing in the source (as can happen for
627   ;; FUNCTIONALs, e.g. for anonymous LAMBDAs or for functions for
628   ;; top-level forms; and can also happen for anonymous constants) or
629   ;; perhaps also if the match between the name and the thing is
630   ;; skewed enough (e.g. for macro functions or method functions) that
631   ;; we don't want to have that name affect compilation
632   ;;
633   ;; (We use .ANONYMOUS. here more or less the way we'd ordinarily use
634   ;; NIL, but we're afraid to use NIL because it's a symbol which could
635   ;; be the name of a leaf, if only the constant named NIL.)
636   ;;
637   ;; The value of this slot in can affect ordinary runtime behavior,
638   ;; e.g. of special variables and known functions, not just debugging.
639   ;;
640   ;; See also the LEAF-DEBUG-NAME function and the
641   ;; FUNCTIONAL-%DEBUG-NAME slot.
642   (%source-name (missing-arg)
643                 :type (or symbol (and cons (satisfies legal-fun-name-p)))
644                 :read-only t)
645   ;; the type which values of this leaf must have
646   (type *universal-type* :type ctype)
647   ;; where the TYPE information came from:
648   ;;  :DECLARED, from a declaration.
649   ;;  :ASSUMED, from uses of the object.
650   ;;  :DEFINED, from examination of the definition.
651   ;; FIXME: This should be a named type. (LEAF-WHERE-FROM? Or
652   ;; perhaps just WHERE-FROM, since it's not just used in LEAF,
653   ;; but also in various DEFINE-INFO-TYPEs in globaldb.lisp,
654   ;; and very likely elsewhere too.)
655   (where-from :assumed :type (member :declared :assumed :defined))
656   ;; list of the REF nodes for this leaf
657   (refs () :type list)
658   ;; true if there was ever a REF or SET node for this leaf. This may
659   ;; be true when REFS and SETS are null, since code can be deleted.
660   (ever-used nil :type boolean)
661   ;; some kind of info used by the back end
662   (info nil))
663
664 ;;; LEAF name operations
665 ;;;
666 ;;; KLUDGE: wants CLOS..
667 (defun leaf-has-source-name-p (leaf)
668   (not (eq (leaf-%source-name leaf)
669            '.anonymous.)))
670 (defun leaf-source-name (leaf)
671   (aver (leaf-has-source-name-p leaf))
672   (leaf-%source-name leaf))
673 (defun leaf-debug-name (leaf)
674   (if (functional-p leaf)
675       ;; FUNCTIONALs have additional %DEBUG-NAME behavior.
676       (functional-debug-name leaf)
677       ;; Other objects just use their source name.
678       ;;
679       ;; (As of sbcl-0.pre7.85, there are a few non-FUNCTIONAL
680       ;; anonymous objects, (anonymous constants..) and those would
681       ;; fail here if we ever tried to get debug names from them, but
682       ;; it looks as though it's never interesting to get debug names
683       ;; from them, so it's moot. -- WHN)
684       (leaf-source-name leaf)))
685
686 ;;; The CONSTANT structure is used to represent known constant values.
687 ;;; If NAME is not null, then it is the name of the named constant
688 ;;; which this leaf corresponds to, otherwise this is an anonymous
689 ;;; constant.
690 (def!struct (constant (:include leaf))
691   ;; the value of the constant
692   (value nil :type t))
693 (defprinter (constant :identity t)
694   (%source-name :test %source-name)
695   value)
696
697 ;;; The BASIC-VAR structure represents information common to all
698 ;;; variables which don't correspond to known local functions.
699 (def!struct (basic-var (:include leaf)
700                        (:constructor nil))
701   ;; Lists of the set nodes for this variable.
702   (sets () :type list))
703
704 ;;; The GLOBAL-VAR structure represents a value hung off of the symbol
705 ;;; NAME.
706 (def!struct (global-var (:include basic-var))
707   ;; kind of variable described
708   (kind (missing-arg)
709         :type (member :special :global-function :global)))
710 (defprinter (global-var :identity t)
711   %source-name
712   #!+sb-show id
713   (type :test (not (eq type *universal-type*)))
714   (where-from :test (not (eq where-from :assumed)))
715   kind)
716
717 ;;; A DEFINED-FUN represents a function that is defined in the same
718 ;;; compilation block, or that has an inline expansion, or that has a
719 ;;; non-NIL INLINEP value. Whenever we change the INLINEP state (i.e.
720 ;;; an inline proclamation) we copy the structure so that former
721 ;;; INLINEP values are preserved.
722 (def!struct (defined-fun (:include global-var
723                                    (where-from :defined)
724                                    (kind :global-function)))
725   ;; The values of INLINEP and INLINE-EXPANSION initialized from the
726   ;; global environment.
727   (inlinep nil :type inlinep)
728   (inline-expansion nil :type (or cons null))
729   ;; the block-local definition of this function (either because it
730   ;; was semi-inline, or because it was defined in this block). If
731   ;; this function is not an entry point, then this may be deleted or
732   ;; LET-converted. Null if we haven't converted the expansion yet.
733   (functional nil :type (or functional null)))
734 (defprinter (defined-fun :identity t)
735   %source-name
736   #!+sb-show id
737   inlinep
738   (functional :test functional))
739 \f
740 ;;;; function stuff
741
742 ;;; We default the WHERE-FROM and TYPE slots to :DEFINED and FUNCTION.
743 ;;; We don't normally manipulate function types for defined functions,
744 ;;; but if someone wants to know, an approximation is there.
745 (def!struct (functional (:include leaf
746                                   (%source-name '.anonymous.)
747                                   (where-from :defined)
748                                   (type (specifier-type 'function))))
749   ;; (For public access to this slot, use LEAF-DEBUG-NAME.)
750   ;;
751   ;; the name of FUNCTIONAL for debugging purposes, or NIL if we
752   ;; should just let the SOURCE-NAME fall through
753   ;; 
754   ;; Unlike the SOURCE-NAME slot, this slot's value should never
755   ;; affect ordinary code behavior, only debugging/diagnostic behavior.
756   ;;
757   ;; The value of this slot can be anything, except that it shouldn't
758   ;; be a legal function name, since otherwise debugging gets
759   ;; confusing. (If a legal function name is a good name for the
760   ;; function, it should be in %SOURCE-NAME, and then we shouldn't
761   ;; need a %DEBUG-NAME.) In SBCL as of 0.pre7.87, it's always a
762   ;; string unless it's NIL, since that's how CMU CL represented debug
763   ;; names. However, eventually I (WHN) think it we should start using
764   ;; list values instead, since they have much nicer print properties
765   ;; (abbreviation, skipping package prefixes when unneeded, and
766   ;; renaming package prefixes when we do things like renaming SB!EXT
767   ;; to SB-EXT).
768   ;;
769   ;; E.g. for the function which implements (DEFUN FOO ...), we could
770   ;; have
771   ;;   %SOURCE-NAME=FOO
772   ;;   %DEBUG-NAME=NIL
773   ;; for the function which implements the top level form
774   ;; (IN-PACKAGE :FOO) we could have
775   ;;   %SOURCE-NAME=NIL
776   ;;   %DEBUG-NAME="top level form (IN-PACKAGE :FOO)"
777   ;; for the function which implements FOO in
778   ;;   (DEFUN BAR (...) (FLET ((FOO (...) ...)) ...))
779   ;; we could have
780   ;;   %SOURCE-NAME=FOO
781   ;;   %DEBUG-NAME="FLET FOO in BAR"
782   ;; and for the function which implements FOO in
783   ;;   (DEFMACRO FOO (...) ...)
784   ;; we could have
785   ;;   %SOURCE-NAME=FOO (or maybe .ANONYMOUS.?)
786   ;;   %DEBUG-NAME="DEFMACRO FOO"
787   (%debug-name nil
788                :type (or null (not (satisfies legal-fun-name-p)))
789                :read-only t)
790   ;; some information about how this function is used. These values
791   ;; are meaningful:
792   ;;
793   ;;    NIL
794   ;;    an ordinary function, callable using local call
795   ;;
796   ;;    :LET
797   ;;    a lambda that is used in only one local call, and has in
798   ;;    effect been substituted directly inline. The return node is
799   ;;    deleted, and the result is computed with the actual result
800   ;;    continuation for the call.
801   ;;
802   ;;    :MV-LET
803   ;;    Similar to :LET (as per FUNCTIONAL-LETLIKE-P), but the call
804   ;;    is an MV-CALL.
805   ;;
806   ;;    :ASSIGNMENT
807   ;;    similar to a LET (as per FUNCTIONAL-SOMEWHAT-LETLIKE-P), but
808   ;;    can have other than one call as long as there is at most
809   ;;    one non-tail call.
810   ;;
811   ;;    :OPTIONAL
812   ;;    a lambda that is an entry point for an OPTIONAL-DISPATCH.
813   ;;    Similar to NIL, but requires greater caution, since local call
814   ;;    analysis may create new references to this function. Also, the
815   ;;    function cannot be deleted even if it has *no* references. The
816   ;;    OPTIONAL-DISPATCH is in the LAMDBA-OPTIONAL-DISPATCH.
817   ;;
818   ;;    :EXTERNAL
819   ;;    an external entry point lambda. The function it is an entry
820   ;;    for is in the ENTRY-FUN slot.
821   ;;
822   ;;    :TOPLEVEL
823   ;;    a top level lambda, holding a compiled top level form.
824   ;;    Compiled very much like NIL, but provides an indication of
825   ;;    top level context. A :TOPLEVEL lambda should have *no*
826   ;;    references. Its ENTRY-FUN is a self-pointer.
827   ;;
828   ;;    :TOPLEVEL-XEP
829   ;;    After a component is compiled, we clobber any top level code
830   ;;    references to its non-closure XEPs with dummy FUNCTIONAL
831   ;;    structures having this kind. This prevents the retained
832   ;;    top level code from holding onto the IR for the code it
833   ;;    references.
834   ;;
835   ;;    :ESCAPE
836   ;;    :CLEANUP
837   ;;    special functions used internally by CATCH and UNWIND-PROTECT.
838   ;;    These are pretty much like a normal function (NIL), but are
839   ;;    treated specially by local call analysis and stuff. Neither
840   ;;    kind should ever be given an XEP even though they appear as
841   ;;    args to funny functions. An :ESCAPE function is never actually
842   ;;    called, and thus doesn't need to have code generated for it.
843   ;;
844   ;;    :DELETED
845   ;;    This function has been found to be uncallable, and has been
846   ;;    marked for deletion.
847   (kind nil :type (member nil :optional :deleted :external :toplevel
848                           :escape :cleanup :let :mv-let :assignment
849                           :toplevel-xep))
850   ;; Is this a function that some external entity (e.g. the fasl dumper)
851   ;; refers to, so that even when it appears to have no references, it
852   ;; shouldn't be deleted? In the old days (before
853   ;; sbcl-0.pre7.37.flaky5.2) this was sort of implicitly true when
854   ;; KIND was :TOPLEVEL. Now it must be set explicitly, both for
855   ;; :TOPLEVEL functions and for any other kind of functions that we
856   ;; want to dump or return from #'CL:COMPILE or whatever.
857   (has-external-references-p nil) 
858   ;; In a normal function, this is the external entry point (XEP)
859   ;; lambda for this function, if any. Each function that is used
860   ;; other than in a local call has an XEP, and all of the
861   ;; non-local-call references are replaced with references to the
862   ;; XEP.
863   ;;
864   ;; In an XEP lambda (indicated by the :EXTERNAL kind), this is the
865   ;; function that the XEP is an entry-point for. The body contains
866   ;; local calls to all the actual entry points in the function. In a
867   ;; :TOPLEVEL lambda (which is its own XEP) this is a self-pointer.
868   ;;
869   ;; With all other kinds, this is null.
870   (entry-fun nil :type (or functional null))
871   ;; the value of any inline/notinline declaration for a local
872   ;; function (or NIL in any case if no inline expansion is available)
873   (inlinep nil :type inlinep)
874   ;; If we have a lambda that can be used as in inline expansion for
875   ;; this function, then this is it. If there is no source-level
876   ;; lambda corresponding to this function then this is null (but then
877   ;; INLINEP will always be NIL as well.)
878   (inline-expansion nil :type list)
879   ;; the lexical environment that the INLINE-EXPANSION should be converted in
880   (lexenv *lexenv* :type lexenv)
881   ;; the original function or macro lambda list, or :UNSPECIFIED if
882   ;; this is a compiler created function
883   (arg-documentation nil :type (or list (member :unspecified)))
884   ;; various rare miscellaneous info that drives code generation & stuff
885   (plist () :type list))
886 (defprinter (functional :identity t)
887   %source-name
888   %debug-name
889   #!+sb-show id)
890
891 ;;; Is FUNCTIONAL LET-converted? (where we're indifferent to whether
892 ;;; it returns one value or multiple values)
893 (defun functional-letlike-p (functional)
894   (member (functional-kind functional)
895           '(:let :mv-let)))
896
897 ;;; Is FUNCTIONAL sorta LET-converted? (where even an :ASSIGNMENT counts)
898 ;;;
899 ;;; FIXME: I (WHN) don't understand this one well enough to give a good
900 ;;; definition or even a good function name, it's just a literal copy
901 ;;; of a CMU CL idiom. Does anyone have a better name or explanation?
902 (defun functional-somewhat-letlike-p (functional)
903   (or (functional-letlike-p functional)
904       (eql (functional-kind functional) :assignment)))
905
906 ;;; FUNCTIONAL name operations
907 (defun functional-debug-name (functional)
908   ;; FUNCTIONAL-%DEBUG-NAME takes precedence over FUNCTIONAL-SOURCE-NAME
909   ;; here because we want different debug names for the functions in
910   ;; DEFUN FOO and FLET FOO even though they have the same source name.
911   (or (functional-%debug-name functional)
912       ;; Note that this will cause an error if the function is
913       ;; anonymous. In SBCL (as opposed to CMU CL) we make all
914       ;; FUNCTIONALs have debug names. The CMU CL code didn't bother
915       ;; in many FUNCTIONALs, especially those which were likely to be
916       ;; optimized away before the user saw them. However, getting 
917       ;; that right requires a global understanding of the code,
918       ;; which seems bad, so we just require names for everything.
919       (leaf-source-name functional)))
920
921 ;;; The CLAMBDA only deals with required lexical arguments. Special,
922 ;;; optional, keyword and rest arguments are handled by transforming
923 ;;; into simpler stuff.
924 (def!struct (clambda (:include functional)
925                      (:conc-name lambda-)
926                      (:predicate lambda-p)
927                      (:constructor make-lambda)
928                      (:copier copy-lambda))
929   ;; list of LAMBDA-VAR descriptors for arguments
930   (vars nil :type list :read-only t)
931   ;; If this function was ever a :OPTIONAL function (an entry-point
932   ;; for an OPTIONAL-DISPATCH), then this is that OPTIONAL-DISPATCH.
933   ;; The optional dispatch will be :DELETED if this function is no
934   ;; longer :OPTIONAL.
935   (optional-dispatch nil :type (or optional-dispatch null))
936   ;; the BIND node for this LAMBDA. This node marks the beginning of
937   ;; the lambda, and serves to explicitly represent the lambda binding
938   ;; semantics within the flow graph representation. This is null in
939   ;; deleted functions, and also in LETs where we deleted the call and
940   ;; bind (because there are no variables left), but have not yet
941   ;; actually deleted the LAMBDA yet.
942   (bind nil :type (or bind null))
943   ;; the RETURN node for this LAMBDA, or NIL if it has been deleted.
944   ;; This marks the end of the lambda, receiving the result of the
945   ;; body. In a LET, the return node is deleted, and the body delivers
946   ;; the value to the actual continuation. The return may also be
947   ;; deleted if it is unreachable.
948   (return nil :type (or creturn null))
949   ;; If this CLAMBDA is a LET, then this slot holds the LAMBDA whose
950   ;; LETS list we are in, otherwise it is a self-pointer.
951   (home nil :type (or clambda null))
952   ;; all the lambdas that have been LET-substituted in this lambda.
953   ;; This is only non-null in lambdas that aren't LETs.
954   (lets nil :type list)
955   ;; all the ENTRY nodes in this function and its LETs, or null in a LET
956   (entries nil :type list)
957   ;; CLAMBDAs which are locally called by this lambda, and other
958   ;; objects (closed-over LAMBDA-VARs and XEPs) which this lambda
959   ;; depends on in such a way that DFO shouldn't put them in separate
960   ;; components.
961   (calls-or-closes nil :type list)
962   ;; the TAIL-SET that this LAMBDA is in. This is null during creation.
963   ;;
964   ;; In CMU CL, and old SBCL, this was also NILed out when LET
965   ;; conversion happened. That caused some problems, so as of
966   ;; sbcl-0.pre7.37.flaky5.2 when I was trying to get the compiler to
967   ;; emit :EXTERNAL functions directly, and so now the value
968   ;; is no longer NILed out in LET conversion, but instead copied
969   ;; (so that any further optimizations on the rest of the tail
970   ;; set won't modify the value) if necessary.
971   (tail-set nil :type (or tail-set null))
972   ;; the structure which represents the phsical environment that this
973   ;; function's variables are allocated in. This is filled in by
974   ;; physical environment analysis. In a LET, this is EQ to our home's
975   ;; physical environment.
976   (physenv nil :type (or physenv null))
977   ;; In a LET, this is the NODE-LEXENV of the combination node. We
978   ;; retain it so that if the LET is deleted (due to a lack of vars),
979   ;; we will still have caller's lexenv to figure out which cleanup is
980   ;; in effect.
981   (call-lexenv nil :type (or lexenv null)))
982 (defprinter (clambda :conc-name lambda- :identity t)
983   %source-name
984   %debug-name
985   #!+sb-show id
986   (type :test (not (eq type *universal-type*)))
987   (where-from :test (not (eq where-from :assumed)))
988   (vars :prin1 (mapcar #'leaf-source-name vars)))
989
990 ;;; The OPTIONAL-DISPATCH leaf is used to represent hairy lambdas. It
991 ;;; is a FUNCTIONAL, like LAMBDA. Each legal number of arguments has a
992 ;;; function which is called when that number of arguments is passed.
993 ;;; The function is called with all the arguments actually passed. If
994 ;;; additional arguments are legal, then the LEXPR style MORE-ENTRY
995 ;;; handles them. The value returned by the function is the value
996 ;;; which results from calling the OPTIONAL-DISPATCH.
997 ;;;
998 ;;; The theory is that each entry-point function calls the next entry
999 ;;; point tail-recursively, passing all the arguments passed in and
1000 ;;; the default for the argument the entry point is for. The last
1001 ;;; entry point calls the real body of the function. In the presence
1002 ;;; of SUPPLIED-P args and other hair, things are more complicated. In
1003 ;;; general, there is a distinct internal function that takes the
1004 ;;; SUPPLIED-P args as parameters. The preceding entry point calls
1005 ;;; this function with NIL filled in for the SUPPLIED-P args, while
1006 ;;; the current entry point calls it with T in the SUPPLIED-P
1007 ;;; positions.
1008 ;;;
1009 ;;; Note that it is easy to turn a call with a known number of
1010 ;;; arguments into a direct call to the appropriate entry-point
1011 ;;; function, so functions that are compiled together can avoid doing
1012 ;;; the dispatch.
1013 (def!struct (optional-dispatch (:include functional))
1014   ;; the original parsed argument list, for anyone who cares
1015   (arglist nil :type list)
1016   ;; true if &ALLOW-OTHER-KEYS was supplied
1017   (allowp nil :type boolean)
1018   ;; true if &KEY was specified (which doesn't necessarily mean that
1019   ;; there are any &KEY arguments..)
1020   (keyp nil :type boolean)
1021   ;; the number of required arguments. This is the smallest legal
1022   ;; number of arguments.
1023   (min-args 0 :type unsigned-byte)
1024   ;; the total number of required and optional arguments. Args at
1025   ;; positions >= to this are &REST, &KEY or illegal args.
1026   (max-args 0 :type unsigned-byte)
1027   ;; list of the LAMBDAs which are the entry points for non-rest,
1028   ;; non-key calls. The entry for MIN-ARGS is first, MIN-ARGS+1
1029   ;; second, ... MAX-ARGS last. The last entry-point always calls the
1030   ;; main entry; in simple cases it may be the main entry.
1031   (entry-points nil :type list)
1032   ;; an entry point which takes MAX-ARGS fixed arguments followed by
1033   ;; an argument context pointer and an argument count. This entry
1034   ;; point deals with listifying rest args and parsing keywords. This
1035   ;; is null when extra arguments aren't legal.
1036   (more-entry nil :type (or clambda null))
1037   ;; the main entry-point into the function, which takes all arguments
1038   ;; including keywords as fixed arguments. The format of the
1039   ;; arguments must be determined by examining the arglist. This may
1040   ;; be used by callers that supply at least MAX-ARGS arguments and
1041   ;; know what they are doing.
1042   (main-entry nil :type (or clambda null)))
1043 (defprinter (optional-dispatch :identity t)
1044   %source-name
1045   %debug-name
1046   #!+sb-show id
1047   (type :test (not (eq type *universal-type*)))
1048   (where-from :test (not (eq where-from :assumed)))
1049   arglist
1050   allowp
1051   keyp
1052   min-args
1053   max-args
1054   (entry-points :test entry-points)
1055   (more-entry :test more-entry)
1056   main-entry)
1057
1058 ;;; The ARG-INFO structure allows us to tack various information onto
1059 ;;; LAMBDA-VARs during IR1 conversion. If we use one of these things,
1060 ;;; then the var will have to be massaged a bit before it is simple
1061 ;;; and lexical.
1062 (def!struct arg-info
1063   ;; true if this arg is to be specially bound
1064   (specialp nil :type boolean)
1065   ;; the kind of argument being described. Required args only have arg
1066   ;; info structures if they are special.
1067   (kind (missing-arg)
1068         :type (member :required :optional :keyword :rest
1069                       :more-context :more-count))
1070   ;; If true, this is the VAR for SUPPLIED-P variable of a keyword or
1071   ;; optional arg. This is true for keywords with non-constant
1072   ;; defaults even when there is no user-specified supplied-p var.
1073   (supplied-p nil :type (or lambda-var null))
1074   ;; the default for a keyword or optional, represented as the
1075   ;; original Lisp code. This is set to NIL in &KEY arguments that are
1076   ;; defaulted using the SUPPLIED-P arg.
1077   (default nil :type t)
1078   ;; the actual key for a &KEY argument. Note that in ANSI CL this is
1079   ;; not necessarily a keyword: (DEFUN FOO (&KEY ((BAR BAR))) ...).
1080   (key nil :type symbol))
1081 (defprinter (arg-info :identity t)
1082   (specialp :test specialp)
1083   kind
1084   (supplied-p :test supplied-p)
1085   (default :test default)
1086   (key :test key))
1087
1088 ;;; The LAMBDA-VAR structure represents a lexical lambda variable.
1089 ;;; This structure is also used during IR1 conversion to describe
1090 ;;; lambda arguments which may ultimately turn out not to be simple
1091 ;;; and lexical.
1092 ;;;
1093 ;;; LAMBDA-VARs with no REFs are considered to be deleted; physical
1094 ;;; environment analysis isn't done on these variables, so the back
1095 ;;; end must check for and ignore unreferenced variables. Note that a
1096 ;;; deleted LAMBDA-VAR may have sets; in this case the back end is
1097 ;;; still responsible for propagating the SET-VALUE to the set's CONT.
1098 (def!struct (lambda-var (:include basic-var))
1099   ;; true if this variable has been declared IGNORE
1100   (ignorep nil :type boolean)
1101   ;; the CLAMBDA that this var belongs to. This may be null when we are
1102   ;; building a lambda during IR1 conversion.
1103   (home nil :type (or null clambda))
1104   ;; This is set by physical environment analysis if it chooses an
1105   ;; indirect (value cell) representation for this variable because it
1106   ;; is both set and closed over.
1107   (indirect nil :type boolean)
1108   ;; The following two slots are only meaningful during IR1 conversion
1109   ;; of hairy lambda vars:
1110   ;;
1111   ;; The ARG-INFO structure which holds information obtained from
1112   ;; &keyword parsing.
1113   (arg-info nil :type (or arg-info null))
1114   ;; if true, the GLOBAL-VAR structure for the special variable which
1115   ;; is to be bound to the value of this argument
1116   (specvar nil :type (or global-var null))
1117   ;; Set of the CONSTRAINTs on this variable. Used by constraint
1118   ;; propagation. This is left null by the lambda pre-pass if it
1119   ;; determine that this is a set closure variable, and is thus not a
1120   ;; good subject for flow analysis.
1121   (constraints nil :type (or sset null)))
1122 (defprinter (lambda-var :identity t)
1123   %source-name
1124   #!+sb-show id
1125   (type :test (not (eq type *universal-type*)))
1126   (where-from :test (not (eq where-from :assumed)))
1127   (ignorep :test ignorep)
1128   (arg-info :test arg-info)
1129   (specvar :test specvar))
1130 \f
1131 ;;;; basic node types
1132
1133 ;;; A REF represents a reference to a LEAF. REF-REOPTIMIZE is
1134 ;;; initially (and forever) NIL, since REFs don't receive any values
1135 ;;; and don't have any IR1 optimizer.
1136 (defstruct (ref (:include node (reoptimize nil))
1137                 (:constructor make-ref (derived-type leaf))
1138                 (:copier nil))
1139   ;; The leaf referenced.
1140   (leaf nil :type leaf))
1141 (defprinter (ref :identity t)
1142   #!+sb-show id
1143   leaf)
1144
1145 ;;; Naturally, the IF node always appears at the end of a block.
1146 ;;; NODE-CONT is a dummy continuation, and is there only to keep
1147 ;;; people happy.
1148 (defstruct (cif (:include node)
1149                 (:conc-name if-)
1150                 (:predicate if-p)
1151                 (:constructor make-if)
1152                 (:copier copy-if))
1153   ;; CONTINUATION for the predicate
1154   (test (missing-arg) :type continuation)
1155   ;; the blocks that we execute next in true and false case,
1156   ;; respectively (may be the same)
1157   (consequent (missing-arg) :type cblock)
1158   (alternative (missing-arg) :type cblock))
1159 (defprinter (cif :conc-name if- :identity t)
1160   (test :prin1 (continuation-use test))
1161   consequent
1162   alternative)
1163
1164 (defstruct (cset (:include node
1165                            (derived-type *universal-type*))
1166                  (:conc-name set-)
1167                  (:predicate set-p)
1168                  (:constructor make-set)
1169                  (:copier copy-set))
1170   ;; descriptor for the variable set
1171   (var (missing-arg) :type basic-var)
1172   ;; continuation for the value form
1173   (value (missing-arg) :type continuation))
1174 (defprinter (cset :conc-name set- :identity t)
1175   var
1176   (value :prin1 (continuation-use value)))
1177
1178 ;;; The BASIC-COMBINATION structure is used to represent both normal
1179 ;;; and multiple value combinations. In a local function call, this
1180 ;;; node appears at the end of its block and the body of the called
1181 ;;; function appears as the successor. The NODE-CONT remains the
1182 ;;; continuation which receives the value of the call.
1183 (defstruct (basic-combination (:include node)
1184                               (:constructor nil)
1185                               (:copier nil))
1186   ;; continuation for the function
1187   (fun (missing-arg) :type continuation)
1188   ;; list of CONTINUATIONs for the args. In a local call, an argument
1189   ;; continuation may be replaced with NIL to indicate that the
1190   ;; corresponding variable is unreferenced, and thus no argument
1191   ;; value need be passed.
1192   (args nil :type list)
1193   ;; the kind of function call being made. :LOCAL means that this is a
1194   ;; local call to a function in the same component, and that argument
1195   ;; syntax checking has been done, etc. Calls to known global
1196   ;; functions are represented by storing the FUN-INFO for the
1197   ;; function in this slot. :FULL is a call to an (as yet) unknown
1198   ;; function. :ERROR is like :FULL, but means that we have discovered
1199   ;; that the call contains an error, and should not be reconsidered
1200   ;; for optimization.
1201   (kind :full :type (or (member :local :full :error) fun-info))
1202   ;; some kind of information attached to this node by the back end
1203   (info nil))
1204
1205 ;;; The COMBINATION node represents all normal function calls,
1206 ;;; including FUNCALL. This is distinct from BASIC-COMBINATION so that
1207 ;;; an MV-COMBINATION isn't COMBINATION-P.
1208 (defstruct (combination (:include basic-combination)
1209                         (:constructor make-combination (fun))
1210                         (:copier nil)))
1211 (defprinter (combination :identity t)
1212   #!+sb-show id
1213   (fun :prin1 (continuation-use fun))
1214   (args :prin1 (mapcar (lambda (x)
1215                          (if x
1216                              (continuation-use x)
1217                              "<deleted>"))
1218                        args)))
1219
1220 ;;; An MV-COMBINATION is to MULTIPLE-VALUE-CALL as a COMBINATION is to
1221 ;;; FUNCALL. This is used to implement all the multiple-value
1222 ;;; receiving forms.
1223 (defstruct (mv-combination (:include basic-combination)
1224                            (:constructor make-mv-combination (fun))
1225                            (:copier nil)))
1226 (defprinter (mv-combination)
1227   (fun :prin1 (continuation-use fun))
1228   (args :prin1 (mapcar #'continuation-use args)))
1229
1230 ;;; The BIND node marks the beginning of a lambda body and represents
1231 ;;; the creation and initialization of the variables.
1232 (defstruct (bind (:include node)
1233                  (:copier nil))
1234   ;; the lambda we are binding variables for. Null when we are
1235   ;; creating the LAMBDA during IR1 translation.
1236   (lambda nil :type (or clambda null)))
1237 (defprinter (bind)
1238   lambda)
1239
1240 ;;; The RETURN node marks the end of a lambda body. It collects the
1241 ;;; return values and represents the control transfer on return. This
1242 ;;; is also where we stick information used for TAIL-SET type
1243 ;;; inference.
1244 (defstruct (creturn (:include node)
1245                     (:conc-name return-)
1246                     (:predicate return-p)
1247                     (:constructor make-return)
1248                     (:copier copy-return))
1249   ;; the lambda we are returning from. Null temporarily during
1250   ;; ir1tran.
1251   (lambda nil :type (or clambda null))
1252   ;; the continuation which yields the value of the lambda
1253   (result (missing-arg) :type continuation)
1254   ;; the union of the node-derived-type of all uses of the result
1255   ;; other than by a local call, intersected with the result's
1256   ;; asserted-type. If there are no non-call uses, this is
1257   ;; *EMPTY-TYPE*
1258   (result-type *wild-type* :type ctype))
1259 (defprinter (creturn :conc-name return- :identity t)
1260   lambda
1261   result-type)
1262 \f
1263 ;;;; non-local exit support
1264 ;;;;
1265 ;;;; In IR1, we insert special nodes to mark potentially non-local
1266 ;;;; lexical exits.
1267
1268 ;;; The ENTRY node serves to mark the start of the dynamic extent of a
1269 ;;; lexical exit. It is the mess-up node for the corresponding :ENTRY
1270 ;;; cleanup.
1271 (defstruct (entry (:include node)
1272                   (:copier nil))
1273   ;; All of the EXIT nodes for potential non-local exits to this point.
1274   (exits nil :type list)
1275   ;; The cleanup for this entry. NULL only temporarily.
1276   (cleanup nil :type (or cleanup null)))
1277 (defprinter (entry :identity t)
1278   #!+sb-show id)
1279
1280 ;;; The EXIT node marks the place at which exit code would be emitted,
1281 ;;; if necessary. This is interposed between the uses of the exit
1282 ;;; continuation and the exit continuation's DEST. Instead of using
1283 ;;; the returned value being delivered directly to the exit
1284 ;;; continuation, it is delivered to our VALUE continuation. The
1285 ;;; original exit continuation is the exit node's CONT.
1286 (defstruct (exit (:include node)
1287                  (:copier nil))
1288   ;; the ENTRY node that this is an exit for. If null, this is a
1289   ;; degenerate exit. A degenerate exit is used to "fill" an empty
1290   ;; block (which isn't allowed in IR1.) In a degenerate exit, Value
1291   ;; is always also null.
1292   (entry nil :type (or entry null))
1293   ;; the continuation yielding the value we are to exit with. If NIL,
1294   ;; then no value is desired (as in GO).
1295   (value nil :type (or continuation null)))
1296 (defprinter (exit :identity t)
1297   #!+sb-show id
1298   (entry :test entry)
1299   (value :test value))
1300 \f
1301 ;;;; miscellaneous IR1 structures
1302
1303 (defstruct (undefined-warning
1304             #-no-ansi-print-object
1305             (:print-object (lambda (x s)
1306                              (print-unreadable-object (x s :type t)
1307                                (prin1 (undefined-warning-name x) s))))
1308             (:copier nil))
1309   ;; the name of the unknown thing
1310   (name nil :type (or symbol list))
1311   ;; the kind of reference to NAME
1312   (kind (missing-arg) :type (member :function :type :variable))
1313   ;; the number of times this thing was used
1314   (count 0 :type unsigned-byte)
1315   ;; a list of COMPILER-ERROR-CONTEXT structures describing places
1316   ;; where this thing was used. Note that we only record the first
1317   ;; *UNDEFINED-WARNING-LIMIT* calls.
1318   (warnings () :type list))
1319 \f
1320 ;;; a helper for the POLICY macro, defined late here so that the
1321 ;;; various type tests can be inlined
1322 (declaim (ftype (function ((or list lexenv node functional)) list)
1323                 %coerce-to-policy))
1324 (defun %coerce-to-policy (thing)
1325   (let ((result (etypecase thing
1326                   (list thing)
1327                   (lexenv (lexenv-policy thing))
1328                   (node (lexenv-policy (node-lexenv thing)))
1329                   (functional (lexenv-policy (functional-lexenv thing))))))
1330     ;; Test the first element of the list as a rudimentary sanity
1331     ;; that it really does look like a valid policy.
1332     (aver (or (null result) (policy-quality-name-p (caar result))))
1333     ;; Voila.
1334     result))
1335 \f
1336 ;;;; Freeze some structure types to speed type testing.
1337
1338 #!-sb-fluid
1339 (declaim (freeze-type node leaf lexenv continuation cblock component cleanup
1340                       physenv tail-set nlx-info))