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