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