Let OFFSET-CONFLICTS-IN-SB check multiple offsets at a time
[sbcl.git] / src / compiler / ir1tran-lambda.lisp
index c2ae9ae..ce06727 100644 (file)
 ;;; If it is losing, we punt with a COMPILER-ERROR. NAMES-SO-FAR is a
 ;;; list of names which have previously been bound. If the NAME is in
 ;;; this list, then we error out.
-(declaim (ftype (sfunction (t list) lambda-var) varify-lambda-arg))
-(defun varify-lambda-arg (name names-so-far)
+(declaim (ftype (sfunction (t list &optional t) lambda-var) varify-lambda-arg))
+(defun varify-lambda-arg (name names-so-far &optional (context "lambda list"))
   (declare (inline member))
   (unless (symbolp name)
-    (compiler-error "The lambda variable ~S is not a symbol." name))
+    (compiler-error "~S is not a symbol, and cannot be used as a variable." name))
   (when (member name names-so-far :test #'eq)
-    (compiler-error "The variable ~S occurs more than once in the lambda list."
-                    name))
+    (compiler-error "The variable ~S occurs more than once in the ~A."
+                    name
+                    context))
   (let ((kind (info :variable :kind name)))
-    (when (or (keywordp name) (eq kind :constant))
-      (compiler-error "The name of the lambda variable ~S is already in use to name a constant."
-                      name))
-    (cond ((eq kind :special)
+    (cond ((keywordp name)
+           (compiler-error "~S is a keyword, and cannot be used as a local variable."
+                           name))
+          ((eq kind :constant)
+           (compiler-error "~@<~S names a defined constant, and cannot be used as a ~
+                            local variable.~:@>"
+                           name))
+          ((eq :global kind)
+           (compiler-error "~@<~S names a global lexical variable, and cannot be used ~
+                            as a local variable.~:@>"
+                           name))
+          ((eq kind :special)
            (let ((specvar (find-free-var name)))
              (make-lambda-var :%source-name name
                               :type (leaf-type specvar)
                                 (source-name '.anonymous.)
                                 debug-name
                                 (note-lexical-bindings t)
-                                post-binding-lexenv)
+                                post-binding-lexenv
+                                system-lambda)
   (declare (list body vars aux-vars aux-vals))
 
   ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
          (lambda (make-lambda :vars vars
                   :bind bind
                   :%source-name source-name
-                  :%debug-name debug-name))
+                  :%debug-name debug-name
+                  :system-lambda-p system-lambda))
          (result-ctran (make-ctran))
          (result-lvar (make-lvar)))
 
          (fun (collect ((default-bindings)
                         (default-vals))
                 (dolist (default defaults)
-                  (if (constantp default)
+                  (if (sb!xc:constantp default)
                       (default-vals default)
-                      (let ((var (gensym)))
+                      (let ((var (sb!xc:gensym)))
                         (default-bindings `(,var ,default))
                         (default-vals var))))
-                (ir1-convert-lambda-body `((let (,@(default-bindings))
-                                             (%funcall ,fun
-                                                       ,@(reverse vals)
-                                                       ,@(default-vals))))
-                                         arg-vars
-                                         ;; FIXME: Would be nice to
-                                         ;; share these names instead
-                                         ;; of consing up several
-                                         ;; identical ones. Oh well.
-                                         :debug-name (debug-name
-                                                      '&optional-processor
-                                                      name)
-                                         :note-lexical-bindings nil))))
+                (let ((bindings (default-bindings))
+                      (call `(%funcall ,fun ,@(reverse vals) ,@(default-vals))))
+                  (ir1-convert-lambda-body (if bindings
+                                               `((let (,@bindings) ,call))
+                                               `(,call))
+                                          arg-vars
+                                          ;; FIXME: Would be nice to
+                                          ;; share these names instead
+                                          ;; of consing up several
+                                          ;; identical ones. Oh well.
+                                          :debug-name (debug-name
+                                                       '&optional-processor
+                                                       name)
+                                          :note-lexical-bindings nil
+                                          :system-lambda t)))))
     (mapc (lambda (var arg-var)
             (when (cdr (leaf-refs arg-var))
               (setf (leaf-ever-used var) t)))
                                         vars supplied-p-p body
                                         aux-vars aux-vals
                                         source-name debug-name
-                                        force post-binding-lexenv)
+                                        force post-binding-lexenv
+                                        system-lambda)
   (declare (type optional-dispatch res)
            (list default-vars default-vals entry-vars entry-vals vars body
                  aux-vars aux-vals))
                   (list* t arg-name entry-vals)
                   (rest vars) t body aux-vars aux-vals
                   source-name debug-name
-                  force post-binding-lexenv)
+                  force post-binding-lexenv system-lambda)
                  (ir1-convert-hairy-args
                   res
                   (cons arg default-vars)
                   (cons arg-name entry-vals)
                   (rest vars) supplied-p-p body aux-vars aux-vals
                   source-name debug-name
-                  force post-binding-lexenv))))
+                  force post-binding-lexenv system-lambda))))
 
     ;; We want to delay converting the entry, but there exist
     ;; problems: hidden references should not be established to
                                  :type (leaf-type var)
                                  :where-from (leaf-where-from var))))
 
-    (let* ((*allow-instrumenting* nil)
-           (n-context (gensym "N-CONTEXT-"))
+    (let* ((n-context (sb!xc:gensym "N-CONTEXT-"))
            (context-temp (make-lambda-var :%source-name n-context))
-           (n-count (gensym "N-COUNT-"))
+           (n-count (sb!xc:gensym "N-COUNT-"))
            (count-temp (make-lambda-var :%source-name n-count
                                         :type (specifier-type 'index))))
 
       (arg-vars context-temp count-temp)
 
       (when rest
-        (arg-vals `(%listify-rest-args
-                    ,n-context ,n-count)))
+        (arg-vals `(%listify-rest-args ,n-context ,n-count)))
       (when morep
         (arg-vals n-context)
         (arg-vals n-count))
 
+      ;; The reason for all the noise with
+      ;; STACK-GROWS-DOWNWARD-NOT-UPWARD is to enable generation of
+      ;; slightly more efficient code on x86oid processors.  (We can
+      ;; hoist the negation of the index outside the main parsing loop
+      ;; and take advantage of the base+index+displacement addressing
+      ;; mode on x86oids.)
       (when (optional-dispatch-keyp res)
-        (let ((n-index (gensym "N-INDEX-"))
-              (n-key (gensym "N-KEY-"))
-              (n-value-temp (gensym "N-VALUE-TEMP-"))
-              (n-allowp (gensym "N-ALLOWP-"))
-              (n-losep (gensym "N-LOSEP-"))
+        (let ((n-index (sb!xc:gensym "N-INDEX-"))
+              (n-key (sb!xc:gensym "N-KEY-"))
+              (n-value-temp (sb!xc:gensym "N-VALUE-TEMP-"))
+              (n-allowp (sb!xc:gensym "N-ALLOWP-"))
+              (n-lose (sb!xc:gensym "N-LOSE-"))
+              (n-losep (sb!xc:gensym "N-LOSEP-"))
               (allowp (or (optional-dispatch-allowp res)
                           (policy *lexenv* (zerop safety))))
               (found-allow-p nil))
 
-          (temps `(,n-index (1- ,n-count)) n-key n-value-temp)
-          (body `(declare (fixnum ,n-index) (ignorable ,n-key ,n-value-temp)))
+          (temps #!-stack-grows-downward-not-upward
+                 `(,n-index (1- ,n-count))
+                 #!+stack-grows-downward-not-upward
+                 `(,n-index (- (1- ,n-count)))
+                 #!-stack-grows-downward-not-upward n-value-temp
+                 #!-stack-grows-downward-not-upward n-key)
+          (body `(declare (fixnum ,n-index)
+                          #!-stack-grows-downward-not-upward
+                          (ignorable ,n-value-temp ,n-key)))
 
           (collect ((tests))
             (dolist (key keys)
                      (default (arg-info-default info))
                      (keyword (arg-info-key info))
                      (supplied-p (arg-info-supplied-p info))
-                     (n-value (gensym "N-VALUE-"))
+                     (n-value (sb!xc:gensym "N-VALUE-"))
                      (clause (cond (supplied-p
-                                    (let ((n-supplied (gensym "N-SUPPLIED-")))
+                                    (let ((n-supplied (sb!xc:gensym "N-SUPPLIED-")))
                                       (temps n-supplied)
                                       (arg-vals n-value n-supplied)
                                       `((eq ,n-key ',keyword)
                 (tests clause)))
 
             (unless allowp
-              (temps n-allowp n-losep)
+              (temps n-allowp n-lose n-losep)
               (unless found-allow-p
                 (tests `((eq ,n-key :allow-other-keys)
                          (setq ,n-allowp ,n-value-temp))))
               (tests `(t
-                       (setq ,n-losep (list ,n-key)))))
+                       (setq ,n-lose ,n-key
+                             ,n-losep t))))
 
             (body
              `(when (oddp ,n-count)
                 (%odd-key-args-error)))
 
             (body
+             #!-stack-grows-downward-not-upward
              `(locally
                 (declare (optimize (safety 0)))
                 (loop
                   (decf ,n-index)
                   (setq ,n-key (%more-arg ,n-context ,n-index))
                   (decf ,n-index)
-                  (cond ,@(tests)))))
+                  (cond ,@(tests))))
+             #!+stack-grows-downward-not-upward
+             `(locally (declare (optimize (safety 0)))
+                (loop
+                  (when (plusp ,n-index) (return))
+                  (multiple-value-bind (,n-value-temp ,n-key)
+                      (%more-kw-arg ,n-context ,n-index)
+                    (declare (ignorable ,n-value-temp ,n-key))
+                    (incf ,n-index 2)
+                    (cond ,@(tests))))))
 
             (unless allowp
               (body `(when (and ,n-losep (not ,n-allowp))
-                       (%unknown-key-arg-error (car ,n-losep))))))))
+                       (%unknown-key-arg-error ,n-lose)))))))
 
       (let ((ep (ir1-convert-lambda-body
                  `((let ,(temps)
                                ,@(arg-vals))))
                  (arg-vars)
                  :debug-name (debug-name '&more-processor name)
-                 :note-lexical-bindings nil)))
+                 :note-lexical-bindings nil
+                 :system-lambda t)))
         (setf (optional-dispatch-more-entry res)
               (register-entry-point ep res)))))
 
 ;;; incoming value is NIL, so we must union NULL with the declared
 ;;; type when computing the type for the main entry's argument.
 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
-                             rest more-context more-count keys supplied-p-p
-                             body aux-vars aux-vals
-                             source-name debug-name post-binding-lexenv)
+                         rest more-context more-count keys supplied-p-p
+                         body aux-vars aux-vals source-name debug-name
+                         post-binding-lexenv system-lambda)
   (declare (type optional-dispatch res)
            (list default-vars default-vals entry-vars entry-vals keys body
                  aux-vars aux-vals))
             (bind-vals))
     (when rest
       (main-vars rest)
-      (main-vals '()))
+      (main-vals '())
+      (unless (lambda-var-ignorep rest)
+        ;; Make up two extra variables, and squirrel them away in
+        ;; ARG-INFO-DEFAULT for transforming (VALUES-LIST REST) into
+        ;; (%MORE-ARG-VALUES CONTEXT 0 COUNT) when possible.
+        (let* ((context-name (sb!xc:gensym "REST-CONTEXT-"))
+               (context (make-lambda-var :%source-name context-name
+                                         :arg-info (make-arg-info :kind :more-context)))
+               (count-name (sb!xc:gensym "REST-COUNT-"))
+               (count (make-lambda-var :%source-name count-name
+                                       :arg-info (make-arg-info :kind :more-count)
+                                       :type (specifier-type 'index))))
+          (setf (arg-info-default (lambda-var-arg-info rest)) (list context count)
+                (lambda-var-ever-used context) t
+                (lambda-var-ever-used count) t)
+          (setf more-context context
+                more-count count))))
     (when more-context
       (main-vars more-context)
       (main-vals nil)
              (n-val (make-symbol (format nil
                                          "~A-DEFAULTING-TEMP"
                                          (leaf-source-name key))))
-             (key-type (leaf-type key))
-             (val-temp (make-lambda-var
-                        :%source-name n-val
-                        :type (if hairy-default
-                                  (type-union key-type (specifier-type 'null))
-                                  key-type))))
+             (val-temp (make-lambda-var :%source-name n-val)))
         (main-vars val-temp)
         (bind-vars key)
         (cond ((or hairy-default supplied-p)
-               (let* ((n-supplied (gensym "N-SUPPLIED-"))
+               (let* ((n-supplied (sb!xc:gensym "N-SUPPLIED-"))
                       (supplied-temp (make-lambda-var
                                       :%source-name n-supplied)))
                  (unless supplied-p
                (main-vals (arg-info-default info))
                (bind-vals n-val)))))
 
-    (let* ((name (or debug-name source-name))
-           (main-entry (ir1-convert-lambda-body
+    (let* ((main-entry (ir1-convert-lambda-body
                         body (main-vars)
                         :aux-vars (append (bind-vars) aux-vars)
                         :aux-vals (append (bind-vals) aux-vals)
                         :post-binding-lexenv post-binding-lexenv
-                        :debug-name (debug-name 'varargs-entry name)))
+                        :source-name source-name
+                        :debug-name debug-name
+                        :system-lambda system-lambda))
+           (name (or debug-name source-name))
            (last-entry (convert-optional-entry main-entry default-vars
                                                (main-vals) () name)))
       (setf (optional-dispatch-main-entry res)
                                vars supplied-p-p body aux-vars
                                aux-vals
                                source-name debug-name
-                               force post-binding-lexenv)
+                               force post-binding-lexenv
+                               system-lambda)
   (declare (type optional-dispatch res)
            (list default-vars default-vals entry-vars entry-vals vars body
                  aux-vars aux-vals))
+  (aver (or debug-name (neq '.anonymous. source-name)))
   (cond ((not vars)
          (if (optional-dispatch-keyp res)
              ;; Handle &KEY with no keys...
                                entry-vars entry-vals
                                nil nil nil vars supplied-p-p body aux-vars
                                aux-vals source-name debug-name
-                               post-binding-lexenv)
+                               post-binding-lexenv system-lambda)
              (let* ((name (or debug-name source-name))
                     (fun (ir1-convert-lambda-body
                          body (reverse default-vars)
                          :aux-vars aux-vars
                          :aux-vals aux-vals
                          :post-binding-lexenv post-binding-lexenv
-                         :debug-name (debug-name 'hairy-arg-processor name))))
+                         :source-name source-name
+                         :debug-name debug-name
+                         :system-lambda system-lambda)))
 
                (setf (optional-dispatch-main-entry res) fun)
                (register-entry-point fun res)
            (ir1-convert-hairy-args res nvars nvals nvars nvals
                                    (rest vars) nil body aux-vars aux-vals
                                    source-name debug-name
-                                   nil post-binding-lexenv)))
+                                   nil post-binding-lexenv system-lambda)))
         (t
          (let* ((arg (first vars))
                 (info (lambda-var-arg-info arg))
                          entry-vars entry-vals vars supplied-p-p body
                          aux-vars aux-vals
                          source-name debug-name
-                         force post-binding-lexenv)))
+                         force post-binding-lexenv
+                         system-lambda)))
                 ;; See GENERATE-OPTIONAL-DEFAULT-ENTRY.
                 (push (if (lambda-p ep)
                           (register-entry-point
                                 arg nil nil (rest vars) supplied-p-p body
                                 aux-vars aux-vals
                                 source-name debug-name
-                                post-binding-lexenv))
+                                post-binding-lexenv system-lambda))
              (:more-context
               (ir1-convert-more res default-vars default-vals
                                 entry-vars entry-vals
                                 nil arg (second vars) (cddr vars) supplied-p-p
                                 body aux-vars aux-vals
                                 source-name debug-name
-                                post-binding-lexenv))
+                                post-binding-lexenv system-lambda))
              (:keyword
               (ir1-convert-more res default-vars default-vals
                                 entry-vars entry-vals
                                 nil nil nil vars supplied-p-p body aux-vars
                                 aux-vals source-name debug-name
-                                post-binding-lexenv)))))))
+                                post-binding-lexenv system-lambda)))))))
 
 ;;; This function deals with the case where we have to make an
 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
 ;;; figure out the MIN-ARGS and MAX-ARGS.
 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals
-                                      &key
-                                      post-binding-lexenv
-                                      (source-name '.anonymous.)
-                                      (debug-name
-                                       (debug-name '&optional-dispatch vars)))
+                                 &key post-binding-lexenv
+                                 (source-name '.anonymous.)
+                                 debug-name system-lambda)
   (declare (list body vars aux-vars aux-vals))
+  (aver (or debug-name (neq '.anonymous. source-name)))
   (let ((res (make-optional-dispatch :arglist vars
                                      :allowp allowp
                                      :keyp keyp
                                                ,*current-path*))))
         (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
     (aver-live-component *current-component*)
-    (push res (component-new-functionals *current-component*))
     (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
-                            source-name debug-name nil post-binding-lexenv)
+                            source-name debug-name nil post-binding-lexenv
+                            system-lambda)
+    ;; ir1-convert-hairy-args can throw 'locall-already-let-converted
+    ;; push optional-dispatch into the current component only after it
+    ;; normally returned
+    (push res (component-new-functionals *current-component*))
     (setf (optional-dispatch-min-args res) min)
     (setf (optional-dispatch-max-args res)
           (+ (1- (length (optional-dispatch-entry-points res))) min))
 
 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
 (defun ir1-convert-lambda (form &key (source-name '.anonymous.)
-                           debug-name)
+                           debug-name maybe-add-debug-catch
+                           system-lambda)
   (unless (consp form)
     (compiler-error "A ~S was found when expecting a lambda expression:~%  ~S"
                     (type-of form)
     (compiler-error
      "The lambda expression has a missing or non-list lambda list:~%  ~S"
      form))
-
+  (when (and system-lambda maybe-add-debug-catch)
+    (bug "Both SYSTEM-LAMBDA and MAYBE-ADD-DEBUG-CATCH specified"))
+  (unless (or debug-name (neq '.anonymous. source-name))
+    (setf debug-name (name-lambdalike form)))
   (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
       (make-lambda-vars (cadr form))
-    (multiple-value-bind (forms decls) (parse-body (cddr form))
+    (multiple-value-bind (forms decls doc) (parse-body (cddr form))
       (binding* (((*lexenv* result-type post-binding-lexenv)
                   (process-decls decls (append aux-vars vars) nil
                                  :binding-form-p t))
-                 (forms (if (and *allow-instrumenting*
-                                 (policy *lexenv* (>= insert-debug-catch 2)))
-                            `((catch (locally
-                                         (declare (optimize (insert-step-conditions 0)))
-                                       ;; Using MAKE-SYMBOL would lead
-                                       ;; to recursive disaster.
-                                       (%make-symbol "SB-DEBUG-CATCH-TAG"))
-                                ,@forms))
+                 (debug-catch-p (and maybe-add-debug-catch
+                                     *allow-instrumenting*
+                                     (policy *lexenv*
+                                             (>= insert-debug-catch 2))))
+                 (forms (if debug-catch-p
+                            (wrap-forms-in-debug-catch forms)
                             forms))
                  (forms (if (eq result-type *wild-type*)
                             forms
-                            `((the ,result-type (progn ,@forms)))))
-                 (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
-                          (ir1-convert-hairy-lambda forms vars keyp
-                                                    allow-other-keys
-                                                    aux-vars aux-vals
-                                                    :post-binding-lexenv post-binding-lexenv
-                                                    :source-name source-name
-                                                    :debug-name debug-name)
-                          (ir1-convert-lambda-body forms vars
-                                                   :aux-vars aux-vars
-                                                   :aux-vals aux-vals
-                                                   :post-binding-lexenv post-binding-lexenv
-                                                   :source-name source-name
-                                                   :debug-name debug-name))))
+                            `((the ,(type-specifier result-type) (progn ,@forms)))))
+                 (*allow-instrumenting* (and (not system-lambda) *allow-instrumenting*))
+                 (res (cond ((or (find-if #'lambda-var-arg-info vars) keyp)
+                             (ir1-convert-hairy-lambda forms vars keyp
+                                                       allow-other-keys
+                                                       aux-vars aux-vals
+                                                       :post-binding-lexenv post-binding-lexenv
+                                                       :source-name source-name
+                                                       :debug-name debug-name
+                                                       :system-lambda system-lambda))
+                            (t
+                             (ir1-convert-lambda-body forms vars
+                                                      :aux-vars aux-vars
+                                                      :aux-vals aux-vals
+                                                      :post-binding-lexenv post-binding-lexenv
+                                                      :source-name source-name
+                                                      :debug-name debug-name
+                                                      :system-lambda system-lambda)))))
         (setf (functional-inline-expansion res) form)
         (setf (functional-arg-documentation res) (cadr form))
+        (setf (functional-documentation res) doc)
+        (when (boundp '*lambda-conversions*)
+          ;; KLUDGE: Not counting TL-XEPs is a lie, of course, but
+          ;; keeps things less confusing to users of TIME, where this
+          ;; count gets used.
+          (unless (and (consp debug-name) (eq 'tl-xep (car debug-name)))
+            (incf *lambda-conversions*)))
         res))))
 
+(defun wrap-forms-in-debug-catch (forms)
+  #!+unwind-to-frame-and-call-vop
+  `((multiple-value-prog1
+      (progn
+        ,@forms)
+      ;; Just ensure that there won't be any tail-calls, IR2 magic will
+      ;; handle the rest.
+      (values)))
+  #!-unwind-to-frame-and-call-vop
+  `( ;; Normally, we'll return from this block with the below RETURN-FROM.
+    (block
+        return-value-tag
+      ;; If DEBUG-CATCH-TAG is thrown (with a thunk as the value) the
+      ;; RETURN-FROM is elided and we funcall the thunk instead. That
+      ;; thunk might either return a value (for a RETURN-FROM-FRAME)
+      ;; or call this same function again (for a RESTART-FRAME).
+      ;; -- JES, 2007-01-09
+      (funcall
+       (the function
+         ;; Use a constant catch tag instead of consing a new one for every
+         ;; entry to this block. The uniquencess of the catch tags is
+         ;; ensured when the tag is throw by the debugger. It'll allocate a
+         ;; new tag, and modify the reference this tag in the proper
+         ;; catch-block structure to refer to that new tag. This
+         ;; significantly decreases the runtime cost of high debug levels.
+         ;;  -- JES, 2007-01-09
+         (catch 'debug-catch-tag
+           (return-from return-value-tag
+             (progn
+               ,@forms))))))))
+
 ;;; helper for LAMBDA-like things, to massage them into a form
 ;;; suitable for IR1-CONVERT-LAMBDA.
 (defun ir1-convert-lambdalike (thing
                                &key
                                (source-name '.anonymous.)
                                debug-name)
+  (when (and (not debug-name) (eq '.anonymous. source-name))
+    (setf debug-name (name-lambdalike thing)))
   (ecase (car thing)
     ((lambda)
      (ir1-convert-lambda thing
-                         :source-name source-name
-                         :debug-name debug-name))
-    ((instance-lambda)
-     (deprecation-warning 'instance-lambda 'lambda)
-     (ir1-convert-lambda `(lambda ,@(cdr thing))
+                         :maybe-add-debug-catch t
                          :source-name source-name
                          :debug-name debug-name))
     ((named-lambda)
      (let ((name (cadr thing))
            (lambda-expression `(lambda ,@(cddr thing))))
-       (if (legal-fun-name-p name)
-           (let ((defined-fun-res (get-defined-fun name))
+       (if (and name (legal-fun-name-p name))
+           (let ((defined-fun-res (get-defined-fun name (second lambda-expression)))
                  (res (ir1-convert-lambda lambda-expression
+                                          :maybe-add-debug-catch t
                                           :source-name name)))
              (assert-global-function-definition-type name res)
-             (setf (defined-fun-functional defined-fun-res) res)
+             (push res (defined-fun-functionals defined-fun-res))
              (unless (eq (defined-fun-inlinep defined-fun-res) :notinline)
                (substitute-leaf-if
                 (lambda (ref)
                   (policy ref (> recognize-self-calls 0)))
                 res defined-fun-res))
              res)
-           (ir1-convert-lambda lambda-expression :debug-name name))))
+           (ir1-convert-lambda lambda-expression
+                               :maybe-add-debug-catch t
+                               :debug-name
+                               (or name (name-lambdalike thing))))))
     ((lambda-with-lexenv)
      (ir1-convert-inline-lambda thing
                                 :source-name source-name
 ;;; current compilation policy. Note that FUN may be a
 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
 ;;; reflect the state at the definition site.
-(defun ir1-convert-inline-lambda (fun &key
-                                      (source-name '.anonymous.)
-                                      debug-name)
+(defun ir1-convert-inline-lambda (fun
+                                  &key
+                                  (source-name '.anonymous.)
+                                  debug-name
+                                  system-lambda)
+  (when (and (not debug-name) (eq '.anonymous. source-name))
+    (setf debug-name (name-lambdalike fun)))
   (destructuring-bind (decls macros symbol-macros &rest body)
-                      (if (eq (car fun) 'lambda-with-lexenv)
-                          (cdr fun)
-                          `(() () () . ,(cdr fun)))
-    (let ((*lexenv* (make-lexenv
-                     :default (process-decls decls nil nil
-                                             :lexenv (make-null-lexenv))
-                     :vars (copy-list symbol-macros)
-                     :funs (mapcar (lambda (x)
-                                     `(,(car x) .
-                                       (macro . ,(coerce (cdr x) 'function))))
-                                   macros)
-                     :policy (lexenv-policy *lexenv*))))
-      (ir1-convert-lambda `(lambda ,@body)
-                          :source-name source-name
-                          :debug-name debug-name))))
+      (if (eq (car fun) 'lambda-with-lexenv)
+          (cdr fun)
+          `(() () () . ,(cdr fun)))
+    (let* ((*lexenv* (make-lexenv
+                      :default (process-decls decls nil nil
+                                              :lexenv (make-null-lexenv))
+                      :vars (copy-list symbol-macros)
+                      :funs (mapcar (lambda (x)
+                                      `(,(car x) .
+                                         (macro . ,(coerce (cdr x) 'function))))
+                                    macros)
+                      ;; Inherit MUFFLE-CONDITIONS from the call-site lexenv
+                      ;; rather than the definition-site lexenv, since it seems
+                      ;; like a much more common case.
+                      :handled-conditions (lexenv-handled-conditions *lexenv*)
+                      :policy (lexenv-policy *lexenv*)))
+           (clambda (ir1-convert-lambda `(lambda ,@body)
+                                        :source-name source-name
+                                        :debug-name debug-name
+                                        :system-lambda system-lambda)))
+      (setf (functional-inline-expanded clambda) t)
+      clambda)))
+
+;;; Given a lambda-list, return a FUN-TYPE object representing the signature:
+;;; return type is *, and each individual arguments type is T -- but we get
+;;; the argument counts and keywords.
+(defun ftype-from-lambda-list (lambda-list)
+  (multiple-value-bind (req opt restp rest-name keyp key-list allowp morep)
+      (parse-lambda-list lambda-list)
+    (declare (ignore rest-name))
+    (flet ((t (list)
+             (mapcar (constantly t) list)))
+      (let ((reqs (t req))
+            (opts (when opt (cons '&optional (t opt))))
+            ;; When it comes to building a type, &REST means pretty much the
+            ;; same thing as &MORE.
+            (rest (when (or morep restp) (list '&rest t)))
+            (keys (when keyp
+                    (cons '&key (mapcar (lambda (spec)
+                                          (let ((key/var (if (consp spec)
+                                                             (car spec)
+                                                             spec)))
+                                            (list (if (consp key/var)
+                                                      (car key/var)
+                                                      (keywordicate key/var))
+                                                  t)))
+                                        key-list))))
+            (allow (when allowp (list '&allow-other-keys))))
+        (specifier-type `(function (,@reqs ,@opts ,@rest ,@keys ,@allow) *))))))
 
 ;;; Get a DEFINED-FUN object for a function we are about to define. If
 ;;; the function has been forward referenced, then substitute for the
 ;;; previous references.
-(defun get-defined-fun (name)
+(defun get-defined-fun (name &optional (lambda-list nil lp))
   (proclaim-as-fun-name name)
-  (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
-    (note-name-defined name :function)
-    (cond ((not (defined-fun-p found))
-           (aver (not (info :function :inlinep name)))
-           (let* ((where-from (leaf-where-from found))
-                  (res (make-defined-fun
-                        :%source-name name
-                        :where-from (if (eq where-from :declared)
-                                        :declared :defined)
-                        :type (leaf-type found))))
-             (substitute-leaf res found)
-             (setf (gethash name *free-funs*) res)))
-          ;; If *FREE-FUNS* has a previously converted definition
-          ;; for this name, then blow it away and try again.
-          ((defined-fun-functional found)
-           (remhash name *free-funs*)
-           (get-defined-fun name))
-          (t found))))
+  (when (boundp '*free-funs*)
+    (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
+      (note-name-defined name :function)
+      (cond ((not (defined-fun-p found))
+             (aver (not (info :function :inlinep name)))
+             (let* ((where-from (leaf-where-from found))
+                    (res (make-defined-fun
+                          :%source-name name
+                          :where-from (if (eq where-from :declared)
+                                          :declared
+                                          :defined-here)
+                          :type (if (eq :declared where-from)
+                                    (leaf-type found)
+                                    (if lp
+                                        (ftype-from-lambda-list lambda-list)
+                                        (specifier-type 'function))))))
+               (substitute-leaf res found)
+               (setf (gethash name *free-funs*) res)))
+            ;; If *FREE-FUNS* has a previously converted definition
+            ;; for this name, then blow it away and try again.
+            ((defined-fun-functionals found)
+             (remhash name *free-funs*)
+             (get-defined-fun name lambda-list))
+            (t found)))))
 
 ;;; Check a new global function definition for consistency with
 ;;; previous declaration or definition, and assert argument/result
                 "previous declaration"
                 "previous definition"))))
 
-;;; Convert a lambda doing all the basic stuff we would do if we were
-;;; converting a DEFUN. In the old CMU CL system, this was used both
-;;; by the %DEFUN translator and for global inline expansion, but
-;;; since sbcl-0.pre7.something %DEFUN does things differently.
-;;; FIXME: And now it's probably worth rethinking whether this
-;;; function is a good idea.
-;;;
-;;; Unless a :INLINE function, we temporarily clobber the inline
-;;; expansion. This prevents recursive inline expansion of
-;;; opportunistic pseudo-inlines.
-(defun ir1-convert-lambda-for-defun (lambda var expansion converter)
-  (declare (cons lambda) (function converter) (type defined-fun var))
-  (let ((var-expansion (defined-fun-inline-expansion var)))
-    (unless (eq (defined-fun-inlinep var) :inline)
-      (setf (defined-fun-inline-expansion var) nil))
-    (let* ((name (leaf-source-name var))
-           (fun (funcall converter lambda
-                         :source-name name))
-           (fun-info (info :function :info name)))
-      (setf (functional-inlinep fun) (defined-fun-inlinep var))
-      (assert-new-definition var fun)
-      (setf (defined-fun-inline-expansion var) var-expansion)
-      ;; If definitely not an interpreter stub, then substitute for
-      ;; any old references.
-      (unless (or (eq (defined-fun-inlinep var) :notinline)
-                  (not *block-compile*)
-                  (and fun-info
-                       (or (fun-info-transforms fun-info)
-                           (fun-info-templates fun-info)
-                           (fun-info-ir2-convert fun-info))))
-        (substitute-leaf fun var)
-        ;; If in a simple environment, then we can allow backward
-        ;; references to this function from following top level forms.
-        (when expansion (setf (defined-fun-functional var) fun)))
-      fun)))
+;;; Used for global inline expansion. Earlier something like this was
+;;; used by %DEFUN too. FIXME: And now it's probably worth rethinking
+;;; whether this function is a good idea at all.
+(defun ir1-convert-inline-expansion (name expansion var inlinep info)
+  ;; Unless a :INLINE function, we temporarily clobber the inline
+  ;; expansion. This prevents recursive inline expansion of
+  ;; opportunistic pseudo-inlines.
+  (unless (eq inlinep :inline)
+    (setf (defined-fun-inline-expansion var) nil))
+  (let ((fun (ir1-convert-inline-lambda expansion
+                                        :source-name name
+                                        ;; prevent instrumentation of
+                                        ;; known function expansions
+                                        :system-lambda (and info t))))
+    (setf (functional-inlinep fun) inlinep)
+    (assert-new-definition var fun)
+    (setf (defined-fun-inline-expansion var) expansion)
+    ;; Associate VAR with the FUN -- and in case of an optional dispatch
+    ;; with the various entry-points. This allows XREF to know where the
+    ;; inline CLAMBDA comes from.
+    (flet ((note-inlining (f)
+             (typecase f
+               (functional
+                (setf (functional-inline-expanded f) var))
+               (cons
+                ;; Delayed entry-point.
+                (if (car f)
+                    (setf (functional-inline-expanded (cdr f)) var)
+                    (let ((old-thunk (cdr f)))
+                      (setf (cdr f) (lambda ()
+                                      (let ((g (funcall old-thunk)))
+                                        (setf (functional-inline-expanded g) var)
+                                        g)))))))))
+      (note-inlining fun)
+      (when (optional-dispatch-p fun)
+        (note-inlining (optional-dispatch-main-entry fun))
+        (note-inlining (optional-dispatch-more-entry fun))
+        (mapc #'note-inlining (optional-dispatch-entry-points fun))))
+    ;; substitute for any old references
+    (unless (or (not *block-compile*)
+                (and info
+                     (or (fun-info-transforms info)
+                         (fun-info-templates info)
+                         (fun-info-ir2-convert info))))
+      (substitute-leaf fun var))
+    fun))
+
+(defun %set-inline-expansion (name defined-fun inline-lambda)
+  (cond (inline-lambda
+         (setf (info :function :inline-expansion-designator name)
+               inline-lambda)
+         (when defined-fun
+           (setf (defined-fun-inline-expansion defined-fun)
+                 inline-lambda)))
+        (t
+         (clear-info :function :inline-expansion-designator name))))
 
 ;;; the even-at-compile-time part of DEFUN
 ;;;
-;;; The INLINE-EXPANSION is a LAMBDA-WITH-LEXENV, or NIL if there is
-;;; no inline expansion.
-(defun %compiler-defun (name lambda-with-lexenv compile-toplevel)
+;;; The INLINE-LAMBDA is a LAMBDA-WITH-LEXENV, or NIL if there is no
+;;; inline expansion.
+(defun %compiler-defun (name inline-lambda compile-toplevel)
   (let ((defined-fun nil)) ; will be set below if we're in the compiler
     (when compile-toplevel
-      ;; better be in the compiler
-      (aver (boundp '*lexenv*))
-      (remhash name *free-funs*)
-      (setf defined-fun (get-defined-fun name))
-      (aver (fasl-output-p *compile-object*))
-      (if (member name *fun-names-in-this-file* :test #'equal)
-          (warn 'duplicate-definition :name name)
-          (push name *fun-names-in-this-file*)))
+      (with-single-package-locked-error
+          (:symbol name "defining ~S as a function")
+        (setf defined-fun
+              (if inline-lambda
+                  (get-defined-fun name (fifth inline-lambda))
+                  (get-defined-fun name))))
+      (when (boundp '*lexenv*)
+        (aver (fasl-output-p *compile-object*))
+        (if (member name *fun-names-in-this-file* :test #'equal)
+            (warn 'duplicate-definition :name name)
+            (push name *fun-names-in-this-file*)))
+      (%set-inline-expansion name defined-fun inline-lambda))
 
     (become-defined-fun-name name)
 
-    (cond (lambda-with-lexenv
-           (setf (info :function :inline-expansion-designator name)
-                 lambda-with-lexenv)
-           (when defined-fun
-             (setf (defined-fun-inline-expansion defined-fun)
-                   lambda-with-lexenv)))
-          (t
-           (clear-info :function :inline-expansion-designator name)))
-
     ;; old CMU CL comment:
     ;;   If there is a type from a previous definition, blast it,
     ;;   since it is obsolete.
-    (when (and defined-fun
-               (eq (leaf-where-from defined-fun) :defined))
+    (when (and defined-fun (neq :declared (leaf-where-from defined-fun)))
       (setf (leaf-type defined-fun)
             ;; FIXME: If this is a block compilation thing, shouldn't
             ;; we be setting the type to the full derived type for the