- (type ir2-block ir2-block)
- (type functional functional)
- (type tn res))
- (aver (not (eql (functional-kind functional) :deleted)))
- (unless (leaf-info functional)
- (setf (leaf-info functional)
- (make-entry-info :name (functional-debug-name functional))))
- (let ((entry (make-load-time-constant-tn :entry functional))
- (closure (etypecase functional
- (clambda
- (assertions-on-ir2-converted-clambda functional)
- (physenv-closure (get-lambda-physenv functional)))
- (functional
- (aver (eq (functional-kind functional) :toplevel-xep))
- nil))))
-
- (cond (closure
- (let ((this-env (node-physenv ref)))
- (vop make-closure ref ir2-block entry (length closure) res)
- (loop for what in closure and n from 0 do
- (unless (and (lambda-var-p what)
- (null (leaf-refs what)))
- (vop closure-init ref ir2-block
- res
- (find-in-physenv what this-env)
- n)))))
- (t
- (emit-move ref ir2-block entry res))))
+ (type ir2-block ir2-block)
+ (type functional functional)
+ (type tn res))
+ (flet ((prepare ()
+ (aver (not (eql (functional-kind functional) :deleted)))
+ (unless (leaf-info functional)
+ (setf (leaf-info functional)
+ (make-entry-info :name
+ (functional-debug-name functional))))))
+ (let ((closure (etypecase functional
+ (clambda
+ (assertions-on-ir2-converted-clambda functional)
+ (physenv-closure (get-lambda-physenv functional)))
+ (functional
+ (aver (eq (functional-kind functional) :toplevel-xep))
+ nil)))
+ global-var)
+ (cond (closure
+ (prepare)
+ (let* ((physenv (node-physenv ref))
+ (tn (find-in-physenv functional physenv)))
+ (emit-move ref ir2-block tn res)))
+ ;; we're about to emit a reference to a "closure" that's actually
+ ;; an inlinable global function.
+ ((and (global-var-p (setf global-var
+ (functional-inline-expanded functional)))
+ (eq :global-function (global-var-kind global-var)))
+ (ir2-convert-global-var ref ir2-block global-var res))
+ (t
+ ;; if we're here, we should have either a toplevel-xep (some
+ ;; global scope function in a different component) or an external
+ ;; reference to the "closure"'s body.
+ (prepare)
+ (aver (memq (functional-kind functional) '(:external :toplevel-xep)))
+ (let ((entry (make-load-time-constant-tn :entry functional)))
+ (emit-move ref ir2-block entry res))))))
+ (values))
+
+(defun closure-initial-value (what this-env current-fp)
+ (declare (type (or nlx-info lambda-var clambda) what)
+ (type physenv this-env)
+ (type (or tn null) current-fp))
+ ;; If we have an indirect LAMBDA-VAR that does not require an
+ ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
+ ;; closed over), we need to store the current frame pointer.
+ (if (and (lambda-var-p what)
+ (lambda-var-indirect what)
+ (not (lambda-var-explicit-value-cell what))
+ (eq (lambda-physenv (lambda-var-home what))
+ this-env))
+ current-fp
+ (find-in-physenv what this-env)))
+
+(defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
+ ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
+ (when (lvar-dynamic-extent leaves)
+ (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
+ (setf (ir2-lvar-kind info) :delayed)
+ (setf (lvar-info leaves) info)
+ (setf (ir2-lvar-stack-pointer info)
+ (make-stack-pointer-tn)))))
+
+(defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
+ (let ((dx-p (lvar-dynamic-extent leaves)))
+ (collect ((delayed))
+ (when dx-p
+ (vop current-stack-pointer call 2block
+ (ir2-lvar-stack-pointer (lvar-info leaves))))
+ (dolist (leaf (lvar-value leaves))
+ (binding* ((xep (awhen (functional-entry-fun leaf)
+ ;; if the xep's been deleted then we can skip it
+ (if (eq (functional-kind it) :deleted)
+ nil it))
+ :exit-if-null)
+ (nil (aver (xep-p xep)))
+ (entry-info (lambda-info xep) :exit-if-null)
+ (tn (entry-info-closure-tn entry-info) :exit-if-null)
+ (closure (physenv-closure (get-lambda-physenv xep)))
+ (entry (make-load-time-constant-tn :entry xep)))
+ (let ((this-env (node-physenv call))
+ (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
+ (vop make-closure call 2block entry (length closure)
+ leaf-dx-p tn)
+ (loop for what in closure and n from 0 do
+ (unless (and (lambda-var-p what)
+ (null (leaf-refs what)))
+ ;; In LABELS a closure may refer to another closure
+ ;; in the same group, so we must be sure that we
+ ;; store a closure only after its creation.
+ ;;
+ ;; TODO: Here is a simple solution: we postpone
+ ;; putting of all closures after all creations
+ ;; (though it may require more registers).
+ (if (lambda-p what)
+ (delayed (list tn (find-in-physenv what this-env) n))
+ (let ((initial-value (closure-initial-value
+ what this-env nil)))
+ (if initial-value
+ (vop closure-init call 2block
+ tn initial-value n)
+ ;; An initial-value of NIL means to stash
+ ;; the frame pointer... which requires a
+ ;; different VOP.
+ (vop closure-init-from-fp call 2block tn n)))))))))
+ (loop for (tn what n) in (delayed)
+ do (vop closure-init call 2block
+ tn what n))))