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