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