Mercurial > emacs
annotate lisp/emacs-lisp/byte-opt.el @ 13666:e97eebb1206d
(../info/cc-mode, cc-mode.dvi): New targets.
(INFO_TARGETS): Add ../info/cc-mode.
(DVI_TARGETS): Add cc-mode.dvi.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 30 Nov 1995 01:11:23 +0000 |
parents | 9154b9967e2b |
children | 10c76db77107 |
rev | line source |
---|---|
848 | 1 ;;; byte-opt.el --- the optimization passes of the emacs-lisp byte compiler. |
2 | |
7298 | 3 ;;; Copyright (c) 1991, 1994 Free Software Foundation, Inc. |
848 | 4 |
5 ;; Author: Jamie Zawinski <jwz@lucid.com> | |
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no> | |
7 ;; Keywords: internal | |
757 | 8 |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
848 | 13 ;; the Free Software Foundation; either version 2, or (at your option) |
757 | 14 ;; any later version. |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | |
848 | 25 ;;; Commentary: |
26 | |
757 | 27 ;;; ======================================================================== |
28 ;;; "No matter how hard you try, you can't make a racehorse out of a pig. | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
29 ;;; You can, however, make a faster pig." |
757 | 30 ;;; |
31 ;;; Or, to put it another way, the emacs byte compiler is a VW Bug. This code | |
32 ;;; makes it be a VW Bug with fuel injection and a turbocharger... You're | |
33 ;;; still not going to make it go faster than 70 mph, but it might be easier | |
34 ;;; to get it there. | |
35 ;;; | |
36 | |
37 ;;; TO DO: | |
38 ;;; | |
39 ;;; (apply '(lambda (x &rest y) ...) 1 (foo)) | |
40 ;;; | |
41 ;;; maintain a list of functions known not to access any global variables | |
42 ;;; (actually, give them a 'dynamically-safe property) and then | |
43 ;;; (let ( v1 v2 ... vM vN ) <...dynamically-safe...> ) ==> | |
44 ;;; (let ( v1 v2 ... vM ) vN <...dynamically-safe...> ) | |
45 ;;; by recursing on this, we might be able to eliminate the entire let. | |
46 ;;; However certain variables should never have their bindings optimized | |
47 ;;; away, because they affect everything. | |
48 ;;; (put 'debug-on-error 'binding-is-magic t) | |
49 ;;; (put 'debug-on-abort 'binding-is-magic t) | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
50 ;;; (put 'debug-on-next-call 'binding-is-magic t) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
51 ;;; (put 'mocklisp-arguments 'binding-is-magic t) |
757 | 52 ;;; (put 'inhibit-quit 'binding-is-magic t) |
53 ;;; (put 'quit-flag 'binding-is-magic t) | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
54 ;;; (put 't 'binding-is-magic t) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
55 ;;; (put 'nil 'binding-is-magic t) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
56 ;;; possibly also |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
57 ;;; (put 'gc-cons-threshold 'binding-is-magic t) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
58 ;;; (put 'track-mouse 'binding-is-magic t) |
757 | 59 ;;; others? |
60 ;;; | |
61 ;;; Simple defsubsts often produce forms like | |
62 ;;; (let ((v1 (f1)) (v2 (f2)) ...) | |
63 ;;; (FN v1 v2 ...)) | |
64 ;;; It would be nice if we could optimize this to | |
65 ;;; (FN (f1) (f2) ...) | |
66 ;;; but we can't unless FN is dynamically-safe (it might be dynamically | |
67 ;;; referring to the bindings that the lambda arglist established.) | |
68 ;;; One of the uncountable lossages introduced by dynamic scope... | |
69 ;;; | |
70 ;;; Maybe there should be a control-structure that says "turn on | |
71 ;;; fast-and-loose type-assumptive optimizations here." Then when | |
72 ;;; we see a form like (car foo) we can from then on assume that | |
73 ;;; the variable foo is of type cons, and optimize based on that. | |
74 ;;; But, this won't win much because of (you guessed it) dynamic | |
75 ;;; scope. Anything down the stack could change the value. | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
76 ;;; (Another reason it doesn't work is that it is perfectly valid |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
77 ;;; to call car with a null argument.) A better approach might |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
78 ;;; be to allow type-specification of the form |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
79 ;;; (put 'foo 'arg-types '(float (list integer) dynamic)) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
80 ;;; (put 'foo 'result-type 'bool) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
81 ;;; It should be possible to have these types checked to a certain |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
82 ;;; degree. |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
83 ;;; |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
84 ;;; collapse common subexpressions |
757 | 85 ;;; |
86 ;;; It would be nice if redundant sequences could be factored out as well, | |
87 ;;; when they are known to have no side-effects: | |
88 ;;; (list (+ a b c) (+ a b c)) --> a b add c add dup list-2 | |
89 ;;; but beware of traps like | |
90 ;;; (cons (list x y) (list x y)) | |
91 ;;; | |
848 | 92 ;;; Tail-recursion elimination is not really possible in Emacs Lisp. |
93 ;;; Tail-recursion elimination is almost always impossible when all variables | |
94 ;;; have dynamic scope, but given that the "return" byteop requires the | |
95 ;;; binding stack to be empty (rather than emptying it itself), there can be | |
96 ;;; no truly tail-recursive Emacs Lisp functions that take any arguments or | |
97 ;;; make any bindings. | |
757 | 98 ;;; |
848 | 99 ;;; Here is an example of an Emacs Lisp function which could safely be |
757 | 100 ;;; byte-compiled tail-recursively: |
101 ;;; | |
102 ;;; (defun tail-map (fn list) | |
103 ;;; (cond (list | |
104 ;;; (funcall fn (car list)) | |
105 ;;; (tail-map fn (cdr list))))) | |
106 ;;; | |
107 ;;; However, if there was even a single let-binding around the COND, | |
108 ;;; it could not be byte-compiled, because there would be an "unbind" | |
109 ;;; byte-op between the final "call" and "return." Adding a | |
110 ;;; Bunbind_all byteop would fix this. | |
111 ;;; | |
112 ;;; (defun foo (x y z) ... (foo a b c)) | |
113 ;;; ... (const foo) (varref a) (varref b) (varref c) (call 3) END: (return) | |
114 ;;; ... (varref a) (varbind x) (varref b) (varbind y) (varref c) (varbind z) (goto 0) END: (unbind-all) (return) | |
115 ;;; ... (varref a) (varset x) (varref b) (varset y) (varref c) (varset z) (goto 0) END: (return) | |
116 ;;; | |
117 ;;; this also can be considered tail recursion: | |
118 ;;; | |
119 ;;; ... (const foo) (varref a) (call 1) (goto X) ... X: (return) | |
120 ;;; could generalize this by doing the optimization | |
121 ;;; (goto X) ... X: (return) --> (return) | |
122 ;;; | |
123 ;;; But this doesn't solve all of the problems: although by doing tail- | |
124 ;;; recursion elimination in this way, the call-stack does not grow, the | |
125 ;;; binding-stack would grow with each recursive step, and would eventually | |
126 ;;; overflow. I don't believe there is any way around this without lexical | |
127 ;;; scope. | |
128 ;;; | |
848 | 129 ;;; Wouldn't it be nice if Emacs Lisp had lexical scope. |
757 | 130 ;;; |
131 ;;; Idea: the form (lexical-scope) in a file means that the file may be | |
132 ;;; compiled lexically. This proclamation is file-local. Then, within | |
133 ;;; that file, "let" would establish lexical bindings, and "let-dynamic" | |
134 ;;; would do things the old way. (Or we could use CL "declare" forms.) | |
135 ;;; We'd have to notice defvars and defconsts, since those variables should | |
136 ;;; always be dynamic, and attempting to do a lexical binding of them | |
137 ;;; should simply do a dynamic binding instead. | |
138 ;;; But! We need to know about variables that were not necessarily defvarred | |
139 ;;; in the file being compiled (doing a boundp check isn't good enough.) | |
140 ;;; Fdefvar() would have to be modified to add something to the plist. | |
141 ;;; | |
142 ;;; A major disadvantage of this scheme is that the interpreter and compiler | |
143 ;;; would have different semantics for files compiled with (dynamic-scope). | |
144 ;;; Since this would be a file-local optimization, there would be no way to | |
145 ;;; modify the interpreter to obey this (unless the loader was hacked | |
146 ;;; in some grody way, but that's a really bad idea.) | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
147 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
148 ;; Other things to consider: |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
149 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
150 ;;;;; Associative math should recognize subcalls to identical function: |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
151 ;;;(disassemble (lambda (x) (+ (+ (foo) 1) (+ (bar) 2)))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
152 ;;;;; This should generate the same as (1+ x) and (1- x) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
153 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
154 ;;;(disassemble (lambda (x) (cons (+ x 1) (- x 1)))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
155 ;;;;; An awful lot of functions always return a non-nil value. If they're |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
156 ;;;;; error free also they may act as true-constants. |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
157 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
158 ;;;(disassemble (lambda (x) (and (point) (foo)))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
159 ;;;;; When |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
160 ;;;;; - all but one arguments to a function are constant |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
161 ;;;;; - the non-constant argument is an if-expression (cond-expression?) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
162 ;;;;; then the outer function can be distributed. If the guarding |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
163 ;;;;; condition is side-effect-free [assignment-free] then the other |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
164 ;;;;; arguments may be any expressions. Since, however, the code size |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
165 ;;;;; can increase this way they should be "simple". Compare: |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
166 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
167 ;;;(disassemble (lambda (x) (eq (if (point) 'a 'b) 'c))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
168 ;;;(disassemble (lambda (x) (if (point) (eq 'a 'c) (eq 'b 'c)))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
169 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
170 ;;;;; (car (cons A B)) -> (progn B A) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
171 ;;;(disassemble (lambda (x) (car (cons (foo) 42)))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
172 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
173 ;;;;; (cdr (cons A B)) -> (progn A B) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
174 ;;;(disassemble (lambda (x) (cdr (cons 42 (foo))))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
175 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
176 ;;;;; (car (list A B ...)) -> (progn B ... A) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
177 ;;;(disassemble (lambda (x) (car (list (foo) 42 (bar))))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
178 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
179 ;;;;; (cdr (list A B ...)) -> (progn A (list B ...)) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
180 ;;;(disassemble (lambda (x) (cdr (list 42 (foo) (bar))))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
181 |
757 | 182 |
848 | 183 ;;; Code: |
757 | 184 |
185 (defun byte-compile-log-lap-1 (format &rest args) | |
186 (if (aref byte-code-vector 0) | |
187 (error "The old version of the disassembler is loaded. Reload new-bytecomp as well.")) | |
188 (byte-compile-log-1 | |
189 (apply 'format format | |
190 (let (c a) | |
191 (mapcar '(lambda (arg) | |
192 (if (not (consp arg)) | |
193 (if (and (symbolp arg) | |
194 (string-match "^byte-" (symbol-name arg))) | |
195 (intern (substring (symbol-name arg) 5)) | |
196 arg) | |
197 (if (integerp (setq c (car arg))) | |
198 (error "non-symbolic byte-op %s" c)) | |
199 (if (eq c 'TAG) | |
200 (setq c arg) | |
201 (setq a (cond ((memq c byte-goto-ops) | |
202 (car (cdr (cdr arg)))) | |
203 ((memq c byte-constref-ops) | |
204 (car (cdr arg))) | |
205 (t (cdr arg)))) | |
206 (setq c (symbol-name c)) | |
207 (if (string-match "^byte-." c) | |
208 (setq c (intern (substring c 5))))) | |
209 (if (eq c 'constant) (setq c 'const)) | |
210 (if (and (eq (cdr arg) 0) | |
211 (not (memq c '(unbind call const)))) | |
212 c | |
213 (format "(%s %s)" c a)))) | |
214 args))))) | |
215 | |
216 (defmacro byte-compile-log-lap (format-string &rest args) | |
217 (list 'and | |
218 '(memq byte-optimize-log '(t byte)) | |
219 (cons 'byte-compile-log-lap-1 | |
220 (cons format-string args)))) | |
221 | |
222 | |
223 ;;; byte-compile optimizers to support inlining | |
224 | |
225 (put 'inline 'byte-optimizer 'byte-optimize-inline-handler) | |
226 | |
227 (defun byte-optimize-inline-handler (form) | |
228 "byte-optimize-handler for the `inline' special-form." | |
229 (cons 'progn | |
230 (mapcar | |
231 '(lambda (sexp) | |
232 (let ((fn (car-safe sexp))) | |
233 (if (and (symbolp fn) | |
234 (or (cdr (assq fn byte-compile-function-environment)) | |
235 (and (fboundp fn) | |
236 (not (or (cdr (assq fn byte-compile-macro-environment)) | |
237 (and (consp (setq fn (symbol-function fn))) | |
238 (eq (car fn) 'macro)) | |
239 (subrp fn)))))) | |
240 (byte-compile-inline-expand sexp) | |
241 sexp))) | |
242 (cdr form)))) | |
243 | |
244 | |
767
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
245 ;; Splice the given lap code into the current instruction stream. |
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
246 ;; If it has any labels in it, you're responsible for making sure there |
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
247 ;; are no collisions, and that byte-compile-tag-number is reasonable |
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
248 ;; after this is spliced in. The provided list is destroyed. |
757 | 249 (defun byte-inline-lapcode (lap) |
250 (setq byte-compile-output (nconc (nreverse lap) byte-compile-output))) | |
251 | |
252 | |
253 (defun byte-compile-inline-expand (form) | |
254 (let* ((name (car form)) | |
255 (fn (or (cdr (assq name byte-compile-function-environment)) | |
256 (and (fboundp name) (symbol-function name))))) | |
257 (if (null fn) | |
258 (progn | |
259 (byte-compile-warn "attempt to inline %s before it was defined" name) | |
260 form) | |
261 ;; else | |
262 (if (and (consp fn) (eq (car fn) 'autoload)) | |
263 (load (nth 1 fn))) | |
264 (if (and (consp fn) (eq (car fn) 'autoload)) | |
265 (error "file \"%s\" didn't define \"%s\"" (nth 1 fn) name)) | |
266 (if (symbolp fn) | |
267 (byte-compile-inline-expand (cons fn (cdr form))) | |
1818
7e3322619e46
compiled-function-p has been renamed to byte-code-function-p.
Jim Blandy <jimb@redhat.com>
parents:
957
diff
changeset
|
268 (if (byte-code-function-p fn) |
11203
31b8e30c1afb
(byte-compile-inline-expand): Fetch actual bytecode
Karl Heuer <kwzh@gnu.org>
parents:
8466
diff
changeset
|
269 (progn |
31b8e30c1afb
(byte-compile-inline-expand): Fetch actual bytecode
Karl Heuer <kwzh@gnu.org>
parents:
8466
diff
changeset
|
270 (fetch-bytecode fn) |
31b8e30c1afb
(byte-compile-inline-expand): Fetch actual bytecode
Karl Heuer <kwzh@gnu.org>
parents:
8466
diff
changeset
|
271 (cons (list 'lambda (aref fn 0) |
31b8e30c1afb
(byte-compile-inline-expand): Fetch actual bytecode
Karl Heuer <kwzh@gnu.org>
parents:
8466
diff
changeset
|
272 (list 'byte-code (aref fn 1) (aref fn 2) (aref fn 3))) |
31b8e30c1afb
(byte-compile-inline-expand): Fetch actual bytecode
Karl Heuer <kwzh@gnu.org>
parents:
8466
diff
changeset
|
273 (cdr form))) |
757 | 274 (if (not (eq (car fn) 'lambda)) (error "%s is not a lambda" name)) |
275 (cons fn (cdr form))))))) | |
276 | |
277 ;;; ((lambda ...) ...) | |
278 ;;; | |
279 (defun byte-compile-unfold-lambda (form &optional name) | |
280 (or name (setq name "anonymous lambda")) | |
281 (let ((lambda (car form)) | |
282 (values (cdr form))) | |
1818
7e3322619e46
compiled-function-p has been renamed to byte-code-function-p.
Jim Blandy <jimb@redhat.com>
parents:
957
diff
changeset
|
283 (if (byte-code-function-p lambda) |
957 | 284 (setq lambda (list 'lambda (aref lambda 0) |
285 (list 'byte-code (aref lambda 1) | |
286 (aref lambda 2) (aref lambda 3))))) | |
757 | 287 (let ((arglist (nth 1 lambda)) |
288 (body (cdr (cdr lambda))) | |
289 optionalp restp | |
290 bindings) | |
291 (if (and (stringp (car body)) (cdr body)) | |
292 (setq body (cdr body))) | |
293 (if (and (consp (car body)) (eq 'interactive (car (car body)))) | |
294 (setq body (cdr body))) | |
295 (while arglist | |
296 (cond ((eq (car arglist) '&optional) | |
297 ;; ok, I'll let this slide because funcall_lambda() does... | |
298 ;; (if optionalp (error "multiple &optional keywords in %s" name)) | |
299 (if restp (error "&optional found after &rest in %s" name)) | |
300 (if (null (cdr arglist)) | |
301 (error "nothing after &optional in %s" name)) | |
302 (setq optionalp t)) | |
303 ((eq (car arglist) '&rest) | |
304 ;; ...but it is by no stretch of the imagination a reasonable | |
305 ;; thing that funcall_lambda() allows (&rest x y) and | |
306 ;; (&rest x &optional y) in arglists. | |
307 (if (null (cdr arglist)) | |
308 (error "nothing after &rest in %s" name)) | |
309 (if (cdr (cdr arglist)) | |
310 (error "multiple vars after &rest in %s" name)) | |
311 (setq restp t)) | |
312 (restp | |
313 (setq bindings (cons (list (car arglist) | |
314 (and values (cons 'list values))) | |
315 bindings) | |
316 values nil)) | |
317 ((and (not optionalp) (null values)) | |
318 (byte-compile-warn "attempt to open-code %s with too few arguments" name) | |
319 (setq arglist nil values 'too-few)) | |
320 (t | |
321 (setq bindings (cons (list (car arglist) (car values)) | |
322 bindings) | |
323 values (cdr values)))) | |
324 (setq arglist (cdr arglist))) | |
325 (if values | |
326 (progn | |
327 (or (eq values 'too-few) | |
328 (byte-compile-warn | |
329 "attempt to open-code %s with too many arguments" name)) | |
330 form) | |
331 (let ((newform | |
332 (if bindings | |
333 (cons 'let (cons (nreverse bindings) body)) | |
334 (cons 'progn body)))) | |
335 (byte-compile-log " %s\t==>\t%s" form newform) | |
336 newform))))) | |
337 | |
338 | |
339 ;;; implementing source-level optimizers | |
340 | |
341 (defun byte-optimize-form-code-walker (form for-effect) | |
342 ;; | |
343 ;; For normal function calls, We can just mapcar the optimizer the cdr. But | |
344 ;; we need to have special knowledge of the syntax of the special forms | |
345 ;; like let and defun (that's why they're special forms :-). (Actually, | |
346 ;; the important aspect is that they are subrs that don't evaluate all of | |
347 ;; their args.) | |
348 ;; | |
349 (let ((fn (car-safe form)) | |
350 tmp) | |
351 (cond ((not (consp form)) | |
352 (if (not (and for-effect | |
353 (or byte-compile-delete-errors | |
354 (not (symbolp form)) | |
355 (eq form t)))) | |
356 form)) | |
357 ((eq fn 'quote) | |
358 (if (cdr (cdr form)) | |
359 (byte-compile-warn "malformed quote form: %s" | |
360 (prin1-to-string form))) | |
361 ;; map (quote nil) to nil to simplify optimizer logic. | |
362 ;; map quoted constants to nil if for-effect (just because). | |
363 (and (nth 1 form) | |
364 (not for-effect) | |
365 form)) | |
1818
7e3322619e46
compiled-function-p has been renamed to byte-code-function-p.
Jim Blandy <jimb@redhat.com>
parents:
957
diff
changeset
|
366 ((or (byte-code-function-p fn) |
757 | 367 (eq 'lambda (car-safe fn))) |
368 (byte-compile-unfold-lambda form)) | |
369 ((memq fn '(let let*)) | |
370 ;; recursively enter the optimizer for the bindings and body | |
371 ;; of a let or let*. This for depth-firstness: forms that | |
372 ;; are more deeply nested are optimized first. | |
373 (cons fn | |
374 (cons | |
375 (mapcar '(lambda (binding) | |
376 (if (symbolp binding) | |
377 binding | |
378 (if (cdr (cdr binding)) | |
379 (byte-compile-warn "malformed let binding: %s" | |
380 (prin1-to-string binding))) | |
381 (list (car binding) | |
382 (byte-optimize-form (nth 1 binding) nil)))) | |
383 (nth 1 form)) | |
384 (byte-optimize-body (cdr (cdr form)) for-effect)))) | |
385 ((eq fn 'cond) | |
386 (cons fn | |
387 (mapcar '(lambda (clause) | |
388 (if (consp clause) | |
389 (cons | |
390 (byte-optimize-form (car clause) nil) | |
391 (byte-optimize-body (cdr clause) for-effect)) | |
392 (byte-compile-warn "malformed cond form: %s" | |
393 (prin1-to-string clause)) | |
394 clause)) | |
395 (cdr form)))) | |
396 ((eq fn 'progn) | |
397 ;; as an extra added bonus, this simplifies (progn <x>) --> <x> | |
398 (if (cdr (cdr form)) | |
399 (progn | |
400 (setq tmp (byte-optimize-body (cdr form) for-effect)) | |
401 (if (cdr tmp) (cons 'progn tmp) (car tmp))) | |
402 (byte-optimize-form (nth 1 form) for-effect))) | |
403 ((eq fn 'prog1) | |
404 (if (cdr (cdr form)) | |
405 (cons 'prog1 | |
406 (cons (byte-optimize-form (nth 1 form) for-effect) | |
407 (byte-optimize-body (cdr (cdr form)) t))) | |
408 (byte-optimize-form (nth 1 form) for-effect))) | |
409 ((eq fn 'prog2) | |
410 (cons 'prog2 | |
411 (cons (byte-optimize-form (nth 1 form) t) | |
412 (cons (byte-optimize-form (nth 2 form) for-effect) | |
413 (byte-optimize-body (cdr (cdr (cdr form))) t))))) | |
414 | |
415 ((memq fn '(save-excursion save-restriction)) | |
416 ;; those subrs which have an implicit progn; it's not quite good | |
417 ;; enough to treat these like normal function calls. | |
418 ;; This can turn (save-excursion ...) into (save-excursion) which | |
419 ;; will be optimized away in the lap-optimize pass. | |
420 (cons fn (byte-optimize-body (cdr form) for-effect))) | |
421 | |
422 ((eq fn 'with-output-to-temp-buffer) | |
423 ;; this is just like the above, except for the first argument. | |
424 (cons fn | |
425 (cons | |
426 (byte-optimize-form (nth 1 form) nil) | |
427 (byte-optimize-body (cdr (cdr form)) for-effect)))) | |
428 | |
429 ((eq fn 'if) | |
430 (cons fn | |
431 (cons (byte-optimize-form (nth 1 form) nil) | |
432 (cons | |
433 (byte-optimize-form (nth 2 form) for-effect) | |
434 (byte-optimize-body (nthcdr 3 form) for-effect))))) | |
435 | |
436 ((memq fn '(and or)) ; remember, and/or are control structures. | |
437 ;; take forms off the back until we can't any more. | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3138
diff
changeset
|
438 ;; In the future it could conceivably be a problem that the |
757 | 439 ;; subexpressions of these forms are optimized in the reverse |
440 ;; order, but it's ok for now. | |
441 (if for-effect | |
442 (let ((backwards (reverse (cdr form)))) | |
443 (while (and backwards | |
444 (null (setcar backwards | |
445 (byte-optimize-form (car backwards) | |
446 for-effect)))) | |
447 (setq backwards (cdr backwards))) | |
448 (if (and (cdr form) (null backwards)) | |
449 (byte-compile-log | |
450 " all subforms of %s called for effect; deleted" form)) | |
451 (and backwards | |
452 (cons fn (nreverse backwards)))) | |
453 (cons fn (mapcar 'byte-optimize-form (cdr form))))) | |
454 | |
455 ((eq fn 'interactive) | |
456 (byte-compile-warn "misplaced interactive spec: %s" | |
457 (prin1-to-string form)) | |
458 nil) | |
459 | |
460 ((memq fn '(defun defmacro function | |
461 condition-case save-window-excursion)) | |
462 ;; These forms are compiled as constants or by breaking out | |
463 ;; all the subexpressions and compiling them separately. | |
464 form) | |
465 | |
466 ((eq fn 'unwind-protect) | |
467 ;; the "protected" part of an unwind-protect is compiled (and thus | |
468 ;; optimized) as a top-level form, so don't do it here. But the | |
469 ;; non-protected part has the same for-effect status as the | |
470 ;; unwind-protect itself. (The protected part is always for effect, | |
471 ;; but that isn't handled properly yet.) | |
472 (cons fn | |
473 (cons (byte-optimize-form (nth 1 form) for-effect) | |
474 (cdr (cdr form))))) | |
475 | |
476 ((eq fn 'catch) | |
477 ;; the body of a catch is compiled (and thus optimized) as a | |
478 ;; top-level form, so don't do it here. The tag is never | |
479 ;; for-effect. The body should have the same for-effect status | |
480 ;; as the catch form itself, but that isn't handled properly yet. | |
481 (cons fn | |
482 (cons (byte-optimize-form (nth 1 form) nil) | |
483 (cdr (cdr form))))) | |
484 | |
485 ;; If optimization is on, this is the only place that macros are | |
486 ;; expanded. If optimization is off, then macroexpansion happens | |
487 ;; in byte-compile-form. Otherwise, the macros are already expanded | |
488 ;; by the time that is reached. | |
489 ((not (eq form | |
490 (setq form (macroexpand form | |
491 byte-compile-macro-environment)))) | |
492 (byte-optimize-form form for-effect)) | |
493 | |
494 ((not (symbolp fn)) | |
495 (or (eq 'mocklisp (car-safe fn)) ; ha! | |
496 (byte-compile-warn "%s is a malformed function" | |
497 (prin1-to-string fn))) | |
498 form) | |
499 | |
500 ((and for-effect (setq tmp (get fn 'side-effect-free)) | |
501 (or byte-compile-delete-errors | |
502 (eq tmp 'error-free) | |
503 (progn | |
504 (byte-compile-warn "%s called for effect" | |
505 (prin1-to-string form)) | |
506 nil))) | |
507 (byte-compile-log " %s called for effect; deleted" fn) | |
508 ;; appending a nil here might not be necessary, but it can't hurt. | |
509 (byte-optimize-form | |
510 (cons 'progn (append (cdr form) '(nil))) t)) | |
511 | |
512 (t | |
513 ;; Otherwise, no args can be considered to be for-effect, | |
514 ;; even if the called function is for-effect, because we | |
515 ;; don't know anything about that function. | |
516 (cons fn (mapcar 'byte-optimize-form (cdr form))))))) | |
517 | |
518 | |
519 (defun byte-optimize-form (form &optional for-effect) | |
520 "The source-level pass of the optimizer." | |
521 ;; | |
522 ;; First, optimize all sub-forms of this one. | |
523 (setq form (byte-optimize-form-code-walker form for-effect)) | |
524 ;; | |
525 ;; after optimizing all subforms, optimize this form until it doesn't | |
526 ;; optimize any further. This means that some forms will be passed through | |
527 ;; the optimizer many times, but that's necessary to make the for-effect | |
528 ;; processing do as much as possible. | |
529 ;; | |
530 (let (opt new) | |
531 (if (and (consp form) | |
532 (symbolp (car form)) | |
533 (or (and for-effect | |
534 ;; we don't have any of these yet, but we might. | |
535 (setq opt (get (car form) 'byte-for-effect-optimizer))) | |
536 (setq opt (get (car form) 'byte-optimizer))) | |
537 (not (eq form (setq new (funcall opt form))))) | |
538 (progn | |
539 ;; (if (equal form new) (error "bogus optimizer -- %s" opt)) | |
540 (byte-compile-log " %s\t==>\t%s" form new) | |
541 (setq new (byte-optimize-form new for-effect)) | |
542 new) | |
543 form))) | |
544 | |
545 | |
546 (defun byte-optimize-body (forms all-for-effect) | |
547 ;; optimize the cdr of a progn or implicit progn; all forms is a list of | |
548 ;; forms, all but the last of which are optimized with the assumption that | |
549 ;; they are being called for effect. the last is for-effect as well if | |
550 ;; all-for-effect is true. returns a new list of forms. | |
551 (let ((rest forms) | |
552 (result nil) | |
553 fe new) | |
554 (while rest | |
555 (setq fe (or all-for-effect (cdr rest))) | |
556 (setq new (and (car rest) (byte-optimize-form (car rest) fe))) | |
557 (if (or new (not fe)) | |
558 (setq result (cons new result))) | |
559 (setq rest (cdr rest))) | |
560 (nreverse result))) | |
561 | |
562 | |
563 ;;; some source-level optimizers | |
564 ;;; | |
565 ;;; when writing optimizers, be VERY careful that the optimizer returns | |
566 ;;; something not EQ to its argument if and ONLY if it has made a change. | |
567 ;;; This implies that you cannot simply destructively modify the list; | |
568 ;;; you must return something not EQ to it if you make an optimization. | |
569 ;;; | |
570 ;;; It is now safe to optimize code such that it introduces new bindings. | |
571 | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3138
diff
changeset
|
572 ;; I'd like this to be a defsubst, but let's not be self-referential... |
757 | 573 (defmacro byte-compile-trueconstp (form) |
574 ;; Returns non-nil if FORM is a non-nil constant. | |
575 (` (cond ((consp (, form)) (eq (car (, form)) 'quote)) | |
576 ((not (symbolp (, form)))) | |
577 ((eq (, form) t))))) | |
578 | |
767
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
579 ;; If the function is being called with constant numeric args, |
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
580 ;; evaluate as much as possible at compile-time. This optimizer |
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
581 ;; assumes that the function is associative, like + or *. |
757 | 582 (defun byte-optimize-associative-math (form) |
583 (let ((args nil) | |
584 (constants nil) | |
585 (rest (cdr form))) | |
586 (while rest | |
587 (if (numberp (car rest)) | |
588 (setq constants (cons (car rest) constants)) | |
589 (setq args (cons (car rest) args))) | |
590 (setq rest (cdr rest))) | |
591 (if (cdr constants) | |
592 (if args | |
593 (list (car form) | |
594 (apply (car form) constants) | |
595 (if (cdr args) | |
596 (cons (car form) (nreverse args)) | |
597 (car args))) | |
598 (apply (car form) constants)) | |
599 form))) | |
600 | |
767
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
601 ;; If the function is being called with constant numeric args, |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
602 ;; evaluate as much as possible at compile-time. This optimizer |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
603 ;; assumes that the function satisfies |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
604 ;; (op x1 x2 ... xn) == (op ...(op (op x1 x2) x3) ...xn) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
605 ;; like - and /. |
757 | 606 (defun byte-optimize-nonassociative-math (form) |
607 (if (or (not (numberp (car (cdr form)))) | |
608 (not (numberp (car (cdr (cdr form)))))) | |
609 form | |
610 (let ((constant (car (cdr form))) | |
611 (rest (cdr (cdr form)))) | |
612 (while (numberp (car rest)) | |
613 (setq constant (funcall (car form) constant (car rest)) | |
614 rest (cdr rest))) | |
615 (if rest | |
616 (cons (car form) (cons constant rest)) | |
617 constant)))) | |
618 | |
619 ;;(defun byte-optimize-associative-two-args-math (form) | |
620 ;; (setq form (byte-optimize-associative-math form)) | |
621 ;; (if (consp form) | |
622 ;; (byte-optimize-two-args-left form) | |
623 ;; form)) | |
624 | |
625 ;;(defun byte-optimize-nonassociative-two-args-math (form) | |
626 ;; (setq form (byte-optimize-nonassociative-math form)) | |
627 ;; (if (consp form) | |
628 ;; (byte-optimize-two-args-right form) | |
629 ;; form)) | |
630 | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
631 (defun byte-optimize-approx-equal (x y) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
632 (< (* (abs (- x y)) 100) (abs (+ x y)))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
633 |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
634 ;; Collect all the constants from FORM, after the STARTth arg, |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
635 ;; and apply FUN to them to make one argument at the end. |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
636 ;; For functions that can handle floats, that optimization |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
637 ;; can be incorrect because reordering can cause an overflow |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
638 ;; that would otherwise be avoided by encountering an arg that is a float. |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
639 ;; We avoid this problem by (1) not moving float constants and |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
640 ;; (2) not moving anything if it would cause an overflow. |
757 | 641 (defun byte-optimize-delay-constants-math (form start fun) |
642 ;; Merge all FORM's constants from number START, call FUN on them | |
643 ;; and put the result at the end. | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
644 (let ((rest (nthcdr (1- start) form)) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
645 (orig form) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
646 ;; t means we must check for overflow. |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
647 (overflow (memq fun '(+ *)))) |
757 | 648 (while (cdr (setq rest (cdr rest))) |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
649 (if (integerp (car rest)) |
757 | 650 (let (constants) |
651 (setq form (copy-sequence form) | |
652 rest (nthcdr (1- start) form)) | |
653 (while (setq rest (cdr rest)) | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
654 (cond ((integerp (car rest)) |
757 | 655 (setq constants (cons (car rest) constants)) |
656 (setcar rest nil)))) | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
657 ;; If necessary, check now for overflow |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
658 ;; that might be caused by reordering. |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
659 (if (and overflow |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
660 ;; We have overflow if the result of doing the arithmetic |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
661 ;; on floats is not even close to the result |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
662 ;; of doing it on integers. |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
663 (not (byte-optimize-approx-equal |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
664 (apply fun (mapcar 'float constants)) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
665 (float (apply fun constants))))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
666 (setq form orig) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
667 (setq form (nconc (delq nil form) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
668 (list (apply fun (nreverse constants))))))))) |
757 | 669 form)) |
670 | |
671 (defun byte-optimize-plus (form) | |
672 (setq form (byte-optimize-delay-constants-math form 1 '+)) | |
673 (if (memq 0 form) (setq form (delq 0 (copy-sequence form)))) | |
674 ;;(setq form (byte-optimize-associative-two-args-math form)) | |
675 (cond ((null (cdr form)) | |
676 (condition-case () | |
677 (eval form) | |
678 (error form))) | |
902
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
679 ;;; It is not safe to delete the function entirely |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
680 ;;; (actually, it would be safe if we know the sole arg |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
681 ;;; is not a marker). |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
682 ;; ((null (cdr (cdr form))) (nth 1 form)) |
757 | 683 (t form))) |
684 | |
685 (defun byte-optimize-minus (form) | |
686 ;; Put constants at the end, except the last constant. | |
687 (setq form (byte-optimize-delay-constants-math form 2 '+)) | |
688 ;; Now only first and last element can be a number. | |
689 (let ((last (car (reverse (nthcdr 3 form))))) | |
690 (cond ((eq 0 last) | |
691 ;; (- x y ... 0) --> (- x y ...) | |
692 (setq form (copy-sequence form)) | |
693 (setcdr (cdr (cdr form)) (delq 0 (nthcdr 3 form)))) | |
694 ;; If form is (- CONST foo... CONST), merge first and last. | |
695 ((and (numberp (nth 1 form)) | |
696 (numberp last)) | |
697 (setq form (nconc (list '- (- (nth 1 form) last) (nth 2 form)) | |
698 (delq last (copy-sequence (nthcdr 3 form)))))))) | |
902
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
699 ;;; It is not safe to delete the function entirely |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
700 ;;; (actually, it would be safe if we know the sole arg |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
701 ;;; is not a marker). |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
702 ;;; (if (eq (nth 2 form) 0) |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
703 ;;; (nth 1 form) ; (- x 0) --> x |
757 | 704 (byte-optimize-predicate |
705 (if (and (null (cdr (cdr (cdr form)))) | |
706 (eq (nth 1 form) 0)) ; (- 0 x) --> (- x) | |
707 (cons (car form) (cdr (cdr form))) | |
902
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
708 form)) |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
709 ;;; ) |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
710 ) |
757 | 711 |
712 (defun byte-optimize-multiply (form) | |
713 (setq form (byte-optimize-delay-constants-math form 1 '*)) | |
714 ;; If there is a constant in FORM, it is now the last element. | |
715 (cond ((null (cdr form)) 1) | |
902
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
716 ;;; It is not safe to delete the function entirely |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
717 ;;; (actually, it would be safe if we know the sole arg |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
718 ;;; is not a marker or if it appears in other arithmetic). |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
719 ;;; ((null (cdr (cdr form))) (nth 1 form)) |
757 | 720 ((let ((last (car (reverse form)))) |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
721 (cond ((eq 0 last) (cons 'progn (cdr form))) |
757 | 722 ((eq 1 last) (delq 1 (copy-sequence form))) |
723 ((eq -1 last) (list '- (delq -1 (copy-sequence form)))) | |
724 ((and (eq 2 last) | |
725 (memq t (mapcar 'symbolp (cdr form)))) | |
726 (prog1 (setq form (delq 2 (copy-sequence form))) | |
727 (while (not (symbolp (car (setq form (cdr form)))))) | |
728 (setcar form (list '+ (car form) (car form))))) | |
729 (form)))))) | |
730 | |
731 (defsubst byte-compile-butlast (form) | |
732 (nreverse (cdr (reverse form)))) | |
733 | |
734 (defun byte-optimize-divide (form) | |
735 (setq form (byte-optimize-delay-constants-math form 2 '*)) | |
736 (let ((last (car (reverse (cdr (cdr form)))))) | |
737 (if (numberp last) | |
3138
80ce80f189f7
(byte-optimize-divide): Don't optimize to less than two arguments.
Richard M. Stallman <rms@gnu.org>
parents:
1818
diff
changeset
|
738 (cond ((= (length form) 3) |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
739 (if (and (numberp (nth 1 form)) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
740 (not (zerop last)) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
741 (condition-case nil |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
742 (/ (nth 1 form) last) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
743 (error nil))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
744 (setq form (list 'progn (/ (nth 1 form) last))))) |
3138
80ce80f189f7
(byte-optimize-divide): Don't optimize to less than two arguments.
Richard M. Stallman <rms@gnu.org>
parents:
1818
diff
changeset
|
745 ((= last 1) |
757 | 746 (setq form (byte-compile-butlast form))) |
747 ((numberp (nth 1 form)) | |
748 (setq form (cons (car form) | |
749 (cons (/ (nth 1 form) last) | |
750 (byte-compile-butlast (cdr (cdr form))))) | |
751 last nil)))) | |
902
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
752 (cond |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
753 ;;; ((null (cdr (cdr form))) |
c81d26e85bef
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
848
diff
changeset
|
754 ;;; (nth 1 form)) |
757 | 755 ((eq (nth 1 form) 0) |
756 (append '(progn) (cdr (cdr form)) '(0))) | |
757 ((eq last -1) | |
758 (list '- (if (nthcdr 3 form) | |
759 (byte-compile-butlast form) | |
760 (nth 1 form)))) | |
761 (form)))) | |
762 | |
763 (defun byte-optimize-logmumble (form) | |
764 (setq form (byte-optimize-delay-constants-math form 1 (car form))) | |
765 (byte-optimize-predicate | |
766 (cond ((memq 0 form) | |
767 (setq form (if (eq (car form) 'logand) | |
768 (cons 'progn (cdr form)) | |
769 (delq 0 (copy-sequence form))))) | |
770 ((and (eq (car-safe form) 'logior) | |
771 (memq -1 form)) | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
772 (cons 'progn (cdr form))) |
757 | 773 (form)))) |
774 | |
775 | |
776 (defun byte-optimize-binary-predicate (form) | |
777 (if (byte-compile-constp (nth 1 form)) | |
778 (if (byte-compile-constp (nth 2 form)) | |
779 (condition-case () | |
780 (list 'quote (eval form)) | |
781 (error form)) | |
782 ;; This can enable some lapcode optimizations. | |
783 (list (car form) (nth 2 form) (nth 1 form))) | |
784 form)) | |
785 | |
786 (defun byte-optimize-predicate (form) | |
787 (let ((ok t) | |
788 (rest (cdr form))) | |
789 (while (and rest ok) | |
790 (setq ok (byte-compile-constp (car rest)) | |
791 rest (cdr rest))) | |
792 (if ok | |
793 (condition-case () | |
794 (list 'quote (eval form)) | |
795 (error form)) | |
796 form))) | |
797 | |
798 (defun byte-optimize-identity (form) | |
799 (if (and (cdr form) (null (cdr (cdr form)))) | |
800 (nth 1 form) | |
801 (byte-compile-warn "identity called with %d arg%s, but requires 1" | |
802 (length (cdr form)) | |
803 (if (= 1 (length (cdr form))) "" "s")) | |
804 form)) | |
805 | |
806 (put 'identity 'byte-optimizer 'byte-optimize-identity) | |
807 | |
808 (put '+ 'byte-optimizer 'byte-optimize-plus) | |
809 (put '* 'byte-optimizer 'byte-optimize-multiply) | |
810 (put '- 'byte-optimizer 'byte-optimize-minus) | |
811 (put '/ 'byte-optimizer 'byte-optimize-divide) | |
812 (put 'max 'byte-optimizer 'byte-optimize-associative-math) | |
813 (put 'min 'byte-optimizer 'byte-optimize-associative-math) | |
814 | |
815 (put '= 'byte-optimizer 'byte-optimize-binary-predicate) | |
816 (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate) | |
817 (put 'eql 'byte-optimizer 'byte-optimize-binary-predicate) | |
818 (put 'equal 'byte-optimizer 'byte-optimize-binary-predicate) | |
819 (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate) | |
820 (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate) | |
821 | |
822 (put '< 'byte-optimizer 'byte-optimize-predicate) | |
823 (put '> 'byte-optimizer 'byte-optimize-predicate) | |
824 (put '<= 'byte-optimizer 'byte-optimize-predicate) | |
825 (put '>= 'byte-optimizer 'byte-optimize-predicate) | |
826 (put '1+ 'byte-optimizer 'byte-optimize-predicate) | |
827 (put '1- 'byte-optimizer 'byte-optimize-predicate) | |
828 (put 'not 'byte-optimizer 'byte-optimize-predicate) | |
829 (put 'null 'byte-optimizer 'byte-optimize-predicate) | |
830 (put 'memq 'byte-optimizer 'byte-optimize-predicate) | |
831 (put 'consp 'byte-optimizer 'byte-optimize-predicate) | |
832 (put 'listp 'byte-optimizer 'byte-optimize-predicate) | |
833 (put 'symbolp 'byte-optimizer 'byte-optimize-predicate) | |
834 (put 'stringp 'byte-optimizer 'byte-optimize-predicate) | |
835 (put 'string< 'byte-optimizer 'byte-optimize-predicate) | |
836 (put 'string-lessp 'byte-optimizer 'byte-optimize-predicate) | |
837 | |
838 (put 'logand 'byte-optimizer 'byte-optimize-logmumble) | |
839 (put 'logior 'byte-optimizer 'byte-optimize-logmumble) | |
840 (put 'logxor 'byte-optimizer 'byte-optimize-logmumble) | |
841 (put 'lognot 'byte-optimizer 'byte-optimize-predicate) | |
842 | |
843 (put 'car 'byte-optimizer 'byte-optimize-predicate) | |
844 (put 'cdr 'byte-optimizer 'byte-optimize-predicate) | |
845 (put 'car-safe 'byte-optimizer 'byte-optimize-predicate) | |
846 (put 'cdr-safe 'byte-optimizer 'byte-optimize-predicate) | |
847 | |
848 | |
849 ;; I'm not convinced that this is necessary. Doesn't the optimizer loop | |
850 ;; take care of this? - Jamie | |
851 ;; I think this may some times be necessary to reduce ie (quote 5) to 5, | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3138
diff
changeset
|
852 ;; so arithmetic optimizers recognize the numeric constant. - Hallvard |
757 | 853 (put 'quote 'byte-optimizer 'byte-optimize-quote) |
854 (defun byte-optimize-quote (form) | |
855 (if (or (consp (nth 1 form)) | |
856 (and (symbolp (nth 1 form)) | |
857 (not (memq (nth 1 form) '(nil t))))) | |
858 form | |
859 (nth 1 form))) | |
860 | |
861 (defun byte-optimize-zerop (form) | |
862 (cond ((numberp (nth 1 form)) | |
863 (eval form)) | |
864 (byte-compile-delete-errors | |
865 (list '= (nth 1 form) 0)) | |
866 (form))) | |
867 | |
868 (put 'zerop 'byte-optimizer 'byte-optimize-zerop) | |
869 | |
870 (defun byte-optimize-and (form) | |
871 ;; Simplify if less than 2 args. | |
872 ;; if there is a literal nil in the args to `and', throw it and following | |
873 ;; forms away, and surround the `and' with (progn ... nil). | |
874 (cond ((null (cdr form))) | |
875 ((memq nil form) | |
876 (list 'progn | |
877 (byte-optimize-and | |
878 (prog1 (setq form (copy-sequence form)) | |
879 (while (nth 1 form) | |
880 (setq form (cdr form))) | |
881 (setcdr form nil))) | |
882 nil)) | |
883 ((null (cdr (cdr form))) | |
884 (nth 1 form)) | |
885 ((byte-optimize-predicate form)))) | |
886 | |
887 (defun byte-optimize-or (form) | |
888 ;; Throw away nil's, and simplify if less than 2 args. | |
889 ;; If there is a literal non-nil constant in the args to `or', throw away all | |
890 ;; following forms. | |
891 (if (memq nil form) | |
892 (setq form (delq nil (copy-sequence form)))) | |
893 (let ((rest form)) | |
894 (while (cdr (setq rest (cdr rest))) | |
895 (if (byte-compile-trueconstp (car rest)) | |
896 (setq form (copy-sequence form) | |
897 rest (setcdr (memq (car rest) form) nil)))) | |
898 (if (cdr (cdr form)) | |
899 (byte-optimize-predicate form) | |
900 (nth 1 form)))) | |
901 | |
902 (defun byte-optimize-cond (form) | |
903 ;; if any clauses have a literal nil as their test, throw them away. | |
904 ;; if any clause has a literal non-nil constant as its test, throw | |
905 ;; away all following clauses. | |
906 (let (rest) | |
907 ;; This must be first, to reduce (cond (t ...) (nil)) to (progn t ...) | |
908 (while (setq rest (assq nil (cdr form))) | |
909 (setq form (delq rest (copy-sequence form)))) | |
910 (if (memq nil (cdr form)) | |
911 (setq form (delq nil (copy-sequence form)))) | |
912 (setq rest form) | |
913 (while (setq rest (cdr rest)) | |
914 (cond ((byte-compile-trueconstp (car-safe (car rest))) | |
915 (cond ((eq rest (cdr form)) | |
916 (setq form | |
917 (if (cdr (car rest)) | |
918 (if (cdr (cdr (car rest))) | |
919 (cons 'progn (cdr (car rest))) | |
920 (nth 1 (car rest))) | |
921 (car (car rest))))) | |
922 ((cdr rest) | |
923 (setq form (copy-sequence form)) | |
924 (setcdr (memq (car rest) form) nil))) | |
925 (setq rest nil))))) | |
926 ;; | |
927 ;; Turn (cond (( <x> )) ... ) into (or <x> (cond ... )) | |
928 (if (eq 'cond (car-safe form)) | |
929 (let ((clauses (cdr form))) | |
930 (if (and (consp (car clauses)) | |
931 (null (cdr (car clauses)))) | |
932 (list 'or (car (car clauses)) | |
933 (byte-optimize-cond | |
934 (cons (car form) (cdr (cdr form))))) | |
935 form)) | |
936 form)) | |
937 | |
938 (defun byte-optimize-if (form) | |
939 ;; (if <true-constant> <then> <else...>) ==> <then> | |
940 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>) | |
941 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>)) | |
942 ;; (if <test> <then> nil) ==> (if <test> <then>) | |
943 (let ((clause (nth 1 form))) | |
944 (cond ((byte-compile-trueconstp clause) | |
945 (nth 2 form)) | |
946 ((null clause) | |
947 (if (nthcdr 4 form) | |
948 (cons 'progn (nthcdr 3 form)) | |
949 (nth 3 form))) | |
950 ((nth 2 form) | |
951 (if (equal '(nil) (nthcdr 3 form)) | |
952 (list 'if clause (nth 2 form)) | |
953 form)) | |
954 ((or (nth 3 form) (nthcdr 4 form)) | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
955 (list 'if |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
956 ;; Don't make a double negative; |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
957 ;; instead, take away the one that is there. |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
958 (if (and (consp clause) (memq (car clause) '(not null)) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
959 (= (length clause) 2)) ; (not xxxx) or (not (xxxx)) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
960 (nth 1 clause) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
961 (list 'not clause)) |
757 | 962 (if (nthcdr 4 form) |
963 (cons 'progn (nthcdr 3 form)) | |
964 (nth 3 form)))) | |
965 (t | |
966 (list 'progn clause nil))))) | |
967 | |
968 (defun byte-optimize-while (form) | |
969 (if (nth 1 form) | |
970 form)) | |
971 | |
972 (put 'and 'byte-optimizer 'byte-optimize-and) | |
973 (put 'or 'byte-optimizer 'byte-optimize-or) | |
974 (put 'cond 'byte-optimizer 'byte-optimize-cond) | |
975 (put 'if 'byte-optimizer 'byte-optimize-if) | |
976 (put 'while 'byte-optimizer 'byte-optimize-while) | |
977 | |
978 ;; byte-compile-negation-optimizer lives in bytecomp.el | |
979 (put '/= 'byte-optimizer 'byte-compile-negation-optimizer) | |
980 (put 'atom 'byte-optimizer 'byte-compile-negation-optimizer) | |
981 (put 'nlistp 'byte-optimizer 'byte-compile-negation-optimizer) | |
982 | |
983 | |
984 (defun byte-optimize-funcall (form) | |
985 ;; (funcall '(lambda ...) ...) ==> ((lambda ...) ...) | |
986 ;; (funcall 'foo ...) ==> (foo ...) | |
987 (let ((fn (nth 1 form))) | |
988 (if (memq (car-safe fn) '(quote function)) | |
989 (cons (nth 1 fn) (cdr (cdr form))) | |
990 form))) | |
991 | |
992 (defun byte-optimize-apply (form) | |
993 ;; If the last arg is a literal constant, turn this into a funcall. | |
994 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...). | |
995 (let ((fn (nth 1 form)) | |
996 (last (nth (1- (length form)) form))) ; I think this really is fastest | |
997 (or (if (or (null last) | |
998 (eq (car-safe last) 'quote)) | |
999 (if (listp (nth 1 last)) | |
1000 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form))))))) | |
957 | 1001 (nconc (list 'funcall fn) butlast |
1002 (mapcar '(lambda (x) (list 'quote x)) (nth 1 last)))) | |
757 | 1003 (byte-compile-warn |
1004 "last arg to apply can't be a literal atom: %s" | |
1005 (prin1-to-string last)) | |
1006 nil)) | |
1007 form))) | |
1008 | |
1009 (put 'funcall 'byte-optimizer 'byte-optimize-funcall) | |
1010 (put 'apply 'byte-optimizer 'byte-optimize-apply) | |
1011 | |
1012 | |
1013 (put 'let 'byte-optimizer 'byte-optimize-letX) | |
1014 (put 'let* 'byte-optimizer 'byte-optimize-letX) | |
1015 (defun byte-optimize-letX (form) | |
1016 (cond ((null (nth 1 form)) | |
1017 ;; No bindings | |
1018 (cons 'progn (cdr (cdr form)))) | |
1019 ((or (nth 2 form) (nthcdr 3 form)) | |
1020 form) | |
1021 ;; The body is nil | |
1022 ((eq (car form) 'let) | |
11509
853f52a85d11
(byte-optimize-letX): Use car-safe and cdr-safe.
Richard M. Stallman <rms@gnu.org>
parents:
11203
diff
changeset
|
1023 (append '(progn) (mapcar 'car-safe (mapcar 'cdr-safe (nth 1 form))) |
853f52a85d11
(byte-optimize-letX): Use car-safe and cdr-safe.
Richard M. Stallman <rms@gnu.org>
parents:
11203
diff
changeset
|
1024 '(nil))) |
757 | 1025 (t |
1026 (let ((binds (reverse (nth 1 form)))) | |
1027 (list 'let* (reverse (cdr binds)) (nth 1 (car binds)) nil))))) | |
1028 | |
1029 | |
1030 (put 'nth 'byte-optimizer 'byte-optimize-nth) | |
1031 (defun byte-optimize-nth (form) | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
1032 (if (and (= (safe-length form) 3) (memq (nth 1 form) '(0 1))) |
757 | 1033 (list 'car (if (zerop (nth 1 form)) |
1034 (nth 2 form) | |
1035 (list 'cdr (nth 2 form)))) | |
1036 (byte-optimize-predicate form))) | |
1037 | |
1038 (put 'nthcdr 'byte-optimizer 'byte-optimize-nthcdr) | |
1039 (defun byte-optimize-nthcdr (form) | |
12550
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
1040 (if (and (= (safe-length form) 3) (not (memq (nth 1 form) '(0 1 2)))) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
1041 (byte-optimize-predicate form) |
c33dd1c62d72
(byte-optimize-nth, byte-optimize-nthcdr):
Karl Heuer <kwzh@gnu.org>
parents:
11509
diff
changeset
|
1042 (let ((count (nth 1 form))) |
757 | 1043 (setq form (nth 2 form)) |
12737
7b804de92243
(byte-optimize-nthcdr): Fix previous change.
Richard M. Stallman <rms@gnu.org>
parents:
12638
diff
changeset
|
1044 (while (>= (setq count (1- count)) 0) |
757 | 1045 (setq form (list 'cdr form))) |
1046 form))) | |
1047 | |
1048 ;;; enumerating those functions which need not be called if the returned | |
1049 ;;; value is not used. That is, something like | |
1050 ;;; (progn (list (something-with-side-effects) (yow)) | |
1051 ;;; (foo)) | |
1052 ;;; may safely be turned into | |
1053 ;;; (progn (progn (something-with-side-effects) (yow)) | |
1054 ;;; (foo)) | |
1055 ;;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo. | |
1056 | |
1057 ;;; I wonder if I missed any :-\) | |
1058 (let ((side-effect-free-fns | |
5315
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1059 '(% * + - / /= 1+ 1- < <= = > >= abs acos append aref ash asin atan |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1060 assoc assq |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1061 boundp buffer-file-name buffer-local-variables buffer-modified-p |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1062 buffer-substring |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1063 capitalize car-less-than-car car cdr ceiling concat coordinates-in-window-p |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1064 copy-marker cos count-lines |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1065 default-boundp default-value documentation downcase |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1066 elt exp expt fboundp featurep |
757 | 1067 file-directory-p file-exists-p file-locked-p file-name-absolute-p |
1068 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p | |
5315
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1069 float floor format |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1070 get get-buffer get-buffer-window getenv get-file-buffer |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1071 int-to-string |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1072 length log log10 logand logb logior lognot logxor lsh |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1073 marker-buffer max member memq min mod |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1074 next-window nth nthcdr number-to-string |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1075 parse-colon-path previous-window |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1076 radians-to-degrees rassq regexp-quote reverse round |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1077 sin sqrt string< string= string-equal string-lessp string-to-char |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1078 string-to-int string-to-number substring symbol-plist |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1079 tan upcase user-variable-p vconcat |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1080 window-buffer window-dedicated-p window-edges window-height |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1081 window-hscroll window-minibuffer-p window-width |
757 | 1082 zerop)) |
1083 (side-effect-and-error-free-fns | |
5315
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1084 '(arrayp atom |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1085 bobp bolp buffer-end buffer-list buffer-size buffer-string bufferp |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1086 car-safe case-table-p cdr-safe char-or-string-p commandp cons consp |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1087 current-buffer |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1088 dot dot-marker eobp eolp eq eql equal eventp floatp framep |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1089 get-largest-window get-lru-window |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1090 identity ignore integerp integer-or-marker-p interactive-p |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1091 invocation-directory invocation-name |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1092 keymapp list listp |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1093 make-marker mark mark-marker markerp memory-limit minibuffer-window |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1094 mouse-movement-p |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1095 natnump nlistp not null number-or-marker-p numberp |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1096 one-window-p overlayp |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1097 point point-marker point-min point-max processp |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1098 selected-window sequencep stringp subrp symbolp syntax-table-p |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1099 user-full-name user-login-name user-original-login-name |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1100 user-real-login-name user-real-uid user-uid |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1101 vector vectorp |
55a8d59088c1
Add side-effect-free props for many functions.
Richard M. Stallman <rms@gnu.org>
parents:
4755
diff
changeset
|
1102 window-configuration-p window-live-p windowp))) |
757 | 1103 (while side-effect-free-fns |
1104 (put (car side-effect-free-fns) 'side-effect-free t) | |
1105 (setq side-effect-free-fns (cdr side-effect-free-fns))) | |
1106 (while side-effect-and-error-free-fns | |
1107 (put (car side-effect-and-error-free-fns) 'side-effect-free 'error-free) | |
1108 (setq side-effect-and-error-free-fns (cdr side-effect-and-error-free-fns))) | |
1109 nil) | |
1110 | |
1111 | |
1112 (defun byte-compile-splice-in-already-compiled-code (form) | |
1113 ;; form is (byte-code "..." [...] n) | |
1114 (if (not (memq byte-optimize '(t lap))) | |
1115 (byte-compile-normal-call form) | |
1116 (byte-inline-lapcode | |
1117 (byte-decompile-bytecode-1 (nth 1 form) (nth 2 form) t)) | |
1118 (setq byte-compile-maxdepth (max (+ byte-compile-depth (nth 3 form)) | |
1119 byte-compile-maxdepth)) | |
1120 (setq byte-compile-depth (1+ byte-compile-depth)))) | |
1121 | |
1122 (put 'byte-code 'byte-compile 'byte-compile-splice-in-already-compiled-code) | |
1123 | |
1124 | |
1125 (defconst byte-constref-ops | |
1126 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind)) | |
1127 | |
1128 ;;; This function extracts the bitfields from variable-length opcodes. | |
1129 ;;; Originally defined in disass.el (which no longer uses it.) | |
1130 | |
1131 (defun disassemble-offset () | |
1132 "Don't call this!" | |
1133 ;; fetch and return the offset for the current opcode. | |
1134 ;; return NIL if this opcode has no offset | |
1135 ;; OP, PTR and BYTES are used and set dynamically | |
1136 (defvar op) | |
1137 (defvar ptr) | |
1138 (defvar bytes) | |
1139 (cond ((< op byte-nth) | |
1140 (let ((tem (logand op 7))) | |
1141 (setq op (logand op 248)) | |
1142 (cond ((eq tem 6) | |
1143 (setq ptr (1+ ptr)) ;offset in next byte | |
1144 (aref bytes ptr)) | |
1145 ((eq tem 7) | |
1146 (setq ptr (1+ ptr)) ;offset in next 2 bytes | |
1147 (+ (aref bytes ptr) | |
1148 (progn (setq ptr (1+ ptr)) | |
1149 (lsh (aref bytes ptr) 8)))) | |
1150 (t tem)))) ;offset was in opcode | |
1151 ((>= op byte-constant) | |
1152 (prog1 (- op byte-constant) ;offset in opcode | |
1153 (setq op byte-constant))) | |
1154 ((and (>= op byte-constant2) | |
1155 (<= op byte-goto-if-not-nil-else-pop)) | |
1156 (setq ptr (1+ ptr)) ;offset in next 2 bytes | |
1157 (+ (aref bytes ptr) | |
1158 (progn (setq ptr (1+ ptr)) | |
1159 (lsh (aref bytes ptr) 8)))) | |
848 | 1160 ((and (>= op byte-listN) |
757 | 1161 (<= op byte-insertN)) |
1162 (setq ptr (1+ ptr)) ;offset in next byte | |
1163 (aref bytes ptr)))) | |
1164 | |
1165 | |
1166 ;;; This de-compiler is used for inline expansion of compiled functions, | |
1167 ;;; and by the disassembler. | |
1168 ;;; | |
8292
6857db0f3c82
(byte-decompile-bytecode-1):
Richard M. Stallman <rms@gnu.org>
parents:
7298
diff
changeset
|
1169 ;;; This list contains numbers, which are pc values, |
6857db0f3c82
(byte-decompile-bytecode-1):
Richard M. Stallman <rms@gnu.org>
parents:
7298
diff
changeset
|
1170 ;;; before each instruction. |
757 | 1171 (defun byte-decompile-bytecode (bytes constvec) |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3138
diff
changeset
|
1172 "Turns BYTECODE into lapcode, referring to CONSTVEC." |
757 | 1173 (let ((byte-compile-constants nil) |
1174 (byte-compile-variables nil) | |
1175 (byte-compile-tag-number 0)) | |
1176 (byte-decompile-bytecode-1 bytes constvec))) | |
1177 | |
767
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1178 ;; As byte-decompile-bytecode, but updates |
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1179 ;; byte-compile-{constants, variables, tag-number}. |
8294
cd3d2474ea10
(byte-decompile-bytecode-1): Don't add pc values
Richard M. Stallman <rms@gnu.org>
parents:
8292
diff
changeset
|
1180 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced |
767
02bfc9709961
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1181 ;; with `goto's destined for the end of the code. |
8294
cd3d2474ea10
(byte-decompile-bytecode-1): Don't add pc values
Richard M. Stallman <rms@gnu.org>
parents:
8292
diff
changeset
|
1182 ;; That is for use by the compiler. |
cd3d2474ea10
(byte-decompile-bytecode-1): Don't add pc values
Richard M. Stallman <rms@gnu.org>
parents:
8292
diff
changeset
|
1183 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler. |
cd3d2474ea10
(byte-decompile-bytecode-1): Don't add pc values
Richard M. Stallman <rms@gnu.org>
parents:
8292
diff
changeset
|
1184 ;; In that case, we put a pc value into the list |
cd3d2474ea10
(byte-decompile-bytecode-1): Don't add pc values
Richard M. Stallman <rms@gnu.org>
parents:
8292
diff
changeset
|
1185 ;; before each insn (or its label). |
cd3d2474ea10
(byte-decompile-bytecode-1): Don't add pc values
Richard M. Stallman <rms@gnu.org>
parents:
8292
diff
changeset
|
1186 (defun byte-decompile-bytecode-1 (bytes constvec &optional make-spliceable) |
757 | 1187 (let ((length (length bytes)) |
1188 (ptr 0) optr tag tags op offset | |
1189 lap tmp | |
1190 endtag | |
1191 (retcount 0)) | |
1192 (while (not (= ptr length)) | |
8294
cd3d2474ea10
(byte-decompile-bytecode-1): Don't add pc values
Richard M. Stallman <rms@gnu.org>
parents:
8292
diff
changeset
|
1193 (or make-spliceable |
cd3d2474ea10
(byte-decompile-bytecode-1): Don't add pc values
Richard M. Stallman <rms@gnu.org>
parents:
8292
diff
changeset
|
1194 (setq lap (cons ptr lap))) |
757 | 1195 (setq op (aref bytes ptr) |
1196 optr ptr | |
1197 offset (disassemble-offset)) ; this does dynamic-scope magic | |
1198 (setq op (aref byte-code-vector op)) | |
848 | 1199 (cond ((memq op byte-goto-ops) |
757 | 1200 ;; it's a pc |
1201 (setq offset | |
1202 (cdr (or (assq offset tags) | |
1203 (car (setq tags | |
1204 (cons (cons offset | |
1205 (byte-compile-make-tag)) | |
1206 tags))))))) | |
1207 ((cond ((eq op 'byte-constant2) (setq op 'byte-constant) t) | |
1208 ((memq op byte-constref-ops))) | |
1209 (setq tmp (aref constvec offset) | |
1210 offset (if (eq op 'byte-constant) | |
1211 (byte-compile-get-constant tmp) | |
1212 (or (assq tmp byte-compile-variables) | |
1213 (car (setq byte-compile-variables | |
1214 (cons (list tmp) | |
1215 byte-compile-variables))))))) | |
8294
cd3d2474ea10
(byte-decompile-bytecode-1): Don't add pc values
Richard M. Stallman <rms@gnu.org>
parents:
8292
diff
changeset
|
1216 ((and make-spliceable |
757 | 1217 (eq op 'byte-return)) |
1218 (if (= ptr (1- length)) | |
1219 (setq op nil) | |
1220 (setq offset (or endtag (setq endtag (byte-compile-make-tag))) | |
1221 op 'byte-goto)))) | |
1222 ;; lap = ( [ (pc . (op . arg)) ]* ) | |
1223 (setq lap (cons (cons optr (cons op (or offset 0))) | |
1224 lap)) | |
1225 (setq ptr (1+ ptr))) | |
1226 ;; take off the dummy nil op that we replaced a trailing "return" with. | |
1227 (let ((rest lap)) | |
1228 (while rest | |
8292
6857db0f3c82
(byte-decompile-bytecode-1):
Richard M. Stallman <rms@gnu.org>
parents:
7298
diff
changeset
|
1229 (cond ((numberp (car rest))) |
6857db0f3c82
(byte-decompile-bytecode-1):
Richard M. Stallman <rms@gnu.org>
parents:
7298
diff
changeset
|
1230 ((setq tmp (assq (car (car rest)) tags)) |
757 | 1231 ;; this addr is jumped to |
1232 (setcdr rest (cons (cons nil (cdr tmp)) | |
1233 (cdr rest))) | |
1234 (setq tags (delq tmp tags)) | |
1235 (setq rest (cdr rest)))) | |
1236 (setq rest (cdr rest)))) | |
1237 (if tags (error "optimizer error: missed tags %s" tags)) | |
1238 (if (null (car (cdr (car lap)))) | |
1239 (setq lap (cdr lap))) | |
1240 (if endtag | |
1241 (setq lap (cons (cons nil endtag) lap))) | |
1242 ;; remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* ) | |
8292
6857db0f3c82
(byte-decompile-bytecode-1):
Richard M. Stallman <rms@gnu.org>
parents:
7298
diff
changeset
|
1243 (mapcar (function (lambda (elt) |
6857db0f3c82
(byte-decompile-bytecode-1):
Richard M. Stallman <rms@gnu.org>
parents:
7298
diff
changeset
|
1244 (if (numberp elt) |
6857db0f3c82
(byte-decompile-bytecode-1):
Richard M. Stallman <rms@gnu.org>
parents:
7298
diff
changeset
|
1245 elt |
6857db0f3c82
(byte-decompile-bytecode-1):
Richard M. Stallman <rms@gnu.org>
parents:
7298
diff
changeset
|
1246 (cdr elt)))) |
6857db0f3c82
(byte-decompile-bytecode-1):
Richard M. Stallman <rms@gnu.org>
parents:
7298
diff
changeset
|
1247 (nreverse lap)))) |
757 | 1248 |
1249 | |
1250 ;;; peephole optimizer | |
1251 | |
1252 (defconst byte-tagref-ops (cons 'TAG byte-goto-ops)) | |
1253 | |
1254 (defconst byte-conditional-ops | |
1255 '(byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop | |
1256 byte-goto-if-not-nil-else-pop)) | |
1257 | |
1258 (defconst byte-after-unbind-ops | |
1259 '(byte-constant byte-dup | |
1260 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-integerp | |
1261 byte-eq byte-equal byte-not | |
1262 byte-cons byte-list1 byte-list2 ; byte-list3 byte-list4 | |
8466
3cca823100db
(byte-after-unbind-ops): Fix paren error wrt doc string.
Richard M. Stallman <rms@gnu.org>
parents:
8294
diff
changeset
|
1263 byte-interactive-p) |
3cca823100db
(byte-after-unbind-ops): Fix paren error wrt doc string.
Richard M. Stallman <rms@gnu.org>
parents:
8294
diff
changeset
|
1264 ;; How about other side-effect-free-ops? Is it safe to move an |
3cca823100db
(byte-after-unbind-ops): Fix paren error wrt doc string.
Richard M. Stallman <rms@gnu.org>
parents:
8294
diff
changeset
|
1265 ;; error invocation (such as from nth) out of an unwind-protect? |
3cca823100db
(byte-after-unbind-ops): Fix paren error wrt doc string.
Richard M. Stallman <rms@gnu.org>
parents:
8294
diff
changeset
|
1266 "Byte-codes that can be moved past an unbind.") |
757 | 1267 |
1268 (defconst byte-compile-side-effect-and-error-free-ops | |
1269 '(byte-constant byte-dup byte-symbolp byte-consp byte-stringp byte-listp | |
1270 byte-integerp byte-numberp byte-eq byte-equal byte-not byte-car-safe | |
1271 byte-cdr-safe byte-cons byte-list1 byte-list2 byte-point byte-point-max | |
1272 byte-point-min byte-following-char byte-preceding-char | |
1273 byte-current-column byte-eolp byte-eobp byte-bolp byte-bobp | |
1274 byte-current-buffer byte-interactive-p)) | |
1275 | |
1276 (defconst byte-compile-side-effect-free-ops | |
1277 (nconc | |
1278 '(byte-varref byte-nth byte-memq byte-car byte-cdr byte-length byte-aref | |
1279 byte-symbol-value byte-get byte-concat2 byte-concat3 byte-sub1 byte-add1 | |
1280 byte-eqlsign byte-gtr byte-lss byte-leq byte-geq byte-diff byte-negate | |
1281 byte-plus byte-max byte-min byte-mult byte-char-after byte-char-syntax | |
1282 byte-buffer-substring byte-string= byte-string< byte-nthcdr byte-elt | |
1283 byte-member byte-assq byte-quo byte-rem) | |
1284 byte-compile-side-effect-and-error-free-ops)) | |
1285 | |
1286 ;;; This piece of shit is because of the way DEFVAR_BOOL() variables work. | |
1287 ;;; Consider the code | |
1288 ;;; | |
1289 ;;; (defun foo (flag) | |
1290 ;;; (let ((old-pop-ups pop-up-windows) | |
1291 ;;; (pop-up-windows flag)) | |
1292 ;;; (cond ((not (eq pop-up-windows old-pop-ups)) | |
1293 ;;; (setq old-pop-ups pop-up-windows) | |
1294 ;;; ...)))) | |
1295 ;;; | |
1296 ;;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is | |
1297 ;;; something else. But if we optimize | |
1298 ;;; | |
1299 ;;; varref flag | |
1300 ;;; varbind pop-up-windows | |
1301 ;;; varref pop-up-windows | |
1302 ;;; not | |
1303 ;;; to | |
1304 ;;; varref flag | |
1305 ;;; dup | |
1306 ;;; varbind pop-up-windows | |
1307 ;;; not | |
1308 ;;; | |
1309 ;;; we break the program, because it will appear that pop-up-windows and | |
1310 ;;; old-pop-ups are not EQ when really they are. So we have to know what | |
1311 ;;; the BOOL variables are, and not perform this optimization on them. | |
1312 ;;; | |
1313 (defconst byte-boolean-vars | |
848 | 1314 '(abbrev-all-caps abbrevs-changed byte-metering-on |
13060
9154b9967e2b
(byte-boolean-vars): Update list to reflect actual DEFVAR_BOOL symbols in
Erik Naggum <erik@naggum.no>
parents:
12737
diff
changeset
|
1315 cannot-suspend completion-auto-help completion-ignore-case |
848 | 1316 cursor-in-echo-area debug-on-next-call debug-on-quit |
13060
9154b9967e2b
(byte-boolean-vars): Update list to reflect actual DEFVAR_BOOL symbols in
Erik Naggum <erik@naggum.no>
parents:
12737
diff
changeset
|
1317 delete-exited-processes enable-recursive-minibuffers |
9154b9967e2b
(byte-boolean-vars): Update list to reflect actual DEFVAR_BOOL symbols in
Erik Naggum <erik@naggum.no>
parents:
12737
diff
changeset
|
1318 highlight-nonselected-windows indent-tabs-mode inhibit-local-menu-bar-menus |
9154b9967e2b
(byte-boolean-vars): Update list to reflect actual DEFVAR_BOOL symbols in
Erik Naggum <erik@naggum.no>
parents:
12737
diff
changeset
|
1319 insert-default-directory inverse-video load-force-doc-strings |
9154b9967e2b
(byte-boolean-vars): Update list to reflect actual DEFVAR_BOOL symbols in
Erik Naggum <erik@naggum.no>
parents:
12737
diff
changeset
|
1320 load-in-progress menu-prompting minibuffer-auto-raise |
9154b9967e2b
(byte-boolean-vars): Update list to reflect actual DEFVAR_BOOL symbols in
Erik Naggum <erik@naggum.no>
parents:
12737
diff
changeset
|
1321 mode-line-inverse-video multiple-frames no-redraw-on-reenter noninteractive |
9154b9967e2b
(byte-boolean-vars): Update list to reflect actual DEFVAR_BOOL symbols in
Erik Naggum <erik@naggum.no>
parents:
12737
diff
changeset
|
1322 parse-sexp-ignore-comments pop-up-frames pop-up-windows |
9154b9967e2b
(byte-boolean-vars): Update list to reflect actual DEFVAR_BOOL symbols in
Erik Naggum <erik@naggum.no>
parents:
12737
diff
changeset
|
1323 print-escape-newlines system-uses-terminfo truncate-partial-width-windows |
9154b9967e2b
(byte-boolean-vars): Update list to reflect actual DEFVAR_BOOL symbols in
Erik Naggum <erik@naggum.no>
parents:
12737
diff
changeset
|
1324 visible-bell vms-stmlf-recfm words-include-escapes) |
757 | 1325 "DEFVAR_BOOL variables. Giving these any non-nil value sets them to t. |
1326 If this does not enumerate all DEFVAR_BOOL variables, the byte-optimizer | |
1327 may generate incorrect code.") | |
1328 | |
1329 (defun byte-optimize-lapcode (lap &optional for-effect) | |
1330 "Simple peephole optimizer. LAP is both modified and returned." | |
1331 (let (lap0 off0 | |
1332 lap1 off1 | |
1333 lap2 off2 | |
1334 (keep-going 'first-time) | |
1335 (add-depth 0) | |
1336 rest tmp tmp2 tmp3 | |
1337 (side-effect-free (if byte-compile-delete-errors | |
1338 byte-compile-side-effect-free-ops | |
1339 byte-compile-side-effect-and-error-free-ops))) | |
1340 (while keep-going | |
1341 (or (eq keep-going 'first-time) | |
1342 (byte-compile-log-lap " ---- next pass")) | |
1343 (setq rest lap | |
1344 keep-going nil) | |
1345 (while rest | |
1346 (setq lap0 (car rest) | |
1347 lap1 (nth 1 rest) | |
1348 lap2 (nth 2 rest)) | |
1349 | |
1350 ;; You may notice that sequences like "dup varset discard" are | |
1351 ;; optimized but sequences like "dup varset TAG1: discard" are not. | |
1352 ;; You may be tempted to change this; resist that temptation. | |
1353 (cond ;; | |
1354 ;; <side-effect-free> pop --> <deleted> | |
1355 ;; ...including: | |
1356 ;; const-X pop --> <deleted> | |
1357 ;; varref-X pop --> <deleted> | |
1358 ;; dup pop --> <deleted> | |
1359 ;; | |
1360 ((and (eq 'byte-discard (car lap1)) | |
1361 (memq (car lap0) side-effect-free)) | |
1362 (setq keep-going t) | |
1363 (setq tmp (aref byte-stack+-info (symbol-value (car lap0)))) | |
1364 (setq rest (cdr rest)) | |
1365 (cond ((= tmp 1) | |
1366 (byte-compile-log-lap | |
1367 " %s discard\t-->\t<deleted>" lap0) | |
1368 (setq lap (delq lap0 (delq lap1 lap)))) | |
1369 ((= tmp 0) | |
1370 (byte-compile-log-lap | |
1371 " %s discard\t-->\t<deleted> discard" lap0) | |
1372 (setq lap (delq lap0 lap))) | |
1373 ((= tmp -1) | |
1374 (byte-compile-log-lap | |
1375 " %s discard\t-->\tdiscard discard" lap0) | |
1376 (setcar lap0 'byte-discard) | |
1377 (setcdr lap0 0)) | |
1378 ((error "Optimizer error: too much on the stack")))) | |
1379 ;; | |
1380 ;; goto*-X X: --> X: | |
1381 ;; | |
1382 ((and (memq (car lap0) byte-goto-ops) | |
1383 (eq (cdr lap0) lap1)) | |
1384 (cond ((eq (car lap0) 'byte-goto) | |
1385 (setq lap (delq lap0 lap)) | |
1386 (setq tmp "<deleted>")) | |
1387 ((memq (car lap0) byte-goto-always-pop-ops) | |
1388 (setcar lap0 (setq tmp 'byte-discard)) | |
1389 (setcdr lap0 0)) | |
1390 ((error "Depth conflict at tag %d" (nth 2 lap0)))) | |
1391 (and (memq byte-optimize-log '(t byte)) | |
1392 (byte-compile-log " (goto %s) %s:\t-->\t%s %s:" | |
1393 (nth 1 lap1) (nth 1 lap1) | |
1394 tmp (nth 1 lap1))) | |
1395 (setq keep-going t)) | |
1396 ;; | |
1397 ;; varset-X varref-X --> dup varset-X | |
1398 ;; varbind-X varref-X --> dup varbind-X | |
1399 ;; const/dup varset-X varref-X --> const/dup varset-X const/dup | |
1400 ;; const/dup varbind-X varref-X --> const/dup varbind-X const/dup | |
1401 ;; The latter two can enable other optimizations. | |
1402 ;; | |
1403 ((and (eq 'byte-varref (car lap2)) | |
1404 (eq (cdr lap1) (cdr lap2)) | |
1405 (memq (car lap1) '(byte-varset byte-varbind))) | |
1406 (if (and (setq tmp (memq (car (cdr lap2)) byte-boolean-vars)) | |
1407 (not (eq (car lap0) 'byte-constant))) | |
1408 nil | |
1409 (setq keep-going t) | |
1410 (if (memq (car lap0) '(byte-constant byte-dup)) | |
1411 (progn | |
1412 (setq tmp (if (or (not tmp) | |
1413 (memq (car (cdr lap0)) '(nil t))) | |
1414 (cdr lap0) | |
1415 (byte-compile-get-constant t))) | |
1416 (byte-compile-log-lap " %s %s %s\t-->\t%s %s %s" | |
1417 lap0 lap1 lap2 lap0 lap1 | |
1418 (cons (car lap0) tmp)) | |
1419 (setcar lap2 (car lap0)) | |
1420 (setcdr lap2 tmp)) | |
1421 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1) | |
1422 (setcar lap2 (car lap1)) | |
1423 (setcar lap1 'byte-dup) | |
1424 (setcdr lap1 0) | |
1425 ;; The stack depth gets locally increased, so we will | |
1426 ;; increase maxdepth in case depth = maxdepth here. | |
1427 ;; This can cause the third argument to byte-code to | |
1428 ;; be larger than necessary. | |
1429 (setq add-depth 1)))) | |
1430 ;; | |
1431 ;; dup varset-X discard --> varset-X | |
1432 ;; dup varbind-X discard --> varbind-X | |
1433 ;; (the varbind variant can emerge from other optimizations) | |
1434 ;; | |
1435 ((and (eq 'byte-dup (car lap0)) | |
1436 (eq 'byte-discard (car lap2)) | |
1437 (memq (car lap1) '(byte-varset byte-varbind))) | |
1438 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1) | |
1439 (setq keep-going t | |
1440 rest (cdr rest)) | |
1441 (setq lap (delq lap0 (delq lap2 lap)))) | |
1442 ;; | |
1443 ;; not goto-X-if-nil --> goto-X-if-non-nil | |
1444 ;; not goto-X-if-non-nil --> goto-X-if-nil | |
1445 ;; | |
1446 ;; it is wrong to do the same thing for the -else-pop variants. | |
1447 ;; | |
1448 ((and (eq 'byte-not (car lap0)) | |
1449 (or (eq 'byte-goto-if-nil (car lap1)) | |
1450 (eq 'byte-goto-if-not-nil (car lap1)))) | |
1451 (byte-compile-log-lap " not %s\t-->\t%s" | |
1452 lap1 | |
1453 (cons | |
1454 (if (eq (car lap1) 'byte-goto-if-nil) | |
1455 'byte-goto-if-not-nil | |
1456 'byte-goto-if-nil) | |
1457 (cdr lap1))) | |
1458 (setcar lap1 (if (eq (car lap1) 'byte-goto-if-nil) | |
1459 'byte-goto-if-not-nil | |
1460 'byte-goto-if-nil)) | |
1461 (setq lap (delq lap0 lap)) | |
1462 (setq keep-going t)) | |
1463 ;; | |
1464 ;; goto-X-if-nil goto-Y X: --> goto-Y-if-non-nil X: | |
1465 ;; goto-X-if-non-nil goto-Y X: --> goto-Y-if-nil X: | |
1466 ;; | |
1467 ;; it is wrong to do the same thing for the -else-pop variants. | |
1468 ;; | |
1469 ((and (or (eq 'byte-goto-if-nil (car lap0)) | |
1470 (eq 'byte-goto-if-not-nil (car lap0))) ; gotoX | |
1471 (eq 'byte-goto (car lap1)) ; gotoY | |
1472 (eq (cdr lap0) lap2)) ; TAG X | |
1473 (let ((inverse (if (eq 'byte-goto-if-nil (car lap0)) | |
1474 'byte-goto-if-not-nil 'byte-goto-if-nil))) | |
1475 (byte-compile-log-lap " %s %s %s:\t-->\t%s %s:" | |
1476 lap0 lap1 lap2 | |
1477 (cons inverse (cdr lap1)) lap2) | |
1478 (setq lap (delq lap0 lap)) | |
1479 (setcar lap1 inverse) | |
1480 (setq keep-going t))) | |
1481 ;; | |
1482 ;; const goto-if-* --> whatever | |
1483 ;; | |
1484 ((and (eq 'byte-constant (car lap0)) | |
1485 (memq (car lap1) byte-conditional-ops)) | |
1486 (cond ((if (or (eq (car lap1) 'byte-goto-if-nil) | |
1487 (eq (car lap1) 'byte-goto-if-nil-else-pop)) | |
1488 (car (cdr lap0)) | |
1489 (not (car (cdr lap0)))) | |
1490 (byte-compile-log-lap " %s %s\t-->\t<deleted>" | |
1491 lap0 lap1) | |
1492 (setq rest (cdr rest) | |
1493 lap (delq lap0 (delq lap1 lap)))) | |
1494 (t | |
1495 (if (memq (car lap1) byte-goto-always-pop-ops) | |
1496 (progn | |
1497 (byte-compile-log-lap " %s %s\t-->\t%s" | |
1498 lap0 lap1 (cons 'byte-goto (cdr lap1))) | |
1499 (setq lap (delq lap0 lap))) | |
1500 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 | |
1501 (cons 'byte-goto (cdr lap1)))) | |
1502 (setcar lap1 'byte-goto))) | |
1503 (setq keep-going t)) | |
1504 ;; | |
1505 ;; varref-X varref-X --> varref-X dup | |
1506 ;; varref-X [dup ...] varref-X --> varref-X [dup ...] dup | |
1507 ;; We don't optimize the const-X variations on this here, | |
1508 ;; because that would inhibit some goto optimizations; we | |
1509 ;; optimize the const-X case after all other optimizations. | |
1510 ;; | |
1511 ((and (eq 'byte-varref (car lap0)) | |
1512 (progn | |
1513 (setq tmp (cdr rest)) | |
1514 (while (eq (car (car tmp)) 'byte-dup) | |
1515 (setq tmp (cdr tmp))) | |
1516 t) | |
1517 (eq (cdr lap0) (cdr (car tmp))) | |
1518 (eq 'byte-varref (car (car tmp)))) | |
1519 (if (memq byte-optimize-log '(t byte)) | |
1520 (let ((str "")) | |
1521 (setq tmp2 (cdr rest)) | |
1522 (while (not (eq tmp tmp2)) | |
1523 (setq tmp2 (cdr tmp2) | |
1524 str (concat str " dup"))) | |
1525 (byte-compile-log-lap " %s%s %s\t-->\t%s%s dup" | |
1526 lap0 str lap0 lap0 str))) | |
1527 (setq keep-going t) | |
1528 (setcar (car tmp) 'byte-dup) | |
1529 (setcdr (car tmp) 0) | |
1530 (setq rest tmp)) | |
1531 ;; | |
1532 ;; TAG1: TAG2: --> TAG1: <deleted> | |
1533 ;; (and other references to TAG2 are replaced with TAG1) | |
1534 ;; | |
1535 ((and (eq (car lap0) 'TAG) | |
1536 (eq (car lap1) 'TAG)) | |
1537 (and (memq byte-optimize-log '(t byte)) | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3138
diff
changeset
|
1538 (byte-compile-log " adjacent tags %d and %d merged" |
757 | 1539 (nth 1 lap1) (nth 1 lap0))) |
1540 (setq tmp3 lap) | |
1541 (while (setq tmp2 (rassq lap0 tmp3)) | |
1542 (setcdr tmp2 lap1) | |
1543 (setq tmp3 (cdr (memq tmp2 tmp3)))) | |
1544 (setq lap (delq lap0 lap) | |
1545 keep-going t)) | |
1546 ;; | |
1547 ;; unused-TAG: --> <deleted> | |
1548 ;; | |
1549 ((and (eq 'TAG (car lap0)) | |
1550 (not (rassq lap0 lap))) | |
1551 (and (memq byte-optimize-log '(t byte)) | |
1552 (byte-compile-log " unused tag %d removed" (nth 1 lap0))) | |
1553 (setq lap (delq lap0 lap) | |
1554 keep-going t)) | |
1555 ;; | |
1556 ;; goto ... --> goto <delete until TAG or end> | |
1557 ;; return ... --> return <delete until TAG or end> | |
1558 ;; | |
1559 ((and (memq (car lap0) '(byte-goto byte-return)) | |
1560 (not (memq (car lap1) '(TAG nil)))) | |
1561 (setq tmp rest) | |
1562 (let ((i 0) | |
1563 (opt-p (memq byte-optimize-log '(t lap))) | |
1564 str deleted) | |
1565 (while (and (setq tmp (cdr tmp)) | |
1566 (not (eq 'TAG (car (car tmp))))) | |
1567 (if opt-p (setq deleted (cons (car tmp) deleted) | |
1568 str (concat str " %s") | |
1569 i (1+ i)))) | |
1570 (if opt-p | |
1571 (let ((tagstr | |
1572 (if (eq 'TAG (car (car tmp))) | |
12638
4adf53113ec9
(byte-optimize-lapcode): Fix format calls.
Richard M. Stallman <rms@gnu.org>
parents:
12550
diff
changeset
|
1573 (format "%d:" (car (cdr (car tmp)))) |
757 | 1574 (or (car tmp) "")))) |
1575 (if (< i 6) | |
1576 (apply 'byte-compile-log-lap-1 | |
1577 (concat " %s" str | |
1578 " %s\t-->\t%s <deleted> %s") | |
1579 lap0 | |
1580 (nconc (nreverse deleted) | |
1581 (list tagstr lap0 tagstr))) | |
1582 (byte-compile-log-lap | |
1583 " %s <%d unreachable op%s> %s\t-->\t%s <deleted> %s" | |
1584 lap0 i (if (= i 1) "" "s") | |
1585 tagstr lap0 tagstr)))) | |
1586 (rplacd rest tmp)) | |
1587 (setq keep-going t)) | |
1588 ;; | |
1589 ;; <safe-op> unbind --> unbind <safe-op> | |
1590 ;; (this may enable other optimizations.) | |
1591 ;; | |
1592 ((and (eq 'byte-unbind (car lap1)) | |
1593 (memq (car lap0) byte-after-unbind-ops)) | |
1594 (byte-compile-log-lap " %s %s\t-->\t%s %s" lap0 lap1 lap1 lap0) | |
1595 (setcar rest lap1) | |
1596 (setcar (cdr rest) lap0) | |
1597 (setq keep-going t)) | |
1598 ;; | |
1599 ;; varbind-X unbind-N --> discard unbind-(N-1) | |
1600 ;; save-excursion unbind-N --> unbind-(N-1) | |
1601 ;; save-restriction unbind-N --> unbind-(N-1) | |
1602 ;; | |
1603 ((and (eq 'byte-unbind (car lap1)) | |
1604 (memq (car lap0) '(byte-varbind byte-save-excursion | |
1605 byte-save-restriction)) | |
1606 (< 0 (cdr lap1))) | |
1607 (if (zerop (setcdr lap1 (1- (cdr lap1)))) | |
1608 (delq lap1 rest)) | |
1609 (if (eq (car lap0) 'byte-varbind) | |
1610 (setcar rest (cons 'byte-discard 0)) | |
1611 (setq lap (delq lap0 lap))) | |
1612 (byte-compile-log-lap " %s %s\t-->\t%s %s" | |
1613 lap0 (cons (car lap1) (1+ (cdr lap1))) | |
1614 (if (eq (car lap0) 'byte-varbind) | |
1615 (car rest) | |
1616 (car (cdr rest))) | |
1617 (if (and (/= 0 (cdr lap1)) | |
1618 (eq (car lap0) 'byte-varbind)) | |
1619 (car (cdr rest)) | |
1620 "")) | |
1621 (setq keep-going t)) | |
1622 ;; | |
1623 ;; goto*-X ... X: goto-Y --> goto*-Y | |
1624 ;; goto-X ... X: return --> return | |
1625 ;; | |
1626 ((and (memq (car lap0) byte-goto-ops) | |
1627 (memq (car (setq tmp (nth 1 (memq (cdr lap0) lap)))) | |
1628 '(byte-goto byte-return))) | |
1629 (cond ((and (not (eq tmp lap0)) | |
1630 (or (eq (car lap0) 'byte-goto) | |
1631 (eq (car tmp) 'byte-goto))) | |
1632 (byte-compile-log-lap " %s [%s]\t-->\t%s" | |
1633 (car lap0) tmp tmp) | |
1634 (if (eq (car tmp) 'byte-return) | |
1635 (setcar lap0 'byte-return)) | |
1636 (setcdr lap0 (cdr tmp)) | |
1637 (setq keep-going t)))) | |
1638 ;; | |
1639 ;; goto-*-else-pop X ... X: goto-if-* --> whatever | |
1640 ;; goto-*-else-pop X ... X: discard --> whatever | |
1641 ;; | |
1642 ((and (memq (car lap0) '(byte-goto-if-nil-else-pop | |
1643 byte-goto-if-not-nil-else-pop)) | |
1644 (memq (car (car (setq tmp (cdr (memq (cdr lap0) lap))))) | |
1645 (eval-when-compile | |
1646 (cons 'byte-discard byte-conditional-ops))) | |
1647 (not (eq lap0 (car tmp)))) | |
1648 (setq tmp2 (car tmp)) | |
1649 (setq tmp3 (assq (car lap0) '((byte-goto-if-nil-else-pop | |
1650 byte-goto-if-nil) | |
1651 (byte-goto-if-not-nil-else-pop | |
1652 byte-goto-if-not-nil)))) | |
1653 (if (memq (car tmp2) tmp3) | |
1654 (progn (setcar lap0 (car tmp2)) | |
1655 (setcdr lap0 (cdr tmp2)) | |
1656 (byte-compile-log-lap " %s-else-pop [%s]\t-->\t%s" | |
1657 (car lap0) tmp2 lap0)) | |
1658 ;; Get rid of the -else-pop's and jump one step further. | |
1659 (or (eq 'TAG (car (nth 1 tmp))) | |
1660 (setcdr tmp (cons (byte-compile-make-tag) | |
1661 (cdr tmp)))) | |
1662 (byte-compile-log-lap " %s [%s]\t-->\t%s <skip>" | |
1663 (car lap0) tmp2 (nth 1 tmp3)) | |
1664 (setcar lap0 (nth 1 tmp3)) | |
1665 (setcdr lap0 (nth 1 tmp))) | |
1666 (setq keep-going t)) | |
1667 ;; | |
1668 ;; const goto-X ... X: goto-if-* --> whatever | |
1669 ;; const goto-X ... X: discard --> whatever | |
1670 ;; | |
1671 ((and (eq (car lap0) 'byte-constant) | |
1672 (eq (car lap1) 'byte-goto) | |
1673 (memq (car (car (setq tmp (cdr (memq (cdr lap1) lap))))) | |
1674 (eval-when-compile | |
1675 (cons 'byte-discard byte-conditional-ops))) | |
1676 (not (eq lap1 (car tmp)))) | |
1677 (setq tmp2 (car tmp)) | |
1678 (cond ((memq (car tmp2) | |
1679 (if (null (car (cdr lap0))) | |
1680 '(byte-goto-if-nil byte-goto-if-nil-else-pop) | |
1681 '(byte-goto-if-not-nil | |
1682 byte-goto-if-not-nil-else-pop))) | |
1683 (byte-compile-log-lap " %s goto [%s]\t-->\t%s %s" | |
1684 lap0 tmp2 lap0 tmp2) | |
1685 (setcar lap1 (car tmp2)) | |
1686 (setcdr lap1 (cdr tmp2)) | |
1687 ;; Let next step fix the (const,goto-if*) sequence. | |
1688 (setq rest (cons nil rest))) | |
1689 (t | |
1690 ;; Jump one step further | |
1691 (byte-compile-log-lap | |
1692 " %s goto [%s]\t-->\t<deleted> goto <skip>" | |
1693 lap0 tmp2) | |
1694 (or (eq 'TAG (car (nth 1 tmp))) | |
1695 (setcdr tmp (cons (byte-compile-make-tag) | |
1696 (cdr tmp)))) | |
1697 (setcdr lap1 (car (cdr tmp))) | |
1698 (setq lap (delq lap0 lap)))) | |
1699 (setq keep-going t)) | |
1700 ;; | |
1701 ;; X: varref-Y ... varset-Y goto-X --> | |
1702 ;; X: varref-Y Z: ... dup varset-Y goto-Z | |
1703 ;; (varset-X goto-BACK, BACK: varref-X --> copy the varref down.) | |
1704 ;; (This is so usual for while loops that it is worth handling). | |
1705 ;; | |
1706 ((and (eq (car lap1) 'byte-varset) | |
1707 (eq (car lap2) 'byte-goto) | |
1708 (not (memq (cdr lap2) rest)) ;Backwards jump | |
1709 (eq (car (car (setq tmp (cdr (memq (cdr lap2) lap))))) | |
1710 'byte-varref) | |
1711 (eq (cdr (car tmp)) (cdr lap1)) | |
1712 (not (memq (car (cdr lap1)) byte-boolean-vars))) | |
1713 ;;(byte-compile-log-lap " Pulled %s to end of loop" (car tmp)) | |
1714 (let ((newtag (byte-compile-make-tag))) | |
1715 (byte-compile-log-lap | |
1716 " %s: %s ... %s %s\t-->\t%s: %s %s: ... %s %s %s" | |
1717 (nth 1 (cdr lap2)) (car tmp) | |
1718 lap1 lap2 | |
1719 (nth 1 (cdr lap2)) (car tmp) | |
1720 (nth 1 newtag) 'byte-dup lap1 | |
1721 (cons 'byte-goto newtag) | |
1722 ) | |
1723 (setcdr rest (cons (cons 'byte-dup 0) (cdr rest))) | |
1724 (setcdr tmp (cons (setcdr lap2 newtag) (cdr tmp)))) | |
1725 (setq add-depth 1) | |
1726 (setq keep-going t)) | |
1727 ;; | |
1728 ;; goto-X Y: ... X: goto-if*-Y --> goto-if-not-*-X+1 Y: | |
1729 ;; (This can pull the loop test to the end of the loop) | |
1730 ;; | |
1731 ((and (eq (car lap0) 'byte-goto) | |
1732 (eq (car lap1) 'TAG) | |
1733 (eq lap1 | |
1734 (cdr (car (setq tmp (cdr (memq (cdr lap0) lap)))))) | |
1735 (memq (car (car tmp)) | |
1736 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil | |
1737 byte-goto-if-nil-else-pop))) | |
1738 ;; (byte-compile-log-lap " %s %s, %s %s --> moved conditional" | |
1739 ;; lap0 lap1 (cdr lap0) (car tmp)) | |
1740 (let ((newtag (byte-compile-make-tag))) | |
1741 (byte-compile-log-lap | |
1742 "%s %s: ... %s: %s\t-->\t%s ... %s:" | |
1743 lap0 (nth 1 lap1) (nth 1 (cdr lap0)) (car tmp) | |
1744 (cons (cdr (assq (car (car tmp)) | |
1745 '((byte-goto-if-nil . byte-goto-if-not-nil) | |
1746 (byte-goto-if-not-nil . byte-goto-if-nil) | |
1747 (byte-goto-if-nil-else-pop . | |
1748 byte-goto-if-not-nil-else-pop) | |
1749 (byte-goto-if-not-nil-else-pop . | |
1750 byte-goto-if-nil-else-pop)))) | |
1751 newtag) | |
1752 | |
1753 (nth 1 newtag) | |
1754 ) | |
1755 (setcdr tmp (cons (setcdr lap0 newtag) (cdr tmp))) | |
1756 (if (eq (car (car tmp)) 'byte-goto-if-nil-else-pop) | |
1757 ;; We can handle this case but not the -if-not-nil case, | |
1758 ;; because we won't know which non-nil constant to push. | |
1759 (setcdr rest (cons (cons 'byte-constant | |
1760 (byte-compile-get-constant nil)) | |
1761 (cdr rest)))) | |
1762 (setcar lap0 (nth 1 (memq (car (car tmp)) | |
1763 '(byte-goto-if-nil-else-pop | |
1764 byte-goto-if-not-nil | |
1765 byte-goto-if-nil | |
1766 byte-goto-if-not-nil | |
1767 byte-goto byte-goto)))) | |
1768 ) | |
1769 (setq keep-going t)) | |
1770 ) | |
1771 (setq rest (cdr rest))) | |
1772 ) | |
1773 ;; Cleanup stage: | |
1774 ;; Rebuild byte-compile-constants / byte-compile-variables. | |
1775 ;; Simple optimizations that would inhibit other optimizations if they | |
1776 ;; were done in the optimizing loop, and optimizations which there is no | |
1777 ;; need to do more than once. | |
1778 (setq byte-compile-constants nil | |
1779 byte-compile-variables nil) | |
1780 (setq rest lap) | |
1781 (while rest | |
1782 (setq lap0 (car rest) | |
1783 lap1 (nth 1 rest)) | |
1784 (if (memq (car lap0) byte-constref-ops) | |
1785 (if (eq (cdr lap0) 'byte-constant) | |
1786 (or (memq (cdr lap0) byte-compile-variables) | |
1787 (setq byte-compile-variables (cons (cdr lap0) | |
1788 byte-compile-variables))) | |
1789 (or (memq (cdr lap0) byte-compile-constants) | |
1790 (setq byte-compile-constants (cons (cdr lap0) | |
1791 byte-compile-constants))))) | |
1792 (cond (;; | |
1793 ;; const-C varset-X const-C --> const-C dup varset-X | |
1794 ;; const-C varbind-X const-C --> const-C dup varbind-X | |
1795 ;; | |
1796 (and (eq (car lap0) 'byte-constant) | |
1797 (eq (car (nth 2 rest)) 'byte-constant) | |
1798 (eq (cdr lap0) (car (nth 2 rest))) | |
1799 (memq (car lap1) '(byte-varbind byte-varset))) | |
1800 (byte-compile-log-lap " %s %s %s\t-->\t%s dup %s" | |
1801 lap0 lap1 lap0 lap0 lap1) | |
1802 (setcar (cdr (cdr rest)) (cons (car lap1) (cdr lap1))) | |
1803 (setcar (cdr rest) (cons 'byte-dup 0)) | |
1804 (setq add-depth 1)) | |
1805 ;; | |
1806 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup | |
1807 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup | |
1808 ;; | |
1809 ((memq (car lap0) '(byte-constant byte-varref)) | |
1810 (setq tmp rest | |
1811 tmp2 nil) | |
1812 (while (progn | |
1813 (while (eq 'byte-dup (car (car (setq tmp (cdr tmp)))))) | |
1814 (and (eq (cdr lap0) (cdr (car tmp))) | |
1815 (eq (car lap0) (car (car tmp))))) | |
1816 (setcar tmp (cons 'byte-dup 0)) | |
1817 (setq tmp2 t)) | |
1818 (if tmp2 | |
1819 (byte-compile-log-lap | |
12638
4adf53113ec9
(byte-optimize-lapcode): Fix format calls.
Richard M. Stallman <rms@gnu.org>
parents:
12550
diff
changeset
|
1820 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0))) |
757 | 1821 ;; |
1822 ;; unbind-N unbind-M --> unbind-(N+M) | |
1823 ;; | |
1824 ((and (eq 'byte-unbind (car lap0)) | |
1825 (eq 'byte-unbind (car lap1))) | |
1826 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 | |
1827 (cons 'byte-unbind | |
1828 (+ (cdr lap0) (cdr lap1)))) | |
1829 (setq keep-going t) | |
1830 (setq lap (delq lap0 lap)) | |
1831 (setcdr lap1 (+ (cdr lap1) (cdr lap0)))) | |
1832 ) | |
1833 (setq rest (cdr rest))) | |
1834 (setq byte-compile-maxdepth (+ byte-compile-maxdepth add-depth))) | |
1835 lap) | |
1836 | |
1837 (provide 'byte-optimize) | |
1838 | |
1839 | |
1840 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when this file compiles | |
1841 ;; itself, compile some of its most used recursive functions (at load time). | |
1842 ;; | |
1843 (eval-when-compile | |
1818
7e3322619e46
compiled-function-p has been renamed to byte-code-function-p.
Jim Blandy <jimb@redhat.com>
parents:
957
diff
changeset
|
1844 (or (byte-code-function-p (symbol-function 'byte-optimize-form)) |
757 | 1845 (assq 'byte-code (symbol-function 'byte-optimize-form)) |
1846 (let ((byte-optimize nil) | |
1847 (byte-compile-warnings nil)) | |
1848 (mapcar '(lambda (x) | |
1849 (or noninteractive (message "compiling %s..." x)) | |
1850 (byte-compile x) | |
1851 (or noninteractive (message "compiling %s...done" x))) | |
1852 '(byte-optimize-form | |
1853 byte-optimize-body | |
1854 byte-optimize-predicate | |
1855 byte-optimize-binary-predicate | |
1856 ;; Inserted some more than necessary, to speed it up. | |
1857 byte-optimize-form-code-walker | |
1858 byte-optimize-lapcode)))) | |
1859 nil) | |
848 | 1860 |
1861 ;;; byte-opt.el ends here |