Support inlining labels, bytecomp.el.
authorAidan Kehoe <kehoea@parhasard.net>
Sun, 02 Oct 2011 15:32:16 +0100
changeset 5574d4f334808463
parent 5573 f0f1fd0d8486
child 5575 89cb6a66a61f
Support inlining labels, bytecomp.el.

lisp/ChangeLog addition:

2011-10-02 Aidan Kehoe <kehoea@parhasard.net>

* bytecomp.el (byte-compile-initial-macro-environment):
Add #'declare to this, so it doesn't need to rely on
#'cl-compiling file to determine when we're byte-compiling.
Update #'labels to support declaring labels inline, as Common Lisp
requires.
* bytecomp.el (byte-compile-function-form):
Don't error if FUNCTION is quoting a non-lambda, non-symbol, just
return it.
* cl-extra.el (cl-macroexpand-all):
If a label name has been quoted, expand to the label placeholder
quoted with 'function. This allows the byte compiler to
distinguish between uses of the placeholder as data and uses in
contexts where it should be inlined.
* cl-macs.el:
* cl-macs.el (cl-do-proclaim):
When proclaming something as inline, if it is bound as a label,
don't modify the symbol's plist; instead, treat the first element
of its placeholder constant vector as a place to store compile
information.
* cl-macs.el (declare):
Leave processing declarations while compiling to the
implementation of #'declare in
byte-compile-initial-macro-environment.

tests/ChangeLog addition:

2011-10-02 Aidan Kehoe <kehoea@parhasard.net>

