X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcompiler%2Ffopcompile.lisp;h=2ac2385c34db61aec2b6c7f6c1e858e0df1459ac;hb=711f75f20284c41f53485fda882fc7cc9e8e930f;hp=3162a8abbf3cdabd26708938a583c08e31f14d75;hpb=cf65b9804d28c5e6ee2fa53cbac143c2f87f108c;p=sbcl.git diff --git a/src/compiler/fopcompile.lisp b/src/compiler/fopcompile.lisp index 3162a8a..2ac2385 100644 --- a/src/compiler/fopcompile.lisp +++ b/src/compiler/fopcompile.lisp @@ -11,11 +11,6 @@ (in-package "SB!C") -;;; True if the current contour of FOPCOMPILABLE-P has a LET or LET* -;;; with a non-nil bindings list, false otherwise. The effect of this -;;; variable is to -(defvar *fop-complex-lexenv-p* nil) - ;;; SBCL has no proper byte compiler (having ditched the rather ;;; ambitious and slightly flaky byte compiler inherited from CMU CL) ;;; but its FOPs are a sort of byte code which is expressive enough @@ -41,26 +36,22 @@ ;; supporting in the future are LOCALLY (with declarations), ;; MACROLET, SYMBOL-MACROLET and THE. #+sb-xc-host - nil + (declare (ignore form)) #-sb-xc-host (or (and (self-evaluating-p form) (constant-fopcompilable-p form)) (and (symbolp form) (multiple-value-bind (macroexpansion macroexpanded-p) - (macroexpand form) + (%macroexpand form *lexenv*) (if macroexpanded-p (fopcompilable-p macroexpansion) ;; Punt on :ALIEN variables (let ((kind (info :variable :kind form))) - (or (eq kind :special) - ;; Not really a global, but a variable for - ;; which no information exists. - (eq kind :global) - (eq kind :constant)))))) + (member kind '(:special :constant :global :unknown)))))) (and (listp form) (ignore-errors (list-length form)) (multiple-value-bind (macroexpansion macroexpanded-p) - (macroexpand form) + (%macroexpand form *lexenv*) (if macroexpanded-p (fopcompilable-p macroexpansion) (destructuring-bind (operator &rest args) form @@ -86,7 +77,9 @@ ;; analysis would be useful are the PCL ;; slot-definition type-check-functions ;; -- JES, 2007-01-13 - (not *fop-complex-lexenv-p*)) + (notany (lambda (binding) + (lambda-var-p (cdr binding))) + (lexenv-vars *lexenv*))) ;; #'FOO, #'(SETF FOO), etc (legal-fun-name-p (car args))))) ((if) @@ -120,30 +113,7 @@ ;; (And whether there are declarations in the body, ;; see below) ((let let*) - (and (>= (length args) 1) - (loop for binding in (car args) - for complexp = *fop-complex-lexenv-p* then - (if (eq operator 'let) - complexp - t) - for name = (if (consp binding) - (first binding) - binding) - for value = (if (consp binding) - (second binding) - nil) - ;; Only allow binding lexicals, - ;; since special bindings can't be - ;; easily expressed with fops. - always (and (eq (info :variable :kind name) - :global) - (let ((*fop-complex-lexenv-p* - complexp)) - (fopcompilable-p value)))) - (let ((*fop-complex-lexenv-p* - (or *fop-complex-lexenv-p* - (not (null (car args)))))) - (every #'fopcompilable-p (cdr args))))) + (let-fopcompilable-p operator args)) ((locally) (every #'fopcompilable-p args)) (otherwise @@ -166,19 +136,48 @@ (<= (length args) 255) (every #'fopcompilable-p args)))))))))) +(defun let-fopcompilable-p (operator args) + (when (>= (length args) 1) + (multiple-value-bind (body decls) + (parse-body (cdr args) :doc-string-allowed nil) + (declare (ignore body)) + (let* ((orig-lexenv *lexenv*) + (*lexenv* (make-lexenv))) + ;; We need to check for declarations + ;; first. Otherwise the fake lexenv we're + ;; constructing might be invalid. + (and (null decls) + (loop for binding in (car args) + for name = (if (consp binding) + (first binding) + binding) + for value = (if (consp binding) + (second binding) + nil) + ;; Only allow binding locals, since special bindings can't + ;; be easily expressed with fops. + always (and (eq (info :variable :kind name) + :unknown) + (let ((*lexenv* (ecase operator + (let orig-lexenv) + (let* *lexenv*)))) + (fopcompilable-p value))) + do (progn + (setf *lexenv* (make-lexenv)) + (push (cons name + (make-lambda-var :%source-name name)) + (lexenv-vars *lexenv*)))) + (every #'fopcompilable-p (cdr args))))))) + (defun lambda-form-p (form) (and (consp form) (member (car form) - '(lambda named-lambda instance-lambda lambda-with-lexenv)))) + '(lambda named-lambda lambda-with-lexenv)))) ;;; Check that a literal form is fopcompilable. It would not for example ;;; when the form contains structures with funny MAKE-LOAD-FORMS. (defun constant-fopcompilable-p (constant) - (let ((things-processed nil) - (count 0)) - (declare (type (or list hash-table) things-processed) - (type (integer 0 #.(1+ list-to-hash-table-threshold)) count) - (inline member)) + (let ((xset (alloc-xset))) (labels ((grovel (value) ;; Unless VALUE is an object which which obviously ;; can't contain other objects @@ -188,22 +187,9 @@ number character string)) - (etypecase things-processed - (list - (when (member value things-processed :test #'eq) - (return-from grovel nil)) - (push value things-processed) - (incf count) - (when (> count list-to-hash-table-threshold) - (let ((things things-processed)) - (setf things-processed - (make-hash-table :test 'eq)) - (dolist (thing things) - (setf (gethash thing things-processed) t))))) - (hash-table - (when (gethash value things-processed) - (return-from grovel nil)) - (setf (gethash value things-processed) t))) + (if (xset-member-p value xset) + (return-from grovel nil) + (add-to-xset value xset)) (typecase value (cons (grovel (car value)) @@ -250,9 +236,6 @@ (grovel constant)) t)) -;;; An alist mapping lexical varible names to FOP table handles. -(defvar *fop-lexenv* nil) - ;;; FOR-VALUE-P is true if the value will be used (i.e., pushed onto ;;; FOP stack), or NIL if any value will be discarded. FOPCOMPILABLE-P ;;; has already ensured that the form can be fopcompiled. @@ -261,31 +244,39 @@ (fopcompile-constant form for-value-p)) ((symbolp form) (multiple-value-bind (macroexpansion macroexpanded-p) - (macroexpand form) + (%macroexpand form *lexenv*) (if macroexpanded-p ;; Symbol macro (fopcompile macroexpansion path for-value-p) (let ((kind (info :variable :kind form))) - (if (member kind '(:special :constant)) - ;; Special variable - (fopcompile `(symbol-value ',form) path for-value-p) - ;; Lexical - (when for-value-p - (let ((handle (cdr (assoc form *fop-lexenv*)))) - (if handle - (sb!fasl::dump-push handle - *compile-object*) - (progn - ;; Undefined variable. Signal a warning, and - ;; treat it as a special variable reference, - ;; like the real compiler does. - (note-undefined-reference form :variable) - (fopcompile `(symbol-value ',form) - path - for-value-p)))))))))) + (cond + ((eq :special kind) + ;; Special variable + (fopcompile `(symbol-value ',form) path for-value-p)) + + ((member kind '(:global :constant)) + ;; Global variable or constant. + (fopcompile `(symbol-global-value ',form) path for-value-p)) + (t + ;; Lexical + (let* ((lambda-var (cdr (assoc form (lexenv-vars *lexenv*)))) + (handle (when lambda-var + (lambda-var-fop-value lambda-var)))) + (if handle + (when for-value-p + (sb!fasl::dump-push handle *compile-object*)) + (progn + ;; Undefined variable. Signal a warning, and + ;; treat it as a special variable reference, like + ;; the real compiler does -- do not elide even if + ;; the value is unused. + (note-undefined-reference form :variable) + (fopcompile `(symbol-value ',form) + path + for-value-p)))))))))) ((listp form) (multiple-value-bind (macroexpansion macroexpanded-p) - (macroexpand form) + (%macroexpand form *lexenv*) (if macroexpanded-p (fopcompile macroexpansion path for-value-p) (destructuring-bind (operator &rest args) form @@ -314,12 +305,12 @@ for-value-p))) ((if) (fopcompile-if args path for-value-p)) - ((progn) - (loop for (arg . next) on args - do (fopcompile arg - path (if next - nil - for-value-p)))) + ((progn locally) + (loop for (arg . next) on args + do (fopcompile arg + path (if next + nil + for-value-p)))) ((setq) (loop for (name value . next) on args by #'cddr do (fopcompile `(set ',name ,value) path @@ -333,8 +324,8 @@ (fopcompile (cons 'progn body) path for-value-p) (fopcompile nil path for-value-p)))) ((let let*) - (let ((orig-lexenv *fop-lexenv*) - (*fop-lexenv* *fop-lexenv*)) + (let ((orig-lexenv *lexenv*) + (*lexenv* (make-lexenv :default *lexenv*))) (loop for binding in (car args) for name = (if (consp binding) (first binding) @@ -342,15 +333,17 @@ for value = (if (consp binding) (second binding) nil) - do (let ((*fop-lexenv* - (if (eql operator 'let) - orig-lexenv - *fop-lexenv*))) + do (let ((*lexenv* (if (eql operator 'let) + orig-lexenv + *lexenv*))) (fopcompile value path t)) - do (push (cons name - (sb!fasl::dump-pop - *compile-object*)) - *fop-lexenv*)) + do (let ((obj (sb!fasl::dump-pop *compile-object*))) + (setf *lexenv* + (make-lexenv + :vars (list (cons name + (make-lambda-var + :%source-name name + :fop-value obj))))))) (fopcompile (cons 'progn (cdr args)) path for-value-p))) ;; Otherwise it must be an ordinary funcall. (otherwise