0.pre7.55:
[sbcl.git] / src / compiler / ir2tran.lisp
index e1c85a9..9d96c87 100644 (file)
 ;;;; leaf reference
 
 ;;; Return the TN that holds the value of THING in the environment ENV.
-(defun find-in-environment (thing env)
-  (declare (type (or nlx-info lambda-var) thing) (type environment env)
+(defun find-in-physenv (thing physenv)
+  (declare (type (or nlx-info lambda-var) thing) (type physenv physenv)
           (values tn))
-  (or (cdr (assoc thing (ir2-environment-environment (environment-info env))))
+  (or (cdr (assoc thing (ir2-physenv-environment (physenv-info physenv))))
       (etypecase thing
        (lambda-var
-        (assert (eq env (lambda-environment (lambda-var-home thing))))
+        ;; I think that a failure of this assertion means that we're
+        ;; trying to access a variable which was improperly closed
+        ;; over. The PHYSENV describes a physical environment. Every
+        ;; variable that a form refers to should either be in its
+        ;; physical environment directly, or grabbed from a
+        ;; surrounding physical environment when it was closed over.
+        ;; The ASSOC expression above finds closed-over variables, so
+        ;; if we fell through the ASSOC expression, it wasn't closed
+        ;; over. Therefore, it must be in our physical environment
+        ;; directly. If instead it is in some other physical
+        ;; environment, then it's bogus for us to reference it here
+        ;; without it being closed over. -- WHN 2001-09-29
+        (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
         (leaf-info thing))
        (nlx-info
-        (assert (eq env (block-environment (nlx-info-target thing))))
+        (aver (eq physenv (block-physenv (nlx-info-target thing))))
         (ir2-nlx-info-home (nlx-info-info thing))))))
 
 ;;; If LEAF already has a constant TN, return that, otherwise make a
 ;;; isn't directly represented by a TN. ENV is the environment that
 ;;; the reference is done in.
 (defun leaf-tn (leaf env)
-  (declare (type leaf leaf) (type environment env))
+  (declare (type leaf leaf) (type physenv env))
   (typecase leaf
     (lambda-var
      (unless (lambda-var-indirect leaf)
-       (find-in-environment leaf env)))
+       (find-in-physenv leaf env)))
     (constant (constant-tn leaf))
     (t nil)))
 
         (res (first locs)))
     (etypecase leaf
       (lambda-var
-       (let ((tn (find-in-environment leaf (node-environment node))))
+       (let ((tn (find-in-physenv leaf (node-physenv node))))
         (if (lambda-var-indirect leaf)
             (vop value-cell-ref node block tn res)
             (emit-move node block tn res))))
        (let ((unsafe (policy node (zerop safety))))
         (ecase (global-var-kind leaf)
           ((:special :global :constant)
-           (assert (symbolp name))
+           (aver (symbolp name))
            (let ((name-tn (emit-constant name)))
              (if unsafe
                  (vop fast-symbol-value node block name-tn res)
           (:global-function
            (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
              (if unsafe
-                 (vop fdefn-function node block fdefn-tn res)
-                 (vop safe-fdefn-function node block fdefn-tn res))))))))
+                 (vop fdefn-fun node block fdefn-tn res)
+                 (vop safe-fdefn-fun node block fdefn-tn res))))))))
     (move-continuation-result node block locs cont))
   (values))
 
 ;;; the called function, since local call analysis converts all
 ;;; closure references. If a TL-XEP, we know it is not a closure.
 ;;;
