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