* automated/lisp-tests.el:
* automated/lisp-tests.el (+):
Test #'labels and inlining.
lisp/ChangeLog
lisp/bytecomp.el
lisp/cl-extra.el
lisp/cl-macs.el
tests/ChangeLog
tests/automated/lisp-tests.el
     1.1 --- a/lisp/ChangeLog	Sun Sep 25 16:12:07 2011 +0100
     1.2 +++ b/lisp/ChangeLog	Sun Oct 02 15:32:16 2011 +0100
     1.3 @@ -1,3 +1,29 @@
     1.4 +2011-10-02  Aidan Kehoe  <kehoea@parhasard.net>
     1.5 +
     1.6 +	* bytecomp.el (byte-compile-initial-macro-environment):
     1.7 +	Add #'declare to this, so it doesn't need to rely on
     1.8 +	#'cl-compiling file to determine when we're byte-compiling.
     1.9 +	Update #'labels to support declaring labels inline, as Common Lisp
    1.10 +	requires.
    1.11 +	* bytecomp.el (byte-compile-function-form):
    1.12 +	Don't error if FUNCTION is quoting a non-lambda, non-symbol, just
    1.13 +	return it.
    1.14 +	* cl-extra.el (cl-macroexpand-all):
    1.15 +	If a label name has been quoted, expand to the label placeholder
    1.16 +	quoted with 'function. This allows the byte compiler to
    1.17 +	distinguish between uses of the placeholder as data and uses in
    1.18 +	contexts where it should be inlined.
    1.19 +	* cl-macs.el:
    1.20 +	* cl-macs.el (cl-do-proclaim):
    1.21 +	When proclaming something as inline, if it is bound as a label,
    1.22 +	don't modify the symbol's plist; instead, treat the first element
    1.23 +	of its placeholder constant vector as a place to store compile
    1.24 +	information.
    1.25 +	* cl-macs.el (declare):
    1.26 +	Leave processing declarations while compiling to the
    1.27 +	implementation of #'declare in
    1.28 +	byte-compile-initial-macro-environment.
    1.29 +
    1.30  2011-09-25  Aidan Kehoe  <kehoea@parhasard.net>
    1.31  
    1.32  	* files.el (binary-file-regexps):
     2.1 --- a/lisp/bytecomp.el	Sun Sep 25 16:12:07 2011 +0100
     2.2 +++ b/lisp/bytecomp.el	Sun Oct 02 15:32:16 2011 +0100
     2.3 @@ -494,6 +494,11 @@
     2.4  	   (if byte-compile-delete-errors
     2.5  	       form
     2.6  	     (funcall (cdr (symbol-function 'the)) type form))))
     2.7 +    (declare
     2.8 +     . ,#'(lambda (&rest specs)
     2.9 +	    (while specs
    2.10 +	      (if (listp cl-declare-stack) (push (car specs) cl-declare-stack))
    2.11 +	      (cl-do-proclaim (pop specs) nil))))
    2.12      (load-time-value
    2.13       . ,#'(lambda (form &optional read-only)
    2.14              (let* ((gensym (gensym))
    2.15 @@ -517,37 +522,116 @@
    2.16                        (placeholders
    2.17                         (mapcar #'(lambda (lambda)
    2.18                                     (make-byte-code (second lambda) "\xc0\x87"
    2.19 -                                                   [42] 1))
    2.20 +                                                   ;; This list is used for
    2.21 +                                                   ;; the byte-optimize
    2.22 +                                                   ;; property, if the
    2.23 +                                                   ;; function is to be
    2.24 +                                                   ;; inlined. See
    2.25 +                                                   ;; cl-do-proclaim.
    2.26 +                                                   (vector nil) 1))
    2.27                                 lambdas))
    2.28                        (byte-compile-macro-environment
    2.29                         (pairlis names (mapcar
    2.30                                         #'(lambda (placeholder)
    2.31                                             `(lambda (&rest cl-labels-args)
    2.32 +                                              ;; Be careful not to quote
    2.33 +                                              ;; PLACEHOLDER, otherwise
    2.34 +                                              ;; byte-optimize-funcall inlines
    2.35 +                                              ;; it.
    2.36                                                (list* 'funcall ,placeholder
    2.37                                                       cl-labels-args)))
    2.38                                         placeholders)
    2.39                                  byte-compile-macro-environment))
    2.40                        (gensym (gensym)))
    2.41 -                 (put gensym 'byte-compile-label-alist
    2.42 -                      (pairlis placeholders
    2.43 -                               (mapcar 'second (mapcar 'cl-macroexpand-all
    2.44 -                                                       lambdas))))
    2.45 -                 (put gensym 'byte-compile
    2.46 -                      #'(lambda (form)
    2.47 -                          (let* ((byte-compile-label-alist
    2.48 -                                  (get (car form) 'byte-compile-label-alist)))
    2.49 -                            (dolist (acons byte-compile-label-alist)
    2.50 -                              (setf (cdr acons)
    2.51 -                                    (byte-compile-lambda (cdr acons))))
    2.52 -                            (byte-compile-body-do-effect
    2.53 -                             (sublis byte-compile-label-alist (cdr form)
    2.54 -                                     :test #'eq))
    2.55 -                            (dolist (acons byte-compile-label-alist)
    2.56 -                              (nsubst (cdr acons) (car acons)
    2.57 -                                      byte-compile-label-alist :test #'eq
    2.58 -                                      :descend-structures t)))))
    2.59 -                 (cl-macroexpand-all (cons gensym body)
    2.60 -                                     byte-compile-macro-environment))))
    2.61 +                 (labels
    2.62 +                     ((byte-compile-transform-labels (form names lambdas
    2.63 +                                                      placeholders)
    2.64 +                        (let* ((inline
    2.65 +                                 (mapcan
    2.66 +                                  #'(lambda (name placeholder lambda)
    2.67 +                                      (and
    2.68 +                                       (eq
    2.69 +                                        (getf (aref
    2.70 +                                               (compiled-function-constants
    2.71 +                                                placeholder) 0)
    2.72 +                                              'byte-optimizer)
    2.73 +                                        'byte-compile-inline-expand)
    2.74 +                                       `(((function ,placeholder)
    2.75 +                                          ,(byte-compile-lambda lambda)
    2.76 +                                          (function ,lambda)))))
    2.77 +                                  names placeholders lambdas))
    2.78 +                               (compiled
    2.79 +                                (mapcar #'byte-compile-lambda 
    2.80 +                                        (if (not inline)
    2.81 +                                            lambdas
    2.82 +                                          ;; See further down for the
    2.83 +                                          ;; rationale of the sublis calls.
    2.84 +                                          (sublis (pairlis
    2.85 +                                                   (mapcar #'cadar inline)
    2.86 +                                                   (mapcar #'third inline))
    2.87 +                                                  (sublis
    2.88 +                                                   (pairlis
    2.89 +                                                    (mapcar #'car inline)
    2.90 +                                                    (mapcar #'second inline))
    2.91 +                                                   lambdas :test #'equal)
    2.92 +                                                  :test #'eq))))
    2.93 +                               elt)
    2.94 +                          (mapc #'(lambda (placeholder function)
    2.95 +                                    (nsubst function placeholder compiled
    2.96 +                                            :test #'eq
    2.97 +                                            :descend-structures t))
    2.98 +                                placeholders compiled)
    2.99 +                          (when inline
   2.100 +                            (dolist (triad inline)
   2.101 +                              (nsubst (setq elt (elt compiled
   2.102 +                                                     (position (cadar triad)
   2.103 +                                                               placeholders)))
   2.104 +                                      (second triad) compiled :test #'eq
   2.105 +                                      :descend-structures t)
   2.106 +                              (setf (second triad) elt))
   2.107 +                            ;; For inlined labels: first, replace uses of
   2.108 +                            ;; the placeholder in places where it's not an
   2.109 +                            ;; evident, explicit funcall (that is, where
   2.110 +                            ;; it is not to be inlined) with the compiled
   2.111 +                            ;; function:
   2.112 +                            (setq form (sublis
   2.113 +                                        (pairlis (mapcar #'car inline)
   2.114 +                                                 (mapcar #'second inline))
   2.115 +                                        form :test #'equal)
   2.116 +                                  ;; Now replace uses of the placeholder
   2.117 +                                  ;; where it is an evident funcall with the
   2.118 +                                  ;; lambda, quoted as a function, to allow
   2.119 +                                  ;; byte-optimize-funcall to do its
   2.120 +                                  ;; thing. Note that the lambdas still have
   2.121 +                                  ;; the placeholders, so there's no risk
   2.122 +                                  ;; of recursive inlining.
   2.123 +                                  form (sublis (pairlis
   2.124 +                                                (mapcar #'cadar inline)
   2.125 +                                                (mapcar #'third inline))
   2.126 +                                               form :test #'eq)))
   2.127 +                          (sublis (pairlis placeholders compiled) form
   2.128 +                                  :test #'eq))))
   2.129 +                   (put gensym 'byte-compile
   2.130 +                        #'(lambda (form)
   2.131 +                            (let* ((names (cadr (cl-pop2 form)))
   2.132 +                                   (lambdas (mapcar #'cadr (cdr (pop form))))
   2.133 +                                   (placeholders (cadr (pop form))))
   2.134 +                              (byte-compile-body-do-effect
   2.135 +                               (byte-compile-transform-labels form names
   2.136 +                                                              lambdas
   2.137 +                                                              placeholders)))))
   2.138 +                   (put gensym 'byte-hunk-handler
   2.139 +                        #'(lambda (form)
   2.140 +                            (let* ((names (cadr (cl-pop2 form)))
   2.141 +                                   (lambdas (mapcar #'cadr (cdr (pop form))))
   2.142 +                                   (placeholders (cadr (pop form))))
   2.143 +                              (byte-compile-file-form
   2.144 +                               (cons 'progn
   2.145 +                                     (byte-compile-transform-labels
   2.146 +                                      form names lambdas placeholders))))))
   2.147 +                   (cl-macroexpand-all `(,gensym ',names (list ,@lambdas)
   2.148 +                                         ',placeholders ,@body)
   2.149 +                                       byte-compile-macro-environment)))))
   2.150      (flet .
   2.151        ,#'(lambda (bindings &rest body)
   2.152             (let* ((names (mapcar 'car bindings))
   2.153 @@ -3699,10 +3783,9 @@
   2.154    (if (cddr form)
   2.155        (byte-compile-normal-call
   2.156         `(signal 'wrong-number-of-arguments '(function ,(length (cdr form)))))
   2.157 -    (byte-compile-constant
   2.158 -     (cond ((symbolp (nth 1 form))
   2.159 -            (nth 1 form))
   2.160 -           ((byte-compile-lambda (nth 1 form)))))))
   2.161 +    (byte-compile-constant (if (eq 'lambda (car-safe (nth 1 form)))
   2.162 +                               (byte-compile-lambda (nth 1 form))
   2.163 +                             (nth 1 form)))))
   2.164  
   2.165  (defun byte-compile-insert (form)
   2.166    (cond ((null (cdr form))
     3.1 --- a/lisp/cl-extra.el	Sun Sep 25 16:12:07 2011 +0100
     3.2 +++ b/lisp/cl-extra.el	Sun Oct 02 15:32:16 2011 +0100
     3.3 @@ -619,8 +619,11 @@
     3.4                       (cl-macroexpand-all (nth 1 (nth 2 (nth 2 found))) env)
     3.5                     ;; It's an atom, almost certainly a compiled function;
     3.6                     ;; we're using the implementation of labels in
     3.7 -                   ;; bytecomp.el.
     3.8 -                  (nth 2 (nth 2 found)))
     3.9 +                   ;; bytecomp.el. Quote it with FUNCTION so that code can
    3.10 +                   ;; tell uses as data apart from the uses with funcall,
    3.11 +                   ;; where it's unquoted. #### We should warn if (car form)
    3.12 +                   ;; above is quote, rather than function.
    3.13 +                   (list 'function (nth 2 (nth 2 found))))
    3.14  	       form))))
    3.15  	((memq (car form) '(defun defmacro))
    3.16  	 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env)))
     4.1 --- a/lisp/cl-macs.el	Sun Sep 25 16:12:07 2011 +0100
     4.2 +++ b/lisp/cl-macs.el	Sun Oct 02 15:32:16 2011 +0100
     4.3 @@ -1969,18 +1969,38 @@
     4.4  
     4.5  	((eq (car-safe spec) 'inline)
     4.6  	 (while (setq spec (cdr spec))
     4.7 -	   (or (memq (get (car spec) 'byte-optimizer)
     4.8 -		     '(nil byte-compile-inline-expand))
     4.9 -	       (error "%s already has a byte-optimizer, can't make it inline"
    4.10 -		      (car spec)))
    4.11 -	   (put (car spec) 'byte-optimizer 'byte-compile-inline-expand)))
    4.12 -
    4.13 +	   (let ((assq (cdr (assq (car spec) byte-compile-macro-environment))))
    4.14 +	     (if (and (consp assq) (eq (nth 1 (nth 1 assq)) 'cl-labels-args)
    4.15 +		      (atom (setq assq (nth 2 (nth 2 assq)))))
    4.16 +		 ;; It's a label, and we're using the labels
    4.17 +		 ;; implementation in bytecomp.el. Tell the compiler
    4.18 +		 ;; to inline it, don't mark the symbol to be inlined
    4.19 +		 ;; globally.
    4.20 +		 (setf (getf (aref (compiled-function-constants assq) 0)
    4.21 +                             'byte-optimizer)
    4.22 +                       'byte-compile-inline-expand)
    4.23 +	       (or (memq (get (car spec) 'byte-optimizer)
    4.24 +			 '(nil byte-compile-inline-expand))
    4.25 +		   (error
    4.26 +		    "%s already has a byte-optimizer, can't make it inline"
    4.27 +		    (car spec)))
    4.28 +	       (put (car spec) 'byte-optimizer 'byte-compile-inline-expand)))))
    4.29  	((eq (car-safe spec) 'notinline)
    4.30  	 (while (setq spec (cdr spec))
    4.31 -	   (if (eq (get (car spec) 'byte-optimizer)
    4.32 -		   'byte-compile-inline-expand)
    4.33 -	       (put (car spec) 'byte-optimizer nil))))
    4.34 -
    4.35 +	   (let ((assq (cdr (assq (car spec) byte-compile-macro-environment))))
    4.36 +	     (if (and (consp assq) (eq (nth 1 (nth 1 assq)) 'cl-labels-args)
    4.37 +		      (atom (setq assq (nth 2 (nth 2 assq)))))
    4.38 +		 ;; It's a label, and we're using the labels
    4.39 +		 ;; implementation in bytecomp.el. Tell the compiler
    4.40 +		 ;; not to inline it.
    4.41 +                 (if (eq 'byte-compile-inline-expand
    4.42 +                         (getf (aref (compiled-function-constants assq) 0)
    4.43 +                               'byte-optimizer))
    4.44 +                     (remf (aref (compiled-function-constants assq) 0)
    4.45 +                           'byte-optimizer))
    4.46 +	       (if (eq (get (car spec) 'byte-optimizer)
    4.47 +		       'byte-compile-inline-expand)
    4.48 +		   (put (car spec) 'byte-optimizer nil))))))
    4.49  	((eq (car-safe spec) 'optimize)
    4.50  	 (let ((speed (assq (nth 1 (assq 'speed (cdr spec)))
    4.51  			    '((0 . nil) (1 . t) (2 . t) (3 . t))))
    4.52 @@ -2014,14 +2034,8 @@
    4.53  
    4.54  ;;;###autoload
    4.55  (defmacro declare (&rest specs)
    4.56 -  (if (cl-compiling-file)
    4.57 -      (while specs
    4.58 -	(if (listp cl-declare-stack) (push (car specs) cl-declare-stack))
    4.59 -	(cl-do-proclaim (pop specs) nil)))
    4.60    nil)
    4.61  
    4.62 -
    4.63 -
    4.64  ;;; Generalized variables.
    4.65  
    4.66  ;;;###autoload
     5.1 --- a/tests/ChangeLog	Sun Sep 25 16:12:07 2011 +0100
     5.2 +++ b/tests/ChangeLog	Sun Oct 02 15:32:16 2011 +0100
     5.3 @@ -1,3 +1,9 @@
     5.4 +2011-10-02  Aidan Kehoe  <kehoea@parhasard.net>
     5.5 +
     5.6 +	* automated/lisp-tests.el:
     5.7 +	* automated/lisp-tests.el (+):
     5.8 +	Test #'labels and inlining.
     5.9 +
    5.10  2011-09-04  Aidan Kehoe  <kehoea@parhasard.net>
    5.11  
    5.12  	* automated/lisp-reader-tests.el:
     6.1 --- a/tests/automated/lisp-tests.el	Sun Sep 25 16:12:07 2011 +0100
     6.2 +++ b/tests/automated/lisp-tests.el	Sun Oct 02 15:32:16 2011 +0100
     6.3 @@ -2939,4 +2939,50 @@
     6.4    (Check-Error wrong-number-of-arguments (apply-partially))
     6.5    (Assert (equal (funcall construct-list) '(5 6 7))))
     6.6  
     6.7 +;; Test labels and inlining.
     6.8 +(labels
     6.9 +    ((+ (&rest arguments)
    6.10 +       ;; Shades of Java, hah.
    6.11 +       (mapconcat #'prin1-to-string arguments ", "))
    6.12 +     (print-with-commas (stream one two three four five)
    6.13 +       (princ (+ one two three four five) stream))
    6.14 +     (bookend (open close &rest arguments)
    6.15 +       (refer-to-bookend (concat open (apply #'+ arguments) close)))
    6.16 +     (refer-to-bookend (string)
    6.17 +       (bookend "[" "]" string "hello" "there")))
    6.18 +  (declare (inline + print-with-commas bookend refer-to-bookend))
    6.19 +  (macrolet
    6.20 +      ((with-first-arguments (&optional form)
    6.21 +        (append form (list 1 [hi there] 40 "this is a string" pi)))
    6.22 +       (with-second-arguments (&optional form)
    6.23 +         (append form (list pi e ''hello ''there [40 50 60])))
    6.24 +       (with-both-arguments (&optional form)
    6.25 +         (append form
    6.26 +                 (macroexpand '(with-first-arguments))
    6.27 +                 (macroexpand '(with-second-arguments)))))
    6.28 +
    6.29 +    (with-temp-buffer
    6.30 +      (Assert
    6.31 +       (equal
    6.32 +        (mapconcat #'prin1-to-string (with-first-arguments (list)) ", ")
    6.33 +        (with-first-arguments (print-with-commas (current-buffer))))
    6.34 +     "checking print-with-commas gives the expected result")
    6.35 +      (Assert
    6.36 +       (or
    6.37 +        (not (compiled-function-p (indirect-function #'print-with-commas)))
    6.38 +        (notany #'compiled-function-p
    6.39 +                (compiled-function-constants
    6.40 +                 (indirect-function #'print-with-commas))))
    6.41 +       "checking the label + was inlined correctly")
    6.42 +      (insert ", ")
    6.43 +      ;; This call to + will be inline in compiled code, but there's
    6.44 +      ;; no easy way for us to check that:
    6.45 +      (Assert (null (insert (with-second-arguments (+)))))
    6.46 +      (Assert (equal
    6.47 +               (mapconcat #'prin1-to-string (with-both-arguments (list)) ", ")
    6.48 +               (buffer-string))
    6.49 +              "checking the buffer contents are as expected at the end.")
    6.50 +      (Assert (not (funcall (intern "eq") #'bookend #'refer-to-bookend))
    6.51 +	      "checking two mutually recursive functions compiled OK"))))
    6.52 +
    6.53  ;;; end of lisp-tests.el