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