-;;; If a closed-over lambda-var has no refs (is deleted), then we
+;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
 ;;; don't initialize that slot. This can happen with closures over
 ;;; top-level variables, where optimization of the closure deleted the
 ;;; variable. Since we committed to the closure format when we
   (let ((entry (make-load-time-constant-tn :entry leaf))
        (closure (etypecase leaf
                   (clambda
-                   (environment-closure (get-lambda-environment leaf)))
+                   (physenv-closure (get-lambda-physenv leaf)))
                   (functional
-                   (assert (eq (functional-kind leaf) :top-level-xep))
+                   (aver (eq (functional-kind leaf) :top-level-xep))
                    nil))))
     (cond (closure
-          (let ((this-env (node-environment node)))
+          (let ((this-env (node-physenv node)))
             (vop make-closure node 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 node block
                      res
-                     (find-in-environment what this-env)
+                     (find-in-physenv what this-env)
                      n)))))
          (t
           (emit-move node block entry res))))
     (etypecase leaf
       (lambda-var
        (when (leaf-refs leaf)
-        (let ((tn (find-in-environment leaf (node-environment node))))
+        (let ((tn (find-in-physenv leaf (node-physenv node))))
           (if (lambda-var-indirect leaf)
               (vop value-cell-set node block tn val)
               (emit-move node block val tn)))))
       (global-var
        (ecase (global-var-kind leaf)
         ((:special :global)
-         (assert (symbolp (leaf-name leaf)))
+         (aver (symbolp (leaf-name leaf)))
          (vop set node block (emit-constant (leaf-name leaf)) val)))))
     (when locs
       (emit-move node block val (first locs))
          (ecase (ir2-continuation-kind 2cont)
            (:delayed
             (let ((ref (continuation-use cont)))
-              (leaf-tn (ref-leaf ref) (node-environment ref))))
+              (leaf-tn (ref-leaf ref) (node-physenv ref))))
            (:fixed
-            (assert (= (length (ir2-continuation-locs 2cont)) 1))
+            (aver (= (length (ir2-continuation-locs 2cont)) 1))
             (first (ir2-continuation-locs 2cont)))))
         (ptype (ir2-continuation-primitive-type 2cont)))
 
     (cond ((and (eq (continuation-type-check cont) t)
                (multiple-value-bind (check types)
                    (continuation-check-types cont)
-                 (assert (eq check :simple))
+                 (aver (eq check :simple))
                  ;; If the proven type is a subtype of the possibly
                  ;; weakened type check then it's always true and is
                  ;; flushed.
           (type continuation cont) (list ptypes))
   (let* ((locs (ir2-continuation-locs (continuation-info cont)))
         (nlocs (length locs)))
-    (assert (= nlocs (length ptypes)))
+    (aver (= nlocs (length ptypes)))
     (if (eq (continuation-type-check cont) t)
        (multiple-value-bind (check types) (continuation-check-types cont)
-         (assert (eq check :simple))
+         (aver (eq check :simple))
          (let ((ntypes (length types)))
            (mapcar #'(lambda (from to-type assertion)
                        (let ((temp (make-normal-tn to-type)))
   (declare (type node node) (type ir2-block block)
           (type template template) (type (or tn-ref null) args)
           (list info-args) (type cif if) (type boolean not-p))
-  (assert (= (template-info-arg-count template) (+ (length info-args) 2)))
+  (aver (= (template-info-arg-count template) (+ (length info-args) 2)))
   (let ((consequent (if-consequent if))
        (alternative (if-alternative if)))
     (cond ((drop-thru-p if consequent)
         (rtypes (template-result-types template)))
     (multiple-value-bind (args info-args)
        (reference-arguments call block (combination-args call) template)
-      (assert (not (template-more-results-type template)))
+      (aver (not (template-more-results-type template)))
       (if (eq rtypes :conditional)
          (ir2-convert-conditional call block template args info-args
                                   (continuation-dest cont) nil)
          (let* ((results (make-template-result-tns call cont template rtypes))
                 (r-refs (reference-tn-list results t)))
-           (assert (= (length info-args)
-                      (template-info-arg-count template)))
+           (aver (= (length info-args)
+                    (template-info-arg-count template)))
            (if info-args
                (emit-template call block template args r-refs info-args)
                (emit-template call block template args r-refs))
     (multiple-value-bind (args info-args)
        (reference-arguments call block (cddr (combination-args call))
                             template)
-      (assert (not (template-more-results-type template)))
-      (assert (not (eq rtypes :conditional)))
-      (assert (null info-args))
+      (aver (not (template-more-results-type template)))
+      (aver (not (eq rtypes :conditional)))
+      (aver (null info-args))
 
       (if info
          (emit-template call block template args r-refs info)
 (defun emit-psetq-moves (node block fun old-fp)
   (declare (type combination node) (type ir2-block block) (type clambda fun)
           (type (or tn null) old-fp))
-  (let* ((called-env (environment-info (lambda-environment fun)))
-        (this-1env (node-environment node))
+  (let* ((called-env (physenv-info (lambda-physenv fun)))
+        (this-1env (node-physenv node))
         (actuals (mapcar #'(lambda (x)
                             (when x
                               (continuation-tn node block x)))
            (locs loc))))
 
       (when old-fp
-       (dolist (thing (ir2-environment-environment called-env))
-         (temps (find-in-environment (car thing) this-1env))
+       (dolist (thing (ir2-physenv-environment called-env))
+         (temps (find-in-physenv (car thing) this-1env))
          (locs (cdr thing)))
        
        (temps old-fp)
-       (locs (ir2-environment-old-fp called-env)))
+       (locs (ir2-physenv-old-fp called-env)))
 
       (values (temps) (locs)))))
 
 ;;; function's passing location.
 (defun ir2-convert-tail-local-call (node block fun)
   (declare (type combination node) (type ir2-block block) (type clambda fun))
-  (let ((this-env (environment-info (node-environment node))))
+  (let ((this-env (physenv-info (node-physenv node))))
     (multiple-value-bind (temps locs)
-       (emit-psetq-moves node block fun (ir2-environment-old-fp this-env))
+       (emit-psetq-moves node block fun (ir2-physenv-old-fp this-env))
 
       (mapc #'(lambda (temp loc)
                (emit-move node block temp loc))
            temps locs))
 
     (emit-move node block
-              (ir2-environment-return-pc this-env)
-              (ir2-environment-return-pc-pass
-               (environment-info
-                (lambda-environment fun)))))
+              (ir2-physenv-return-pc this-env)
+              (ir2-physenv-return-pc-pass
+               (physenv-info
+                (lambda-physenv fun)))))
 
   (values))
 
        (emit-psetq-moves node block fun old-fp)
       (vop current-fp node block old-fp)
       (vop allocate-frame node block
-          (environment-info (lambda-environment fun))
+          (physenv-info (lambda-physenv fun))
           fp nfp)
       (values fp nfp temps (mapcar #'make-alias-tn locs)))))
 
       (vop* known-call-local node block
            (fp nfp (reference-tn-list temps nil))
            ((reference-tn-list locs t))
-           arg-locs (environment-info (lambda-environment fun)) start)
+           arg-locs (physenv-info (lambda-physenv fun)) start)
       (move-continuation-result node block locs cont)))
   (values))
 
   (multiple-value-bind (fp nfp temps arg-locs)
       (ir2-convert-local-call-args node block fun)
     (let ((2cont (continuation-info cont))
-         (env (environment-info (lambda-environment fun)))
+         (env (physenv-info (lambda-physenv fun)))
          (temp-refs (reference-tn-list temps nil)))
       (if (and 2cont (eq (ir2-continuation-kind 2cont) :unknown))
          (vop* multiple-call-local node block (fp nfp temp-refs)
   (let ((2cont (continuation-info cont)))
     (if (eq (ir2-continuation-kind 2cont) :delayed)
        (let ((name (continuation-function-name cont t)))
-         (assert name)
+         (aver name)
          (values (make-load-time-constant-tn :fdefinition name) t))
        (let* ((locs (ir2-continuation-locs 2cont))
               (loc (first locs))
               (check (continuation-type-check cont))
               (function-ptype (primitive-type-or-lose 'function)))
-         (assert (and (eq (ir2-continuation-kind 2cont) :fixed)
-                      (= (length locs) 1)))
+         (aver (and (eq (ir2-continuation-kind 2cont) :fixed)
+                    (= (length locs) 1)))
          (cond ((eq (tn-primitive-type loc) function-ptype)
-                (assert (not (eq check t)))
+                (aver (not (eq check t)))
                 (values loc nil))
                (t
                 (let ((temp (make-normal-tn function-ptype)))
-                  (assert (and (eq (ir2-continuation-primitive-type 2cont)
-                                   function-ptype)
-                               (eq check t)))
+                  (aver (and (eq (ir2-continuation-primitive-type 2cont)
+                                 function-ptype)
+                             (eq check t)))
                   (emit-type-check node block loc temp
                                    (specifier-type 'function))
                   (values temp nil))))))))
 ;;; named) tail call.
 (defun ir2-convert-tail-full-call (node block)
   (declare (type combination node) (type ir2-block block))
-  (let* ((env (environment-info (node-environment node)))
+  (let* ((env (physenv-info (node-physenv node)))
         (args (basic-combination-args node))
         (nargs (length args))
         (pass-refs (move-tail-full-call-args node block))
-        (old-fp (ir2-environment-old-fp env))
-        (return-pc (ir2-environment-return-pc env)))
+        (old-fp (ir2-physenv-old-fp env))
+        (return-pc (ir2-physenv-return-pc env)))
 
     (multiple-value-bind (fun-tn named)
        (function-continuation-tn node block (basic-combination-fun node))
 
     (when (consp fname)
       (destructuring-bind (setf stem) fname
-       (assert (eq setf 'setf))
+       (aver (eq setf 'setf))
        (setf (gethash stem *setf-assumed-fboundp*) t)))))
 
 ;;; If the call is in a tail recursive position and the return
 (defun init-xep-environment (node block fun)
   (declare (type bind node) (type ir2-block block) (type clambda fun))
   (let ((start-label (entry-info-offset (leaf-info fun)))
-       (env (environment-info (node-environment node))))
+       (env (physenv-info (node-physenv node))))
     (let ((ef (functional-entry-function fun)))
       (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
             ;; Special case the xep-allocate-frame + copy-more-arg case.
            (t
             ;; No more args, so normal entry.
             (vop xep-allocate-frame node block start-label nil)))
-      (if (ir2-environment-environment env)
+      (if (ir2-physenv-environment env)
          (let ((closure (make-normal-tn *backend-t-primitive-type*)))
            (vop setup-closure-environment node block start-label closure)
            (when (getf (functional-plist ef) :fin-function)
              (vop funcallable-instance-lexenv node block closure closure))
            (let ((n -1))
-             (dolist (loc (ir2-environment-environment env))
+             (dolist (loc (ir2-physenv-environment env))
                (vop closure-ref node block closure (incf n) (cdr loc)))))
          (vop setup-environment node block start-label)))
 
          (incf n))))
 
     (emit-move node block (make-old-fp-passing-location t)
-              (ir2-environment-old-fp env)))
+              (ir2-physenv-old-fp env)))
 
   (values))
 
 ;;; Emit function prolog code. This is only called on bind nodes for
 ;;; functions that allocate environments. All semantics of let calls
-;;; are handled by IR2-Convert-Let.
+;;; are handled by IR2-CONVERT-LET.
 ;;;
 ;;; If not an XEP, all we do is move the return PC from its passing
 ;;; location, since in a local call, the caller allocates the frame
 (defun ir2-convert-bind (node block)
   (declare (type bind node) (type ir2-block block))
   (let* ((fun (bind-lambda node))
-        (env (environment-info (lambda-environment fun))))
-    (assert (member (functional-kind fun)
-                   '(nil :external :optional :top-level :cleanup)))
+        (env (physenv-info (lambda-physenv fun))))
+    (aver (member (functional-kind fun)
+                 '(nil :external :optional :top-level :cleanup)))
 
     (when (external-entry-point-p fun)
       (init-xep-environment node block fun)
        (vop count-me node block *dynamic-counts-tn*
             (block-number (ir2-block-block block)))))
 
-    (emit-move node block (ir2-environment-return-pc-pass env)
-              (ir2-environment-return-pc env))
+    (emit-move node
+              block
+              (ir2-physenv-return-pc-pass env)
+              (ir2-physenv-return-pc env))
 
     (let ((lab (gen-label)))
-      (setf (ir2-environment-environment-start env) lab)
+      (setf (ir2-physenv-environment-start env) lab)
       (vop note-environment-start node block lab)))
 
   (values))
         (2cont (continuation-info cont))
         (cont-kind (ir2-continuation-kind 2cont))
         (fun (return-lambda node))
-        (env (environment-info (lambda-environment fun)))
-        (old-fp (ir2-environment-old-fp env))
-        (return-pc (ir2-environment-return-pc env))
+        (env (physenv-info (lambda-physenv fun)))
+        (old-fp (ir2-physenv-old-fp env))
+        (return-pc (ir2-physenv-return-pc env))
         (returns (tail-set-info (lambda-tail-set fun))))
     (cond
      ((and (eq (return-info-kind returns) :fixed)
                  (nil)
                  nvals))))
      (t
-      (assert (eq cont-kind :unknown))
+      (aver (eq cont-kind :unknown))
       (vop* return-multiple node block
            (old-fp return-pc
                    (reference-tn-list (ir2-continuation-locs 2cont) nil))
 ;;; stack. It returns the OLD-FP and RETURN-PC for the current
 ;;; function as multiple values.
 (defoptimizer (sb!kernel:%caller-frame-and-pc ir2-convert) (() node block)
-  (let ((env (environment-info (node-environment node))))
+  (let ((env (physenv-info (node-physenv node))))
     (move-continuation-result node block
-                             (list (ir2-environment-old-fp env)
-                                   (ir2-environment-return-pc env))
+                             (list (ir2-physenv-old-fp env)
+                                   (ir2-physenv-return-pc env))
                              (node-cont node))))
 \f
 ;;;; multiple values
   (let* ((cont (first (basic-combination-args node)))
         (fun (ref-leaf (continuation-use (basic-combination-fun node))))
         (vars (lambda-vars fun)))
-    (assert (eq (functional-kind fun) :mv-let))
+    (aver (eq (functional-kind fun) :mv-let))
     (mapc #'(lambda (src var)
              (when (leaf-refs var)
                (let ((dest (leaf-info var)))
 ;;; contiguous and on stack top.
 (defun ir2-convert-mv-call (node block)
   (declare (type mv-combination node) (type ir2-block block))
-  (assert (basic-combination-args node))
+  (aver (basic-combination-args node))
   (let* ((start-cont (continuation-info (first (basic-combination-args node))))
         (start (first (ir2-continuation-locs start-cont)))
         (tails (and (node-tail-p node)
         (2cont (continuation-info cont)))
     (multiple-value-bind (fun named)
        (function-continuation-tn node block (basic-combination-fun node))
-      (assert (and (not named)
-                  (eq (ir2-continuation-kind start-cont) :unknown)))
+      (aver (and (not named)
+                (eq (ir2-continuation-kind start-cont) :unknown)))
       (cond
        (tails
-       (let ((env (environment-info (node-environment node))))
+       (let ((env (physenv-info (node-physenv node))))
          (vop tail-call-variable node block start fun
-              (ir2-environment-old-fp env)
-              (ir2-environment-return-pc env))))
+              (ir2-physenv-old-fp env)
+              (ir2-physenv-return-pc env))))
        ((and 2cont
             (eq (ir2-continuation-kind 2cont) :unknown))
        (vop* multiple-call-variable node block (start fun nil)
 ;;; top of it.)
 (defoptimizer (%pop-values ir2-convert) ((continuation) node block)
   (let ((2cont (continuation-info (continuation-value continuation))))
-    (assert (eq (ir2-continuation-kind 2cont) :unknown))
+    (aver (eq (ir2-continuation-kind 2cont) :unknown))
     (vop reset-stack-pointer node block
         (first (ir2-continuation-locs 2cont)))))
 
 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
   (vop unbind node block))
 
-;;; ### Not clear that this really belongs in this file, or should
-;;; really be done this way, but this is the least violation of
+;;; ### It's not clear that this really belongs in this file, or
+;;; should really be done this way, but this is the least violation of
 ;;; abstraction in the current setup. We don't want to wire
 ;;; shallow-binding assumptions into IR1tran.
 (def-ir1-translator progv ((vars vals &body body) start cont)
   (ir1-convert
    start cont
-   (if (or *converting-for-interpreter* (byte-compiling))
-       `(%progv ,vars ,vals #'(lambda () ,@body))
-       (once-only ((n-save-bs '(%primitive current-binding-pointer)))
-        `(unwind-protect
-             (progn
-               (mapc #'(lambda (var val)
-                         (%primitive bind val var))
-                     ,vars
-                     ,vals)
-               ,@body)
-           (%primitive unbind-to-here ,n-save-bs))))))
+   (once-only ((n-save-bs '(%primitive current-binding-pointer)))
+     `(unwind-protect
+         (progn
+           (mapc #'(lambda (var val)
+                     (%primitive bind val var))
+                 ,vars
+                 ,vals)
+           ,@body)
+       (%primitive unbind-to-here ,n-save-bs)))))
 \f
 ;;;; non-local exit
 
 ;;; IR2 converted.
 (defun ir2-convert-exit (node block)
   (declare (type exit node) (type ir2-block block))
-  (let ((loc (find-in-environment (find-nlx-info (exit-entry node)
-                                                (node-cont node))
-                                 (node-environment node)))
+  (let ((loc (find-in-physenv (find-nlx-info (exit-entry node)
+                                            (node-cont node))
+                             (node-physenv node)))
        (temp (make-stack-pointer-tn))
        (value (exit-value node)))
     (vop value-cell-ref node block loc temp)
 ;;; cell that holds the closed unwind block.
 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
   (vop value-cell-set node block
-       (find-in-environment (continuation-value info) (node-environment node))
+       (find-in-physenv (continuation-value info) (node-physenv node))
        (emit-constant 0)))
 
 ;;; We have to do a spurious move of no values to the result
           (type (or continuation null) tag))
   (let* ((2info (nlx-info-info info))
         (kind (cleanup-kind (nlx-info-cleanup info)))
-        (block-tn (environment-live-tn
+        (block-tn (physenv-live-tn
                    (make-normal-tn (primitive-type-or-lose 'catch-block))
-                   (node-environment node)))
+                   (node-physenv node)))
         (res (make-stack-pointer-tn))
         (target-label (ir2-nlx-info-target 2info)))
 
         (last (block-last block))
         (succ (block-succ block)))
     (unless (if-p last)
-      (assert (and succ (null (rest succ))))
+      (aver (and succ (null (rest succ))))
       (let ((target (first succ)))
        (cond ((eq target (component-tail (block-component block)))
               (when (and (basic-combination-p last)
                              (emit-constant name)
                              (multiple-value-bind (tn named)
                                  (function-continuation-tn last 2block fun)
-                               (assert (not named))
+                               (aver (not named))
                                tn)))))))
              ((not (eq (ir2-block-next 2block) (block-info target)))
               (vop branch last 2block (block-label target)))))))