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