Mercurial > hg > xemacs-beta
annotate lisp/byte-optimize.el @ 5656:e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
lisp/ChangeLog addition:
2012-05-05 Aidan Kehoe <kehoea@parhasard.net>
Co-operate with the byte-optimizer in the bytecomp.el labels
implementation, don't work against it.
* byte-optimize.el:
* byte-optimize.el (byte-compile-inline-expand):
Call #'byte-compile-unfold-lambda explicitly here, don't assume
that the byte-optimizer will do it.
* byte-optimize.el (byte-compile-unfold-lambda):
Call #'byte-optimize-body on the body, don't just mapcar
#'byte-optimize-form along it.
* byte-optimize.el (byte-optimize-lambda): New. Optimize a lambda
form.
* byte-optimize.el (byte-optimize-form-code-walker):
Descend lambda expressions, defun, and defmacro, relevant for
lexically-oriented operators like #'labels.
* byte-optimize.el (byte-optimize-body): Only return a non-eq
object if we've actually optimized something
* bytecomp.el (byte-compile-initial-macro-environment):
In the labels implementation, work with the byte optimizer, not
against it; warn when labels are defined but not used,
automatically inline labels that are used only once.
* bytecomp.el (byte-recompile-directory):
No need to wrap #'byte-compile-report-error in a lambda with
#'call-with-condition-handler here.
* bytecomp.el (byte-compile-form):
Don't inline compiled-function objects, they're probably labels.
* bytecomp.el (byte-compile-funcall):
No longer inline lambdas, trust the byte optimizer to have done it
properly, even for labels.
* cl-extra.el (cl-macroexpand-all):
Treat labels established by the byte compiler distinctly from
those established by cl-macs.el.
* cl-macs.el (cl-do-proclaim):
Treat labels established by the byte compiler distinctly from
those established by cl-macs.el.
* gui.el (make-gui-button):
When referring to the #'gui-button-action label, quote it using
function, otherwise there's a warning from the byte compiler.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sat, 05 May 2012 20:48:24 +0100 |
parents | cc6f0266bc36 |
children | 2a870a7b86bd |
rev | line source |
---|---|
428 | 1 ;;; byte-optimize.el --- the optimization passes of the emacs-lisp byte compiler. |
2 | |
3 ;;; Copyright (c) 1991, 1994 Free Software Foundation, Inc. | |
4 | |
446 | 5 ;; Authors: Jamie Zawinski <jwz@jwz.org> |
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no> | |
7 ;; Martin Buchholz <martin@xemacs.org> | |
428 | 8 ;; Keywords: internal |
9 | |
10 ;; This file is part of XEmacs. | |
11 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
12 ;; XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
13 ;; under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
14 ;; Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
15 ;; option) any later version. |
428 | 16 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
20 ;; for more details. |
428 | 21 |
22 ;; You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
428 | 24 |
1297 | 25 ;;; Synched up with: FSF 20.7 except where marked. |
26 ;;; [[ Synched up with: FSF 20.7. ]] | |
27 ;;; DO NOT PUT IN AN INVALID SYNC MESSAGE WHEN YOU DO A PARTIAL SYNC. --ben | |
28 | |
29 ;; BEGIN SYNC WITH 20.7. | |
428 | 30 |
31 ;;; Commentary: | |
32 | |
33 ;; ======================================================================== | |
34 ;; "No matter how hard you try, you can't make a racehorse out of a pig. | |
35 ;; You can, however, make a faster pig." | |
36 ;; | |
37 ;; Or, to put it another way, the emacs byte compiler is a VW Bug. This code | |
440 | 38 ;; makes it be a VW Bug with fuel injection and a turbocharger... You're |
428 | 39 ;; still not going to make it go faster than 70 mph, but it might be easier |
40 ;; to get it there. | |
41 ;; | |
42 | |
43 ;; TO DO: | |
44 ;; | |
45 ;; (apply #'(lambda (x &rest y) ...) 1 (foo)) | |
46 ;; | |
47 ;; maintain a list of functions known not to access any global variables | |
48 ;; (actually, give them a 'dynamically-safe property) and then | |
49 ;; (let ( v1 v2 ... vM vN ) <...dynamically-safe...> ) ==> | |
50 ;; (let ( v1 v2 ... vM ) vN <...dynamically-safe...> ) | |
51 ;; by recursing on this, we might be able to eliminate the entire let. | |
52 ;; However certain variables should never have their bindings optimized | |
53 ;; away, because they affect everything. | |
54 ;; (put 'debug-on-error 'binding-is-magic t) | |
55 ;; (put 'debug-on-abort 'binding-is-magic t) | |
56 ;; (put 'debug-on-next-call 'binding-is-magic t) | |
57 ;; (put 'inhibit-quit 'binding-is-magic t) | |
58 ;; (put 'quit-flag 'binding-is-magic t) | |
59 ;; (put 't 'binding-is-magic t) | |
60 ;; (put 'nil 'binding-is-magic t) | |
61 ;; possibly also | |
62 ;; (put 'gc-cons-threshold 'binding-is-magic t) | |
63 ;; (put 'track-mouse 'binding-is-magic t) | |
64 ;; others? | |
65 ;; | |
66 ;; Simple defsubsts often produce forms like | |
67 ;; (let ((v1 (f1)) (v2 (f2)) ...) | |
68 ;; (FN v1 v2 ...)) | |
440 | 69 ;; It would be nice if we could optimize this to |
428 | 70 ;; (FN (f1) (f2) ...) |
71 ;; but we can't unless FN is dynamically-safe (it might be dynamically | |
72 ;; referring to the bindings that the lambda arglist established.) | |
73 ;; One of the uncountable lossages introduced by dynamic scope... | |
74 ;; | |
440 | 75 ;; Maybe there should be a control-structure that says "turn on |
428 | 76 ;; fast-and-loose type-assumptive optimizations here." Then when |
77 ;; we see a form like (car foo) we can from then on assume that | |
78 ;; the variable foo is of type cons, and optimize based on that. | |
440 | 79 ;; But, this won't win much because of (you guessed it) dynamic |
428 | 80 ;; scope. Anything down the stack could change the value. |
81 ;; (Another reason it doesn't work is that it is perfectly valid | |
82 ;; to call car with a null argument.) A better approach might | |
83 ;; be to allow type-specification of the form | |
84 ;; (put 'foo 'arg-types '(float (list integer) dynamic)) | |
85 ;; (put 'foo 'result-type 'bool) | |
86 ;; It should be possible to have these types checked to a certain | |
87 ;; degree. | |
88 ;; | |
89 ;; collapse common subexpressions | |
90 ;; | |
91 ;; It would be nice if redundant sequences could be factored out as well, | |
92 ;; when they are known to have no side-effects: | |
93 ;; (list (+ a b c) (+ a b c)) --> a b add c add dup list-2 | |
94 ;; but beware of traps like | |
95 ;; (cons (list x y) (list x y)) | |
96 ;; | |
97 ;; Tail-recursion elimination is not really possible in Emacs Lisp. | |
98 ;; Tail-recursion elimination is almost always impossible when all variables | |
99 ;; have dynamic scope, but given that the "return" byteop requires the | |
100 ;; binding stack to be empty (rather than emptying it itself), there can be | |
101 ;; no truly tail-recursive Emacs Lisp functions that take any arguments or | |
102 ;; make any bindings. | |
103 ;; | |
104 ;; Here is an example of an Emacs Lisp function which could safely be | |
105 ;; byte-compiled tail-recursively: | |
106 ;; | |
107 ;; (defun tail-map (fn list) | |
108 ;; (cond (list | |
109 ;; (funcall fn (car list)) | |
110 ;; (tail-map fn (cdr list))))) | |
111 ;; | |
112 ;; However, if there was even a single let-binding around the COND, | |
113 ;; it could not be byte-compiled, because there would be an "unbind" | |
440 | 114 ;; byte-op between the final "call" and "return." Adding a |
428 | 115 ;; Bunbind_all byteop would fix this. |
116 ;; | |
117 ;; (defun foo (x y z) ... (foo a b c)) | |
118 ;; ... (const foo) (varref a) (varref b) (varref c) (call 3) END: (return) | |
119 ;; ... (varref a) (varbind x) (varref b) (varbind y) (varref c) (varbind z) (goto 0) END: (unbind-all) (return) | |
120 ;; ... (varref a) (varset x) (varref b) (varset y) (varref c) (varset z) (goto 0) END: (return) | |
121 ;; | |
122 ;; this also can be considered tail recursion: | |
123 ;; | |
124 ;; ... (const foo) (varref a) (call 1) (goto X) ... X: (return) | |
125 ;; could generalize this by doing the optimization | |
126 ;; (goto X) ... X: (return) --> (return) | |
127 ;; | |
128 ;; But this doesn't solve all of the problems: although by doing tail- | |
129 ;; recursion elimination in this way, the call-stack does not grow, the | |
130 ;; binding-stack would grow with each recursive step, and would eventually | |
131 ;; overflow. I don't believe there is any way around this without lexical | |
132 ;; scope. | |
133 ;; | |
134 ;; Wouldn't it be nice if Emacs Lisp had lexical scope. | |
135 ;; | |
440 | 136 ;; Idea: the form (lexical-scope) in a file means that the file may be |
137 ;; compiled lexically. This proclamation is file-local. Then, within | |
428 | 138 ;; that file, "let" would establish lexical bindings, and "let-dynamic" |
139 ;; would do things the old way. (Or we could use CL "declare" forms.) | |
140 ;; We'd have to notice defvars and defconsts, since those variables should | |
141 ;; always be dynamic, and attempting to do a lexical binding of them | |
142 ;; should simply do a dynamic binding instead. | |
143 ;; But! We need to know about variables that were not necessarily defvarred | |
144 ;; in the file being compiled (doing a boundp check isn't good enough.) | |
145 ;; Fdefvar() would have to be modified to add something to the plist. | |
146 ;; | |
440 | 147 ;; A major disadvantage of this scheme is that the interpreter and compiler |
148 ;; would have different semantics for files compiled with (dynamic-scope). | |
428 | 149 ;; Since this would be a file-local optimization, there would be no way to |
440 | 150 ;; modify the interpreter to obey this (unless the loader was hacked |
428 | 151 ;; in some grody way, but that's a really bad idea.) |
152 ;; | |
153 ;; HA! RMS removed the following paragraph from his version of | |
154 ;; byte-optimize.el. | |
155 ;; | |
156 ;; Really the Right Thing is to make lexical scope the default across | |
440 | 157 ;; the board, in the interpreter and compiler, and just FIX all of |
428 | 158 ;; the code that relies on dynamic scope of non-defvarred variables. |
159 | |
160 ;; Other things to consider: | |
161 | |
162 ;; Associative math should recognize subcalls to identical function: | |
163 ;;(disassemble #'(lambda (x) (+ (+ (foo) 1) (+ (bar) 2)))) | |
164 ;; This should generate the same as (1+ x) and (1- x) | |
165 | |
166 ;;(disassemble #'(lambda (x) (cons (+ x 1) (- x 1)))) | |
167 ;; An awful lot of functions always return a non-nil value. If they're | |
168 ;; error free also they may act as true-constants. | |
169 | |
170 ;;(disassemble #'(lambda (x) (and (point) (foo)))) | |
440 | 171 ;; When |
428 | 172 ;; - all but one arguments to a function are constant |
173 ;; - the non-constant argument is an if-expression (cond-expression?) | |
174 ;; then the outer function can be distributed. If the guarding | |
175 ;; condition is side-effect-free [assignment-free] then the other | |
176 ;; arguments may be any expressions. Since, however, the code size | |
177 ;; can increase this way they should be "simple". Compare: | |
178 | |
179 ;;(disassemble #'(lambda (x) (eq (if (point) 'a 'b) 'c))) | |
180 ;;(disassemble #'(lambda (x) (if (point) (eq 'a 'c) (eq 'b 'c)))) | |
181 | |
444 | 182 ;; (car (cons A B)) -> (prog1 A B) |
428 | 183 ;;(disassemble #'(lambda (x) (car (cons (foo) 42)))) |
184 | |
185 ;; (cdr (cons A B)) -> (progn A B) | |
186 ;;(disassemble #'(lambda (x) (cdr (cons 42 (foo))))) | |
187 | |
444 | 188 ;; (car (list A B ...)) -> (prog1 A ... B) |
428 | 189 ;;(disassemble #'(lambda (x) (car (list (foo) 42 (bar))))) |
190 | |
191 ;; (cdr (list A B ...)) -> (progn A (list B ...)) | |
192 ;;(disassemble #'(lambda (x) (cdr (list 42 (foo) (bar))))) | |
193 | |
194 | |
195 ;;; Code: | |
196 | |
197 (require 'byte-compile "bytecomp") | |
198 | |
199 (defun byte-compile-log-lap-1 (format &rest args) | |
200 (if (aref byte-code-vector 0) | |
201 (error "The old version of the disassembler is loaded. Reload new-bytecomp as well.")) | |
202 (byte-compile-log-1 | |
203 (apply 'format format | |
204 (let (c a) | |
205 (mapcar | |
206 #'(lambda (arg) | |
207 (if (not (consp arg)) | |
208 (if (and (symbolp arg) | |
209 (string-match "^byte-" (symbol-name arg))) | |
210 (intern (substring (symbol-name arg) 5)) | |
211 arg) | |
212 (if (integerp (setq c (car arg))) | |
213 (error "non-symbolic byte-op %s" c)) | |
214 (if (eq c 'TAG) | |
215 (setq c arg) | |
216 (setq a (cond ((memq c byte-goto-ops) | |
217 (car (cdr (cdr arg)))) | |
218 ((memq c byte-constref-ops) | |
219 (car (cdr arg))) | |
220 (t (cdr arg)))) | |
221 (setq c (symbol-name c)) | |
222 (if (string-match "^byte-." c) | |
223 (setq c (intern (substring c 5))))) | |
224 (if (eq c 'constant) (setq c 'const)) | |
225 (if (and (eq (cdr arg) 0) | |
226 (not (memq c '(unbind call const)))) | |
227 c | |
228 (format "(%s %s)" c a)))) | |
229 args))))) | |
230 | |
231 (defmacro byte-compile-log-lap (format-string &rest args) | |
232 (list 'and | |
233 '(memq byte-optimize-log '(t byte)) | |
234 (cons 'byte-compile-log-lap-1 | |
235 (cons format-string args)))) | |
236 | |
237 | |
238 ;;; byte-compile optimizers to support inlining | |
239 | |
240 (put 'inline 'byte-optimizer 'byte-optimize-inline-handler) | |
241 | |
242 (defun byte-optimize-inline-handler (form) | |
243 "byte-optimize-handler for the `inline' special-form." | |
244 (cons | |
245 'progn | |
246 (mapcar | |
247 #'(lambda (sexp) | |
248 (let ((fn (car-safe sexp))) | |
249 (if (and (symbolp fn) | |
250 (or (cdr (assq fn byte-compile-function-environment)) | |
251 (and (fboundp fn) | |
252 (not (or (cdr (assq fn byte-compile-macro-environment)) | |
253 (and (consp (setq fn (symbol-function fn))) | |
254 (eq (car fn) 'macro)) | |
255 (subrp fn)))))) | |
256 (byte-compile-inline-expand sexp) | |
257 sexp))) | |
258 (cdr form)))) | |
259 | |
260 | |
261 ;; Splice the given lap code into the current instruction stream. | |
262 ;; If it has any labels in it, you're responsible for making sure there | |
263 ;; are no collisions, and that byte-compile-tag-number is reasonable | |
264 ;; after this is spliced in. The provided list is destroyed. | |
265 (defun byte-inline-lapcode (lap) | |
266 (setq byte-compile-output (nconc (nreverse lap) byte-compile-output))) | |
267 | |
268 | |
269 (defun byte-compile-inline-expand (form) | |
270 (let* ((name (car form)) | |
271 (fn (or (cdr (assq name byte-compile-function-environment)) | |
272 (and (fboundp name) (symbol-function name))))) | |
273 (if (null fn) | |
274 (progn | |
275 (byte-compile-warn "attempt to inline %s before it was defined" name) | |
276 form) | |
277 ;; else | |
278 (if (and (consp fn) (eq (car fn) 'autoload)) | |
279 (progn | |
280 (load (nth 1 fn)) | |
281 (setq fn (or (cdr (assq name byte-compile-function-environment)) | |
282 (and (fboundp name) (symbol-function name)))))) | |
283 (if (and (consp fn) (eq (car fn) 'autoload)) | |
284 (error "file \"%s\" didn't define \"%s\"" (nth 1 fn) name)) | |
285 (if (symbolp fn) | |
286 (byte-compile-inline-expand (cons fn (cdr form))) | |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
287 (if (or (eq (car-safe fn) 'lambda) (compiled-function-p fn)) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
288 (byte-compile-unfold-lambda (cons fn (cdr form))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
289 ;; Give up on inlining. |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
290 form))))) |
428 | 291 |
292 ;;; ((lambda ...) ...) | |
440 | 293 ;;; |
428 | 294 (defun byte-compile-unfold-lambda (form &optional name) |
295 (or name (setq name "anonymous lambda")) | |
296 (let ((lambda (car form)) | |
297 (values (cdr form))) | |
298 (if (compiled-function-p lambda) | |
299 (setq lambda (list 'lambda (compiled-function-arglist lambda) | |
300 (list 'byte-code | |
301 (compiled-function-instructions lambda) | |
302 (compiled-function-constants lambda) | |
303 (compiled-function-stack-depth lambda))))) | |
304 (let ((arglist (nth 1 lambda)) | |
305 (body (cdr (cdr lambda))) | |
306 optionalp restp | |
307 bindings) | |
308 (if (and (stringp (car body)) (cdr body)) | |
309 (setq body (cdr body))) | |
310 (if (and (consp (car body)) (eq 'interactive (car (car body)))) | |
311 (setq body (cdr body))) | |
312 (while arglist | |
313 (cond ((eq (car arglist) '&optional) | |
314 ;; ok, I'll let this slide because funcall_lambda() does... | |
315 ;; (if optionalp (error "multiple &optional keywords in %s" name)) | |
316 (if restp (error "&optional found after &rest in %s" name)) | |
317 (if (null (cdr arglist)) | |
318 (error "nothing after &optional in %s" name)) | |
319 (setq optionalp t)) | |
320 ((eq (car arglist) '&rest) | |
321 ;; ...but it is by no stretch of the imagination a reasonable | |
322 ;; thing that funcall_lambda() allows (&rest x y) and | |
323 ;; (&rest x &optional y) in arglists. | |
324 (if (null (cdr arglist)) | |
325 (error "nothing after &rest in %s" name)) | |
326 (if (cdr (cdr arglist)) | |
327 (error "multiple vars after &rest in %s" name)) | |
328 (setq restp t)) | |
329 (restp | |
330 (setq bindings (cons (list (car arglist) | |
331 (and values (cons 'list values))) | |
332 bindings) | |
333 values nil)) | |
334 ((and (not optionalp) (null values)) | |
335 (byte-compile-warn "attempt to open-code %s with too few arguments" name) | |
336 (setq arglist nil values 'too-few)) | |
337 (t | |
338 (setq bindings (cons (list (car arglist) (car values)) | |
339 bindings) | |
340 values (cdr values)))) | |
341 (setq arglist (cdr arglist))) | |
342 (if values | |
343 (progn | |
344 (or (eq values 'too-few) | |
345 (byte-compile-warn | |
346 "attempt to open-code %s with too many arguments" name)) | |
347 form) | |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
348 (setq body (byte-optimize-body body nil)) |
440 | 349 (let ((newform |
428 | 350 (if bindings |
351 (cons 'let (cons (nreverse bindings) body)) | |
352 (cons 'progn body)))) | |
353 (byte-compile-log " %s\t==>\t%s" form newform) | |
354 newform))))) | |
355 | |
356 | |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
357 (defun byte-optimize-lambda (form) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
358 (let* ((offset 2) (body (nthcdr offset form))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
359 (if (stringp (car body)) (setq body (nthcdr (incf offset) form))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
360 (if (eq 'interactive (car-safe (car body))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
361 (setq body (nthcdr (incf offset) form))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
362 (if (eq body (setq body (byte-optimize-body body nil))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
363 form |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
364 (nconc (subseq form 0 offset) body)))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
365 |
428 | 366 ;;; implementing source-level optimizers |
367 | |
368 (defun byte-optimize-form-code-walker (form for-effect) | |
369 ;; | |
370 ;; For normal function calls, We can just mapcar the optimizer the cdr. But | |
4905
755ae5b97edb
Change "special form" to "special operator" in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4885
diff
changeset
|
371 ;; we need to have special knowledge of the syntax of the special operators |
755ae5b97edb
Change "special form" to "special operator" in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4885
diff
changeset
|
372 ;; like let and defun (that's why they're special operators :-). (Actually, |
428 | 373 ;; the important aspect is that they are subrs that don't evaluate all of |
374 ;; their args.) | |
375 ;; | |
376 (let ((fn (car-safe form)) | |
377 tmp) | |
378 (cond ((not (consp form)) | |
379 (if (not (and for-effect | |
380 (or byte-compile-delete-errors | |
381 (not (symbolp form)) | |
382 (eq form t)))) | |
383 form)) | |
384 ((eq fn 'quote) | |
385 (if (cdr (cdr form)) | |
386 (byte-compile-warn "malformed quote form: %s" | |
387 (prin1-to-string form))) | |
388 ;; map (quote nil) to nil to simplify optimizer logic. | |
389 ;; map quoted constants to nil if for-effect (just because). | |
390 (and (nth 1 form) | |
391 (not for-effect) | |
392 form)) | |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
393 ((eq fn 'function) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
394 (when (cddr form) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
395 (byte-compile-warn "malformed function form: %S" form)) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
396 (cond |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
397 (for-effect nil) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
398 ((and (eq (car-safe (cadr form)) 'lambda) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
399 (not (eq (cadr form) (setq tmp (byte-optimize-lambda |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
400 (cadr form)))))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
401 (list fn tmp)) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
402 (t form))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
403 ((and (eq 'lambda (car-safe fn)) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
404 (not (eq form (setq form (byte-compile-unfold-lambda form))))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
405 form) |
428 | 406 ((memq fn '(let let*)) |
407 ;; recursively enter the optimizer for the bindings and body | |
408 ;; of a let or let*. This for depth-firstness: forms that | |
409 ;; are more deeply nested are optimized first. | |
410 (cons fn | |
411 (cons | |
412 (mapcar | |
413 #'(lambda (binding) | |
414 (if (symbolp binding) | |
415 binding | |
416 (if (cdr (cdr binding)) | |
417 (byte-compile-warn "malformed let binding: %s" | |
418 (prin1-to-string binding))) | |
419 (list (car binding) | |
420 (byte-optimize-form (nth 1 binding) nil)))) | |
421 (nth 1 form)) | |
422 (byte-optimize-body (cdr (cdr form)) for-effect)))) | |
423 ((eq fn 'cond) | |
424 (cons fn | |
425 (mapcar | |
426 #'(lambda (clause) | |
427 (if (consp clause) | |
428 (cons | |
429 (byte-optimize-form (car clause) nil) | |
430 (byte-optimize-body (cdr clause) for-effect)) | |
431 (byte-compile-warn "malformed cond form: %s" | |
432 (prin1-to-string clause)) | |
433 clause)) | |
434 (cdr form)))) | |
435 ((eq fn 'progn) | |
436 ;; as an extra added bonus, this simplifies (progn <x>) --> <x> | |
437 (if (cdr (cdr form)) | |
438 (progn | |
439 (setq tmp (byte-optimize-body (cdr form) for-effect)) | |
440 (if (cdr tmp) (cons 'progn tmp) (car tmp))) | |
441 (byte-optimize-form (nth 1 form) for-effect))) | |
442 ((eq fn 'prog1) | |
443 (if (cdr (cdr form)) | |
5651
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
444 (cons (if for-effect 'progn 'prog1) |
428 | 445 (cons (byte-optimize-form (nth 1 form) for-effect) |
446 (byte-optimize-body (cdr (cdr form)) t))) | |
4686
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
447 (byte-optimize-form `(or ,(nth 1 form) nil) for-effect))) |
428 | 448 ((eq fn 'prog2) |
449 (cons 'prog2 | |
450 (cons (byte-optimize-form (nth 1 form) t) | |
451 (cons (byte-optimize-form (nth 2 form) for-effect) | |
452 (byte-optimize-body (cdr (cdr (cdr form))) t))))) | |
440 | 453 |
428 | 454 ((memq fn '(save-excursion save-restriction save-current-buffer)) |
455 ;; those subrs which have an implicit progn; it's not quite good | |
456 ;; enough to treat these like normal function calls. | |
457 ;; This can turn (save-excursion ...) into (save-excursion) which | |
458 ;; will be optimized away in the lap-optimize pass. | |
459 (cons fn (byte-optimize-body (cdr form) for-effect))) | |
440 | 460 |
428 | 461 ((eq fn 'with-output-to-temp-buffer) |
462 ;; this is just like the above, except for the first argument. | |
463 (cons fn | |
464 (cons | |
465 (byte-optimize-form (nth 1 form) nil) | |
466 (byte-optimize-body (cdr (cdr form)) for-effect)))) | |
440 | 467 |
428 | 468 ((eq fn 'if) |
469 (cons fn | |
470 (cons (byte-optimize-form (nth 1 form) nil) | |
471 (cons | |
472 (byte-optimize-form (nth 2 form) for-effect) | |
473 (byte-optimize-body (nthcdr 3 form) for-effect))))) | |
440 | 474 |
428 | 475 ((memq fn '(and or)) ; remember, and/or are control structures. |
476 ;; take forms off the back until we can't any more. | |
477 ;; In the future it could conceivably be a problem that the | |
478 ;; subexpressions of these forms are optimized in the reverse | |
479 ;; order, but it's ok for now. | |
480 (if for-effect | |
481 (let ((backwards (reverse (cdr form)))) | |
482 (while (and backwards | |
483 (null (setcar backwards | |
484 (byte-optimize-form (car backwards) | |
485 for-effect)))) | |
486 (setq backwards (cdr backwards))) | |
487 (if (and (cdr form) (null backwards)) | |
488 (byte-compile-log | |
489 " all subforms of %s called for effect; deleted" form)) | |
452 | 490 (when backwards |
491 ;; Now optimize the rest of the forms. We need the return | |
492 ;; values. We already did the car. | |
493 (setcdr backwards | |
494 (mapcar 'byte-optimize-form (cdr backwards)))) | |
495 (cons fn (nreverse backwards))) | |
428 | 496 (cons fn (mapcar 'byte-optimize-form (cdr form))))) |
497 | |
498 ((eq fn 'interactive) | |
499 (byte-compile-warn "misplaced interactive spec: %s" | |
500 (prin1-to-string form)) | |
501 nil) | |
440 | 502 |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
503 ((memq fn '(defun defmacro)) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
504 (if (eq (setq tmp (cons 'lambda (cddr form))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
505 (setq tmp (byte-optimize-lambda tmp))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
506 (cons fn (cdr tmp)) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
507 form)) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
508 |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
509 ((eq fn 'condition-case) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
510 (list* fn (nth 1 form) (byte-optimize-form (nth 2 form) for-effect) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
511 (mapcar #'(lambda (handler) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
512 (cons (car handler) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
513 (byte-optimize-body (cdr handler) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
514 for-effect))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
515 (cdddr form)))) |
428 | 516 |
517 ((eq fn 'unwind-protect) | |
518 ;; the "protected" part of an unwind-protect is compiled (and thus | |
519 ;; optimized) as a top-level form, so don't do it here. But the | |
520 ;; non-protected part has the same for-effect status as the | |
521 ;; unwind-protect itself. (The protected part is always for effect, | |
522 ;; but that isn't handled properly yet.) | |
523 (cons fn | |
524 (cons (byte-optimize-form (nth 1 form) for-effect) | |
525 (cdr (cdr form))))) | |
440 | 526 |
428 | 527 ((eq fn 'catch) |
528 ;; the body of a catch is compiled (and thus optimized) as a | |
529 ;; top-level form, so don't do it here. The tag is never | |
530 ;; for-effect. The body should have the same for-effect status | |
531 ;; as the catch form itself, but that isn't handled properly yet. | |
532 (cons fn | |
533 (cons (byte-optimize-form (nth 1 form) nil) | |
534 (cdr (cdr form))))) | |
535 | |
536 ;; If optimization is on, this is the only place that macros are | |
537 ;; expanded. If optimization is off, then macroexpansion happens | |
538 ;; in byte-compile-form. Otherwise, the macros are already expanded | |
539 ;; by the time that is reached. | |
540 ((not (eq form | |
541 (setq form (macroexpand form | |
542 byte-compile-macro-environment)))) | |
543 (byte-optimize-form form for-effect)) | |
440 | 544 |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
545 ((compiled-function-p fn) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
546 (cons fn (mapcar #'byte-optimize-form (cdr form)))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
547 |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
548 ((not (symbolp fn)) |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
549 (byte-compile-warn "%S is a malformed function" fn) |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
550 form) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
551 |
1297 | 552 ;; Support compiler macros as in cl.el. |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
553 ((and (get fn 'cl-compiler-macro) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
554 (not (eq form (setq form (compiler-macroexpand form))))) |
1297 | 555 (byte-optimize-form form for-effect)) |
556 | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
557 ((and for-effect |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
558 (setq tmp (byte-optimize-side-effect-free-p form)) |
428 | 559 (or byte-compile-delete-errors |
560 (eq tmp 'error-free) | |
5651
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
561 ;; XEmacs; GNU handles the expansion of (pop foo) specially |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
562 ;; here. We changed the macro to expand to (prog1 (car-safe |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
563 ;; PLACE) (setq PLACE (cdr PLACE))) , which has the same |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
564 ;; effect. (This only matters when |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
565 ;; byte-compile-delete-errors is nil, which is usually true |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
566 ;; for GNU and usually false for XEmacs.) |
428 | 567 (progn |
568 (byte-compile-warn "%s called for effect" | |
569 (prin1-to-string form)) | |
570 nil))) | |
571 (byte-compile-log " %s called for effect; deleted" fn) | |
572 ;; appending a nil here might not be necessary, but it can't hurt. | |
573 (byte-optimize-form | |
574 (cons 'progn (append (cdr form) '(nil))) t)) | |
440 | 575 |
428 | 576 (t |
577 ;; Otherwise, no args can be considered to be for-effect, | |
578 ;; even if the called function is for-effect, because we | |
579 ;; don't know anything about that function. | |
580 (cons fn (mapcar 'byte-optimize-form (cdr form))))))) | |
581 | |
582 | |
583 (defun byte-optimize-form (form &optional for-effect) | |
584 "The source-level pass of the optimizer." | |
585 ;; | |
586 ;; First, optimize all sub-forms of this one. | |
587 (setq form (byte-optimize-form-code-walker form for-effect)) | |
588 ;; | |
589 ;; After optimizing all subforms, optimize this form until it doesn't | |
590 ;; optimize any further. This means that some forms will be passed through | |
591 ;; the optimizer many times, but that's necessary to make the for-effect | |
592 ;; processing do as much as possible. | |
593 ;; | |
594 (let (opt new) | |
595 (if (and (consp form) | |
596 (symbolp (car form)) | |
597 (or (and for-effect | |
598 ;; we don't have any of these yet, but we might. | |
599 (setq opt (get (car form) 'byte-for-effect-optimizer))) | |
600 (setq opt (get (car form) 'byte-optimizer))) | |
601 (not (eq form (setq new (funcall opt form))))) | |
602 (progn | |
603 ;; (if (equal form new) (error "bogus optimizer -- %s" opt)) | |
604 (byte-compile-log " %s\t==>\t%s" form new) | |
1297 | 605 (setq new (byte-optimize-form new for-effect)) |
606 new) | |
428 | 607 form))) |
608 | |
609 | |
610 (defun byte-optimize-body (forms all-for-effect) | |
611 ;; Optimize the cdr of a progn or implicit progn; `forms' is a list of | |
612 ;; forms, all but the last of which are optimized with the assumption that | |
613 ;; they are being called for effect. The last is for-effect as well if | |
614 ;; all-for-effect is true. Returns a new list of forms. | |
615 (let ((rest forms) | |
616 (result nil) | |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
617 (modified nil) |
428 | 618 fe new) |
619 (while rest | |
620 (setq fe (or all-for-effect (cdr rest))) | |
621 (setq new (and (car rest) (byte-optimize-form (car rest) fe))) | |
622 (if (or new (not fe)) | |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
623 (setq result (cons new result) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
624 modified (or modified (not (eq new (car rest))))) |
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
625 (setq modified t)) |
428 | 626 (setq rest (cdr rest))) |
5656
e9c3fe82127d
Co-operate with the byte-optimizer in the bytecomp.el labels implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5652
diff
changeset
|
627 (if modified (nreverse result) forms))) |
428 | 628 |
629 | |
630 ;;; some source-level optimizers | |
631 ;;; | |
632 ;;; when writing optimizers, be VERY careful that the optimizer returns | |
633 ;;; something not EQ to its argument if and ONLY if it has made a change. | |
634 ;;; This implies that you cannot simply destructively modify the list; | |
635 ;;; you must return something not EQ to it if you make an optimization. | |
636 ;;; | |
637 ;;; It is now safe to optimize code such that it introduces new bindings. | |
638 | |
639 ;; I'd like this to be a defsubst, but let's not be self-referential... | |
640 (defmacro byte-compile-trueconstp (form) | |
641 ;; Returns non-nil if FORM is a non-nil constant. | |
642 `(cond ((consp ,form) (eq (car ,form) 'quote)) | |
643 ((not (symbolp ,form))) | |
644 ((eq ,form t)) | |
645 ((keywordp ,form)))) | |
646 | |
647 ;; If the function is being called with constant numeric args, | |
440 | 648 ;; evaluate as much as possible at compile-time. This optimizer |
428 | 649 ;; assumes that the function is associative, like + or *. |
650 (defun byte-optimize-associative-math (form) | |
651 (let ((args nil) | |
652 (constants nil) | |
653 (rest (cdr form))) | |
654 (while rest | |
655 (if (numberp (car rest)) | |
656 (setq constants (cons (car rest) constants)) | |
657 (setq args (cons (car rest) args))) | |
658 (setq rest (cdr rest))) | |
659 (if (cdr constants) | |
660 (if args | |
661 (list (car form) | |
662 (apply (car form) constants) | |
663 (if (cdr args) | |
664 (cons (car form) (nreverse args)) | |
665 (car args))) | |
666 (apply (car form) constants)) | |
667 form))) | |
668 | |
669 ;; If the function is being called with constant numeric args, | |
670 ;; evaluate as much as possible at compile-time. This optimizer | |
671 ;; assumes that the function satisfies | |
672 ;; (op x1 x2 ... xn) == (op ...(op (op x1 x2) x3) ...xn) | |
673 ;; like - and /. | |
674 (defun byte-optimize-nonassociative-math (form) | |
675 (if (or (not (numberp (car (cdr form)))) | |
676 (not (numberp (car (cdr (cdr form)))))) | |
677 form | |
678 (let ((constant (car (cdr form))) | |
679 (rest (cdr (cdr form)))) | |
680 (while (numberp (car rest)) | |
681 (setq constant (funcall (car form) constant (car rest)) | |
682 rest (cdr rest))) | |
683 (if rest | |
684 (cons (car form) (cons constant rest)) | |
685 constant)))) | |
686 | |
687 ;;(defun byte-optimize-associative-two-args-math (form) | |
688 ;; (setq form (byte-optimize-associative-math form)) | |
689 ;; (if (consp form) | |
690 ;; (byte-optimize-two-args-left form) | |
691 ;; form)) | |
692 | |
693 ;;(defun byte-optimize-nonassociative-two-args-math (form) | |
694 ;; (setq form (byte-optimize-nonassociative-math form)) | |
695 ;; (if (consp form) | |
696 ;; (byte-optimize-two-args-right form) | |
697 ;; form)) | |
698 | |
699 ;; jwz: (byte-optimize-approx-equal 0.0 0.0) was returning nil | |
700 ;; in xemacs 19.15 because it used < instead of <=. | |
701 (defun byte-optimize-approx-equal (x y) | |
702 (<= (* (abs (- x y)) 100) (abs (+ x y)))) | |
703 | |
704 ;; Collect all the constants from FORM, after the STARTth arg, | |
705 ;; and apply FUN to them to make one argument at the end. | |
706 ;; For functions that can handle floats, that optimization | |
707 ;; can be incorrect because reordering can cause an overflow | |
708 ;; that would otherwise be avoided by encountering an arg that is a float. | |
709 ;; We avoid this problem by (1) not moving float constants and | |
710 ;; (2) not moving anything if it would cause an overflow. | |
711 (defun byte-optimize-delay-constants-math (form start fun) | |
712 ;; Merge all FORM's constants from number START, call FUN on them | |
713 ;; and put the result at the end. | |
714 (let ((rest (nthcdr (1- start) form)) | |
715 (orig form) | |
716 ;; t means we must check for overflow. | |
717 (overflow (memq fun '(+ *)))) | |
718 (while (cdr (setq rest (cdr rest))) | |
719 (if (integerp (car rest)) | |
720 (let (constants) | |
721 (setq form (copy-sequence form) | |
722 rest (nthcdr (1- start) form)) | |
723 (while (setq rest (cdr rest)) | |
724 (cond ((integerp (car rest)) | |
725 (setq constants (cons (car rest) constants)) | |
726 (setcar rest nil)))) | |
727 ;; If necessary, check now for overflow | |
728 ;; that might be caused by reordering. | |
729 (if (and overflow | |
730 ;; We have overflow if the result of doing the arithmetic | |
731 ;; on floats is not even close to the result | |
732 ;; of doing it on integers. | |
733 (not (byte-optimize-approx-equal | |
734 (apply fun (mapcar 'float constants)) | |
735 (float (apply fun constants))))) | |
736 (setq form orig) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
737 (setq form (nconc (delete* nil form) |
428 | 738 (list (apply fun (nreverse constants))))))))) |
739 form)) | |
740 | |
1297 | 741 ;; END SYNC WITH 20.7. |
742 | |
446 | 743 ;;; It is not safe to optimize calls to arithmetic ops with one arg |
744 ;;; away entirely (actually, it would be safe if we know the sole arg | |
745 ;;; is not a marker or if it appears in other arithmetic). | |
428 | 746 |
446 | 747 ;;; But this degree of paranoia is normally unjustified, so optimize unless |
547 | 748 ;;; the user has done (declaim (optimize (safety 3))). See bytecomp.el. |
442 | 749 |
446 | 750 (defun byte-optimize-plus (form) |
751 (byte-optimize-predicate (byte-optimize-delay-constants-math form 1 '+))) | |
428 | 752 |
753 (defun byte-optimize-multiply (form) | |
754 (setq form (byte-optimize-delay-constants-math form 1 '*)) | |
442 | 755 ;; If there is a constant integer in FORM, it is now the last element. |
446 | 756 |
757 (case (car (last form)) | |
758 ;; (* x y 0) --> (progn x y 0) | |
759 (0 (cons 'progn (cdr form))) | |
760 (t (byte-optimize-predicate form)))) | |
761 | |
762 (defun byte-optimize-minus (form) | |
763 ;; Put constants at the end, except the first arg. | |
764 (setq form (byte-optimize-delay-constants-math form 2 '+)) | |
765 ;; Now only the first and last args can be integers. | |
766 (let ((last (car (last (nthcdr 3 form))))) | |
767 (cond | |
768 ;; If form is (- CONST foo... CONST), merge first and last. | |
769 ((and (numberp (nth 1 form)) (numberp last)) | |
770 (decf (nth 1 form) last) | |
771 (butlast form)) | |
772 | |
464 | 773 ;; (- 0 ...) --> |
774 ((eq 0 (nth 1 form)) | |
775 (case (length form) | |
776 ;; (- 0) --> 0 | |
777 (2 0) | |
778 ;; (- 0 x) --> (- x) | |
779 (3 `(- ,(nth 2 form))) | |
780 ;; (- 0 x y ...) --> (- (- x) y ...) | |
781 (t `(- (- ,(nth 2 form)) ,@(nthcdr 3 form))))) | |
446 | 782 |
783 (t (byte-optimize-predicate form))))) | |
428 | 784 |
785 (defun byte-optimize-divide (form) | |
446 | 786 ;; Put constants at the end, except the first arg. |
428 | 787 (setq form (byte-optimize-delay-constants-math form 2 '*)) |
446 | 788 ;; Now only the first and last args can be integers. |
789 (let ((last (car (last (nthcdr 3 form))))) | |
440 | 790 (cond |
446 | 791 ;; If form is (/ CONST foo... CONST), merge first and last. |
792 ((and (numberp (nth 1 form)) (numberp last)) | |
793 (condition-case nil | |
794 (cons (nth 0 form) | |
795 (cons (/ (nth 1 form) last) | |
796 (butlast (cdr (cdr form))))) | |
797 (error form))) | |
798 | |
799 ;; (/ 0 x y) --> (progn x y 0) | |
442 | 800 ((eq (nth 1 form) 0) |
801 (append '(progn) (cdr (cdr form)) '(0))) | |
446 | 802 |
803 ;; We don't have to check for divide-by-zero because `/' does. | |
804 (t (byte-optimize-predicate form))))) | |
428 | 805 |
1297 | 806 ;; BEGIN SYNC WITH 20.7. |
807 | |
428 | 808 (defun byte-optimize-logmumble (form) |
809 (setq form (byte-optimize-delay-constants-math form 1 (car form))) | |
810 (byte-optimize-predicate | |
811 (cond ((memq 0 form) | |
812 (setq form (if (eq (car form) 'logand) | |
813 (cons 'progn (cdr form)) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
814 (remove* 0 form)))) |
428 | 815 ((and (eq (car-safe form) 'logior) |
816 (memq -1 form)) | |
817 (cons 'progn (cdr form))) | |
818 (form)))) | |
819 | |
820 | |
821 (defun byte-optimize-binary-predicate (form) | |
822 (if (byte-compile-constp (nth 1 form)) | |
823 (if (byte-compile-constp (nth 2 form)) | |
824 (condition-case () | |
825 (list 'quote (eval form)) | |
826 (error form)) | |
827 ;; This can enable some lapcode optimizations. | |
828 (list (car form) (nth 2 form) (nth 1 form))) | |
829 form)) | |
830 | |
831 (defun byte-optimize-predicate (form) | |
832 (let ((ok t) | |
833 (rest (cdr form))) | |
834 (while (and rest ok) | |
835 (setq ok (byte-compile-constp (car rest)) | |
836 rest (cdr rest))) | |
837 (if ok | |
446 | 838 (condition-case err |
428 | 839 (list 'quote (eval form)) |
446 | 840 (error |
841 (byte-compile-warn "evaluating %s: %s" form err) | |
842 form)) | |
428 | 843 form))) |
844 | |
845 (defun byte-optimize-identity (form) | |
846 (if (and (cdr form) (null (cdr (cdr form)))) | |
847 (nth 1 form) | |
848 (byte-compile-warn "identity called with %d arg%s, but requires 1" | |
849 (length (cdr form)) | |
5366
f00192e1cd49
Examining the result of #'length: `eql', not `=', it's better style & cheaper
Aidan Kehoe <kehoea@parhasard.net>
parents:
5311
diff
changeset
|
850 (if (eql 1 (length (cdr form))) "" "s")) |
428 | 851 form)) |
852 | |
444 | 853 (defun byte-optimize-car (form) |
854 (let ((arg (cadr form))) | |
855 (cond | |
856 ((and (byte-compile-trueconstp arg) | |
857 (not (and (consp arg) | |
858 (eq (car arg) 'quote) | |
859 (listp (cadr arg))))) | |
860 (byte-compile-warn | |
861 "taking car of a constant: %s" arg) | |
862 form) | |
863 ((and (eq (car-safe arg) 'cons) | |
864 (eq (length arg) 3)) | |
865 `(prog1 ,(nth 1 arg) ,(nth 2 arg))) | |
866 ((eq (car-safe arg) 'list) | |
867 `(prog1 ,@(cdr arg))) | |
868 (t | |
869 (byte-optimize-predicate form))))) | |
870 | |
871 (defun byte-optimize-cdr (form) | |
872 (let ((arg (cadr form))) | |
873 (cond | |
874 ((and (byte-compile-trueconstp arg) | |
875 (not (and (consp arg) | |
876 (eq (car arg) 'quote) | |
877 (listp (cadr arg))))) | |
878 (byte-compile-warn | |
879 "taking cdr of a constant: %s" arg) | |
880 form) | |
881 ((and (eq (car-safe arg) 'cons) | |
882 (eq (length arg) 3)) | |
883 `(progn ,(nth 1 arg) ,(nth 2 arg))) | |
884 ((eq (car-safe arg) 'list) | |
885 (if (> (length arg) 2) | |
886 `(progn ,(cadr arg) (list ,@(cddr arg))) | |
887 (cadr arg))) | |
888 (t | |
889 (byte-optimize-predicate form))))) | |
890 | |
428 | 891 (put 'identity 'byte-optimizer 'byte-optimize-identity) |
892 | |
893 (put '+ 'byte-optimizer 'byte-optimize-plus) | |
894 (put '* 'byte-optimizer 'byte-optimize-multiply) | |
895 (put '- 'byte-optimizer 'byte-optimize-minus) | |
896 (put '/ 'byte-optimizer 'byte-optimize-divide) | |
446 | 897 (put '% 'byte-optimizer 'byte-optimize-predicate) |
428 | 898 (put 'max 'byte-optimizer 'byte-optimize-associative-math) |
899 (put 'min 'byte-optimizer 'byte-optimize-associative-math) | |
900 | |
901 (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate) | |
902 (put 'eql 'byte-optimizer 'byte-optimize-binary-predicate) | |
903 (put 'equal 'byte-optimizer 'byte-optimize-binary-predicate) | |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
4905
diff
changeset
|
904 (put 'equalp 'byte-optimizer 'byte-optimize-binary-predicate) |
428 | 905 (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate) |
906 (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate) | |
907 | |
550 | 908 (put '= 'byte-optimizer 'byte-optimize-predicate) |
428 | 909 (put '< 'byte-optimizer 'byte-optimize-predicate) |
910 (put '> 'byte-optimizer 'byte-optimize-predicate) | |
911 (put '<= 'byte-optimizer 'byte-optimize-predicate) | |
912 (put '>= 'byte-optimizer 'byte-optimize-predicate) | |
913 (put '1+ 'byte-optimizer 'byte-optimize-predicate) | |
914 (put '1- 'byte-optimizer 'byte-optimize-predicate) | |
915 (put 'not 'byte-optimizer 'byte-optimize-predicate) | |
916 (put 'null 'byte-optimizer 'byte-optimize-predicate) | |
917 (put 'memq 'byte-optimizer 'byte-optimize-predicate) | |
918 (put 'consp 'byte-optimizer 'byte-optimize-predicate) | |
919 (put 'listp 'byte-optimizer 'byte-optimize-predicate) | |
920 (put 'symbolp 'byte-optimizer 'byte-optimize-predicate) | |
921 (put 'stringp 'byte-optimizer 'byte-optimize-predicate) | |
922 (put 'string< 'byte-optimizer 'byte-optimize-predicate) | |
923 (put 'string-lessp 'byte-optimizer 'byte-optimize-predicate) | |
440 | 924 (put 'length 'byte-optimizer 'byte-optimize-predicate) |
428 | 925 |
926 (put 'logand 'byte-optimizer 'byte-optimize-logmumble) | |
927 (put 'logior 'byte-optimizer 'byte-optimize-logmumble) | |
928 (put 'logxor 'byte-optimizer 'byte-optimize-logmumble) | |
929 (put 'lognot 'byte-optimizer 'byte-optimize-predicate) | |
930 | |
444 | 931 (put 'car 'byte-optimizer 'byte-optimize-car) |
932 (put 'cdr 'byte-optimizer 'byte-optimize-cdr) | |
428 | 933 (put 'car-safe 'byte-optimizer 'byte-optimize-predicate) |
934 (put 'cdr-safe 'byte-optimizer 'byte-optimize-predicate) | |
935 | |
936 | |
440 | 937 ;; I'm not convinced that this is necessary. Doesn't the optimizer loop |
428 | 938 ;; take care of this? - Jamie |
939 ;; I think this may some times be necessary to reduce eg. (quote 5) to 5, | |
940 ;; so arithmetic optimizers recognize the numeric constant. - Hallvard | |
941 (put 'quote 'byte-optimizer 'byte-optimize-quote) | |
942 (defun byte-optimize-quote (form) | |
943 (if (or (consp (nth 1 form)) | |
944 (and (symbolp (nth 1 form)) | |
945 ;; XEmacs addition: | |
946 (not (keywordp (nth 1 form))) | |
947 (not (memq (nth 1 form) '(nil t))))) | |
948 form | |
949 (nth 1 form))) | |
950 | |
951 (defun byte-optimize-zerop (form) | |
952 (cond ((numberp (nth 1 form)) | |
953 (eval form)) | |
954 (byte-compile-delete-errors | |
955 (list '= (nth 1 form) 0)) | |
956 (form))) | |
957 | |
958 (put 'zerop 'byte-optimizer 'byte-optimize-zerop) | |
959 | |
960 (defun byte-optimize-and (form) | |
961 ;; Simplify if less than 2 args. | |
962 ;; if there is a literal nil in the args to `and', throw it and following | |
963 ;; forms away, and surround the `and' with (progn ... nil). | |
964 (cond ((null (cdr form))) | |
965 ((memq nil form) | |
966 (list 'progn | |
967 (byte-optimize-and | |
968 (prog1 (setq form (copy-sequence form)) | |
969 (while (nth 1 form) | |
970 (setq form (cdr form))) | |
971 (setcdr form nil))) | |
972 nil)) | |
973 ((null (cdr (cdr form))) | |
974 (nth 1 form)) | |
975 ((byte-optimize-predicate form)))) | |
976 | |
977 (defun byte-optimize-or (form) | |
4686
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
978 ;; Throw away unneeded nils, and simplify if less than 2 args. |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
979 ;; XEmacs; change to be more careful about discarding multiple values. |
5651
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
980 (if (memq nil form) |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
981 (setq form (remove* nil form |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
982 ;; A trailing nil indicates to discard multiple |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
983 ;; values, and we need to respect that. No need if |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
984 ;; this is for-effect, though, multiple values |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
985 ;; will be discarded anyway. |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
986 :end (if (not for-effect) (1- (length form)))))) |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
987 ;; If there is a literal non-nil constant in the args to `or', throw |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
988 ;; away all following forms. We can do this because a literal non-nil |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
989 ;; constant cannot be multiple. |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
990 (let ((rest form)) |
428 | 991 (while (cdr (setq rest (cdr rest))) |
992 (if (byte-compile-trueconstp (car rest)) | |
993 (setq form (copy-sequence form) | |
994 rest (setcdr (memq (car rest) form) nil)))) | |
995 (if (cdr (cdr form)) | |
996 (byte-optimize-predicate form) | |
997 (nth 1 form)))) | |
998 | |
1297 | 999 ;; END SYNC WITH 20.7. |
1000 | |
448 | 1001 ;;; For the byte optimizer, `cond' is just overly sweet syntactic sugar. |
1002 ;;; So we rewrite (cond ...) in terms of `if' and `or', | |
1003 ;;; which are easier to optimize. | |
428 | 1004 (defun byte-optimize-cond (form) |
448 | 1005 (byte-optimize-cond-1 (cdr form))) |
1006 | |
1007 (defun byte-optimize-cond-1 (clauses) | |
1008 (cond | |
1009 ((null clauses) nil) | |
1010 ((consp (car clauses)) | |
1011 (nconc | |
1012 (case (length (car clauses)) | |
4686
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
1013 (1 (if (cdr clauses) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
1014 `(or ,(nth 0 (car clauses))) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
1015 ;; XEmacs: don't pass any multiple values back: |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
1016 `(or ,(nth 0 (car clauses)) nil))) |
448 | 1017 (2 `(if ,(nth 0 (car clauses)) ,(nth 1 (car clauses)))) |
1018 (t `(if ,(nth 0 (car clauses)) (progn ,@(cdr (car clauses)))))) | |
1019 (when (cdr clauses) (list (byte-optimize-cond-1 (cdr clauses)))))) | |
1020 (t (error "malformed cond clause %s" (car clauses))))) | |
428 | 1021 |
1297 | 1022 ;; BEGIN SYNC WITH 20.7. |
1023 | |
428 | 1024 (defun byte-optimize-if (form) |
1025 ;; (if <true-constant> <then> <else...>) ==> <then> | |
1026 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>) | |
1027 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>)) | |
1028 ;; (if <test> <then> nil) ==> (if <test> <then>) | |
1029 (let ((clause (nth 1 form))) | |
1030 (cond ((byte-compile-trueconstp clause) | |
1031 (nth 2 form)) | |
1032 ((null clause) | |
1033 (if (nthcdr 4 form) | |
1034 (cons 'progn (nthcdr 3 form)) | |
1035 (nth 3 form))) | |
1036 ((nth 2 form) | |
1037 (if (equal '(nil) (nthcdr 3 form)) | |
1038 (list 'if clause (nth 2 form)) | |
1039 form)) | |
1040 ((or (nth 3 form) (nthcdr 4 form)) | |
1041 (list 'if | |
1042 ;; Don't make a double negative; | |
1043 ;; instead, take away the one that is there. | |
1044 (if (and (consp clause) (memq (car clause) '(not null)) | |
5366
f00192e1cd49
Examining the result of #'length: `eql', not `=', it's better style & cheaper
Aidan Kehoe <kehoea@parhasard.net>
parents:
5311
diff
changeset
|
1045 (eql (length clause) 2)) ; (not xxxx) or (not (xxxx)) |
428 | 1046 (nth 1 clause) |
1047 (list 'not clause)) | |
1048 (if (nthcdr 4 form) | |
1049 (cons 'progn (nthcdr 3 form)) | |
1050 (nth 3 form)))) | |
1051 (t | |
1052 (list 'progn clause nil))))) | |
1053 | |
1054 (defun byte-optimize-while (form) | |
1055 (if (nth 1 form) | |
1056 form)) | |
1057 | |
1058 (put 'and 'byte-optimizer 'byte-optimize-and) | |
1059 (put 'or 'byte-optimizer 'byte-optimize-or) | |
1060 (put 'cond 'byte-optimizer 'byte-optimize-cond) | |
1061 (put 'if 'byte-optimizer 'byte-optimize-if) | |
1062 (put 'while 'byte-optimizer 'byte-optimize-while) | |
1063 | |
446 | 1064 ;; The supply of bytecodes is small and constrained by backward compatibility. |
1065 ;; Several functions have byte-coded versions and hence are very efficient. | |
1066 ;; Related functions which can be expressed in terms of the byte-coded | |
1067 ;; ones should be transformed into bytecoded calls for efficiency. | |
1068 ;; This is especially the case for functions with a backward- and | |
1069 ;; forward- version, but with a bytecode only for the forward one. | |
1070 | |
1071 ;; Some programmers have hand-optimized calls like (backward-char) | |
1072 ;; into the call (forward-char -1). | |
1073 ;; But it's so much nicer for the byte-compiler to do this automatically! | |
1074 | |
1075 ;; (char-before) ==> (char-after (1- (point))) | |
1076 (put 'char-before 'byte-optimizer 'byte-optimize-char-before) | |
434 | 1077 (defun byte-optimize-char-before (form) |
446 | 1078 `(char-after |
1079 ,(cond | |
1080 ((null (nth 1 form)) | |
1081 '(1- (point))) | |
1082 ((equal '(point) (nth 1 form)) | |
1083 '(1- (point))) | |
1084 (t `(1- (or ,(nth 1 form) (point))))) | |
1085 ,@(cdr (cdr form)))) | |
1086 | |
1087 ;; (backward-char n) ==> (forward-char (- n)) | |
1088 (put 'backward-char 'byte-optimizer 'byte-optimize-backward-char) | |
1089 (defun byte-optimize-backward-char (form) | |
1090 `(forward-char | |
1091 ,(typecase (nth 1 form) | |
1092 (null -1) | |
1093 (integer (- (nth 1 form))) | |
1094 (t `(- (or ,(nth 1 form) 1)))) | |
1095 ,@(cdr (cdr form)))) | |
440 | 1096 |
446 | 1097 ;; (backward-word n) ==> (forward-word (- n)) |
1098 (put 'backward-word 'byte-optimizer 'byte-optimize-backward-word) | |
1099 (defun byte-optimize-backward-word (form) | |
1100 `(forward-word | |
1101 ,(typecase (nth 1 form) | |
1102 (null -1) | |
1103 (integer (- (nth 1 form))) | |
1104 (t `(- (or ,(nth 1 form) 1)))) | |
1105 ,@(cdr (cdr form)))) | |
1106 | |
1107 ;; The following would be a valid optimization of the above kind, but | |
1108 ;; the gain in performance is very small, since the saved funcall is | |
1109 ;; counterbalanced by the necessity of adding a bytecode for (point). | |
1110 ;; | |
1111 ;; Also, users are more likely to have modified the behavior of | |
1112 ;; delete-char via advice or some similar mechanism. This is much | |
1113 ;; less of a problem for the previous functions because it wouldn't | |
1114 ;; make sense to modify the behaviour of `backward-char' without also | |
1115 ;; modifying `forward-char', for example. | |
1116 | |
1117 ;; (delete-char n) ==> (delete-region (point) (+ (point) n)) | |
1118 ;; (put 'delete-char 'byte-optimizer 'byte-optimize-delete-char) | |
1119 ;; (defun byte-optimize-delete-char (form) | |
1120 ;; (case (length (cdr form)) | |
1121 ;; (0 `(delete-region (point) (1+ (point)))) | |
1122 ;; (1 `(delete-region (point) (+ (point) ,(nth 1 form)))) | |
1123 ;; (t form))) | |
434 | 1124 |
428 | 1125 ;; byte-compile-negation-optimizer lives in bytecomp.el |
1126 ;(put '/= 'byte-optimizer 'byte-compile-negation-optimizer) | |
1127 (put 'atom 'byte-optimizer 'byte-compile-negation-optimizer) | |
1128 (put 'nlistp 'byte-optimizer 'byte-compile-negation-optimizer) | |
1129 | |
1130 (defun byte-optimize-funcall (form) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4601
diff
changeset
|
1131 ;; (funcall #'(lambda ...) ...) ==> ((lambda ...) ...) |
428 | 1132 ;; (funcall 'foo ...) ==> (foo ...) |
1133 (let ((fn (nth 1 form))) | |
1134 (if (memq (car-safe fn) '(quote function)) | |
1135 (cons (nth 1 fn) (cdr (cdr form))) | |
1136 form))) | |
1137 | |
1138 (defun byte-optimize-apply (form) | |
1139 ;; If the last arg is a literal constant, turn this into a funcall. | |
1140 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...). | |
1141 (let ((fn (nth 1 form)) | |
1142 (last (nth (1- (length form)) form))) ; I think this really is fastest | |
5264
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1143 (if (and (eq last (third form)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1144 (consp last) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1145 (eq 'mapcar (car last)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1146 (equal fn ''nconc)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1147 (progn |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1148 (byte-compile-warn |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1149 "(apply 'nconc (mapcar ..)), use #'mapcan instead: %s" last) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1150 (cons 'mapcan (cdr last))) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1151 (or (if (or (null last) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1152 (eq (car-safe last) 'quote)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1153 (if (listp (nth 1 last)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1154 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form))))))) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1155 (nconc (list 'funcall fn) butlast |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1156 (mapcar #'(lambda (x) (list 'quote x)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1157 (nth 1 last)))) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1158 (byte-compile-warn |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1159 "last arg to apply can't be a literal atom: %s" |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1160 (prin1-to-string last)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1161 nil)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1162 form)))) |
428 | 1163 |
1164 (put 'funcall 'byte-optimizer 'byte-optimize-funcall) | |
1165 (put 'apply 'byte-optimizer 'byte-optimize-apply) | |
1166 | |
1167 | |
1168 (put 'let 'byte-optimizer 'byte-optimize-letX) | |
1169 (put 'let* 'byte-optimizer 'byte-optimize-letX) | |
1170 (defun byte-optimize-letX (form) | |
1171 (cond ((null (nth 1 form)) | |
1172 ;; No bindings | |
1173 (cons 'progn (cdr (cdr form)))) | |
1174 ((or (nth 2 form) (nthcdr 3 form)) | |
1175 form) | |
1176 ;; The body is nil | |
1177 ((eq (car form) 'let) | |
1178 (append '(progn) (mapcar 'car-safe (mapcar 'cdr-safe (nth 1 form))) | |
1179 '(nil))) | |
1180 (t | |
1181 (let ((binds (reverse (nth 1 form)))) | |
1182 (list 'let* (reverse (cdr binds)) (nth 1 (car binds)) nil))))) | |
1183 | |
1184 | |
1185 (put 'nth 'byte-optimizer 'byte-optimize-nth) | |
1186 (defun byte-optimize-nth (form) | |
5366
f00192e1cd49
Examining the result of #'length: `eql', not `=', it's better style & cheaper
Aidan Kehoe <kehoea@parhasard.net>
parents:
5311
diff
changeset
|
1187 (if (and (eql (safe-length form) 3) (memq (nth 1 form) '(0 1))) |
428 | 1188 (list 'car (if (zerop (nth 1 form)) |
1189 (nth 2 form) | |
1190 (list 'cdr (nth 2 form)))) | |
1191 (byte-optimize-predicate form))) | |
1192 | |
1193 (put 'nthcdr 'byte-optimizer 'byte-optimize-nthcdr) | |
1194 (defun byte-optimize-nthcdr (form) | |
5366
f00192e1cd49
Examining the result of #'length: `eql', not `=', it's better style & cheaper
Aidan Kehoe <kehoea@parhasard.net>
parents:
5311
diff
changeset
|
1195 (if (and (eql (safe-length form) 3) (not (memq (nth 1 form) '(0 1 2)))) |
428 | 1196 (byte-optimize-predicate form) |
1197 (let ((count (nth 1 form))) | |
1198 (setq form (nth 2 form)) | |
1199 (while (>= (setq count (1- count)) 0) | |
1200 (setq form (list 'cdr form))) | |
1201 form))) | |
444 | 1202 |
1203 (put 'concat 'byte-optimizer 'byte-optimize-concat) | |
1204 (defun byte-optimize-concat (form) | |
1205 (let ((args (cdr form)) | |
1206 (constant t)) | |
1207 (while (and args constant) | |
1208 (or (byte-compile-constp (car args)) | |
1209 (setq constant nil)) | |
1210 (setq args (cdr args))) | |
1211 (if constant | |
1212 (eval form) | |
1213 form))) | |
4160 | 1214 |
4228 | 1215 (defvar byte-optimize-ever-present-features |
1216 '(xemacs cl cl-extra cl-19 backquote)) | |
1217 | |
1218 (put 'featurep 'byte-optimizer 'byte-optimize-featurep) | |
1219 (defun byte-optimize-featurep (form) | |
4288 | 1220 (if (memq (car-safe |
1221 (cdr-safe | |
1222 (car-safe | |
1223 (cdr-safe | |
1224 form)))) | |
1225 byte-optimize-ever-present-features) | |
1226 t | |
1227 form)) | |
4228 | 1228 |
428 | 1229 |
440 | 1230 ;;; enumerating those functions which need not be called if the returned |
428 | 1231 ;;; value is not used. That is, something like |
1232 ;;; (progn (list (something-with-side-effects) (yow)) | |
1233 ;;; (foo)) | |
1234 ;;; may safely be turned into | |
1235 ;;; (progn (progn (something-with-side-effects) (yow)) | |
1236 ;;; (foo)) | |
1237 ;;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo. | |
1238 | |
1239 ;;; I wonder if I missed any :-\) | |
1240 (let ((side-effect-free-fns | |
1241 '(% * + - / /= 1+ 1- < <= = > >= abs acos append aref ash asin atan | |
1242 assoc assq | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1243 bigfloat-get-precision boundp buffer-file-name buffer-local-variables |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1244 buffer-modified-p buffer-substring |
428 | 1245 capitalize car-less-than-car car cdr ceiling concat |
1246 ;; coordinates-in-window-p not in XEmacs | |
1247 copy-marker cos count-lines | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1248 default-boundp default-value denominator documentation downcase |
5285
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5264
diff
changeset
|
1249 elt endp exp expt fboundp featurep |
428 | 1250 file-directory-p file-exists-p file-locked-p file-name-absolute-p |
1251 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p | |
1252 float floor format | |
1253 get get-buffer get-buffer-window getenv get-file-buffer | |
1254 ;; hash-table functions | |
1255 make-hash-table copy-hash-table | |
1256 gethash | |
1257 hash-table-count | |
1258 hash-table-rehash-size | |
1259 hash-table-rehash-threshold | |
1260 hash-table-size | |
1261 hash-table-test | |
1262 hash-table-type | |
1263 ;; | |
1264 int-to-string | |
1265 length log log10 logand logb logior lognot logxor lsh | |
1266 marker-buffer max member memq min mod | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1267 next-window nth nthcdr number-to-string numerator |
440 | 1268 parse-colon-path plist-get previous-window |
5311
07d24b1f27a7
Mark #'remove, #'remq as free of side-effects.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5294
diff
changeset
|
1269 radians-to-degrees rassq rassoc remove remq regexp-quote reverse round |
428 | 1270 sin sqrt string< string= string-equal string-lessp string-to-char |
5294
bbff29a01820
Add compiler macros and compilation sanity-checks for functions with keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
1271 string-to-int string-to-number substring symbol-plist symbol-value |
bbff29a01820
Add compiler macros and compilation sanity-checks for functions with keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
1272 symbol-name symbol-function symbol |
428 | 1273 tan upcase user-variable-p vconcat |
1274 ;; XEmacs change: window-edges -> window-pixel-edges | |
1275 window-buffer window-dedicated-p window-pixel-edges window-height | |
1276 window-hscroll window-minibuffer-p window-width | |
1277 zerop | |
1278 ;; functions defined by cl | |
1279 oddp evenp plusp minusp | |
1280 abs expt signum last butlast ldiff | |
1281 pairlis gcd lcm | |
1282 isqrt floor* ceiling* truncate* round* mod* rem* subseq | |
440 | 1283 list-length getf |
428 | 1284 )) |
1285 (side-effect-and-error-free-fns | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1286 '(acons arrayp atom |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1287 bigfloatp bignump bobp bolp buffer-end buffer-list buffer-size |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1288 buffer-string bufferp |
428 | 1289 car-safe case-table-p cdr-safe char-or-string-p char-table-p |
1290 characterp commandp cons | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1291 consolep console-live-p consp copy-tree |
428 | 1292 current-buffer |
1293 ;; XEmacs: extent functions, frame-live-p, various other stuff | |
1294 devicep device-live-p | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1295 eobp eolp eq eql equal equalp eventp extentp |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1296 extent-live-p fixnump floatingp floatp framep frame-live-p |
428 | 1297 get-largest-window get-lru-window |
1298 hash-table-p | |
1299 identity ignore integerp integer-or-marker-p interactive-p | |
1300 invocation-directory invocation-name | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1301 keymapp list list* listp |
428 | 1302 make-marker mark mark-marker markerp memory-limit minibuffer-window |
1303 ;; mouse-movement-p not in XEmacs | |
1304 natnump nlistp not null number-or-marker-p numberp | |
1305 one-window-p ;; overlayp not in XEmacs | |
1306 point point-marker point-min point-max processp | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1307 random-state-p rationalp ratiop range-table-p realp |
428 | 1308 selected-window sequencep stringp subrp symbolp syntax-table-p |
1309 user-full-name user-login-name user-original-login-name | |
1310 user-real-login-name user-real-uid user-uid | |
1311 vector vectorp | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1312 window-configuration-p window-live-p windowp))) |
428 | 1313 (dolist (fn side-effect-free-fns) |
1314 (put fn 'side-effect-free t)) | |
1315 (dolist (fn side-effect-and-error-free-fns) | |
1316 (put fn 'side-effect-free 'error-free))) | |
1317 | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1318 (dolist (function |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1319 '(adjoin assoc* count find intersection member* mismatch position |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1320 rassoc* remove* remove-duplicates search set-difference |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1321 set-exclusive-or stable-intersection stable-sort stable-union |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1322 sublis subsetp subst substitute tree-equal union)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1323 ;; These all throw errors, there's no point implementing an error-free |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1324 ;; version of the list. |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1325 (put function 'side-effect-free-if-keywords-are t)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1326 |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1327 (defun byte-optimize-side-effect-free-p (form) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1328 (or (get (car-safe form) 'side-effect-free) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1329 (and (get (car-safe form) 'side-effect-free-if-keywords-are) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1330 (loop |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1331 for (key value) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1332 on (nthcdr (get (car form) 'byte-compile-keyword-start) form) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1333 by #'cddr |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1334 never (or (and (member* key |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1335 '(:test :test-not :key :if :if-not)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1336 (or (not (byte-compile-constp value)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1337 (not (and (consp value) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1338 (symbolp (cadr value)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1339 (get (cadr value) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1340 'side-effect-free))))) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1341 (not (keywordp key))))))) |
428 | 1342 |
1343 (defun byte-compile-splice-in-already-compiled-code (form) | |
1344 ;; form is (byte-code "..." [...] n) | |
446 | 1345 (if (not (memq byte-optimize '(t byte))) |
428 | 1346 (byte-compile-normal-call form) |
1347 (byte-inline-lapcode | |
1348 (byte-decompile-bytecode-1 (nth 1 form) (nth 2 form) t)) | |
1349 (setq byte-compile-maxdepth (max (+ byte-compile-depth (nth 3 form)) | |
1350 byte-compile-maxdepth)) | |
1351 (setq byte-compile-depth (1+ byte-compile-depth)))) | |
1352 | |
1353 (put 'byte-code 'byte-compile 'byte-compile-splice-in-already-compiled-code) | |
1354 | |
1355 | |
1356 (defconst byte-constref-ops | |
1357 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind)) | |
1358 | |
1359 ;;; This function extracts the bitfields from variable-length opcodes. | |
1360 ;;; Originally defined in disass.el (which no longer uses it.) | |
1361 | |
1362 (defun disassemble-offset () | |
1363 "Don't call this!" | |
1364 ;; fetch and return the offset for the current opcode. | |
1365 ;; return NIL if this opcode has no offset | |
1366 ;; OP, PTR and BYTES are used and set dynamically | |
442 | 1367 (declare (special op ptr bytes)) |
428 | 1368 (cond ((< op byte-nth) |
1369 (let ((tem (logand op 7))) | |
1370 (setq op (logand op 248)) | |
1371 (cond ((eq tem 6) | |
1372 (setq ptr (1+ ptr)) ;offset in next byte | |
1373 ;; char-to-int to avoid downstream problems | |
1374 ;; caused by chars appearing where ints are | |
1375 ;; expected. In bytecode the bytes in the | |
1376 ;; opcode string are always interpreted as ints. | |
1377 (char-to-int (aref bytes ptr))) | |
1378 ((eq tem 7) | |
1379 (setq ptr (1+ ptr)) ;offset in next 2 bytes | |
1380 (+ (aref bytes ptr) | |
1381 (progn (setq ptr (1+ ptr)) | |
1382 (lsh (aref bytes ptr) 8)))) | |
1383 (t tem)))) ;offset was in opcode | |
1384 ((>= op byte-constant) | |
1385 (prog1 (- op byte-constant) ;offset in opcode | |
1386 (setq op byte-constant))) | |
1387 ((and (>= op byte-constant2) | |
1388 (<= op byte-goto-if-not-nil-else-pop)) | |
1389 (setq ptr (1+ ptr)) ;offset in next 2 bytes | |
1390 (+ (aref bytes ptr) | |
1391 (progn (setq ptr (1+ ptr)) | |
1392 (lsh (aref bytes ptr) 8)))) | |
1393 ;; XEmacs: this code was here before. FSF's first comparison | |
1394 ;; is (>= op byte-listN). It appears that the rel-goto stuff | |
1395 ;; does not exist in FSF 19.30. It doesn't exist in 19.28 | |
1396 ;; either, so I'm going to assume that this is an improvement | |
1397 ;; on our part and leave it in. --ben | |
1398 ((and (>= op byte-rel-goto) | |
1399 (<= op byte-insertN)) | |
1400 (setq ptr (1+ ptr)) ;offset in next byte | |
1401 ;; Use char-to-int to avoid downstream problems caused by | |
1402 ;; chars appearing where ints are expected. In bytecode | |
1403 ;; the bytes in the opcode string are always interpreted as | |
1404 ;; ints. | |
1405 (char-to-int (aref bytes ptr))))) | |
1406 | |
1407 | |
1408 ;;; This de-compiler is used for inline expansion of compiled functions, | |
1409 ;;; and by the disassembler. | |
1410 ;;; | |
1411 ;;; This list contains numbers, which are pc values, | |
1412 ;;; before each instruction. | |
1413 (defun byte-decompile-bytecode (bytes constvec) | |
1414 "Turns BYTECODE into lapcode, referring to CONSTVEC." | |
1415 (let ((byte-compile-constants nil) | |
1416 (byte-compile-variables nil) | |
1417 (byte-compile-tag-number 0)) | |
1418 (byte-decompile-bytecode-1 bytes constvec))) | |
1419 | |
1420 ;; As byte-decompile-bytecode, but updates | |
1421 ;; byte-compile-{constants, variables, tag-number}. | |
1422 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced | |
1423 ;; with `goto's destined for the end of the code. | |
1424 ;; That is for use by the compiler. | |
1425 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler. | |
1426 ;; In that case, we put a pc value into the list | |
1427 ;; before each insn (or its label). | |
1428 (defun byte-decompile-bytecode-1 (bytes constvec &optional make-spliceable) | |
1429 (let ((length (length bytes)) | |
1430 (ptr 0) optr tags op offset | |
1431 ;; tag unused | |
1432 lap tmp | |
1433 endtag | |
1434 ;; (retcount 0) unused | |
1435 ) | |
1436 (while (not (= ptr length)) | |
1437 (or make-spliceable | |
1438 (setq lap (cons ptr lap))) | |
1439 (setq op (aref bytes ptr) | |
1440 optr ptr | |
1441 offset (disassemble-offset)) ; this does dynamic-scope magic | |
1442 (setq op (aref byte-code-vector op)) | |
1443 ;; XEmacs: the next line in FSF 19.30 reads | |
1444 ;; (cond ((memq op byte-goto-ops) | |
1445 ;; see the comment above about byte-rel-goto in XEmacs. | |
1446 (cond ((or (memq op byte-goto-ops) | |
1447 (cond ((memq op byte-rel-goto-ops) | |
1448 (setq op (aref byte-code-vector | |
1449 (- (symbol-value op) | |
1450 (- byte-rel-goto byte-goto)))) | |
1451 (setq offset (+ ptr (- offset 127))) | |
1452 t))) | |
1453 ;; it's a pc | |
1454 (setq offset | |
1455 (cdr (or (assq offset tags) | |
1456 (car (setq tags | |
1457 (cons (cons offset | |
1458 (byte-compile-make-tag)) | |
1459 tags))))))) | |
1460 ((cond ((eq op 'byte-constant2) (setq op 'byte-constant) t) | |
1461 ((memq op byte-constref-ops))) | |
1297 | 1462 (setq tmp (if (>= offset (length constvec)) |
1463 (list 'out-of-range offset) | |
1464 (aref constvec offset)) | |
428 | 1465 offset (if (eq op 'byte-constant) |
1466 (byte-compile-get-constant tmp) | |
1467 (or (assq tmp byte-compile-variables) | |
1468 (car (setq byte-compile-variables | |
1469 (cons (list tmp) | |
1470 byte-compile-variables))))))) | |
1471 ((and make-spliceable | |
1472 (eq op 'byte-return)) | |
1473 (if (= ptr (1- length)) | |
1474 (setq op nil) | |
1475 (setq offset (or endtag (setq endtag (byte-compile-make-tag))) | |
1476 op 'byte-goto)))) | |
1477 ;; lap = ( [ (pc . (op . arg)) ]* ) | |
1478 (setq lap (cons (cons optr (cons op (or offset 0))) | |
1479 lap)) | |
1480 (setq ptr (1+ ptr))) | |
1481 ;; take off the dummy nil op that we replaced a trailing "return" with. | |
1482 (let ((rest lap)) | |
1483 (while rest | |
1484 (cond ((numberp (car rest))) | |
1485 ((setq tmp (assq (car (car rest)) tags)) | |
1486 ;; this addr is jumped to | |
1487 (setcdr rest (cons (cons nil (cdr tmp)) | |
1488 (cdr rest))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1489 (setq tags (delete* tmp tags)) |
428 | 1490 (setq rest (cdr rest)))) |
1491 (setq rest (cdr rest)))) | |
1492 (if tags (error "optimizer error: missed tags %s" tags)) | |
1493 (if (null (car (cdr (car lap)))) | |
1494 (setq lap (cdr lap))) | |
1495 (if endtag | |
1496 (setq lap (cons (cons nil endtag) lap))) | |
1497 ;; remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* ) | |
1498 (mapcar #'(lambda (elt) (if (numberp elt) elt (cdr elt))) | |
1499 (nreverse lap)))) | |
1500 | |
1501 | |
1502 ;;; peephole optimizer | |
1503 | |
1504 (defconst byte-tagref-ops (cons 'TAG byte-goto-ops)) | |
1505 | |
1506 (defconst byte-conditional-ops | |
1507 '(byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop | |
1508 byte-goto-if-not-nil-else-pop)) | |
1509 | |
1510 (defconst byte-after-unbind-ops | |
1511 '(byte-constant byte-dup | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1512 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-fixnump |
444 | 1513 byte-eq byte-not |
428 | 1514 byte-cons byte-list1 byte-list2 ; byte-list3 byte-list4 |
1515 byte-interactive-p) | |
1516 ;; How about other side-effect-free-ops? Is it safe to move an | |
1517 ;; error invocation (such as from nth) out of an unwind-protect? | |
444 | 1518 ;; No, it is not, because the unwind-protect forms can alter |
1519 ;; the inside of the object to which nth would apply. | |
1520 ;; For the same reason, byte-equal was deleted from this list. | |
428 | 1521 "Byte-codes that can be moved past an unbind.") |
1522 | |
1523 (defconst byte-compile-side-effect-and-error-free-ops | |
1524 '(byte-constant byte-dup byte-symbolp byte-consp byte-stringp byte-listp | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1525 byte-fixnump byte-numberp byte-eq byte-equal byte-not byte-car-safe |
428 | 1526 byte-cdr-safe byte-cons byte-list1 byte-list2 byte-point byte-point-max |
1527 byte-point-min byte-following-char byte-preceding-char | |
1528 byte-current-column byte-eolp byte-eobp byte-bolp byte-bobp | |
1529 byte-current-buffer byte-interactive-p)) | |
1530 | |
1531 (defconst byte-compile-side-effect-free-ops | |
440 | 1532 (nconc |
428 | 1533 '(byte-varref byte-nth byte-memq byte-car byte-cdr byte-length byte-aref |
1534 byte-symbol-value byte-get byte-concat2 byte-concat3 byte-sub1 byte-add1 | |
1535 byte-eqlsign byte-gtr byte-lss byte-leq byte-geq byte-diff byte-negate | |
1536 byte-plus byte-max byte-min byte-mult byte-char-after byte-char-syntax | |
1537 byte-buffer-substring byte-string= byte-string< byte-nthcdr byte-elt | |
1538 byte-member byte-assq byte-quo byte-rem) | |
1539 byte-compile-side-effect-and-error-free-ops)) | |
1540 | |
1541 ;;; This piece of shit is because of the way DEFVAR_BOOL() variables work. | |
1542 ;;; Consider the code | |
1543 ;;; | |
1544 ;;; (defun foo (flag) | |
1545 ;;; (let ((old-pop-ups pop-up-windows) | |
1546 ;;; (pop-up-windows flag)) | |
1547 ;;; (cond ((not (eq pop-up-windows old-pop-ups)) | |
1548 ;;; (setq old-pop-ups pop-up-windows) | |
1549 ;;; ...)))) | |
1550 ;;; | |
1551 ;;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is | |
1552 ;;; something else. But if we optimize | |
1553 ;;; | |
1554 ;;; varref flag | |
1555 ;;; varbind pop-up-windows | |
1556 ;;; varref pop-up-windows | |
1557 ;;; not | |
1558 ;;; to | |
1559 ;;; varref flag | |
1560 ;;; dup | |
1561 ;;; varbind pop-up-windows | |
1562 ;;; not | |
1563 ;;; | |
440 | 1564 ;;; we break the program, because it will appear that pop-up-windows and |
428 | 1565 ;;; old-pop-ups are not EQ when really they are. So we have to know what |
1566 ;;; the BOOL variables are, and not perform this optimization on them. | |
1567 ;;; | |
1568 | |
1569 ;;; This used to hold a large list of boolean variables, which had to | |
1570 ;;; be updated every time a new DEFVAR_BOOL is added, making it very | |
1571 ;;; hard to maintain. Such a list is not necessary under XEmacs, | |
1572 ;;; where we can use `built-in-variable-type' to query for boolean | |
1573 ;;; variables. | |
1574 | |
1575 ;(defconst byte-boolean-vars | |
1297 | 1576 ; ...) |
428 | 1577 |
1578 (defun byte-optimize-lapcode (lap &optional for-effect) | |
1579 "Simple peephole optimizer. LAP is both modified and returned." | |
442 | 1580 (let (lap0 |
1581 lap1 | |
1582 lap2 | |
1583 variable-frequency | |
428 | 1584 (keep-going 'first-time) |
1585 (add-depth 0) | |
1586 rest tmp tmp2 tmp3 | |
1587 (side-effect-free (if byte-compile-delete-errors | |
1588 byte-compile-side-effect-free-ops | |
1589 byte-compile-side-effect-and-error-free-ops))) | |
1590 (while keep-going | |
1591 (or (eq keep-going 'first-time) | |
1592 (byte-compile-log-lap " ---- next pass")) | |
1593 (setq rest lap | |
1594 keep-going nil) | |
1595 (while rest | |
1596 (setq lap0 (car rest) | |
1597 lap1 (nth 1 rest) | |
1598 lap2 (nth 2 rest)) | |
1599 | |
1600 ;; You may notice that sequences like "dup varset discard" are | |
1601 ;; optimized but sequences like "dup varset TAG1: discard" are not. | |
1602 ;; You may be tempted to change this; resist that temptation. | |
1603 (cond ;; | |
1604 ;; <side-effect-free> pop --> <deleted> | |
1605 ;; ...including: | |
1606 ;; const-X pop --> <deleted> | |
1607 ;; varref-X pop --> <deleted> | |
1608 ;; dup pop --> <deleted> | |
1609 ;; | |
1610 ((and (eq 'byte-discard (car lap1)) | |
1611 (memq (car lap0) side-effect-free)) | |
1612 (setq keep-going t) | |
1613 (setq tmp (aref byte-stack+-info (symbol-value (car lap0)))) | |
1614 (setq rest (cdr rest)) | |
1615 (cond ((= tmp 1) | |
1616 (byte-compile-log-lap | |
1617 " %s discard\t-->\t<deleted>" lap0) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1618 (setq lap (delete* lap0 (delete* lap1 lap)))) |
428 | 1619 ((= tmp 0) |
1620 (byte-compile-log-lap | |
1621 " %s discard\t-->\t<deleted> discard" lap0) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1622 (setq lap (delete* lap0 lap))) |
428 | 1623 ((= tmp -1) |
1624 (byte-compile-log-lap | |
1625 " %s discard\t-->\tdiscard discard" lap0) | |
1626 (setcar lap0 'byte-discard) | |
1627 (setcdr lap0 0)) | |
1628 ((error "Optimizer error: too much on the stack")))) | |
1629 ;; | |
1630 ;; goto*-X X: --> X: | |
1631 ;; | |
1632 ((and (memq (car lap0) byte-goto-ops) | |
1633 (eq (cdr lap0) lap1)) | |
1634 (cond ((eq (car lap0) 'byte-goto) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1635 (setq lap (delete* lap0 lap)) |
428 | 1636 (setq tmp "<deleted>")) |
1637 ((memq (car lap0) byte-goto-always-pop-ops) | |
1638 (setcar lap0 (setq tmp 'byte-discard)) | |
1639 (setcdr lap0 0)) | |
1640 ((error "Depth conflict at tag %d" (nth 2 lap0)))) | |
1641 (and (memq byte-optimize-log '(t byte)) | |
1642 (byte-compile-log " (goto %s) %s:\t-->\t%s %s:" | |
1643 (nth 1 lap1) (nth 1 lap1) | |
1644 tmp (nth 1 lap1))) | |
1645 (setq keep-going t)) | |
1646 ;; | |
1647 ;; varset-X varref-X --> dup varset-X | |
1648 ;; varbind-X varref-X --> dup varbind-X | |
1649 ;; const/dup varset-X varref-X --> const/dup varset-X const/dup | |
1650 ;; const/dup varbind-X varref-X --> const/dup varbind-X const/dup | |
1651 ;; The latter two can enable other optimizations. | |
1652 ;; | |
1653 ((and (eq 'byte-varref (car lap2)) | |
1654 (eq (cdr lap1) (cdr lap2)) | |
1655 (memq (car lap1) '(byte-varset byte-varbind))) | |
1656 (if (and (setq tmp (eq (built-in-variable-type (car (cdr lap2))) | |
1657 'boolean)) | |
1658 (not (eq (car lap0) 'byte-constant))) | |
1659 nil | |
1660 (setq keep-going t) | |
1661 (if (memq (car lap0) '(byte-constant byte-dup)) | |
1662 (progn | |
1663 (setq tmp (if (or (not tmp) | |
1664 (memq (car (cdr lap0)) '(nil t))) | |
1665 (cdr lap0) | |
1666 (byte-compile-get-constant t))) | |
1667 (byte-compile-log-lap " %s %s %s\t-->\t%s %s %s" | |
1668 lap0 lap1 lap2 lap0 lap1 | |
1669 (cons (car lap0) tmp)) | |
1670 (setcar lap2 (car lap0)) | |
1671 (setcdr lap2 tmp)) | |
1672 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1) | |
1673 (setcar lap2 (car lap1)) | |
1674 (setcar lap1 'byte-dup) | |
1675 (setcdr lap1 0) | |
1676 ;; The stack depth gets locally increased, so we will | |
1677 ;; increase maxdepth in case depth = maxdepth here. | |
1678 ;; This can cause the third argument to byte-code to | |
1679 ;; be larger than necessary. | |
1680 (setq add-depth 1)))) | |
1681 ;; | |
1682 ;; dup varset-X discard --> varset-X | |
1683 ;; dup varbind-X discard --> varbind-X | |
1684 ;; (the varbind variant can emerge from other optimizations) | |
1685 ;; | |
1686 ((and (eq 'byte-dup (car lap0)) | |
1687 (eq 'byte-discard (car lap2)) | |
1688 (memq (car lap1) '(byte-varset byte-varbind))) | |
1689 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1) | |
1690 (setq keep-going t | |
1691 rest (cdr rest)) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1692 (setq lap (delete* lap0 (delete* lap2 lap)))) |
428 | 1693 ;; |
1694 ;; not goto-X-if-nil --> goto-X-if-non-nil | |
1695 ;; not goto-X-if-non-nil --> goto-X-if-nil | |
1696 ;; | |
1697 ;; it is wrong to do the same thing for the -else-pop variants. | |
1698 ;; | |
1699 ((and (eq 'byte-not (car lap0)) | |
1700 (or (eq 'byte-goto-if-nil (car lap1)) | |
1701 (eq 'byte-goto-if-not-nil (car lap1)))) | |
1702 (byte-compile-log-lap " not %s\t-->\t%s" | |
1703 lap1 | |
1704 (cons | |
1705 (if (eq (car lap1) 'byte-goto-if-nil) | |
1706 'byte-goto-if-not-nil | |
1707 'byte-goto-if-nil) | |
1708 (cdr lap1))) | |
1709 (setcar lap1 (if (eq (car lap1) 'byte-goto-if-nil) | |
1710 'byte-goto-if-not-nil | |
1711 'byte-goto-if-nil)) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1712 (setq lap (delete* lap0 lap)) |
428 | 1713 (setq keep-going t)) |
1714 ;; | |
1715 ;; goto-X-if-nil goto-Y X: --> goto-Y-if-non-nil X: | |
1716 ;; goto-X-if-non-nil goto-Y X: --> goto-Y-if-nil X: | |
1717 ;; | |
1718 ;; it is wrong to do the same thing for the -else-pop variants. | |
440 | 1719 ;; |
428 | 1720 ((and (or (eq 'byte-goto-if-nil (car lap0)) |
1721 (eq 'byte-goto-if-not-nil (car lap0))) ; gotoX | |
1722 (eq 'byte-goto (car lap1)) ; gotoY | |
1723 (eq (cdr lap0) lap2)) ; TAG X | |
1724 (let ((inverse (if (eq 'byte-goto-if-nil (car lap0)) | |
1725 'byte-goto-if-not-nil 'byte-goto-if-nil))) | |
1726 (byte-compile-log-lap " %s %s %s:\t-->\t%s %s:" | |
1727 lap0 lap1 lap2 | |
1728 (cons inverse (cdr lap1)) lap2) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1729 (setq lap (delete* lap0 lap)) |
428 | 1730 (setcar lap1 inverse) |
1731 (setq keep-going t))) | |
1732 ;; | |
1733 ;; const goto-if-* --> whatever | |
1734 ;; | |
1735 ((and (eq 'byte-constant (car lap0)) | |
1736 (memq (car lap1) byte-conditional-ops)) | |
1737 (cond ((if (or (eq (car lap1) 'byte-goto-if-nil) | |
1738 (eq (car lap1) 'byte-goto-if-nil-else-pop)) | |
1739 (car (cdr lap0)) | |
1740 (not (car (cdr lap0)))) | |
1741 (byte-compile-log-lap " %s %s\t-->\t<deleted>" | |
1742 lap0 lap1) | |
1743 (setq rest (cdr rest) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1744 lap (delete* lap0 (delete* lap1 lap)))) |
428 | 1745 (t |
1746 (if (memq (car lap1) byte-goto-always-pop-ops) | |
1747 (progn | |
1748 (byte-compile-log-lap " %s %s\t-->\t%s" | |
1749 lap0 lap1 (cons 'byte-goto (cdr lap1))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1750 (setq lap (delete* lap0 lap))) |
428 | 1751 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 |
1752 (cons 'byte-goto (cdr lap1)))) | |
1753 (setcar lap1 'byte-goto))) | |
1754 (setq keep-going t)) | |
1755 ;; | |
1756 ;; varref-X varref-X --> varref-X dup | |
1757 ;; varref-X [dup ...] varref-X --> varref-X [dup ...] dup | |
1758 ;; We don't optimize the const-X variations on this here, | |
1759 ;; because that would inhibit some goto optimizations; we | |
1760 ;; optimize the const-X case after all other optimizations. | |
1761 ;; | |
1762 ((and (eq 'byte-varref (car lap0)) | |
1763 (progn | |
1764 (setq tmp (cdr rest)) | |
1765 (while (eq (car (car tmp)) 'byte-dup) | |
1766 (setq tmp (cdr tmp))) | |
1767 t) | |
1768 (eq (cdr lap0) (cdr (car tmp))) | |
1769 (eq 'byte-varref (car (car tmp)))) | |
1770 (if (memq byte-optimize-log '(t byte)) | |
1771 (let ((str "")) | |
1772 (setq tmp2 (cdr rest)) | |
1773 (while (not (eq tmp tmp2)) | |
1774 (setq tmp2 (cdr tmp2) | |
1775 str (concat str " dup"))) | |
1776 (byte-compile-log-lap " %s%s %s\t-->\t%s%s dup" | |
1777 lap0 str lap0 lap0 str))) | |
1778 (setq keep-going t) | |
1779 (setcar (car tmp) 'byte-dup) | |
1780 (setcdr (car tmp) 0) | |
1781 (setq rest tmp)) | |
1782 ;; | |
1783 ;; TAG1: TAG2: --> TAG1: <deleted> | |
1784 ;; (and other references to TAG2 are replaced with TAG1) | |
1785 ;; | |
1786 ((and (eq (car lap0) 'TAG) | |
1787 (eq (car lap1) 'TAG)) | |
1788 (and (memq byte-optimize-log '(t byte)) | |
1789 (byte-compile-log " adjacent tags %d and %d merged" | |
1790 (nth 1 lap1) (nth 1 lap0))) | |
1791 (setq tmp3 lap) | |
1792 (while (setq tmp2 (rassq lap0 tmp3)) | |
1793 (setcdr tmp2 lap1) | |
1794 (setq tmp3 (cdr (memq tmp2 tmp3)))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1795 (setq lap (delete* lap0 lap) |
428 | 1796 keep-going t)) |
1797 ;; | |
1798 ;; unused-TAG: --> <deleted> | |
1799 ;; | |
1800 ((and (eq 'TAG (car lap0)) | |
1801 (not (rassq lap0 lap))) | |
1802 (and (memq byte-optimize-log '(t byte)) | |
1803 (byte-compile-log " unused tag %d removed" (nth 1 lap0))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1804 (setq lap (delete* lap0 lap) |
428 | 1805 keep-going t)) |
1806 ;; | |
1807 ;; goto ... --> goto <delete until TAG or end> | |
1808 ;; return ... --> return <delete until TAG or end> | |
1809 ;; | |
1810 ((and (memq (car lap0) '(byte-goto byte-return)) | |
1811 (not (memq (car lap1) '(TAG nil)))) | |
1812 (setq tmp rest) | |
1813 (let ((i 0) | |
1814 (opt-p (memq byte-optimize-log '(t lap))) | |
1815 str deleted) | |
1816 (while (and (setq tmp (cdr tmp)) | |
1817 (not (eq 'TAG (car (car tmp))))) | |
1818 (if opt-p (setq deleted (cons (car tmp) deleted) | |
1819 str (concat str " %s") | |
1820 i (1+ i)))) | |
1821 (if opt-p | |
440 | 1822 (let ((tagstr |
428 | 1823 (if (eq 'TAG (car (car tmp))) |
1824 (format "%d:" (car (cdr (car tmp)))) | |
1825 (or (car tmp) "")))) | |
1826 (if (< i 6) | |
1827 (apply 'byte-compile-log-lap-1 | |
1828 (concat " %s" str | |
1829 " %s\t-->\t%s <deleted> %s") | |
1830 lap0 | |
1831 (nconc (nreverse deleted) | |
1832 (list tagstr lap0 tagstr))) | |
1833 (byte-compile-log-lap | |
1834 " %s <%d unreachable op%s> %s\t-->\t%s <deleted> %s" | |
1835 lap0 i (if (= i 1) "" "s") | |
1836 tagstr lap0 tagstr)))) | |
1837 (rplacd rest tmp)) | |
1838 (setq keep-going t)) | |
1839 ;; | |
1840 ;; <safe-op> unbind --> unbind <safe-op> | |
1841 ;; (this may enable other optimizations.) | |
1842 ;; | |
1843 ((and (eq 'byte-unbind (car lap1)) | |
1844 (memq (car lap0) byte-after-unbind-ops)) | |
1845 (byte-compile-log-lap " %s %s\t-->\t%s %s" lap0 lap1 lap1 lap0) | |
1846 (setcar rest lap1) | |
1847 (setcar (cdr rest) lap0) | |
1848 (setq keep-going t)) | |
1849 ;; | |
1850 ;; varbind-X unbind-N --> discard unbind-(N-1) | |
1851 ;; save-excursion unbind-N --> unbind-(N-1) | |
1852 ;; save-restriction unbind-N --> unbind-(N-1) | |
1853 ;; | |
1854 ((and (eq 'byte-unbind (car lap1)) | |
1855 (memq (car lap0) '(byte-varbind byte-save-excursion | |
1856 byte-save-restriction)) | |
1857 (< 0 (cdr lap1))) | |
1858 (if (zerop (setcdr lap1 (1- (cdr lap1)))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1859 (delete* lap1 rest)) |
428 | 1860 (if (eq (car lap0) 'byte-varbind) |
1861 (setcar rest (cons 'byte-discard 0)) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1862 (setq lap (delete* lap0 lap))) |
428 | 1863 (byte-compile-log-lap " %s %s\t-->\t%s %s" |
1864 lap0 (cons (car lap1) (1+ (cdr lap1))) | |
1865 (if (eq (car lap0) 'byte-varbind) | |
1866 (car rest) | |
1867 (car (cdr rest))) | |
1868 (if (and (/= 0 (cdr lap1)) | |
1869 (eq (car lap0) 'byte-varbind)) | |
1870 (car (cdr rest)) | |
1871 "")) | |
1872 (setq keep-going t)) | |
1873 ;; | |
1874 ;; goto*-X ... X: goto-Y --> goto*-Y | |
1875 ;; goto-X ... X: return --> return | |
1876 ;; | |
1877 ((and (memq (car lap0) byte-goto-ops) | |
1878 (memq (car (setq tmp (nth 1 (memq (cdr lap0) lap)))) | |
1879 '(byte-goto byte-return))) | |
1880 (cond ((and (not (eq tmp lap0)) | |
1881 (or (eq (car lap0) 'byte-goto) | |
1882 (eq (car tmp) 'byte-goto))) | |
1883 (byte-compile-log-lap " %s [%s]\t-->\t%s" | |
1884 (car lap0) tmp tmp) | |
1885 (if (eq (car tmp) 'byte-return) | |
1886 (setcar lap0 'byte-return)) | |
1887 (setcdr lap0 (cdr tmp)) | |
1888 (setq keep-going t)))) | |
1889 ;; | |
1890 ;; goto-*-else-pop X ... X: goto-if-* --> whatever | |
1891 ;; goto-*-else-pop X ... X: discard --> whatever | |
1892 ;; | |
1893 ((and (memq (car lap0) '(byte-goto-if-nil-else-pop | |
1894 byte-goto-if-not-nil-else-pop)) | |
1895 (memq (car (car (setq tmp (cdr (memq (cdr lap0) lap))))) | |
1896 (eval-when-compile | |
1897 (cons 'byte-discard byte-conditional-ops))) | |
1898 (not (eq lap0 (car tmp)))) | |
1899 (setq tmp2 (car tmp)) | |
1900 (setq tmp3 (assq (car lap0) '((byte-goto-if-nil-else-pop | |
1901 byte-goto-if-nil) | |
1902 (byte-goto-if-not-nil-else-pop | |
1903 byte-goto-if-not-nil)))) | |
1904 (if (memq (car tmp2) tmp3) | |
1905 (progn (setcar lap0 (car tmp2)) | |
1906 (setcdr lap0 (cdr tmp2)) | |
1907 (byte-compile-log-lap " %s-else-pop [%s]\t-->\t%s" | |
1908 (car lap0) tmp2 lap0)) | |
1909 ;; Get rid of the -else-pop's and jump one step further. | |
1910 (or (eq 'TAG (car (nth 1 tmp))) | |
1911 (setcdr tmp (cons (byte-compile-make-tag) | |
1912 (cdr tmp)))) | |
1913 (byte-compile-log-lap " %s [%s]\t-->\t%s <skip>" | |
1914 (car lap0) tmp2 (nth 1 tmp3)) | |
1915 (setcar lap0 (nth 1 tmp3)) | |
1916 (setcdr lap0 (nth 1 tmp))) | |
1917 (setq keep-going t)) | |
1918 ;; | |
1919 ;; const goto-X ... X: goto-if-* --> whatever | |
1920 ;; const goto-X ... X: discard --> whatever | |
1921 ;; | |
1922 ((and (eq (car lap0) 'byte-constant) | |
1923 (eq (car lap1) 'byte-goto) | |
1924 (memq (car (car (setq tmp (cdr (memq (cdr lap1) lap))))) | |
1925 (eval-when-compile | |
1926 (cons 'byte-discard byte-conditional-ops))) | |
1927 (not (eq lap1 (car tmp)))) | |
1928 (setq tmp2 (car tmp)) | |
1929 (cond ((memq (car tmp2) | |
1930 (if (null (car (cdr lap0))) | |
1931 '(byte-goto-if-nil byte-goto-if-nil-else-pop) | |
1932 '(byte-goto-if-not-nil | |
1933 byte-goto-if-not-nil-else-pop))) | |
1934 (byte-compile-log-lap " %s goto [%s]\t-->\t%s %s" | |
1935 lap0 tmp2 lap0 tmp2) | |
1936 (setcar lap1 (car tmp2)) | |
1937 (setcdr lap1 (cdr tmp2)) | |
1938 ;; Let next step fix the (const,goto-if*) sequence. | |
1939 (setq rest (cons nil rest))) | |
1940 (t | |
1941 ;; Jump one step further | |
1942 (byte-compile-log-lap | |
1943 " %s goto [%s]\t-->\t<deleted> goto <skip>" | |
1944 lap0 tmp2) | |
1945 (or (eq 'TAG (car (nth 1 tmp))) | |
1946 (setcdr tmp (cons (byte-compile-make-tag) | |
1947 (cdr tmp)))) | |
1948 (setcdr lap1 (car (cdr tmp))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1949 (setq lap (delete* lap0 lap)))) |
428 | 1950 (setq keep-going t)) |
1951 ;; | |
1952 ;; X: varref-Y ... varset-Y goto-X --> | |
1953 ;; X: varref-Y Z: ... dup varset-Y goto-Z | |
1954 ;; (varset-X goto-BACK, BACK: varref-X --> copy the varref down.) | |
1955 ;; (This is so usual for while loops that it is worth handling). | |
1956 ;; | |
1957 ((and (eq (car lap1) 'byte-varset) | |
1958 (eq (car lap2) 'byte-goto) | |
1959 (not (memq (cdr lap2) rest)) ;Backwards jump | |
1960 (eq (car (car (setq tmp (cdr (memq (cdr lap2) lap))))) | |
1961 'byte-varref) | |
1962 (eq (cdr (car tmp)) (cdr lap1)) | |
1963 (not (eq (built-in-variable-type (car (cdr lap1))) | |
1964 'boolean))) | |
1965 ;;(byte-compile-log-lap " Pulled %s to end of loop" (car tmp)) | |
1966 (let ((newtag (byte-compile-make-tag))) | |
1967 (byte-compile-log-lap | |
1968 " %s: %s ... %s %s\t-->\t%s: %s %s: ... %s %s %s" | |
1969 (nth 1 (cdr lap2)) (car tmp) | |
1970 lap1 lap2 | |
1971 (nth 1 (cdr lap2)) (car tmp) | |
1972 (nth 1 newtag) 'byte-dup lap1 | |
1973 (cons 'byte-goto newtag) | |
1974 ) | |
1975 (setcdr rest (cons (cons 'byte-dup 0) (cdr rest))) | |
1976 (setcdr tmp (cons (setcdr lap2 newtag) (cdr tmp)))) | |
1977 (setq add-depth 1) | |
1978 (setq keep-going t)) | |
1979 ;; | |
1980 ;; goto-X Y: ... X: goto-if*-Y --> goto-if-not-*-X+1 Y: | |
1981 ;; (This can pull the loop test to the end of the loop) | |
1982 ;; | |
1983 ((and (eq (car lap0) 'byte-goto) | |
1984 (eq (car lap1) 'TAG) | |
1985 (eq lap1 | |
1986 (cdr (car (setq tmp (cdr (memq (cdr lap0) lap)))))) | |
1987 (memq (car (car tmp)) | |
1988 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil | |
1989 byte-goto-if-nil-else-pop))) | |
1990 ;; (byte-compile-log-lap " %s %s, %s %s --> moved conditional" | |
1991 ;; lap0 lap1 (cdr lap0) (car tmp)) | |
1992 (let ((newtag (byte-compile-make-tag))) | |
1993 (byte-compile-log-lap | |
1994 "%s %s: ... %s: %s\t-->\t%s ... %s:" | |
1995 lap0 (nth 1 lap1) (nth 1 (cdr lap0)) (car tmp) | |
1996 (cons (cdr (assq (car (car tmp)) | |
1997 '((byte-goto-if-nil . byte-goto-if-not-nil) | |
1998 (byte-goto-if-not-nil . byte-goto-if-nil) | |
1999 (byte-goto-if-nil-else-pop . | |
2000 byte-goto-if-not-nil-else-pop) | |
2001 (byte-goto-if-not-nil-else-pop . | |
2002 byte-goto-if-nil-else-pop)))) | |
2003 newtag) | |
440 | 2004 |
428 | 2005 (nth 1 newtag) |
2006 ) | |
2007 (setcdr tmp (cons (setcdr lap0 newtag) (cdr tmp))) | |
2008 (if (eq (car (car tmp)) 'byte-goto-if-nil-else-pop) | |
2009 ;; We can handle this case but not the -if-not-nil case, | |
2010 ;; because we won't know which non-nil constant to push. | |
2011 (setcdr rest (cons (cons 'byte-constant | |
2012 (byte-compile-get-constant nil)) | |
2013 (cdr rest)))) | |
2014 (setcar lap0 (nth 1 (memq (car (car tmp)) | |
2015 '(byte-goto-if-nil-else-pop | |
2016 byte-goto-if-not-nil | |
2017 byte-goto-if-nil | |
2018 byte-goto-if-not-nil | |
2019 byte-goto byte-goto)))) | |
2020 ) | |
2021 (setq keep-going t)) | |
2022 ) | |
2023 (setq rest (cdr rest))) | |
2024 ) | |
2025 ;; Cleanup stage: | |
2026 ;; Rebuild byte-compile-constants / byte-compile-variables. | |
2027 ;; Simple optimizations that would inhibit other optimizations if they | |
2028 ;; were done in the optimizing loop, and optimizations which there is no | |
442 | 2029 ;; need to do more than once. |
428 | 2030 (setq byte-compile-constants nil |
442 | 2031 byte-compile-variables nil |
2032 variable-frequency (make-hash-table :test 'eq)) | |
428 | 2033 (setq rest lap) |
2034 (while rest | |
2035 (setq lap0 (car rest) | |
2036 lap1 (nth 1 rest)) | |
1297 | 2037 (if (memq (car lap0) byte-constref-ops) |
2038 (if (not (eq (car lap0) 'byte-constant)) | |
2039 (progn | |
2040 (incf (gethash (cdr lap0) variable-frequency 0)) | |
2041 (or (memq (cdr lap0) byte-compile-variables) | |
2042 (setq byte-compile-variables | |
2043 (cons (cdr lap0) byte-compile-variables)))) | |
2044 (or (memq (cdr lap0) byte-compile-constants) | |
2045 (setq byte-compile-constants (cons (cdr lap0) | |
2046 byte-compile-constants))))) | |
428 | 2047 (cond (;; |
442 | 2048 ;; const-C varset-X const-C --> const-C dup varset-X |
428 | 2049 ;; const-C varbind-X const-C --> const-C dup varbind-X |
2050 ;; | |
2051 (and (eq (car lap0) 'byte-constant) | |
2052 (eq (car (nth 2 rest)) 'byte-constant) | |
442 | 2053 (eq (cdr lap0) (cdr (nth 2 rest))) |
428 | 2054 (memq (car lap1) '(byte-varbind byte-varset))) |
2055 (byte-compile-log-lap " %s %s %s\t-->\t%s dup %s" | |
2056 lap0 lap1 lap0 lap0 lap1) | |
2057 (setcar (cdr (cdr rest)) (cons (car lap1) (cdr lap1))) | |
2058 (setcar (cdr rest) (cons 'byte-dup 0)) | |
2059 (setq add-depth 1)) | |
2060 ;; | |
2061 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup | |
2062 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup | |
2063 ;; | |
2064 ((memq (car lap0) '(byte-constant byte-varref)) | |
2065 (setq tmp rest | |
2066 tmp2 nil) | |
2067 (while (progn | |
2068 (while (eq 'byte-dup (car (car (setq tmp (cdr tmp)))))) | |
2069 (and (eq (cdr lap0) (cdr (car tmp))) | |
2070 (eq (car lap0) (car (car tmp))))) | |
2071 (setcar tmp (cons 'byte-dup 0)) | |
2072 (setq tmp2 t)) | |
2073 (if tmp2 | |
2074 (byte-compile-log-lap | |
2075 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0))) | |
2076 ;; | |
2077 ;; unbind-N unbind-M --> unbind-(N+M) | |
2078 ;; | |
2079 ((and (eq 'byte-unbind (car lap0)) | |
2080 (eq 'byte-unbind (car lap1))) | |
2081 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 | |
2082 (cons 'byte-unbind | |
2083 (+ (cdr lap0) (cdr lap1)))) | |
2084 (setq keep-going t) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
2085 (setq lap (delete* lap0 lap)) |
428 | 2086 (setcdr lap1 (+ (cdr lap1) (cdr lap0)))) |
2087 ) | |
2088 (setq rest (cdr rest))) | |
442 | 2089 ;; Since the first 6 entries of the compiled-function constants |
2090 ;; vector are most efficient for varref/set/bind ops, we sort by | |
2091 ;; reference count. This generates maximally space efficient and | |
2092 ;; pretty time-efficient byte-code. See `byte-compile-constants-vector'. | |
2093 (setq byte-compile-variables | |
2094 (sort byte-compile-variables | |
2095 #'(lambda (v1 v2) | |
2096 (< (gethash v1 variable-frequency) | |
2097 (gethash v2 variable-frequency))))) | |
2098 ;; Another hack - put the most used variable in position 6, for | |
2099 ;; better locality of reference with adjoining constants. | |
2100 (let ((tail (last byte-compile-variables 6))) | |
2101 (setq byte-compile-variables | |
2102 (append (nbutlast byte-compile-variables 6) | |
2103 (nreverse tail)))) | |
428 | 2104 (setq byte-compile-maxdepth (+ byte-compile-maxdepth add-depth))) |
2105 lap) | |
2106 | |
2107 (provide 'byte-optimize) | |
2108 | |
2109 | |
2110 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when this file compiles | |
2111 ;; itself, compile some of its most used recursive functions (at load time). | |
2112 ;; | |
2113 (eval-when-compile | |
2114 (or (compiled-function-p (symbol-function 'byte-optimize-form)) | |
2115 (assq 'byte-code (symbol-function 'byte-optimize-form)) | |
2116 (let ((byte-optimize nil) | |
2117 (byte-compile-warnings nil)) | |
2118 (mapcar | |
2119 #'(lambda (x) | |
2120 (or noninteractive (message "compiling %s..." x)) | |
2121 (byte-compile x) | |
2122 (or noninteractive (message "compiling %s...done" x))) | |
2123 '(byte-optimize-form | |
2124 byte-optimize-body | |
2125 byte-optimize-predicate | |
2126 byte-optimize-binary-predicate | |
2127 ;; Inserted some more than necessary, to speed it up. | |
2128 byte-optimize-form-code-walker | |
2129 byte-optimize-lapcode)))) | |
2130 nil) | |
2131 | |
1297 | 2132 ;; END SYNC WITH 20.7. |
2133 | |
428 | 2134 ;;; byte-optimize.el ends here |