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