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