X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcompiler%2Fir1tran-lambda.lisp;h=5bb387be4244dda8335c8231b3d01f8b0f4389b3;hb=e3113504fca73ebd1b992930315386d9d3ae5d18;hp=c2ae9ae26719631ac65dafbd0d76aa8c1dbbdc71;hpb=203e466f5fbb3bffa7b4beb19d87bed6f8a1b5db;p=sbcl.git diff --git a/src/compiler/ir1tran-lambda.lisp b/src/compiler/ir1tran-lambda.lisp index c2ae9ae..5bb387b 100644 --- a/src/compiler/ir1tran-lambda.lisp +++ b/src/compiler/ir1tran-lambda.lisp @@ -285,7 +285,8 @@ (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*. @@ -295,7 +296,8 @@ (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))) @@ -393,24 +395,26 @@ (fun (collect ((default-bindings) (default-vals)) (dolist (default defaults) - (if (constantp default) + (if (sb!xc:constantp default) (default-vals default) (let ((var (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))) @@ -428,7 +432,8 @@ 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)) @@ -448,7 +453,7 @@ (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) @@ -457,7 +462,7 @@ (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 @@ -521,8 +526,7 @@ :type (leaf-type var) :where-from (leaf-where-from var)))) - (let* ((*allow-instrumenting* nil) - (n-context (gensym "N-CONTEXT-")) + (let* ((n-context (gensym "N-CONTEXT-")) (context-temp (make-lambda-var :%source-name n-context)) (n-count (gensym "N-COUNT-")) (count-temp (make-lambda-var :%source-name n-count @@ -537,6 +541,12 @@ (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-")) @@ -547,8 +557,15 @@ (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) @@ -589,6 +606,7 @@ (%odd-key-args-error))) (body + #!-stack-grows-downward-not-upward `(locally (declare (optimize (safety 0))) (loop @@ -597,7 +615,16 @@ (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)) @@ -610,7 +637,8 @@ ,@(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))))) @@ -634,9 +662,9 @@ ;;; 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)) @@ -661,12 +689,7 @@ (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) @@ -697,7 +720,8 @@ :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))) + :debug-name (debug-name 'varargs-entry name) + :system-lambda system-lambda)) (last-entry (convert-optional-entry main-entry default-vars (main-vals) () name))) (setf (optional-dispatch-main-entry res) @@ -751,10 +775,12 @@ 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... @@ -762,14 +788,15 @@ 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)))) + :debug-name (debug-name 'hairy-arg-processor name) + :system-lambda system-lambda))) (setf (optional-dispatch-main-entry res) fun) (register-entry-point fun res) @@ -788,7 +815,7 @@ (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)) @@ -800,7 +827,8 @@ 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 @@ -820,32 +848,31 @@ 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 @@ -858,7 +885,8 @@ (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) (setf (optional-dispatch-min-args res) min) (setf (optional-dispatch-max-args res) (+ (1- (length (optional-dispatch-entry-points res))) min)) @@ -867,7 +895,8 @@ ;;; 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) @@ -881,51 +910,96 @@ (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)) (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)))) + (*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)) + (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 + :maybe-add-debug-catch t :source-name source-name :debug-name debug-name)) ((instance-lambda) @@ -936,9 +1010,10 @@ ((named-lambda) (let ((name (cadr thing)) (lambda-expression `(lambda ,@(cddr thing)))) - (if (legal-fun-name-p name) + (if (and name (legal-fun-name-p name)) (let ((defined-fun-res (get-defined-fun name)) (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) @@ -948,7 +1023,10 @@ (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 @@ -960,25 +1038,36 @@ ;;; 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))) ;;; Get a DEFINED-FUN object for a function we are about to define. If ;;; the function has been forward referenced, then substitute for the @@ -1036,41 +1125,31 @@ "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) + ;; 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)) ;;; the even-at-compile-time part of DEFUN ;;;