1 ;;;; This file contains the virtual-machine-independent parts of the
2 ;;;; code which does the actual translation of nodes to VOPs.
4 ;;;; This software is part of the SBCL system. See the README file for
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.
15 ;;;; moves and type checks
17 ;;; Move X to Y unless they are EQ.
18 (defun emit-move (node block x y)
19 (declare (type node node) (type ir2-block block) (type tn x y))
21 (vop move node block x y))
24 ;;; If there is any CHECK-xxx template for TYPE, then return it,
25 ;;; otherwise return NIL.
26 (defun type-check-template (type)
27 (declare (type ctype type))
28 (multiple-value-bind (check-ptype exact) (primitive-type type)
30 (primitive-type-check check-ptype)
31 (let ((name (hairy-type-check-template-name type)))
33 (template-or-lose name)
36 ;;; Emit code in BLOCK to check that VALUE is of the specified TYPE,
37 ;;; yielding the checked result in RESULT. VALUE and result may be of
38 ;;; any primitive type. There must be CHECK-xxx VOP for TYPE. Any
39 ;;; other type checks should have been converted to an explicit type
41 (defun emit-type-check (node block value result type)
42 (declare (type tn value result) (type node node) (type ir2-block block)
44 (emit-move-template node block (type-check-template type) value result)
47 ;;; Allocate an indirect value cell. Maybe do some clever stack
48 ;;; allocation someday.
50 ;;; FIXME: DO-MAKE-VALUE-CELL is a bad name, since it doesn't make
51 ;;; clear what's the distinction between it and the MAKE-VALUE-CELL
52 ;;; VOP, and since the DO- further connotes iteration, which has
53 ;;; nothing to do with this. Clearer, more systematic names, anyone?
54 (defevent make-value-cell-event "Allocate heap value cell for lexical var.")
55 (defun do-make-value-cell (node block value res)
56 (event make-value-cell-event node)
57 (vop make-value-cell node block value res))
61 ;;; Return the TN that holds the value of THING in the environment ENV.
62 (declaim (ftype (function ((or nlx-info lambda-var) physenv) tn)
64 (defun find-in-physenv (thing physenv)
65 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
68 ;; I think that a failure of this assertion means that we're
69 ;; trying to access a variable which was improperly closed
70 ;; over. The PHYSENV describes a physical environment. Every
71 ;; variable that a form refers to should either be in its
72 ;; physical environment directly, or grabbed from a
73 ;; surrounding physical environment when it was closed over.
74 ;; The ASSOC expression above finds closed-over variables, so
75 ;; if we fell through the ASSOC expression, it wasn't closed
76 ;; over. Therefore, it must be in our physical environment
77 ;; directly. If instead it is in some other physical
78 ;; environment, then it's bogus for us to reference it here
79 ;; without it being closed over. -- WHN 2001-09-29
80 (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
83 (aver (eq physenv (block-physenv (nlx-info-target thing))))
84 (ir2-nlx-info-home (nlx-info-info thing))))))
86 ;;; If LEAF already has a constant TN, return that, otherwise make a
88 (defun constant-tn (leaf)
89 (declare (type constant leaf))
91 (setf (leaf-info leaf)
92 (make-constant-tn leaf))))
94 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
95 ;;; isn't directly represented by a TN. ENV is the environment that
96 ;;; the reference is done in.
97 (defun leaf-tn (leaf env)
98 (declare (type leaf leaf) (type physenv env))
101 (unless (lambda-var-indirect leaf)
102 (find-in-physenv leaf env)))
103 (constant (constant-tn leaf))
106 ;;; This is used to conveniently get a handle on a constant TN during
107 ;;; IR2 conversion. It returns a constant TN representing the Lisp
109 (defun emit-constant (value)
110 (constant-tn (find-constant value)))
112 ;;; Convert a REF node. The reference must not be delayed.
113 (defun ir2-convert-ref (node block)
114 (declare (type ref node) (type ir2-block block))
115 (let* ((cont (node-cont node))
116 (leaf (ref-leaf node))
117 (locs (continuation-result-tns
118 cont (list (primitive-type (leaf-type leaf)))))
122 (let ((tn (find-in-physenv leaf (node-physenv node))))
123 (if (lambda-var-indirect leaf)
124 (vop value-cell-ref node block tn res)
125 (emit-move node block tn res))))
127 (if (legal-immediate-constant-p leaf)
128 (emit-move node block (constant-tn leaf) res)
129 (let* ((name (leaf-source-name leaf))
130 (name-tn (emit-constant name)))
131 (if (policy node (zerop safety))
132 (vop fast-symbol-value node block name-tn res)
133 (vop symbol-value node block name-tn res)))))
135 (ir2-convert-closure node block leaf res))
137 (let ((unsafe (policy node (zerop safety)))
138 (name (leaf-source-name leaf)))
139 (ecase (global-var-kind leaf)
141 (aver (symbolp name))
142 (let ((name-tn (emit-constant name)))
144 (vop fast-symbol-value node block name-tn res)
145 (vop symbol-value node block name-tn res))))
147 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
149 (vop fdefn-fun node block fdefn-tn res)
150 (vop safe-fdefn-fun node block fdefn-tn res))))))))
151 (move-continuation-result node block locs cont))
154 ;;; Emit code to load a function object implementing FUN into
155 ;;; RES. This gets interesting when the referenced function is a
156 ;;; closure: we must make the closure and move the closed-over values
159 ;;; FUN is either a :TOPLEVEL-XEP functional or the XEP lambda for the
160 ;;; called function, since local call analysis converts all closure
161 ;;; references. If a :TOPLEVEL-XEP, we know it is not a closure.
163 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
164 ;;; don't initialize that slot. This can happen with closures over
165 ;;; top level variables, where optimization of the closure deleted the
166 ;;; variable. Since we committed to the closure format when we
167 ;;; pre-analyzed the top level code, we just leave an empty slot.
168 (defun ir2-convert-closure (ref ir2-block fun res)
169 (declare (type ref ref) (type ir2-block ir2-block)
170 (type functional fun) (type tn res))
172 (unless (leaf-info fun)
173 (setf (leaf-info fun)
174 (make-entry-info :name (functional-debug-name fun))))
175 (let ((entry (make-load-time-constant-tn :entry fun))
176 (closure (etypecase fun
179 ;; This assertion was sort of an experiment. It
180 ;; would be nice and sane and easier to understand
181 ;; things if it were *always* true, but
182 ;; experimentally I observe that it's only
183 ;; *almost* always true. -- WHN 2001-01-02
185 (aver (eql (lambda-component fun)
186 (block-component (ir2-block-block ir2-block))))
188 ;; Check for some weirdness which came up in bug
191 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts
192 ;; an :ENTRY record into the
193 ;; IR2-COMPONENT-CONSTANTS table. The
194 ;; dump-a-COMPONENT code
195 ;; * treats every HANDLEless :ENTRY record into a
197 ;; * expects every patch to correspond to an
198 ;; IR2-COMPONENT-ENTRIES record.
199 ;; The IR2-COMPONENT-ENTRIES records are set by
200 ;; ENTRY-ANALYZE walking over COMPONENT-LAMBDAS.
201 ;; Bug 138b arose because there was a HANDLEless
202 ;; :ENTRY record which didn't correspond to an
203 ;; IR2-COMPONENT-ENTRIES record. That problem is
204 ;; hard to debug when it's caught at dump time, so
205 ;; this assertion tries to catch it here.
207 (component-lambdas (lambda-component fun))))
209 ;; another bug-138-related issue: COMPONENT-NEW-FUNS
210 ;; is an IR1 temporary, and now that we're doing IR2
211 ;; it should've been completely flushed (but wasn't).
212 (aver (null (component-new-funs (lambda-component fun))))
214 (physenv-closure (get-lambda-physenv fun)))
216 (aver (eq (functional-kind fun) :toplevel-xep))
220 (let ((this-env (node-physenv ref)))
221 (vop make-closure ref ir2-block entry (length closure) res)
222 (loop for what in closure and n from 0 do
223 (unless (and (lambda-var-p what)
224 (null (leaf-refs what)))
225 (vop closure-init ref ir2-block
227 (find-in-physenv what this-env)
230 (emit-move ref ir2-block entry res))))
233 ;;; Convert a SET node. If the node's CONT is annotated, then we also
234 ;;; deliver the value to that continuation. If the var is a lexical
235 ;;; variable with no refs, then we don't actually set anything, since
236 ;;; the variable has been deleted.
237 (defun ir2-convert-set (node block)
238 (declare (type cset node) (type ir2-block block))
239 (let* ((cont (node-cont node))
240 (leaf (set-var node))
241 (val (continuation-tn node block (set-value node)))
242 (locs (if (continuation-info cont)
243 (continuation-result-tns
244 cont (list (primitive-type (leaf-type leaf))))
248 (when (leaf-refs leaf)
249 (let ((tn (find-in-physenv leaf (node-physenv node))))
250 (if (lambda-var-indirect leaf)
251 (vop value-cell-set node block tn val)
252 (emit-move node block val tn)))))
254 (ecase (global-var-kind leaf)
256 (aver (symbolp (leaf-source-name leaf)))
257 (vop set node block (emit-constant (leaf-source-name leaf)) val)))))
259 (emit-move node block val (first locs))
260 (move-continuation-result node block locs cont)))
263 ;;;; utilities for receiving fixed values
265 ;;; Return a TN that can be referenced to get the value of CONT. CONT
266 ;;; must be LTN-Annotated either as a delayed leaf ref or as a fixed,
267 ;;; single-value continuation. If a type check is called for, do it.
269 ;;; The primitive-type of the result will always be the same as the
270 ;;; IR2-CONTINUATION-PRIMITIVE-TYPE, ensuring that VOPs are always
271 ;;; called with TNs that satisfy the operand primitive-type
272 ;;; restriction. We may have to make a temporary of the desired type
273 ;;; and move the actual continuation TN into it. This happens when we
274 ;;; delete a type check in unsafe code or when we locally know
275 ;;; something about the type of an argument variable.
276 (defun continuation-tn (node block cont)
277 (declare (type node node) (type ir2-block block) (type continuation cont))
278 (let* ((2cont (continuation-info cont))
280 (ecase (ir2-continuation-kind 2cont)
282 (let ((ref (continuation-use cont)))
283 (leaf-tn (ref-leaf ref) (node-physenv ref))))
285 (aver (= (length (ir2-continuation-locs 2cont)) 1))
286 (first (ir2-continuation-locs 2cont)))))
287 (ptype (ir2-continuation-primitive-type 2cont)))
289 (cond ((and (eq (continuation-type-check cont) t)
290 (multiple-value-bind (check types)
291 (continuation-check-types cont)
292 (aver (eq check :simple))
293 ;; If the proven type is a subtype of the possibly
294 ;; weakened type check then it's always true and is
296 (unless (values-subtypep (continuation-proven-type cont)
298 (let ((temp (make-normal-tn ptype)))
299 (emit-type-check node block cont-tn temp
302 ((eq (tn-primitive-type cont-tn) ptype) cont-tn)
304 (let ((temp (make-normal-tn ptype)))
305 (emit-move node block cont-tn temp)
308 ;;; This is similar to CONTINUATION-TN, but hacks multiple values. We
309 ;;; return continuations holding the values of CONT with PTYPES as
310 ;;; their primitive types. CONT must be annotated for the same number
311 ;;; of fixed values are there are PTYPES.
313 ;;; If the continuation has a type check, check the values into temps
314 ;;; and return the temps. When we have more values than assertions, we
315 ;;; move the extra values with no check.
316 (defun continuation-tns (node block cont ptypes)
317 (declare (type node node) (type ir2-block block)
318 (type continuation cont) (list ptypes))
319 (let* ((locs (ir2-continuation-locs (continuation-info cont)))
320 (nlocs (length locs)))
321 (aver (= nlocs (length ptypes)))
322 (if (eq (continuation-type-check cont) t)
323 (multiple-value-bind (check types) (continuation-check-types cont)
324 (aver (eq check :simple))
325 (let ((ntypes (length types)))
326 (mapcar (lambda (from to-type assertion)
327 (let ((temp (make-normal-tn to-type)))
329 (emit-type-check node block from temp assertion)
330 (emit-move node block from temp))
334 (append types (make-list (- nlocs ntypes)
335 :initial-element nil))
337 (mapcar (lambda (from to-type)
338 (if (eq (tn-primitive-type from) to-type)
340 (let ((temp (make-normal-tn to-type)))
341 (emit-move node block from temp)
346 ;;;; utilities for delivering values to continuations
348 ;;; Return a list of TNs with the specifier TYPES that can be used as
349 ;;; result TNs to evaluate an expression into the continuation CONT.
350 ;;; This is used together with MOVE-CONTINUATION-RESULT to deliver
351 ;;; fixed values to a continuation.
353 ;;; If the continuation isn't annotated (meaning the values are
354 ;;; discarded) or is unknown-values, the then we make temporaries for
355 ;;; each supplied value, providing a place to compute the result in
356 ;;; until we decide what to do with it (if anything.)
358 ;;; If the continuation is fixed-values, and wants the same number of
359 ;;; values as the user wants to deliver, then we just return the
360 ;;; IR2-CONTINUATION-LOCS. Otherwise we make a new list padded as
361 ;;; necessary by discarded TNs. We always return a TN of the specified
362 ;;; type, using the continuation locs only when they are of the
364 (defun continuation-result-tns (cont types)
365 (declare (type continuation cont) (type list types))
366 (let ((2cont (continuation-info cont)))
368 (mapcar #'make-normal-tn types)
369 (ecase (ir2-continuation-kind 2cont)
371 (let* ((locs (ir2-continuation-locs 2cont))
372 (nlocs (length locs))
373 (ntypes (length types)))
374 (if (and (= nlocs ntypes)
375 (do ((loc locs (cdr loc))
376 (type types (cdr type)))
378 (unless (eq (tn-primitive-type (car loc)) (car type))
381 (mapcar (lambda (loc type)
382 (if (eq (tn-primitive-type loc) type)
384 (make-normal-tn type)))
387 (mapcar #'make-normal-tn
388 (subseq types nlocs)))
392 (mapcar #'make-normal-tn types))))))
394 ;;; Make the first N standard value TNs, returning them in a list.
395 (defun make-standard-value-tns (n)
396 (declare (type unsigned-byte n))
399 (res (standard-arg-location i)))
402 ;;; Return a list of TNs wired to the standard value passing
403 ;;; conventions that can be used to receive values according to the
404 ;;; unknown-values convention. This is used with together
405 ;;; MOVE-CONTINUATION-RESULT for delivering unknown values to a fixed
406 ;;; values continuation.
408 ;;; If the continuation isn't annotated, then we treat as 0-values,
409 ;;; returning an empty list of temporaries.
411 ;;; If the continuation is annotated, then it must be :FIXED.
412 (defun standard-result-tns (cont)
413 (declare (type continuation cont))
414 (let ((2cont (continuation-info cont)))
416 (ecase (ir2-continuation-kind 2cont)
418 (make-standard-value-tns (length (ir2-continuation-locs 2cont)))))
421 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
422 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
423 ;;; doing the appropriate coercions.
424 (defun move-results-coerced (node block src dest)
425 (declare (type node node) (type ir2-block block) (list src dest))
426 (let ((nsrc (length src))
427 (ndest (length dest)))
428 (mapc (lambda (from to)
430 (emit-move node block from to)))
432 (append src (make-list (- ndest nsrc)
433 :initial-element (emit-constant nil)))
438 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
439 ;;; the specified continuation. NODE and BLOCK provide context for
440 ;;; emitting code. Although usually obtained from STANDARD-RESULT-TNs
441 ;;; or CONTINUATION-RESULT-TNs, RESULTS my be a list of any type or
444 ;;; If the continuation is fixed values, then move the results into
445 ;;; the continuation locations. If the continuation is unknown values,
446 ;;; then do the moves into the standard value locations, and use
447 ;;; PUSH-VALUES to put the values on the stack.
448 (defun move-continuation-result (node block results cont)
449 (declare (type node node) (type ir2-block block)
450 (list results) (type continuation cont))
451 (let* ((2cont (continuation-info cont)))
453 (ecase (ir2-continuation-kind 2cont)
455 (let ((locs (ir2-continuation-locs 2cont)))
456 (unless (eq locs results)
457 (move-results-coerced node block results locs))))
459 (let* ((nvals (length results))
460 (locs (make-standard-value-tns nvals)))
461 (move-results-coerced node block results locs)
462 (vop* push-values node block
463 ((reference-tn-list locs nil))
464 ((reference-tn-list (ir2-continuation-locs 2cont) t))
468 ;;;; template conversion
470 ;;; Build a TN-Refs list that represents access to the values of the
471 ;;; specified list of continuations ARGS for TEMPLATE. Any :CONSTANT
472 ;;; arguments are returned in the second value as a list rather than
473 ;;; being accessed as a normal argument. NODE and BLOCK provide the
474 ;;; context for emitting any necessary type-checking code.
475 (defun reference-args (node block args template)
476 (declare (type node node) (type ir2-block block) (list args)
477 (type template template))
478 (collect ((info-args))
481 (do ((args args (cdr args))
482 (types (template-arg-types template) (cdr types)))
484 (let ((type (first types))
486 (if (and (consp type) (eq (car type) ':constant))
487 (info-args (continuation-value arg))
488 (let ((ref (reference-tn (continuation-tn node block arg) nil)))
490 (setf (tn-ref-across last) ref)
494 (values (the (or tn-ref null) first) (info-args)))))
496 ;;; Convert a conditional template. We try to exploit any
497 ;;; drop-through, but emit an unconditional branch afterward if we
498 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
500 (defun ir2-convert-conditional (node block template args info-args if not-p)
501 (declare (type node node) (type ir2-block block)
502 (type template template) (type (or tn-ref null) args)
503 (list info-args) (type cif if) (type boolean not-p))
504 (aver (= (template-info-arg-count template) (+ (length info-args) 2)))
505 (let ((consequent (if-consequent if))
506 (alternative (if-alternative if)))
507 (cond ((drop-thru-p if consequent)
508 (emit-template node block template args nil
509 (list* (block-label alternative) (not not-p)
512 (emit-template node block template args nil
513 (list* (block-label consequent) not-p info-args))
514 (unless (drop-thru-p if alternative)
515 (vop branch node block (block-label alternative)))))))
517 ;;; Convert an IF that isn't the DEST of a conditional template.
518 (defun ir2-convert-if (node block)
519 (declare (type ir2-block block) (type cif node))
520 (let* ((test (if-test node))
521 (test-ref (reference-tn (continuation-tn node block test) nil))
522 (nil-ref (reference-tn (emit-constant nil) nil)))
523 (setf (tn-ref-across test-ref) nil-ref)
524 (ir2-convert-conditional node block (template-or-lose 'if-eq)
525 test-ref () node t)))
527 ;;; Return a list of primitive-types that we can pass to
528 ;;; CONTINUATION-RESULT-TNS describing the result types we want for a
529 ;;; template call. We duplicate here the determination of output type
530 ;;; that was done in initially selecting the template, so we know that
531 ;;; the types we find are allowed by the template output type
533 (defun find-template-result-types (call cont template rtypes)
534 (declare (type combination call) (type continuation cont)
535 (type template template) (list rtypes))
536 (let* ((dtype (node-derived-type call))
537 (type (if (and (or (eq (template-ltn-policy template) :safe)
538 (policy call (= safety 0)))
539 (continuation-type-check cont))
540 (values-type-intersection
542 (continuation-asserted-type cont))
544 (types (mapcar #'primitive-type
545 (if (values-type-p type)
546 (append (values-type-required type)
547 (values-type-optional type))
549 (let ((nvals (length rtypes))
550 (ntypes (length types)))
551 (cond ((< ntypes nvals)
553 (make-list (- nvals ntypes)
554 :initial-element *backend-t-primitive-type*)))
556 (subseq types 0 nvals))
560 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering
561 ;;; values to CONT. As an efficiency hack, we pick off the common case
562 ;;; where the continuation is fixed values and has locations that
563 ;;; satisfy the result restrictions. This can fail when there is a
564 ;;; type check or a values count mismatch.
565 (defun make-template-result-tns (call cont template rtypes)
566 (declare (type combination call) (type continuation cont)
567 (type template template) (list rtypes))
568 (let ((2cont (continuation-info cont)))
569 (if (and 2cont (eq (ir2-continuation-kind 2cont) :fixed))
570 (let ((locs (ir2-continuation-locs 2cont)))
571 (if (and (= (length rtypes) (length locs))
572 (do ((loc locs (cdr loc))
573 (rtype rtypes (cdr rtype)))
575 (unless (operand-restriction-ok
577 (tn-primitive-type (car loc))
581 (continuation-result-tns
583 (find-template-result-types call cont template rtypes))))
584 (continuation-result-tns
586 (find-template-result-types call cont template rtypes)))))
588 ;;; Get the operands into TNs, make TN-Refs for them, and then call
589 ;;; the template emit function.
590 (defun ir2-convert-template (call block)
591 (declare (type combination call) (type ir2-block block))
592 (let* ((template (combination-info call))
593 (cont (node-cont call))
594 (rtypes (template-result-types template)))
595 (multiple-value-bind (args info-args)
596 (reference-args call block (combination-args call) template)
597 (aver (not (template-more-results-type template)))
598 (if (eq rtypes :conditional)
599 (ir2-convert-conditional call block template args info-args
600 (continuation-dest cont) nil)
601 (let* ((results (make-template-result-tns call cont template rtypes))
602 (r-refs (reference-tn-list results t)))
603 (aver (= (length info-args)
604 (template-info-arg-count template)))
606 (emit-template call block template args r-refs info-args)
607 (emit-template call block template args r-refs))
608 (move-continuation-result call block results cont)))))
611 ;;; We don't have to do much because operand count checking is done by
612 ;;; IR1 conversion. The only difference between this and the function
613 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
615 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
616 (let* ((template (continuation-value template))
617 (info (continuation-value info))
618 (cont (node-cont call))
619 (rtypes (template-result-types template))
620 (results (make-template-result-tns call cont template rtypes))
621 (r-refs (reference-tn-list results t)))
622 (multiple-value-bind (args info-args)
623 (reference-args call block (cddr (combination-args call)) template)
624 (aver (not (template-more-results-type template)))
625 (aver (not (eq rtypes :conditional)))
626 (aver (null info-args))
629 (emit-template call block template args r-refs info)
630 (emit-template call block template args r-refs))
632 (move-continuation-result call block results cont)))
637 ;;; Convert a LET by moving the argument values into the variables.
638 ;;; Since a LET doesn't have any passing locations, we move the
639 ;;; arguments directly into the variables. We must also allocate any
640 ;;; indirect value cells, since there is no function prologue to do
642 (defun ir2-convert-let (node block fun)
643 (declare (type combination node) (type ir2-block block) (type clambda fun))
644 (mapc (lambda (var arg)
646 (let ((src (continuation-tn node block arg))
647 (dest (leaf-info var)))
648 (if (lambda-var-indirect var)
649 (do-make-value-cell node block src dest)
650 (emit-move node block src dest)))))
651 (lambda-vars fun) (basic-combination-args node))
654 ;;; Emit any necessary moves into assignment temps for a local call to
655 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
656 ;;; values, and (possibly EQ) TNs that are the actual destination of
657 ;;; the arguments. When necessary, we allocate temporaries for
658 ;;; arguments to preserve parallel assignment semantics. These lists
659 ;;; exclude unused arguments and include implicit environment
660 ;;; arguments, i.e. they exactly correspond to the arguments passed.
662 ;;; OLD-FP is the TN currently holding the value we want to pass as
663 ;;; OLD-FP. If null, then the call is to the same environment (an
664 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
665 ;;; environment alone.
666 (defun emit-psetq-moves (node block fun old-fp)
667 (declare (type combination node) (type ir2-block block) (type clambda fun)
668 (type (or tn null) old-fp))
669 (let* ((called-env (physenv-info (lambda-physenv fun)))
670 (this-1env (node-physenv node))
671 (actuals (mapcar (lambda (x)
673 (continuation-tn node block x)))
674 (combination-args node))))
677 (dolist (var (lambda-vars fun))
678 (let ((actual (pop actuals))
679 (loc (leaf-info var)))
682 ((lambda-var-indirect var)
684 (make-normal-tn *backend-t-primitive-type*)))
685 (do-make-value-cell node block actual temp)
687 ((member actual (locs))
688 (let ((temp (make-normal-tn (tn-primitive-type loc))))
689 (emit-move node block actual temp)
696 (dolist (thing (ir2-physenv-closure called-env))
697 (temps (find-in-physenv (car thing) this-1env))
701 (locs (ir2-physenv-old-fp called-env)))
703 (values (temps) (locs)))))
705 ;;; A tail-recursive local call is done by emitting moves of stuff
706 ;;; into the appropriate passing locations. After setting up the args
707 ;;; and environment, we just move our return-pc into the called
708 ;;; function's passing location.
709 (defun ir2-convert-tail-local-call (node block fun)
710 (declare (type combination node) (type ir2-block block) (type clambda fun))
711 (let ((this-env (physenv-info (node-physenv node))))
712 (multiple-value-bind (temps locs)
713 (emit-psetq-moves node block fun (ir2-physenv-old-fp this-env))
715 (mapc (lambda (temp loc)
716 (emit-move node block temp loc))
719 (emit-move node block
720 (ir2-physenv-return-pc this-env)
721 (ir2-physenv-return-pc-pass
723 (lambda-physenv fun)))))
727 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
728 ;;; except that the caller and callee environment are the same, so we
729 ;;; don't need to mess with the environment locations, return PC, etc.
730 (defun ir2-convert-assignment (node block fun)
731 (declare (type combination node) (type ir2-block block) (type clambda fun))
732 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
734 (mapc (lambda (temp loc)
735 (emit-move node block temp loc))
739 ;;; Do stuff to set up the arguments to a non-tail local call
740 ;;; (including implicit environment args.) We allocate a frame
741 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
742 ;;; the values to pass and the list of passing location TNs.
743 (defun ir2-convert-local-call-args (node block fun)
744 (declare (type combination node) (type ir2-block block) (type clambda fun))
745 (let ((fp (make-stack-pointer-tn))
746 (nfp (make-number-stack-pointer-tn))
747 (old-fp (make-stack-pointer-tn)))
748 (multiple-value-bind (temps locs)
749 (emit-psetq-moves node block fun old-fp)
750 (vop current-fp node block old-fp)
751 (vop allocate-frame node block
752 (physenv-info (lambda-physenv fun))
754 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
756 ;;; Handle a non-TR known-values local call. We emit the call, then
757 ;;; move the results to the continuation's destination.
758 (defun ir2-convert-local-known-call (node block fun returns cont start)
759 (declare (type node node) (type ir2-block block) (type clambda fun)
760 (type return-info returns) (type continuation cont)
762 (multiple-value-bind (fp nfp temps arg-locs)
763 (ir2-convert-local-call-args node block fun)
764 (let ((locs (return-info-locations returns)))
765 (vop* known-call-local node block
766 (fp nfp (reference-tn-list temps nil))
767 ((reference-tn-list locs t))
768 arg-locs (physenv-info (lambda-physenv fun)) start)
769 (move-continuation-result node block locs cont)))
772 ;;; Handle a non-TR unknown-values local call. We do different things
773 ;;; depending on what kind of values the continuation wants.
775 ;;; If CONT is :UNKNOWN, then we use the "multiple-" variant, directly
776 ;;; specifying the continuation's LOCS as the VOP results so that we
777 ;;; don't have to do anything after the call.
779 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
780 ;;; then call MOVE-CONTINUATION-RESULT to do any necessary type checks
782 (defun ir2-convert-local-unknown-call (node block fun cont start)
783 (declare (type node node) (type ir2-block block) (type clambda fun)
784 (type continuation cont) (type label start))
785 (multiple-value-bind (fp nfp temps arg-locs)
786 (ir2-convert-local-call-args node block fun)
787 (let ((2cont (continuation-info cont))
788 (env (physenv-info (lambda-physenv fun)))
789 (temp-refs (reference-tn-list temps nil)))
790 (if (and 2cont (eq (ir2-continuation-kind 2cont) :unknown))
791 (vop* multiple-call-local node block (fp nfp temp-refs)
792 ((reference-tn-list (ir2-continuation-locs 2cont) t))
794 (let ((locs (standard-result-tns cont)))
795 (vop* call-local node block
797 ((reference-tn-list locs t))
798 arg-locs env start (length locs))
799 (move-continuation-result node block locs cont)))))
802 ;;; Dispatch to the appropriate function, depending on whether we have
803 ;;; a let, tail or normal call. If the function doesn't return, call
804 ;;; it using the unknown-value convention. We could compile it as a
805 ;;; tail call, but that might seem confusing in the debugger.
806 (defun ir2-convert-local-call (node block)
807 (declare (type combination node) (type ir2-block block))
808 (let* ((fun (ref-leaf (continuation-use (basic-combination-fun node))))
809 (kind (functional-kind fun)))
810 (cond ((eq kind :let)
811 (ir2-convert-let node block fun))
812 ((eq kind :assignment)
813 (ir2-convert-assignment node block fun))
815 (ir2-convert-tail-local-call node block fun))
817 (let ((start (block-label (lambda-block fun)))
818 (returns (tail-set-info (lambda-tail-set fun)))
819 (cont (node-cont node)))
821 (return-info-kind returns)
824 (ir2-convert-local-unknown-call node block fun cont start))
826 (ir2-convert-local-known-call node block fun returns
832 ;;; Given a function continuation FUN, return as values a TN holding
833 ;;; the thing that we call and true if the thing is named (false if it
834 ;;; is a function). There are two interesting non-named cases:
835 ;;; -- Known to be a function, no check needed: return the
836 ;;; continuation loc.
837 ;;; -- Not known what it is.
838 (defun fun-continuation-tn (node block cont)
839 (declare (type continuation cont))
840 (let ((2cont (continuation-info cont)))
841 (if (eq (ir2-continuation-kind 2cont) :delayed)
842 (let ((name (continuation-fun-name cont t)))
844 (values (make-load-time-constant-tn :fdefinition name) t))
845 (let* ((locs (ir2-continuation-locs 2cont))
847 (check (continuation-type-check cont))
848 (function-ptype (primitive-type-or-lose 'function)))
849 (aver (and (eq (ir2-continuation-kind 2cont) :fixed)
850 (= (length locs) 1)))
851 (cond ((eq (tn-primitive-type loc) function-ptype)
852 (aver (not (eq check t)))
855 (let ((temp (make-normal-tn function-ptype)))
856 (aver (and (eq (ir2-continuation-primitive-type 2cont)
859 (emit-type-check node block loc temp
860 (specifier-type 'function))
861 (values temp nil))))))))
863 ;;; Set up the args to Node in the current frame, and return a tn-ref
864 ;;; list for the passing locations.
865 (defun move-tail-full-call-args (node block)
866 (declare (type combination node) (type ir2-block block))
867 (let ((args (basic-combination-args node))
870 (dotimes (num (length args))
871 (let ((loc (standard-arg-location num)))
872 (emit-move node block (continuation-tn node block (elt args num)) loc)
873 (let ((ref (reference-tn loc nil)))
875 (setf (tn-ref-across last) ref)
880 ;;; Move the arguments into the passing locations and do a (possibly
881 ;;; named) tail call.
882 (defun ir2-convert-tail-full-call (node block)
883 (declare (type combination node) (type ir2-block block))
884 (let* ((env (physenv-info (node-physenv node)))
885 (args (basic-combination-args node))
886 (nargs (length args))
887 (pass-refs (move-tail-full-call-args node block))
888 (old-fp (ir2-physenv-old-fp env))
889 (return-pc (ir2-physenv-return-pc env)))
891 (multiple-value-bind (fun-tn named)
892 (fun-continuation-tn node block (basic-combination-fun node))
894 (vop* tail-call-named node block
895 (fun-tn old-fp return-pc pass-refs)
898 (vop* tail-call node block
899 (fun-tn old-fp return-pc pass-refs)
905 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
906 (defun ir2-convert-full-call-args (node block)
907 (declare (type combination node) (type ir2-block block))
908 (let* ((args (basic-combination-args node))
909 (fp (make-stack-pointer-tn))
910 (nargs (length args)))
911 (vop allocate-full-call-frame node block nargs fp)
916 (locs (standard-arg-location num))
917 (let ((ref (reference-tn (continuation-tn node block (elt args num))
920 (setf (tn-ref-across last) ref)
924 (values fp first (locs) nargs)))))
926 ;;; Do full call when a fixed number of values are desired. We make
927 ;;; STANDARD-RESULT-TNS for our continuation, then deliver the result
928 ;;; using MOVE-CONTINUATION-RESULT. We do named or normal call, as
930 (defun ir2-convert-fixed-full-call (node block)
931 (declare (type combination node) (type ir2-block block))
932 (multiple-value-bind (fp args arg-locs nargs)
933 (ir2-convert-full-call-args node block)
934 (let* ((cont (node-cont node))
935 (locs (standard-result-tns cont))
936 (loc-refs (reference-tn-list locs t))
937 (nvals (length locs)))
938 (multiple-value-bind (fun-tn named)
939 (fun-continuation-tn node block (basic-combination-fun node))
941 (vop* call-named node block (fp fun-tn args) (loc-refs)
942 arg-locs nargs nvals)
943 (vop* call node block (fp fun-tn args) (loc-refs)
944 arg-locs nargs nvals))
945 (move-continuation-result node block locs cont))))
948 ;;; Do full call when unknown values are desired.
949 (defun ir2-convert-multiple-full-call (node block)
950 (declare (type combination node) (type ir2-block block))
951 (multiple-value-bind (fp args arg-locs nargs)
952 (ir2-convert-full-call-args node block)
953 (let* ((cont (node-cont node))
954 (locs (ir2-continuation-locs (continuation-info cont)))
955 (loc-refs (reference-tn-list locs t)))
956 (multiple-value-bind (fun-tn named)
957 (fun-continuation-tn node block (basic-combination-fun node))
959 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
961 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
965 ;;; stuff to check in CHECK-FULL-CALL
967 ;;; There are some things which are intended always to be optimized
968 ;;; away by DEFTRANSFORMs and such, and so never compiled into full
969 ;;; calls. This has been a source of bugs so many times that it seems
970 ;;; worth listing some of them here so that we can check the list
971 ;;; whenever we compile a full call.
973 ;;; FIXME: It might be better to represent this property by setting a
974 ;;; flag in DEFKNOWN, instead of representing it by membership in this
976 (defvar *always-optimized-away*
977 '(;; This should always be DEFTRANSFORMed away, but wasn't in a bug
978 ;; reported to cmucl-imp@cons.org 2000-06-20.
980 ;; These should always turn into VOPs, but wasn't in a bug which
981 ;; appeared when LTN-POLICY stuff was being tweaked in
982 ;; sbcl-0.6.9.16. in sbcl-0.6.0
986 ;;; more stuff to check in CHECK-FULL-CALL
988 ;;; These came in handy when troubleshooting cold boot after making
989 ;;; major changes in the package structure: various transforms and
990 ;;; VOPs and stuff got attached to the wrong symbol, so that
991 ;;; references to the right symbol were bogusly translated as full
992 ;;; calls instead of primitives, sending the system off into infinite
993 ;;; space. Having a report on all full calls generated makes it easier
994 ;;; to figure out what form caused the problem this time.
995 #!+sb-show (defvar *show-full-called-fnames-p* nil)
996 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
998 ;;; Do some checks on a full call:
999 ;;; * Is this a full call to something we have reason to know should
1000 ;;; never be full called?
1001 ;;; * Is this a full call to (SETF FOO) which might conflict with
1002 ;;; a DEFSETF or some such thing elsewhere in the program?
1003 (defun check-full-call (node)
1004 (let* ((cont (basic-combination-fun node))
1005 (fname (continuation-fun-name cont t)))
1006 (declare (type (or symbol cons) fname))
1008 #!+sb-show (unless (gethash fname *full-called-fnames*)
1009 (setf (gethash fname *full-called-fnames*) t))
1010 #!+sb-show (when *show-full-called-fnames-p*
1011 (/show "converting full call to named function" fname)
1012 (/show (basic-combination-args node))
1013 (/show (policy node speed) (policy node safety))
1014 (/show (policy node compilation-speed))
1015 (let ((arg-types (mapcar (lambda (maybe-continuation)
1016 (when maybe-continuation
1019 maybe-continuation))))
1020 (basic-combination-args node))))
1023 (when (memq fname *always-optimized-away*)
1024 (/show (policy node speed) (policy node safety))
1025 (/show (policy node compilation-speed))
1026 (error "internal error: full call to ~S" fname))
1029 (destructuring-bind (setf stem) fname
1030 (aver (eq setf 'setf))
1031 (setf (gethash stem *setf-assumed-fboundp*) t)))))
1033 ;;; If the call is in a tail recursive position and the return
1034 ;;; convention is standard, then do a tail full call. If one or fewer
1035 ;;; values are desired, then use a single-value call, otherwise use a
1036 ;;; multiple-values call.
1037 (defun ir2-convert-full-call (node block)
1038 (declare (type combination node) (type ir2-block block))
1039 (check-full-call node)
1040 (let ((2cont (continuation-info (node-cont node))))
1041 (cond ((node-tail-p node)
1042 (ir2-convert-tail-full-call node block))
1044 (eq (ir2-continuation-kind 2cont) :unknown))
1045 (ir2-convert-multiple-full-call node block))
1047 (ir2-convert-fixed-full-call node block))))
1050 ;;;; entering functions
1052 ;;; Do all the stuff that needs to be done on XEP entry:
1053 ;;; -- Create frame.
1054 ;;; -- Copy any more arg.
1055 ;;; -- Set up the environment, accessing any closure variables.
1056 ;;; -- Move args from the standard passing locations to their internal
1058 (defun init-xep-environment (node block fun)
1059 (declare (type bind node) (type ir2-block block) (type clambda fun))
1060 (let ((start-label (entry-info-offset (leaf-info fun)))
1061 (env (physenv-info (node-physenv node))))
1062 (let ((ef (functional-entry-fun fun)))
1063 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1064 ;; Special case the xep-allocate-frame + copy-more-arg case.
1065 (vop xep-allocate-frame node block start-label t)
1066 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1068 ;; No more args, so normal entry.
1069 (vop xep-allocate-frame node block start-label nil)))
1070 (if (ir2-physenv-closure env)
1071 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1072 (vop setup-closure-environment node block start-label closure)
1073 (when (getf (functional-plist ef) :fin-function)
1074 (vop funcallable-instance-lexenv node block closure closure))
1076 (dolist (loc (ir2-physenv-closure env))
1077 (vop closure-ref node block closure (incf n) (cdr loc)))))
1078 (vop setup-environment node block start-label)))
1080 (unless (eq (functional-kind fun) :toplevel)
1081 (let ((vars (lambda-vars fun))
1083 (when (leaf-refs (first vars))
1084 (emit-move node block (make-arg-count-location)
1085 (leaf-info (first vars))))
1086 (dolist (arg (rest vars))
1087 (when (leaf-refs arg)
1088 (let ((pass (standard-arg-location n))
1089 (home (leaf-info arg)))
1090 (if (lambda-var-indirect arg)
1091 (do-make-value-cell node block pass home)
1092 (emit-move node block pass home))))
1095 (emit-move node block (make-old-fp-passing-location t)
1096 (ir2-physenv-old-fp env)))
1100 ;;; Emit function prolog code. This is only called on bind nodes for
1101 ;;; functions that allocate environments. All semantics of let calls
1102 ;;; are handled by IR2-CONVERT-LET.
1104 ;;; If not an XEP, all we do is move the return PC from its passing
1105 ;;; location, since in a local call, the caller allocates the frame
1106 ;;; and sets up the arguments.
1107 (defun ir2-convert-bind (node block)
1108 (declare (type bind node) (type ir2-block block))
1109 (let* ((fun (bind-lambda node))
1110 (env (physenv-info (lambda-physenv fun))))
1111 (aver (member (functional-kind fun)
1112 '(nil :external :optional :toplevel :cleanup)))
1115 (init-xep-environment node block fun)
1117 (when *collect-dynamic-statistics*
1118 (vop count-me node block *dynamic-counts-tn*
1119 (block-number (ir2-block-block block)))))
1123 (ir2-physenv-return-pc-pass env)
1124 (ir2-physenv-return-pc env))
1126 (let ((lab (gen-label)))
1127 (setf (ir2-physenv-environment-start env) lab)
1128 (vop note-environment-start node block lab)))
1132 ;;;; function return
1134 ;;; Do stuff to return from a function with the specified values and
1135 ;;; convention. If the return convention is :FIXED and we aren't
1136 ;;; returning from an XEP, then we do a known return (letting
1137 ;;; representation selection insert the correct move-arg VOPs.)
1138 ;;; Otherwise, we use the unknown-values convention. If there is a
1139 ;;; fixed number of return values, then use RETURN, otherwise use
1140 ;;; RETURN-MULTIPLE.
1141 (defun ir2-convert-return (node block)
1142 (declare (type creturn node) (type ir2-block block))
1143 (let* ((cont (return-result node))
1144 (2cont (continuation-info cont))
1145 (cont-kind (ir2-continuation-kind 2cont))
1146 (fun (return-lambda node))
1147 (env (physenv-info (lambda-physenv fun)))
1148 (old-fp (ir2-physenv-old-fp env))
1149 (return-pc (ir2-physenv-return-pc env))
1150 (returns (tail-set-info (lambda-tail-set fun))))
1152 ((and (eq (return-info-kind returns) :fixed)
1154 (let ((locs (continuation-tns node block cont
1155 (return-info-types returns))))
1156 (vop* known-return node block
1157 (old-fp return-pc (reference-tn-list locs nil))
1159 (return-info-locations returns))))
1160 ((eq cont-kind :fixed)
1161 (let* ((types (mapcar #'tn-primitive-type (ir2-continuation-locs 2cont)))
1162 (cont-locs (continuation-tns node block cont types))
1163 (nvals (length cont-locs))
1164 (locs (make-standard-value-tns nvals)))
1165 (mapc (lambda (val loc)
1166 (emit-move node block val loc))
1170 (vop return-single node block old-fp return-pc (car locs))
1171 (vop* return node block
1172 (old-fp return-pc (reference-tn-list locs nil))
1176 (aver (eq cont-kind :unknown))
1177 (vop* return-multiple node block
1179 (reference-tn-list (ir2-continuation-locs 2cont) nil))
1186 ;;; This is used by the debugger to find the top function on the
1187 ;;; stack. It returns the OLD-FP and RETURN-PC for the current
1188 ;;; function as multiple values.
1189 (defoptimizer (sb!kernel:%caller-frame-and-pc ir2-convert) (() node block)
1190 (let ((ir2-physenv (physenv-info (node-physenv node))))
1191 (move-continuation-result node block
1192 (list (ir2-physenv-old-fp ir2-physenv)
1193 (ir2-physenv-return-pc ir2-physenv))
1196 ;;;; multiple values
1198 ;;; This is almost identical to IR2-Convert-Let. Since LTN annotates
1199 ;;; the continuation for the correct number of values (with the
1200 ;;; continuation user responsible for defaulting), we can just pick
1201 ;;; them up from the continuation.
1202 (defun ir2-convert-mv-bind (node block)
1203 (declare (type mv-combination node) (type ir2-block block))
1204 (let* ((cont (first (basic-combination-args node)))
1205 (fun (ref-leaf (continuation-use (basic-combination-fun node))))
1206 (vars (lambda-vars fun)))
1207 (aver (eq (functional-kind fun) :mv-let))
1208 (mapc (lambda (src var)
1209 (when (leaf-refs var)
1210 (let ((dest (leaf-info var)))
1211 (if (lambda-var-indirect var)
1212 (do-make-value-cell node block src dest)
1213 (emit-move node block src dest)))))
1214 (continuation-tns node block cont
1216 (primitive-type (leaf-type x)))
1221 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1222 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1223 ;;; the first argument: all the other argument continuation TNs are
1224 ;;; ignored. This is because we require all of the values globs to be
1225 ;;; contiguous and on stack top.
1226 (defun ir2-convert-mv-call (node block)
1227 (declare (type mv-combination node) (type ir2-block block))
1228 (aver (basic-combination-args node))
1229 (let* ((start-cont (continuation-info (first (basic-combination-args node))))
1230 (start (first (ir2-continuation-locs start-cont)))
1231 (tails (and (node-tail-p node)
1232 (lambda-tail-set (node-home-lambda node))))
1233 (cont (node-cont node))
1234 (2cont (continuation-info cont)))
1235 (multiple-value-bind (fun named)
1236 (fun-continuation-tn node block (basic-combination-fun node))
1237 (aver (and (not named)
1238 (eq (ir2-continuation-kind start-cont) :unknown)))
1241 (let ((env (physenv-info (node-physenv node))))
1242 (vop tail-call-variable node block start fun
1243 (ir2-physenv-old-fp env)
1244 (ir2-physenv-return-pc env))))
1246 (eq (ir2-continuation-kind 2cont) :unknown))
1247 (vop* multiple-call-variable node block (start fun nil)
1248 ((reference-tn-list (ir2-continuation-locs 2cont) t))))
1250 (let ((locs (standard-result-tns cont)))
1251 (vop* call-variable node block (start fun nil)
1252 ((reference-tn-list locs t)) (length locs))
1253 (move-continuation-result node block locs cont)))))))
1255 ;;; Reset the stack pointer to the start of the specified
1256 ;;; unknown-values continuation (discarding it and all values globs on
1258 (defoptimizer (%pop-values ir2-convert) ((continuation) node block)
1259 (let ((2cont (continuation-info (continuation-value continuation))))
1260 (aver (eq (ir2-continuation-kind 2cont) :unknown))
1261 (vop reset-stack-pointer node block
1262 (first (ir2-continuation-locs 2cont)))))
1264 ;;; Deliver the values TNs to CONT using MOVE-CONTINUATION-RESULT.
1265 (defoptimizer (values ir2-convert) ((&rest values) node block)
1266 (let ((tns (mapcar (lambda (x)
1267 (continuation-tn node block x))
1269 (move-continuation-result node block tns (node-cont node))))
1271 ;;; In the normal case where unknown values are desired, we use the
1272 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1273 ;;; for a fixed number of values, we punt by doing a full call to the
1274 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1275 ;;; defaulting any unsupplied values. It seems unworthwhile to
1276 ;;; optimize this case.
1277 (defoptimizer (values-list ir2-convert) ((list) node block)
1278 (let* ((cont (node-cont node))
1279 (2cont (continuation-info cont)))
1281 (ecase (ir2-continuation-kind 2cont)
1282 (:fixed (ir2-convert-full-call node block))
1284 (let ((locs (ir2-continuation-locs 2cont)))
1285 (vop* values-list node block
1286 ((continuation-tn node block list) nil)
1287 ((reference-tn-list locs t)))))))))
1289 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1290 (let* ((cont (node-cont node))
1291 (2cont (continuation-info cont)))
1293 (ecase (ir2-continuation-kind 2cont)
1294 (:fixed (ir2-convert-full-call node block))
1296 (let ((locs (ir2-continuation-locs 2cont)))
1297 (vop* %more-arg-values node block
1298 ((continuation-tn node block context)
1299 (continuation-tn node block start)
1300 (continuation-tn node block count)
1302 ((reference-tn-list locs t)))))))))
1304 ;;;; special binding
1306 ;;; This is trivial, given our assumption of a shallow-binding
1308 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1309 (let ((name (leaf-source-name (continuation-value var))))
1310 (vop bind node block (continuation-tn node block value)
1311 (emit-constant name))))
1312 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1313 (vop unbind node block))
1315 ;;; ### It's not clear that this really belongs in this file, or
1316 ;;; should really be done this way, but this is the least violation of
1317 ;;; abstraction in the current setup. We don't want to wire
1318 ;;; shallow-binding assumptions into IR1tran.
1319 (def-ir1-translator progv ((vars vals &body body) start cont)
1322 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1325 (mapc (lambda (var val)
1326 (%primitive bind val var))
1330 (%primitive unbind-to-here ,n-save-bs)))))
1334 ;;; Convert a non-local lexical exit. First find the NLX-Info in our
1335 ;;; environment. Note that this is never called on the escape exits
1336 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1338 (defun ir2-convert-exit (node block)
1339 (declare (type exit node) (type ir2-block block))
1340 (let ((loc (find-in-physenv (find-nlx-info (exit-entry node)
1342 (node-physenv node)))
1343 (temp (make-stack-pointer-tn))
1344 (value (exit-value node)))
1345 (vop value-cell-ref node block loc temp)
1347 (let ((locs (ir2-continuation-locs (continuation-info value))))
1348 (vop unwind node block temp (first locs) (second locs)))
1349 (let ((0-tn (emit-constant 0)))
1350 (vop unwind node block temp 0-tn 0-tn))))
1354 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1355 ;;; being entirely deleted.
1356 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1358 ;;; This function invalidates a lexical exit on exiting from the
1359 ;;; dynamic extent. This is done by storing 0 into the indirect value
1360 ;;; cell that holds the closed unwind block.
1361 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1362 (vop value-cell-set node block
1363 (find-in-physenv (continuation-value info) (node-physenv node))
1366 ;;; We have to do a spurious move of no values to the result
1367 ;;; continuation so that lifetime analysis won't get confused.
1368 (defun ir2-convert-throw (node block)
1369 (declare (type mv-combination node) (type ir2-block block))
1370 (let ((args (basic-combination-args node)))
1371 (vop* throw node block
1372 ((continuation-tn node block (first args))
1374 (ir2-continuation-locs (continuation-info (second args)))
1377 (move-continuation-result node block () (node-cont node))
1380 ;;; Emit code to set up a non-local exit. INFO is the NLX-Info for the
1381 ;;; exit, and TAG is the continuation for the catch tag (if any.) We
1382 ;;; get at the target PC by passing in the label to the vop. The vop
1383 ;;; is responsible for building a return-PC object.
1384 (defun emit-nlx-start (node block info tag)
1385 (declare (type node node) (type ir2-block block) (type nlx-info info)
1386 (type (or continuation null) tag))
1387 (let* ((2info (nlx-info-info info))
1388 (kind (cleanup-kind (nlx-info-cleanup info)))
1389 (block-tn (physenv-live-tn
1390 (make-normal-tn (primitive-type-or-lose 'catch-block))
1391 (node-physenv node)))
1392 (res (make-stack-pointer-tn))
1393 (target-label (ir2-nlx-info-target 2info)))
1395 (vop current-binding-pointer node block
1396 (car (ir2-nlx-info-dynamic-state 2info)))
1397 (vop* save-dynamic-state node block
1399 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1400 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1404 (vop make-catch-block node block block-tn
1405 (continuation-tn node block tag) target-label res))
1406 ((:unwind-protect :block :tagbody)
1407 (vop make-unwind-block node block block-tn target-label res)))
1411 (do-make-value-cell node block res (ir2-nlx-info-home 2info)))
1413 (vop set-unwind-protect node block block-tn))
1418 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1419 (defun ir2-convert-entry (node block)
1420 (declare (type entry node) (type ir2-block block))
1421 (dolist (exit (entry-exits node))
1422 (let ((info (find-nlx-info node (node-cont exit))))
1424 (member (cleanup-kind (nlx-info-cleanup info))
1425 '(:block :tagbody)))
1426 (emit-nlx-start node block info nil))))
1429 ;;; Set up the unwind block for these guys.
1430 (defoptimizer (%catch ir2-convert) ((info-cont tag) node block)
1431 (emit-nlx-start node block (continuation-value info-cont) tag))
1432 (defoptimizer (%unwind-protect ir2-convert) ((info-cont cleanup) node block)
1433 (emit-nlx-start node block (continuation-value info-cont) nil))
1435 ;;; Emit the entry code for a non-local exit. We receive values and
1436 ;;; restore dynamic state.
1438 ;;; In the case of a lexical exit or CATCH, we look at the exit
1439 ;;; continuation's kind to determine which flavor of entry VOP to
1440 ;;; emit. If unknown values, emit the xxx-MULTIPLE variant to the
1441 ;;; continuation locs. If fixed values, make the appropriate number of
1442 ;;; temps in the standard values locations and use the other variant,
1443 ;;; delivering the temps to the continuation using
1444 ;;; MOVE-CONTINUATION-RESULT.
1446 ;;; In the UNWIND-PROTECT case, we deliver the first register
1447 ;;; argument, the argument count and the argument pointer to our
1448 ;;; continuation as multiple values. These values are the block exited
1449 ;;; to and the values start and count.
1451 ;;; After receiving values, we restore dynamic state. Except in the
1452 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1453 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1454 ;;; pointer alone, since the thrown values are still out there.
1455 (defoptimizer (%nlx-entry ir2-convert) ((info-cont) node block)
1456 (let* ((info (continuation-value info-cont))
1457 (cont (nlx-info-continuation info))
1458 (2cont (continuation-info cont))
1459 (2info (nlx-info-info info))
1460 (top-loc (ir2-nlx-info-save-sp 2info))
1461 (start-loc (make-nlx-entry-arg-start-location))
1462 (count-loc (make-arg-count-location))
1463 (target (ir2-nlx-info-target 2info)))
1465 (ecase (cleanup-kind (nlx-info-cleanup info))
1466 ((:catch :block :tagbody)
1467 (if (and 2cont (eq (ir2-continuation-kind 2cont) :unknown))
1468 (vop* nlx-entry-multiple node block
1469 (top-loc start-loc count-loc nil)
1470 ((reference-tn-list (ir2-continuation-locs 2cont) t))
1472 (let ((locs (standard-result-tns cont)))
1473 (vop* nlx-entry node block
1474 (top-loc start-loc count-loc nil)
1475 ((reference-tn-list locs t))
1478 (move-continuation-result node block locs cont))))
1480 (let ((block-loc (standard-arg-location 0)))
1481 (vop uwp-entry node block target block-loc start-loc count-loc)
1482 (move-continuation-result
1484 (list block-loc start-loc count-loc)
1488 (when *collect-dynamic-statistics*
1489 (vop count-me node block *dynamic-counts-tn*
1490 (block-number (ir2-block-block block))))
1492 (vop* restore-dynamic-state node block
1493 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1495 (vop unbind-to-here node block
1496 (car (ir2-nlx-info-dynamic-state 2info)))))
1498 ;;;; n-argument functions
1500 (macrolet ((def-frob (name)
1501 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1502 (let* ((refs (move-tail-full-call-args node block))
1503 (cont (node-cont node))
1504 (res (continuation-result-tns
1506 (list (primitive-type (specifier-type 'list))))))
1507 (vop* ,name node block (refs) ((first res) nil)
1509 (move-continuation-result node block res cont)))))
1513 ;;; Convert the code in a component into VOPs.
1514 (defun ir2-convert (component)
1515 (declare (type component component))
1516 (let (#!+sb-dyncount
1517 (*dynamic-counts-tn*
1518 (when *collect-dynamic-statistics*
1520 (block-number (block-next (component-head component))))
1521 (counts (make-array blocks
1522 :element-type '(unsigned-byte 32)
1523 :initial-element 0))
1524 (info (make-dyncount-info
1525 :for (component-name component)
1526 :costs (make-array blocks
1527 :element-type '(unsigned-byte 32)
1530 (setf (ir2-component-dyncount-info (component-info component))
1532 (emit-constant info)
1533 (emit-constant counts)))))
1535 (declare (type index num))
1536 (do-ir2-blocks (2block component)
1537 (let ((block (ir2-block-block 2block)))
1538 (when (block-start block)
1539 (setf (block-number block) num)
1541 (when *collect-dynamic-statistics*
1542 (let ((first-node (continuation-next (block-start block))))
1543 (unless (or (and (bind-p first-node)
1544 (xep-p (bind-lambda first-node)))
1545 (eq (continuation-fun-name
1546 (node-cont first-node))
1551 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1553 (ir2-convert-block block)
1557 ;;; If necessary, emit a terminal unconditional branch to go to the
1558 ;;; successor block. If the successor is the component tail, then
1559 ;;; there isn't really any successor, but if the end is an unknown,
1560 ;;; non-tail call, then we emit an error trap just in case the
1561 ;;; function really does return.
1562 (defun finish-ir2-block (block)
1563 (declare (type cblock block))
1564 (let* ((2block (block-info block))
1565 (last (block-last block))
1566 (succ (block-succ block)))
1568 (aver (and succ (null (rest succ))))
1569 (let ((target (first succ)))
1570 (cond ((eq target (component-tail (block-component block)))
1571 (when (and (basic-combination-p last)
1572 (eq (basic-combination-kind last) :full))
1573 (let* ((fun (basic-combination-fun last))
1574 (use (continuation-use fun))
1575 (name (and (ref-p use)
1576 (leaf-has-source-name-p (ref-leaf use))
1577 (leaf-source-name (ref-leaf use)))))
1578 (unless (or (node-tail-p last)
1579 (info :function :info name)
1580 (policy last (zerop safety)))
1581 (vop nil-fun-returned-error last 2block
1583 (emit-constant name)
1584 (multiple-value-bind (tn named)
1585 (fun-continuation-tn last 2block fun)
1588 ((not (eq (ir2-block-next 2block) (block-info target)))
1589 (vop branch last 2block (block-label target)))))))
1593 ;;; Convert the code in a block into VOPs.
1594 (defun ir2-convert-block (block)
1595 (declare (type cblock block))
1596 (let ((2block (block-info block)))
1597 (do-nodes (node cont block)
1600 (let ((2cont (continuation-info cont)))
1602 (not (eq (ir2-continuation-kind 2cont) :delayed)))
1603 (ir2-convert-ref node 2block))))
1605 (let ((kind (basic-combination-kind node)))
1608 (ir2-convert-local-call node 2block))
1610 (ir2-convert-full-call node 2block))
1612 (let ((fun (fun-info-ir2-convert kind)))
1614 (funcall fun node 2block))
1615 ((eq (basic-combination-info node) :full)
1616 (ir2-convert-full-call node 2block))
1618 (ir2-convert-template node 2block))))))))
1620 (when (continuation-info (if-test node))
1621 (ir2-convert-if node 2block)))
1623 (let ((fun (bind-lambda node)))
1624 (when (eq (lambda-home fun) fun)
1625 (ir2-convert-bind node 2block))))
1627 (ir2-convert-return node 2block))
1629 (ir2-convert-set node 2block))
1632 ((eq (basic-combination-kind node) :local)
1633 (ir2-convert-mv-bind node 2block))
1634 ((eq (continuation-fun-name (basic-combination-fun node))
1636 (ir2-convert-throw node 2block))
1638 (ir2-convert-mv-call node 2block))))
1640 (when (exit-entry node)
1641 (ir2-convert-exit node 2block)))
1643 (ir2-convert-entry node 2block)))))
1645 (finish-ir2-block block)