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