Mercurial > hg > xemacs-beta
annotate lisp/byte-optimize.el @ 5481:5008a40f7460
Added tag r21-5-31 for changeset f5a0b32f4687
author | Stephen J. Turnbull <stephen@xemacs.org> |
---|---|
date | Fri, 29 Apr 2011 22:57:14 +0900 |
parents | ac37a5f7e5be |
children | 5b08be74bb53 |
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))) | |
287 (if (compiled-function-p fn) | |
288 (progn | |
289 (fetch-bytecode fn) | |
290 (cons (list 'lambda (compiled-function-arglist fn) | |
291 (list 'byte-code | |
292 (compiled-function-instructions fn) | |
293 (compiled-function-constants fn) | |
294 (compiled-function-stack-depth fn))) | |
295 (cdr form))) | |
1297 | 296 (if (eq (car-safe fn) 'lambda) |
297 (cons fn (cdr form)) | |
298 ;; Give up on inlining. | |
299 form)))))) | |
428 | 300 |
301 ;;; ((lambda ...) ...) | |
440 | 302 ;;; |
428 | 303 (defun byte-compile-unfold-lambda (form &optional name) |
304 (or name (setq name "anonymous lambda")) | |
305 (let ((lambda (car form)) | |
306 (values (cdr form))) | |
307 (if (compiled-function-p lambda) | |
308 (setq lambda (list 'lambda (compiled-function-arglist lambda) | |
309 (list 'byte-code | |
310 (compiled-function-instructions lambda) | |
311 (compiled-function-constants lambda) | |
312 (compiled-function-stack-depth lambda))))) | |
313 (let ((arglist (nth 1 lambda)) | |
314 (body (cdr (cdr lambda))) | |
315 optionalp restp | |
316 bindings) | |
317 (if (and (stringp (car body)) (cdr body)) | |
318 (setq body (cdr body))) | |
319 (if (and (consp (car body)) (eq 'interactive (car (car body)))) | |
320 (setq body (cdr body))) | |
321 (while arglist | |
322 (cond ((eq (car arglist) '&optional) | |
323 ;; ok, I'll let this slide because funcall_lambda() does... | |
324 ;; (if optionalp (error "multiple &optional keywords in %s" name)) | |
325 (if restp (error "&optional found after &rest in %s" name)) | |
326 (if (null (cdr arglist)) | |
327 (error "nothing after &optional in %s" name)) | |
328 (setq optionalp t)) | |
329 ((eq (car arglist) '&rest) | |
330 ;; ...but it is by no stretch of the imagination a reasonable | |
331 ;; thing that funcall_lambda() allows (&rest x y) and | |
332 ;; (&rest x &optional y) in arglists. | |
333 (if (null (cdr arglist)) | |
334 (error "nothing after &rest in %s" name)) | |
335 (if (cdr (cdr arglist)) | |
336 (error "multiple vars after &rest in %s" name)) | |
337 (setq restp t)) | |
338 (restp | |
339 (setq bindings (cons (list (car arglist) | |
340 (and values (cons 'list values))) | |
341 bindings) | |
342 values nil)) | |
343 ((and (not optionalp) (null values)) | |
344 (byte-compile-warn "attempt to open-code %s with too few arguments" name) | |
345 (setq arglist nil values 'too-few)) | |
346 (t | |
347 (setq bindings (cons (list (car arglist) (car values)) | |
348 bindings) | |
349 values (cdr values)))) | |
350 (setq arglist (cdr arglist))) | |
351 (if values | |
352 (progn | |
353 (or (eq values 'too-few) | |
354 (byte-compile-warn | |
355 "attempt to open-code %s with too many arguments" name)) | |
356 form) | |
1297 | 357 (setq body (mapcar 'byte-optimize-form body)) |
440 | 358 (let ((newform |
428 | 359 (if bindings |
360 (cons 'let (cons (nreverse bindings) body)) | |
361 (cons 'progn body)))) | |
362 (byte-compile-log " %s\t==>\t%s" form newform) | |
363 newform))))) | |
364 | |
365 | |
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)) | |
393 ((or (compiled-function-p fn) | |
394 (eq 'lambda (car-safe fn))) | |
395 (byte-compile-unfold-lambda form)) | |
396 ((memq fn '(let let*)) | |
397 ;; recursively enter the optimizer for the bindings and body | |
398 ;; of a let or let*. This for depth-firstness: forms that | |
399 ;; are more deeply nested are optimized first. | |
400 (cons fn | |
401 (cons | |
402 (mapcar | |
403 #'(lambda (binding) | |
404 (if (symbolp binding) | |
405 binding | |
406 (if (cdr (cdr binding)) | |
407 (byte-compile-warn "malformed let binding: %s" | |
408 (prin1-to-string binding))) | |
409 (list (car binding) | |
410 (byte-optimize-form (nth 1 binding) nil)))) | |
411 (nth 1 form)) | |
412 (byte-optimize-body (cdr (cdr form)) for-effect)))) | |
413 ((eq fn 'cond) | |
414 (cons fn | |
415 (mapcar | |
416 #'(lambda (clause) | |
417 (if (consp clause) | |
418 (cons | |
419 (byte-optimize-form (car clause) nil) | |
420 (byte-optimize-body (cdr clause) for-effect)) | |
421 (byte-compile-warn "malformed cond form: %s" | |
422 (prin1-to-string clause)) | |
423 clause)) | |
424 (cdr form)))) | |
425 ((eq fn 'progn) | |
426 ;; as an extra added bonus, this simplifies (progn <x>) --> <x> | |
427 (if (cdr (cdr form)) | |
428 (progn | |
429 (setq tmp (byte-optimize-body (cdr form) for-effect)) | |
430 (if (cdr tmp) (cons 'progn tmp) (car tmp))) | |
431 (byte-optimize-form (nth 1 form) for-effect))) | |
432 ((eq fn 'prog1) | |
433 (if (cdr (cdr form)) | |
434 (cons 'prog1 | |
435 (cons (byte-optimize-form (nth 1 form) for-effect) | |
436 (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
|
437 (byte-optimize-form `(or ,(nth 1 form) nil) for-effect))) |
428 | 438 ((eq fn 'prog2) |
439 (cons 'prog2 | |
440 (cons (byte-optimize-form (nth 1 form) t) | |
441 (cons (byte-optimize-form (nth 2 form) for-effect) | |
442 (byte-optimize-body (cdr (cdr (cdr form))) t))))) | |
440 | 443 |
428 | 444 ((memq fn '(save-excursion save-restriction save-current-buffer)) |
445 ;; those subrs which have an implicit progn; it's not quite good | |
446 ;; enough to treat these like normal function calls. | |
447 ;; This can turn (save-excursion ...) into (save-excursion) which | |
448 ;; will be optimized away in the lap-optimize pass. | |
449 (cons fn (byte-optimize-body (cdr form) for-effect))) | |
440 | 450 |
428 | 451 ((eq fn 'with-output-to-temp-buffer) |
452 ;; this is just like the above, except for the first argument. | |
453 (cons fn | |
454 (cons | |
455 (byte-optimize-form (nth 1 form) nil) | |
456 (byte-optimize-body (cdr (cdr form)) for-effect)))) | |
440 | 457 |
428 | 458 ((eq fn 'if) |
459 (cons fn | |
460 (cons (byte-optimize-form (nth 1 form) nil) | |
461 (cons | |
462 (byte-optimize-form (nth 2 form) for-effect) | |
463 (byte-optimize-body (nthcdr 3 form) for-effect))))) | |
440 | 464 |
428 | 465 ((memq fn '(and or)) ; remember, and/or are control structures. |
466 ;; take forms off the back until we can't any more. | |
467 ;; In the future it could conceivably be a problem that the | |
468 ;; subexpressions of these forms are optimized in the reverse | |
469 ;; order, but it's ok for now. | |
470 (if for-effect | |
471 (let ((backwards (reverse (cdr form)))) | |
472 (while (and backwards | |
473 (null (setcar backwards | |
474 (byte-optimize-form (car backwards) | |
475 for-effect)))) | |
476 (setq backwards (cdr backwards))) | |
477 (if (and (cdr form) (null backwards)) | |
478 (byte-compile-log | |
479 " all subforms of %s called for effect; deleted" form)) | |
452 | 480 (when backwards |
481 ;; Now optimize the rest of the forms. We need the return | |
482 ;; values. We already did the car. | |
483 (setcdr backwards | |
484 (mapcar 'byte-optimize-form (cdr backwards)))) | |
485 (cons fn (nreverse backwards))) | |
428 | 486 (cons fn (mapcar 'byte-optimize-form (cdr form))))) |
487 | |
488 ((eq fn 'interactive) | |
489 (byte-compile-warn "misplaced interactive spec: %s" | |
490 (prin1-to-string form)) | |
491 nil) | |
440 | 492 |
428 | 493 ((memq fn '(defun defmacro function |
494 condition-case save-window-excursion)) | |
495 ;; These forms are compiled as constants or by breaking out | |
496 ;; all the subexpressions and compiling them separately. | |
497 form) | |
498 | |
499 ((eq fn 'unwind-protect) | |
500 ;; the "protected" part of an unwind-protect is compiled (and thus | |
501 ;; optimized) as a top-level form, so don't do it here. But the | |
502 ;; non-protected part has the same for-effect status as the | |
503 ;; unwind-protect itself. (The protected part is always for effect, | |
504 ;; but that isn't handled properly yet.) | |
505 (cons fn | |
506 (cons (byte-optimize-form (nth 1 form) for-effect) | |
507 (cdr (cdr form))))) | |
440 | 508 |
428 | 509 ((eq fn 'catch) |
510 ;; the body of a catch is compiled (and thus optimized) as a | |
511 ;; top-level form, so don't do it here. The tag is never | |
512 ;; for-effect. The body should have the same for-effect status | |
513 ;; as the catch form itself, but that isn't handled properly yet. | |
514 (cons fn | |
515 (cons (byte-optimize-form (nth 1 form) nil) | |
516 (cdr (cdr form))))) | |
517 | |
518 ;; If optimization is on, this is the only place that macros are | |
519 ;; expanded. If optimization is off, then macroexpansion happens | |
520 ;; in byte-compile-form. Otherwise, the macros are already expanded | |
521 ;; by the time that is reached. | |
522 ((not (eq form | |
523 (setq form (macroexpand form | |
524 byte-compile-macro-environment)))) | |
525 (byte-optimize-form form for-effect)) | |
440 | 526 |
1297 | 527 ;; Support compiler macros as in cl.el. |
528 ((and (fboundp 'compiler-macroexpand) | |
529 (symbolp (car-safe form)) | |
530 (get (car-safe form) 'cl-compiler-macro) | |
531 (not (eq form | |
532 (setq form (compiler-macroexpand form))))) | |
533 (byte-optimize-form form for-effect)) | |
534 | |
428 | 535 ((not (symbolp fn)) |
536 (or (eq 'mocklisp (car-safe fn)) ; ha! | |
537 (byte-compile-warn "%s is a malformed function" | |
538 (prin1-to-string fn))) | |
539 form) | |
540 | |
541 ((and for-effect (setq tmp (get fn 'side-effect-free)) | |
542 (or byte-compile-delete-errors | |
543 (eq tmp 'error-free) | |
544 (progn | |
545 (byte-compile-warn "%s called for effect" | |
546 (prin1-to-string form)) | |
547 nil))) | |
548 (byte-compile-log " %s called for effect; deleted" fn) | |
549 ;; appending a nil here might not be necessary, but it can't hurt. | |
550 (byte-optimize-form | |
551 (cons 'progn (append (cdr form) '(nil))) t)) | |
440 | 552 |
428 | 553 (t |
554 ;; Otherwise, no args can be considered to be for-effect, | |
555 ;; even if the called function is for-effect, because we | |
556 ;; don't know anything about that function. | |
557 (cons fn (mapcar 'byte-optimize-form (cdr form))))))) | |
558 | |
559 | |
560 (defun byte-optimize-form (form &optional for-effect) | |
561 "The source-level pass of the optimizer." | |
562 ;; | |
563 ;; First, optimize all sub-forms of this one. | |
564 (setq form (byte-optimize-form-code-walker form for-effect)) | |
565 ;; | |
566 ;; After optimizing all subforms, optimize this form until it doesn't | |
567 ;; optimize any further. This means that some forms will be passed through | |
568 ;; the optimizer many times, but that's necessary to make the for-effect | |
569 ;; processing do as much as possible. | |
570 ;; | |
571 (let (opt new) | |
572 (if (and (consp form) | |
573 (symbolp (car form)) | |
574 (or (and for-effect | |
575 ;; we don't have any of these yet, but we might. | |
576 (setq opt (get (car form) 'byte-for-effect-optimizer))) | |
577 (setq opt (get (car form) 'byte-optimizer))) | |
578 (not (eq form (setq new (funcall opt form))))) | |
579 (progn | |
580 ;; (if (equal form new) (error "bogus optimizer -- %s" opt)) | |
581 (byte-compile-log " %s\t==>\t%s" form new) | |
1297 | 582 (setq new (byte-optimize-form new for-effect)) |
583 new) | |
428 | 584 form))) |
585 | |
586 | |
587 (defun byte-optimize-body (forms all-for-effect) | |
588 ;; Optimize the cdr of a progn or implicit progn; `forms' is a list of | |
589 ;; forms, all but the last of which are optimized with the assumption that | |
590 ;; they are being called for effect. The last is for-effect as well if | |
591 ;; all-for-effect is true. Returns a new list of forms. | |
592 (let ((rest forms) | |
593 (result nil) | |
594 fe new) | |
595 (while rest | |
596 (setq fe (or all-for-effect (cdr rest))) | |
597 (setq new (and (car rest) (byte-optimize-form (car rest) fe))) | |
598 (if (or new (not fe)) | |
599 (setq result (cons new result))) | |
600 (setq rest (cdr rest))) | |
601 (nreverse result))) | |
602 | |
603 | |
604 ;;; some source-level optimizers | |
605 ;;; | |
606 ;;; when writing optimizers, be VERY careful that the optimizer returns | |
607 ;;; something not EQ to its argument if and ONLY if it has made a change. | |
608 ;;; This implies that you cannot simply destructively modify the list; | |
609 ;;; you must return something not EQ to it if you make an optimization. | |
610 ;;; | |
611 ;;; It is now safe to optimize code such that it introduces new bindings. | |
612 | |
613 ;; I'd like this to be a defsubst, but let's not be self-referential... | |
614 (defmacro byte-compile-trueconstp (form) | |
615 ;; Returns non-nil if FORM is a non-nil constant. | |
616 `(cond ((consp ,form) (eq (car ,form) 'quote)) | |
617 ((not (symbolp ,form))) | |
618 ((eq ,form t)) | |
619 ((keywordp ,form)))) | |
620 | |
621 ;; If the function is being called with constant numeric args, | |
440 | 622 ;; evaluate as much as possible at compile-time. This optimizer |
428 | 623 ;; assumes that the function is associative, like + or *. |
624 (defun byte-optimize-associative-math (form) | |
625 (let ((args nil) | |
626 (constants nil) | |
627 (rest (cdr form))) | |
628 (while rest | |
629 (if (numberp (car rest)) | |
630 (setq constants (cons (car rest) constants)) | |
631 (setq args (cons (car rest) args))) | |
632 (setq rest (cdr rest))) | |
633 (if (cdr constants) | |
634 (if args | |
635 (list (car form) | |
636 (apply (car form) constants) | |
637 (if (cdr args) | |
638 (cons (car form) (nreverse args)) | |
639 (car args))) | |
640 (apply (car form) constants)) | |
641 form))) | |
642 | |
643 ;; If the function is being called with constant numeric args, | |
644 ;; evaluate as much as possible at compile-time. This optimizer | |
645 ;; assumes that the function satisfies | |
646 ;; (op x1 x2 ... xn) == (op ...(op (op x1 x2) x3) ...xn) | |
647 ;; like - and /. | |
648 (defun byte-optimize-nonassociative-math (form) | |
649 (if (or (not (numberp (car (cdr form)))) | |
650 (not (numberp (car (cdr (cdr form)))))) | |
651 form | |
652 (let ((constant (car (cdr form))) | |
653 (rest (cdr (cdr form)))) | |
654 (while (numberp (car rest)) | |
655 (setq constant (funcall (car form) constant (car rest)) | |
656 rest (cdr rest))) | |
657 (if rest | |
658 (cons (car form) (cons constant rest)) | |
659 constant)))) | |
660 | |
661 ;;(defun byte-optimize-associative-two-args-math (form) | |
662 ;; (setq form (byte-optimize-associative-math form)) | |
663 ;; (if (consp form) | |
664 ;; (byte-optimize-two-args-left form) | |
665 ;; form)) | |
666 | |
667 ;;(defun byte-optimize-nonassociative-two-args-math (form) | |
668 ;; (setq form (byte-optimize-nonassociative-math form)) | |
669 ;; (if (consp form) | |
670 ;; (byte-optimize-two-args-right form) | |
671 ;; form)) | |
672 | |
673 ;; jwz: (byte-optimize-approx-equal 0.0 0.0) was returning nil | |
674 ;; in xemacs 19.15 because it used < instead of <=. | |
675 (defun byte-optimize-approx-equal (x y) | |
676 (<= (* (abs (- x y)) 100) (abs (+ x y)))) | |
677 | |
678 ;; Collect all the constants from FORM, after the STARTth arg, | |
679 ;; and apply FUN to them to make one argument at the end. | |
680 ;; For functions that can handle floats, that optimization | |
681 ;; can be incorrect because reordering can cause an overflow | |
682 ;; that would otherwise be avoided by encountering an arg that is a float. | |
683 ;; We avoid this problem by (1) not moving float constants and | |
684 ;; (2) not moving anything if it would cause an overflow. | |
685 (defun byte-optimize-delay-constants-math (form start fun) | |
686 ;; Merge all FORM's constants from number START, call FUN on them | |
687 ;; and put the result at the end. | |
688 (let ((rest (nthcdr (1- start) form)) | |
689 (orig form) | |
690 ;; t means we must check for overflow. | |
691 (overflow (memq fun '(+ *)))) | |
692 (while (cdr (setq rest (cdr rest))) | |
693 (if (integerp (car rest)) | |
694 (let (constants) | |
695 (setq form (copy-sequence form) | |
696 rest (nthcdr (1- start) form)) | |
697 (while (setq rest (cdr rest)) | |
698 (cond ((integerp (car rest)) | |
699 (setq constants (cons (car rest) constants)) | |
700 (setcar rest nil)))) | |
701 ;; If necessary, check now for overflow | |
702 ;; that might be caused by reordering. | |
703 (if (and overflow | |
704 ;; We have overflow if the result of doing the arithmetic | |
705 ;; on floats is not even close to the result | |
706 ;; of doing it on integers. | |
707 (not (byte-optimize-approx-equal | |
708 (apply fun (mapcar 'float constants)) | |
709 (float (apply fun constants))))) | |
710 (setq form orig) | |
711 (setq form (nconc (delq nil form) | |
712 (list (apply fun (nreverse constants))))))))) | |
713 form)) | |
714 | |
1297 | 715 ;; END SYNC WITH 20.7. |
716 | |
446 | 717 ;;; It is not safe to optimize calls to arithmetic ops with one arg |
718 ;;; away entirely (actually, it would be safe if we know the sole arg | |
719 ;;; is not a marker or if it appears in other arithmetic). | |
428 | 720 |
446 | 721 ;;; But this degree of paranoia is normally unjustified, so optimize unless |
547 | 722 ;;; the user has done (declaim (optimize (safety 3))). See bytecomp.el. |
442 | 723 |
446 | 724 (defun byte-optimize-plus (form) |
725 (byte-optimize-predicate (byte-optimize-delay-constants-math form 1 '+))) | |
428 | 726 |
727 (defun byte-optimize-multiply (form) | |
728 (setq form (byte-optimize-delay-constants-math form 1 '*)) | |
442 | 729 ;; If there is a constant integer in FORM, it is now the last element. |
446 | 730 |
731 (case (car (last form)) | |
732 ;; (* x y 0) --> (progn x y 0) | |
733 (0 (cons 'progn (cdr form))) | |
734 (t (byte-optimize-predicate form)))) | |
735 | |
736 (defun byte-optimize-minus (form) | |
737 ;; Put constants at the end, except the first arg. | |
738 (setq form (byte-optimize-delay-constants-math form 2 '+)) | |
739 ;; Now only the first and last args can be integers. | |
740 (let ((last (car (last (nthcdr 3 form))))) | |
741 (cond | |
742 ;; If form is (- CONST foo... CONST), merge first and last. | |
743 ((and (numberp (nth 1 form)) (numberp last)) | |
744 (decf (nth 1 form) last) | |
745 (butlast form)) | |
746 | |
464 | 747 ;; (- 0 ...) --> |
748 ((eq 0 (nth 1 form)) | |
749 (case (length form) | |
750 ;; (- 0) --> 0 | |
751 (2 0) | |
752 ;; (- 0 x) --> (- x) | |
753 (3 `(- ,(nth 2 form))) | |
754 ;; (- 0 x y ...) --> (- (- x) y ...) | |
755 (t `(- (- ,(nth 2 form)) ,@(nthcdr 3 form))))) | |
446 | 756 |
757 (t (byte-optimize-predicate form))))) | |
428 | 758 |
759 (defun byte-optimize-divide (form) | |
446 | 760 ;; Put constants at the end, except the first arg. |
428 | 761 (setq form (byte-optimize-delay-constants-math form 2 '*)) |
446 | 762 ;; Now only the first and last args can be integers. |
763 (let ((last (car (last (nthcdr 3 form))))) | |
440 | 764 (cond |
446 | 765 ;; If form is (/ CONST foo... CONST), merge first and last. |
766 ((and (numberp (nth 1 form)) (numberp last)) | |
767 (condition-case nil | |
768 (cons (nth 0 form) | |
769 (cons (/ (nth 1 form) last) | |
770 (butlast (cdr (cdr form))))) | |
771 (error form))) | |
772 | |
773 ;; (/ 0 x y) --> (progn x y 0) | |
442 | 774 ((eq (nth 1 form) 0) |
775 (append '(progn) (cdr (cdr form)) '(0))) | |
446 | 776 |
777 ;; We don't have to check for divide-by-zero because `/' does. | |
778 (t (byte-optimize-predicate form))))) | |
428 | 779 |
1297 | 780 ;; BEGIN SYNC WITH 20.7. |
781 | |
428 | 782 (defun byte-optimize-logmumble (form) |
783 (setq form (byte-optimize-delay-constants-math form 1 (car form))) | |
784 (byte-optimize-predicate | |
785 (cond ((memq 0 form) | |
786 (setq form (if (eq (car form) 'logand) | |
787 (cons 'progn (cdr form)) | |
788 (delq 0 (copy-sequence form))))) | |
789 ((and (eq (car-safe form) 'logior) | |
790 (memq -1 form)) | |
791 (cons 'progn (cdr form))) | |
792 (form)))) | |
793 | |
794 | |
795 (defun byte-optimize-binary-predicate (form) | |
796 (if (byte-compile-constp (nth 1 form)) | |
797 (if (byte-compile-constp (nth 2 form)) | |
798 (condition-case () | |
799 (list 'quote (eval form)) | |
800 (error form)) | |
801 ;; This can enable some lapcode optimizations. | |
802 (list (car form) (nth 2 form) (nth 1 form))) | |
803 form)) | |
804 | |
805 (defun byte-optimize-predicate (form) | |
806 (let ((ok t) | |
807 (rest (cdr form))) | |
808 (while (and rest ok) | |
809 (setq ok (byte-compile-constp (car rest)) | |
810 rest (cdr rest))) | |
811 (if ok | |
446 | 812 (condition-case err |
428 | 813 (list 'quote (eval form)) |
446 | 814 (error |
815 (byte-compile-warn "evaluating %s: %s" form err) | |
816 form)) | |
428 | 817 form))) |
818 | |
819 (defun byte-optimize-identity (form) | |
820 (if (and (cdr form) (null (cdr (cdr form)))) | |
821 (nth 1 form) | |
822 (byte-compile-warn "identity called with %d arg%s, but requires 1" | |
823 (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
|
824 (if (eql 1 (length (cdr form))) "" "s")) |
428 | 825 form)) |
826 | |
444 | 827 (defun byte-optimize-car (form) |
828 (let ((arg (cadr form))) | |
829 (cond | |
830 ((and (byte-compile-trueconstp arg) | |
831 (not (and (consp arg) | |
832 (eq (car arg) 'quote) | |
833 (listp (cadr arg))))) | |
834 (byte-compile-warn | |
835 "taking car of a constant: %s" arg) | |
836 form) | |
837 ((and (eq (car-safe arg) 'cons) | |
838 (eq (length arg) 3)) | |
839 `(prog1 ,(nth 1 arg) ,(nth 2 arg))) | |
840 ((eq (car-safe arg) 'list) | |
841 `(prog1 ,@(cdr arg))) | |
842 (t | |
843 (byte-optimize-predicate form))))) | |
844 | |
845 (defun byte-optimize-cdr (form) | |
846 (let ((arg (cadr form))) | |
847 (cond | |
848 ((and (byte-compile-trueconstp arg) | |
849 (not (and (consp arg) | |
850 (eq (car arg) 'quote) | |
851 (listp (cadr arg))))) | |
852 (byte-compile-warn | |
853 "taking cdr of a constant: %s" arg) | |
854 form) | |
855 ((and (eq (car-safe arg) 'cons) | |
856 (eq (length arg) 3)) | |
857 `(progn ,(nth 1 arg) ,(nth 2 arg))) | |
858 ((eq (car-safe arg) 'list) | |
859 (if (> (length arg) 2) | |
860 `(progn ,(cadr arg) (list ,@(cddr arg))) | |
861 (cadr arg))) | |
862 (t | |
863 (byte-optimize-predicate form))))) | |
864 | |
428 | 865 (put 'identity 'byte-optimizer 'byte-optimize-identity) |
866 | |
867 (put '+ 'byte-optimizer 'byte-optimize-plus) | |
868 (put '* 'byte-optimizer 'byte-optimize-multiply) | |
869 (put '- 'byte-optimizer 'byte-optimize-minus) | |
870 (put '/ 'byte-optimizer 'byte-optimize-divide) | |
446 | 871 (put '% 'byte-optimizer 'byte-optimize-predicate) |
428 | 872 (put 'max 'byte-optimizer 'byte-optimize-associative-math) |
873 (put 'min 'byte-optimizer 'byte-optimize-associative-math) | |
874 | |
875 (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate) | |
876 (put 'eql 'byte-optimizer 'byte-optimize-binary-predicate) | |
877 (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
|
878 (put 'equalp 'byte-optimizer 'byte-optimize-binary-predicate) |
428 | 879 (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate) |
880 (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate) | |
881 | |
550 | 882 (put '= 'byte-optimizer 'byte-optimize-predicate) |
428 | 883 (put '< 'byte-optimizer 'byte-optimize-predicate) |
884 (put '> 'byte-optimizer 'byte-optimize-predicate) | |
885 (put '<= 'byte-optimizer 'byte-optimize-predicate) | |
886 (put '>= 'byte-optimizer 'byte-optimize-predicate) | |
887 (put '1+ 'byte-optimizer 'byte-optimize-predicate) | |
888 (put '1- 'byte-optimizer 'byte-optimize-predicate) | |
889 (put 'not 'byte-optimizer 'byte-optimize-predicate) | |
890 (put 'null 'byte-optimizer 'byte-optimize-predicate) | |
891 (put 'memq 'byte-optimizer 'byte-optimize-predicate) | |
892 (put 'consp 'byte-optimizer 'byte-optimize-predicate) | |
893 (put 'listp 'byte-optimizer 'byte-optimize-predicate) | |
894 (put 'symbolp 'byte-optimizer 'byte-optimize-predicate) | |
895 (put 'stringp 'byte-optimizer 'byte-optimize-predicate) | |
896 (put 'string< 'byte-optimizer 'byte-optimize-predicate) | |
897 (put 'string-lessp 'byte-optimizer 'byte-optimize-predicate) | |
440 | 898 (put 'length 'byte-optimizer 'byte-optimize-predicate) |
428 | 899 |
900 (put 'logand 'byte-optimizer 'byte-optimize-logmumble) | |
901 (put 'logior 'byte-optimizer 'byte-optimize-logmumble) | |
902 (put 'logxor 'byte-optimizer 'byte-optimize-logmumble) | |
903 (put 'lognot 'byte-optimizer 'byte-optimize-predicate) | |
904 | |
444 | 905 (put 'car 'byte-optimizer 'byte-optimize-car) |
906 (put 'cdr 'byte-optimizer 'byte-optimize-cdr) | |
428 | 907 (put 'car-safe 'byte-optimizer 'byte-optimize-predicate) |
908 (put 'cdr-safe 'byte-optimizer 'byte-optimize-predicate) | |
909 | |
910 | |
440 | 911 ;; I'm not convinced that this is necessary. Doesn't the optimizer loop |
428 | 912 ;; take care of this? - Jamie |
913 ;; I think this may some times be necessary to reduce eg. (quote 5) to 5, | |
914 ;; so arithmetic optimizers recognize the numeric constant. - Hallvard | |
915 (put 'quote 'byte-optimizer 'byte-optimize-quote) | |
916 (defun byte-optimize-quote (form) | |
917 (if (or (consp (nth 1 form)) | |
918 (and (symbolp (nth 1 form)) | |
919 ;; XEmacs addition: | |
920 (not (keywordp (nth 1 form))) | |
921 (not (memq (nth 1 form) '(nil t))))) | |
922 form | |
923 (nth 1 form))) | |
924 | |
925 (defun byte-optimize-zerop (form) | |
926 (cond ((numberp (nth 1 form)) | |
927 (eval form)) | |
928 (byte-compile-delete-errors | |
929 (list '= (nth 1 form) 0)) | |
930 (form))) | |
931 | |
932 (put 'zerop 'byte-optimizer 'byte-optimize-zerop) | |
933 | |
934 (defun byte-optimize-and (form) | |
935 ;; Simplify if less than 2 args. | |
936 ;; if there is a literal nil in the args to `and', throw it and following | |
937 ;; forms away, and surround the `and' with (progn ... nil). | |
938 (cond ((null (cdr form))) | |
939 ((memq nil form) | |
940 (list 'progn | |
941 (byte-optimize-and | |
942 (prog1 (setq form (copy-sequence form)) | |
943 (while (nth 1 form) | |
944 (setq form (cdr form))) | |
945 (setcdr form nil))) | |
946 nil)) | |
947 ((null (cdr (cdr form))) | |
948 (nth 1 form)) | |
949 ((byte-optimize-predicate form)))) | |
950 | |
951 (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
|
952 ;; 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
|
953 ;; XEmacs; change to be more careful about discarding multiple values. |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
954 (let* ((memqueued (memq nil form)) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
955 (trailing-nil (and (cdr memqueued) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
956 (equal '(nil) (last form)))) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
957 rest) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
958 ;; A trailing nil indicates to discard multiple values, and we need to |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
959 ;; respect that: |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
960 (when (and memqueued (cdr memqueued)) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
961 (setq form (delq nil (copy-sequence form))) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
962 (when trailing-nil |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
963 (setcdr (last form) '(nil)))) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
964 (setq rest form) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
965 ;; If there is a literal non-nil constant in the args to `or', throw |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
966 ;; away all following forms. We can do this because a literal non-nil |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
967 ;; constant cannot be multiple. |
428 | 968 (while (cdr (setq rest (cdr rest))) |
969 (if (byte-compile-trueconstp (car rest)) | |
970 (setq form (copy-sequence form) | |
971 rest (setcdr (memq (car rest) form) nil)))) | |
972 (if (cdr (cdr form)) | |
973 (byte-optimize-predicate form) | |
974 (nth 1 form)))) | |
975 | |
1297 | 976 ;; END SYNC WITH 20.7. |
977 | |
448 | 978 ;;; For the byte optimizer, `cond' is just overly sweet syntactic sugar. |
979 ;;; So we rewrite (cond ...) in terms of `if' and `or', | |
980 ;;; which are easier to optimize. | |
428 | 981 (defun byte-optimize-cond (form) |
448 | 982 (byte-optimize-cond-1 (cdr form))) |
983 | |
984 (defun byte-optimize-cond-1 (clauses) | |
985 (cond | |
986 ((null clauses) nil) | |
987 ((consp (car clauses)) | |
988 (nconc | |
989 (case (length (car clauses)) | |
4686
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
990 (1 (if (cdr clauses) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
991 `(or ,(nth 0 (car clauses))) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
992 ;; 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
|
993 `(or ,(nth 0 (car clauses)) nil))) |
448 | 994 (2 `(if ,(nth 0 (car clauses)) ,(nth 1 (car clauses)))) |
995 (t `(if ,(nth 0 (car clauses)) (progn ,@(cdr (car clauses)))))) | |
996 (when (cdr clauses) (list (byte-optimize-cond-1 (cdr clauses)))))) | |
997 (t (error "malformed cond clause %s" (car clauses))))) | |
428 | 998 |
1297 | 999 ;; BEGIN SYNC WITH 20.7. |
1000 | |
428 | 1001 (defun byte-optimize-if (form) |
1002 ;; (if <true-constant> <then> <else...>) ==> <then> | |
1003 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>) | |
1004 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>)) | |
1005 ;; (if <test> <then> nil) ==> (if <test> <then>) | |
1006 (let ((clause (nth 1 form))) | |
1007 (cond ((byte-compile-trueconstp clause) | |
1008 (nth 2 form)) | |
1009 ((null clause) | |
1010 (if (nthcdr 4 form) | |
1011 (cons 'progn (nthcdr 3 form)) | |
1012 (nth 3 form))) | |
1013 ((nth 2 form) | |
1014 (if (equal '(nil) (nthcdr 3 form)) | |
1015 (list 'if clause (nth 2 form)) | |
1016 form)) | |
1017 ((or (nth 3 form) (nthcdr 4 form)) | |
1018 (list 'if | |
1019 ;; Don't make a double negative; | |
1020 ;; instead, take away the one that is there. | |
1021 (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
|
1022 (eql (length clause) 2)) ; (not xxxx) or (not (xxxx)) |
428 | 1023 (nth 1 clause) |
1024 (list 'not clause)) | |
1025 (if (nthcdr 4 form) | |
1026 (cons 'progn (nthcdr 3 form)) | |
1027 (nth 3 form)))) | |
1028 (t | |
1029 (list 'progn clause nil))))) | |
1030 | |
1031 (defun byte-optimize-while (form) | |
1032 (if (nth 1 form) | |
1033 form)) | |
1034 | |
1035 (put 'and 'byte-optimizer 'byte-optimize-and) | |
1036 (put 'or 'byte-optimizer 'byte-optimize-or) | |
1037 (put 'cond 'byte-optimizer 'byte-optimize-cond) | |
1038 (put 'if 'byte-optimizer 'byte-optimize-if) | |
1039 (put 'while 'byte-optimizer 'byte-optimize-while) | |
1040 | |
446 | 1041 ;; The supply of bytecodes is small and constrained by backward compatibility. |
1042 ;; Several functions have byte-coded versions and hence are very efficient. | |
1043 ;; Related functions which can be expressed in terms of the byte-coded | |
1044 ;; ones should be transformed into bytecoded calls for efficiency. | |
1045 ;; This is especially the case for functions with a backward- and | |
1046 ;; forward- version, but with a bytecode only for the forward one. | |
1047 | |
1048 ;; Some programmers have hand-optimized calls like (backward-char) | |
1049 ;; into the call (forward-char -1). | |
1050 ;; But it's so much nicer for the byte-compiler to do this automatically! | |
1051 | |
1052 ;; (char-before) ==> (char-after (1- (point))) | |
1053 (put 'char-before 'byte-optimizer 'byte-optimize-char-before) | |
434 | 1054 (defun byte-optimize-char-before (form) |
446 | 1055 `(char-after |
1056 ,(cond | |
1057 ((null (nth 1 form)) | |
1058 '(1- (point))) | |
1059 ((equal '(point) (nth 1 form)) | |
1060 '(1- (point))) | |
1061 (t `(1- (or ,(nth 1 form) (point))))) | |
1062 ,@(cdr (cdr form)))) | |
1063 | |
1064 ;; (backward-char n) ==> (forward-char (- n)) | |
1065 (put 'backward-char 'byte-optimizer 'byte-optimize-backward-char) | |
1066 (defun byte-optimize-backward-char (form) | |
1067 `(forward-char | |
1068 ,(typecase (nth 1 form) | |
1069 (null -1) | |
1070 (integer (- (nth 1 form))) | |
1071 (t `(- (or ,(nth 1 form) 1)))) | |
1072 ,@(cdr (cdr form)))) | |
440 | 1073 |
446 | 1074 ;; (backward-word n) ==> (forward-word (- n)) |
1075 (put 'backward-word 'byte-optimizer 'byte-optimize-backward-word) | |
1076 (defun byte-optimize-backward-word (form) | |
1077 `(forward-word | |
1078 ,(typecase (nth 1 form) | |
1079 (null -1) | |
1080 (integer (- (nth 1 form))) | |
1081 (t `(- (or ,(nth 1 form) 1)))) | |
1082 ,@(cdr (cdr form)))) | |
1083 | |
1084 ;; The following would be a valid optimization of the above kind, but | |
1085 ;; the gain in performance is very small, since the saved funcall is | |
1086 ;; counterbalanced by the necessity of adding a bytecode for (point). | |
1087 ;; | |
1088 ;; Also, users are more likely to have modified the behavior of | |
1089 ;; delete-char via advice or some similar mechanism. This is much | |
1090 ;; less of a problem for the previous functions because it wouldn't | |
1091 ;; make sense to modify the behaviour of `backward-char' without also | |
1092 ;; modifying `forward-char', for example. | |
1093 | |
1094 ;; (delete-char n) ==> (delete-region (point) (+ (point) n)) | |
1095 ;; (put 'delete-char 'byte-optimizer 'byte-optimize-delete-char) | |
1096 ;; (defun byte-optimize-delete-char (form) | |
1097 ;; (case (length (cdr form)) | |
1098 ;; (0 `(delete-region (point) (1+ (point)))) | |
1099 ;; (1 `(delete-region (point) (+ (point) ,(nth 1 form)))) | |
1100 ;; (t form))) | |
434 | 1101 |
428 | 1102 ;; byte-compile-negation-optimizer lives in bytecomp.el |
1103 ;(put '/= 'byte-optimizer 'byte-compile-negation-optimizer) | |
1104 (put 'atom 'byte-optimizer 'byte-compile-negation-optimizer) | |
1105 (put 'nlistp 'byte-optimizer 'byte-compile-negation-optimizer) | |
1106 | |
1107 (defun byte-optimize-funcall (form) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4601
diff
changeset
|
1108 ;; (funcall #'(lambda ...) ...) ==> ((lambda ...) ...) |
428 | 1109 ;; (funcall 'foo ...) ==> (foo ...) |
1110 (let ((fn (nth 1 form))) | |
1111 (if (memq (car-safe fn) '(quote function)) | |
1112 (cons (nth 1 fn) (cdr (cdr form))) | |
1113 form))) | |
1114 | |
1115 (defun byte-optimize-apply (form) | |
1116 ;; If the last arg is a literal constant, turn this into a funcall. | |
1117 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...). | |
1118 (let ((fn (nth 1 form)) | |
1119 (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
|
1120 (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
|
1121 (consp last) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1122 (eq 'mapcar (car last)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1123 (equal fn ''nconc)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1124 (progn |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1125 (byte-compile-warn |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1126 "(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
|
1127 (cons 'mapcan (cdr last))) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1128 (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
|
1129 (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
|
1130 (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
|
1131 (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
|
1132 (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
|
1133 (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
|
1134 (nth 1 last)))) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1135 (byte-compile-warn |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1136 "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
|
1137 (prin1-to-string last)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1138 nil)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1139 form)))) |
428 | 1140 |
1141 (put 'funcall 'byte-optimizer 'byte-optimize-funcall) | |
1142 (put 'apply 'byte-optimizer 'byte-optimize-apply) | |
1143 | |
1144 | |
1145 (put 'let 'byte-optimizer 'byte-optimize-letX) | |
1146 (put 'let* 'byte-optimizer 'byte-optimize-letX) | |
1147 (defun byte-optimize-letX (form) | |
1148 (cond ((null (nth 1 form)) | |
1149 ;; No bindings | |
1150 (cons 'progn (cdr (cdr form)))) | |
1151 ((or (nth 2 form) (nthcdr 3 form)) | |
1152 form) | |
1153 ;; The body is nil | |
1154 ((eq (car form) 'let) | |
1155 (append '(progn) (mapcar 'car-safe (mapcar 'cdr-safe (nth 1 form))) | |
1156 '(nil))) | |
1157 (t | |
1158 (let ((binds (reverse (nth 1 form)))) | |
1159 (list 'let* (reverse (cdr binds)) (nth 1 (car binds)) nil))))) | |
1160 | |
1161 | |
1162 (put 'nth 'byte-optimizer 'byte-optimize-nth) | |
1163 (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
|
1164 (if (and (eql (safe-length form) 3) (memq (nth 1 form) '(0 1))) |
428 | 1165 (list 'car (if (zerop (nth 1 form)) |
1166 (nth 2 form) | |
1167 (list 'cdr (nth 2 form)))) | |
1168 (byte-optimize-predicate form))) | |
1169 | |
1170 (put 'nthcdr 'byte-optimizer 'byte-optimize-nthcdr) | |
1171 (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
|
1172 (if (and (eql (safe-length form) 3) (not (memq (nth 1 form) '(0 1 2)))) |
428 | 1173 (byte-optimize-predicate form) |
1174 (let ((count (nth 1 form))) | |
1175 (setq form (nth 2 form)) | |
1176 (while (>= (setq count (1- count)) 0) | |
1177 (setq form (list 'cdr form))) | |
1178 form))) | |
444 | 1179 |
1180 (put 'concat 'byte-optimizer 'byte-optimize-concat) | |
1181 (defun byte-optimize-concat (form) | |
1182 (let ((args (cdr form)) | |
1183 (constant t)) | |
1184 (while (and args constant) | |
1185 (or (byte-compile-constp (car args)) | |
1186 (setq constant nil)) | |
1187 (setq args (cdr args))) | |
1188 (if constant | |
1189 (eval form) | |
1190 form))) | |
4160 | 1191 |
4228 | 1192 (defvar byte-optimize-ever-present-features |
1193 '(xemacs cl cl-extra cl-19 backquote)) | |
1194 | |
1195 (put 'featurep 'byte-optimizer 'byte-optimize-featurep) | |
1196 (defun byte-optimize-featurep (form) | |
4288 | 1197 (if (memq (car-safe |
1198 (cdr-safe | |
1199 (car-safe | |
1200 (cdr-safe | |
1201 form)))) | |
1202 byte-optimize-ever-present-features) | |
1203 t | |
1204 form)) | |
4228 | 1205 |
428 | 1206 |
440 | 1207 ;;; enumerating those functions which need not be called if the returned |
428 | 1208 ;;; value is not used. That is, something like |
1209 ;;; (progn (list (something-with-side-effects) (yow)) | |
1210 ;;; (foo)) | |
1211 ;;; may safely be turned into | |
1212 ;;; (progn (progn (something-with-side-effects) (yow)) | |
1213 ;;; (foo)) | |
1214 ;;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo. | |
1215 | |
1216 ;;; I wonder if I missed any :-\) | |
1217 (let ((side-effect-free-fns | |
1218 '(% * + - / /= 1+ 1- < <= = > >= abs acos append aref ash asin atan | |
1219 assoc assq | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1220 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
|
1221 buffer-modified-p buffer-substring |
428 | 1222 capitalize car-less-than-car car cdr ceiling concat |
1223 ;; coordinates-in-window-p not in XEmacs | |
1224 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
|
1225 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
|
1226 elt endp exp expt fboundp featurep |
428 | 1227 file-directory-p file-exists-p file-locked-p file-name-absolute-p |
1228 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p | |
1229 float floor format | |
1230 get get-buffer get-buffer-window getenv get-file-buffer | |
1231 ;; hash-table functions | |
1232 make-hash-table copy-hash-table | |
1233 gethash | |
1234 hash-table-count | |
1235 hash-table-rehash-size | |
1236 hash-table-rehash-threshold | |
1237 hash-table-size | |
1238 hash-table-test | |
1239 hash-table-type | |
1240 ;; | |
1241 int-to-string | |
1242 length log log10 logand logb logior lognot logxor lsh | |
1243 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
|
1244 next-window nth nthcdr number-to-string numerator |
440 | 1245 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
|
1246 radians-to-degrees rassq rassoc remove remq regexp-quote reverse round |
428 | 1247 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
|
1248 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
|
1249 symbol-name symbol-function symbol |
428 | 1250 tan upcase user-variable-p vconcat |
1251 ;; XEmacs change: window-edges -> window-pixel-edges | |
1252 window-buffer window-dedicated-p window-pixel-edges window-height | |
1253 window-hscroll window-minibuffer-p window-width | |
1254 zerop | |
1255 ;; functions defined by cl | |
1256 oddp evenp plusp minusp | |
1257 abs expt signum last butlast ldiff | |
1258 pairlis gcd lcm | |
1259 isqrt floor* ceiling* truncate* round* mod* rem* subseq | |
440 | 1260 list-length getf |
428 | 1261 )) |
1262 (side-effect-and-error-free-fns | |
1263 '(arrayp atom | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1264 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
|
1265 buffer-string bufferp |
428 | 1266 car-safe case-table-p cdr-safe char-or-string-p char-table-p |
1267 characterp commandp cons | |
1268 consolep console-live-p consp | |
1269 current-buffer | |
1270 ;; XEmacs: extent functions, frame-live-p, various other stuff | |
1271 devicep device-live-p | |
5311
07d24b1f27a7
Mark #'remove, #'remq as free of side-effects.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5294
diff
changeset
|
1272 eobp eolp eq eql equal eventp extentp |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1273 extent-live-p fixnump floatingp floatp framep frame-live-p |
428 | 1274 get-largest-window get-lru-window |
1275 hash-table-p | |
1276 identity ignore integerp integer-or-marker-p interactive-p | |
1277 invocation-directory invocation-name | |
444 | 1278 keymapp list listp |
428 | 1279 make-marker mark mark-marker markerp memory-limit minibuffer-window |
1280 ;; mouse-movement-p not in XEmacs | |
1281 natnump nlistp not null number-or-marker-p numberp | |
1282 one-window-p ;; overlayp not in XEmacs | |
1283 point point-marker point-min point-max processp | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1284 rationalp ratiop range-table-p realp |
428 | 1285 selected-window sequencep stringp subrp symbolp syntax-table-p |
1286 user-full-name user-login-name user-original-login-name | |
1287 user-real-login-name user-real-uid user-uid | |
1288 vector vectorp | |
1289 window-configuration-p window-live-p windowp | |
1290 ;; Functions defined by cl | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1291 eql list* subst acons equalp random-state-p |
428 | 1292 copy-tree sublis |
1293 ))) | |
1294 (dolist (fn side-effect-free-fns) | |
1295 (put fn 'side-effect-free t)) | |
1296 (dolist (fn side-effect-and-error-free-fns) | |
1297 (put fn 'side-effect-free 'error-free))) | |
1298 | |
1299 | |
1300 (defun byte-compile-splice-in-already-compiled-code (form) | |
1301 ;; form is (byte-code "..." [...] n) | |
446 | 1302 (if (not (memq byte-optimize '(t byte))) |
428 | 1303 (byte-compile-normal-call form) |
1304 (byte-inline-lapcode | |
1305 (byte-decompile-bytecode-1 (nth 1 form) (nth 2 form) t)) | |
1306 (setq byte-compile-maxdepth (max (+ byte-compile-depth (nth 3 form)) | |
1307 byte-compile-maxdepth)) | |
1308 (setq byte-compile-depth (1+ byte-compile-depth)))) | |
1309 | |
1310 (put 'byte-code 'byte-compile 'byte-compile-splice-in-already-compiled-code) | |
1311 | |
1312 | |
1313 (defconst byte-constref-ops | |
1314 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind)) | |
1315 | |
1316 ;;; This function extracts the bitfields from variable-length opcodes. | |
1317 ;;; Originally defined in disass.el (which no longer uses it.) | |
1318 | |
1319 (defun disassemble-offset () | |
1320 "Don't call this!" | |
1321 ;; fetch and return the offset for the current opcode. | |
1322 ;; return NIL if this opcode has no offset | |
1323 ;; OP, PTR and BYTES are used and set dynamically | |
442 | 1324 (declare (special op ptr bytes)) |
428 | 1325 (cond ((< op byte-nth) |
1326 (let ((tem (logand op 7))) | |
1327 (setq op (logand op 248)) | |
1328 (cond ((eq tem 6) | |
1329 (setq ptr (1+ ptr)) ;offset in next byte | |
1330 ;; char-to-int to avoid downstream problems | |
1331 ;; caused by chars appearing where ints are | |
1332 ;; expected. In bytecode the bytes in the | |
1333 ;; opcode string are always interpreted as ints. | |
1334 (char-to-int (aref bytes ptr))) | |
1335 ((eq tem 7) | |
1336 (setq ptr (1+ ptr)) ;offset in next 2 bytes | |
1337 (+ (aref bytes ptr) | |
1338 (progn (setq ptr (1+ ptr)) | |
1339 (lsh (aref bytes ptr) 8)))) | |
1340 (t tem)))) ;offset was in opcode | |
1341 ((>= op byte-constant) | |
1342 (prog1 (- op byte-constant) ;offset in opcode | |
1343 (setq op byte-constant))) | |
1344 ((and (>= op byte-constant2) | |
1345 (<= op byte-goto-if-not-nil-else-pop)) | |
1346 (setq ptr (1+ ptr)) ;offset in next 2 bytes | |
1347 (+ (aref bytes ptr) | |
1348 (progn (setq ptr (1+ ptr)) | |
1349 (lsh (aref bytes ptr) 8)))) | |
1350 ;; XEmacs: this code was here before. FSF's first comparison | |
1351 ;; is (>= op byte-listN). It appears that the rel-goto stuff | |
1352 ;; does not exist in FSF 19.30. It doesn't exist in 19.28 | |
1353 ;; either, so I'm going to assume that this is an improvement | |
1354 ;; on our part and leave it in. --ben | |
1355 ((and (>= op byte-rel-goto) | |
1356 (<= op byte-insertN)) | |
1357 (setq ptr (1+ ptr)) ;offset in next byte | |
1358 ;; Use char-to-int to avoid downstream problems caused by | |
1359 ;; chars appearing where ints are expected. In bytecode | |
1360 ;; the bytes in the opcode string are always interpreted as | |
1361 ;; ints. | |
1362 (char-to-int (aref bytes ptr))))) | |
1363 | |
1364 | |
1365 ;;; This de-compiler is used for inline expansion of compiled functions, | |
1366 ;;; and by the disassembler. | |
1367 ;;; | |
1368 ;;; This list contains numbers, which are pc values, | |
1369 ;;; before each instruction. | |
1370 (defun byte-decompile-bytecode (bytes constvec) | |
1371 "Turns BYTECODE into lapcode, referring to CONSTVEC." | |
1372 (let ((byte-compile-constants nil) | |
1373 (byte-compile-variables nil) | |
1374 (byte-compile-tag-number 0)) | |
1375 (byte-decompile-bytecode-1 bytes constvec))) | |
1376 | |
1377 ;; As byte-decompile-bytecode, but updates | |
1378 ;; byte-compile-{constants, variables, tag-number}. | |
1379 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced | |
1380 ;; with `goto's destined for the end of the code. | |
1381 ;; That is for use by the compiler. | |
1382 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler. | |
1383 ;; In that case, we put a pc value into the list | |
1384 ;; before each insn (or its label). | |
1385 (defun byte-decompile-bytecode-1 (bytes constvec &optional make-spliceable) | |
1386 (let ((length (length bytes)) | |
1387 (ptr 0) optr tags op offset | |
1388 ;; tag unused | |
1389 lap tmp | |
1390 endtag | |
1391 ;; (retcount 0) unused | |
1392 ) | |
1393 (while (not (= ptr length)) | |
1394 (or make-spliceable | |
1395 (setq lap (cons ptr lap))) | |
1396 (setq op (aref bytes ptr) | |
1397 optr ptr | |
1398 offset (disassemble-offset)) ; this does dynamic-scope magic | |
1399 (setq op (aref byte-code-vector op)) | |
1400 ;; XEmacs: the next line in FSF 19.30 reads | |
1401 ;; (cond ((memq op byte-goto-ops) | |
1402 ;; see the comment above about byte-rel-goto in XEmacs. | |
1403 (cond ((or (memq op byte-goto-ops) | |
1404 (cond ((memq op byte-rel-goto-ops) | |
1405 (setq op (aref byte-code-vector | |
1406 (- (symbol-value op) | |
1407 (- byte-rel-goto byte-goto)))) | |
1408 (setq offset (+ ptr (- offset 127))) | |
1409 t))) | |
1410 ;; it's a pc | |
1411 (setq offset | |
1412 (cdr (or (assq offset tags) | |
1413 (car (setq tags | |
1414 (cons (cons offset | |
1415 (byte-compile-make-tag)) | |
1416 tags))))))) | |
1417 ((cond ((eq op 'byte-constant2) (setq op 'byte-constant) t) | |
1418 ((memq op byte-constref-ops))) | |
1297 | 1419 (setq tmp (if (>= offset (length constvec)) |
1420 (list 'out-of-range offset) | |
1421 (aref constvec offset)) | |
428 | 1422 offset (if (eq op 'byte-constant) |
1423 (byte-compile-get-constant tmp) | |
1424 (or (assq tmp byte-compile-variables) | |
1425 (car (setq byte-compile-variables | |
1426 (cons (list tmp) | |
1427 byte-compile-variables))))))) | |
1428 ((and make-spliceable | |
1429 (eq op 'byte-return)) | |
1430 (if (= ptr (1- length)) | |
1431 (setq op nil) | |
1432 (setq offset (or endtag (setq endtag (byte-compile-make-tag))) | |
1433 op 'byte-goto)))) | |
1434 ;; lap = ( [ (pc . (op . arg)) ]* ) | |
1435 (setq lap (cons (cons optr (cons op (or offset 0))) | |
1436 lap)) | |
1437 (setq ptr (1+ ptr))) | |
1438 ;; take off the dummy nil op that we replaced a trailing "return" with. | |
1439 (let ((rest lap)) | |
1440 (while rest | |
1441 (cond ((numberp (car rest))) | |
1442 ((setq tmp (assq (car (car rest)) tags)) | |
1443 ;; this addr is jumped to | |
1444 (setcdr rest (cons (cons nil (cdr tmp)) | |
1445 (cdr rest))) | |
1446 (setq tags (delq tmp tags)) | |
1447 (setq rest (cdr rest)))) | |
1448 (setq rest (cdr rest)))) | |
1449 (if tags (error "optimizer error: missed tags %s" tags)) | |
1450 (if (null (car (cdr (car lap)))) | |
1451 (setq lap (cdr lap))) | |
1452 (if endtag | |
1453 (setq lap (cons (cons nil endtag) lap))) | |
1454 ;; remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* ) | |
1455 (mapcar #'(lambda (elt) (if (numberp elt) elt (cdr elt))) | |
1456 (nreverse lap)))) | |
1457 | |
1458 | |
1459 ;;; peephole optimizer | |
1460 | |
1461 (defconst byte-tagref-ops (cons 'TAG byte-goto-ops)) | |
1462 | |
1463 (defconst byte-conditional-ops | |
1464 '(byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop | |
1465 byte-goto-if-not-nil-else-pop)) | |
1466 | |
1467 (defconst byte-after-unbind-ops | |
1468 '(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
|
1469 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-fixnump |
444 | 1470 byte-eq byte-not |
428 | 1471 byte-cons byte-list1 byte-list2 ; byte-list3 byte-list4 |
1472 byte-interactive-p) | |
1473 ;; How about other side-effect-free-ops? Is it safe to move an | |
1474 ;; error invocation (such as from nth) out of an unwind-protect? | |
444 | 1475 ;; No, it is not, because the unwind-protect forms can alter |
1476 ;; the inside of the object to which nth would apply. | |
1477 ;; For the same reason, byte-equal was deleted from this list. | |
428 | 1478 "Byte-codes that can be moved past an unbind.") |
1479 | |
1480 (defconst byte-compile-side-effect-and-error-free-ops | |
1481 '(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
|
1482 byte-fixnump byte-numberp byte-eq byte-equal byte-not byte-car-safe |
428 | 1483 byte-cdr-safe byte-cons byte-list1 byte-list2 byte-point byte-point-max |
1484 byte-point-min byte-following-char byte-preceding-char | |
1485 byte-current-column byte-eolp byte-eobp byte-bolp byte-bobp | |
1486 byte-current-buffer byte-interactive-p)) | |
1487 | |
1488 (defconst byte-compile-side-effect-free-ops | |
440 | 1489 (nconc |
428 | 1490 '(byte-varref byte-nth byte-memq byte-car byte-cdr byte-length byte-aref |
1491 byte-symbol-value byte-get byte-concat2 byte-concat3 byte-sub1 byte-add1 | |
1492 byte-eqlsign byte-gtr byte-lss byte-leq byte-geq byte-diff byte-negate | |
1493 byte-plus byte-max byte-min byte-mult byte-char-after byte-char-syntax | |
1494 byte-buffer-substring byte-string= byte-string< byte-nthcdr byte-elt | |
1495 byte-member byte-assq byte-quo byte-rem) | |
1496 byte-compile-side-effect-and-error-free-ops)) | |
1497 | |
1498 ;;; This piece of shit is because of the way DEFVAR_BOOL() variables work. | |
1499 ;;; Consider the code | |
1500 ;;; | |
1501 ;;; (defun foo (flag) | |
1502 ;;; (let ((old-pop-ups pop-up-windows) | |
1503 ;;; (pop-up-windows flag)) | |
1504 ;;; (cond ((not (eq pop-up-windows old-pop-ups)) | |
1505 ;;; (setq old-pop-ups pop-up-windows) | |
1506 ;;; ...)))) | |
1507 ;;; | |
1508 ;;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is | |
1509 ;;; something else. But if we optimize | |
1510 ;;; | |
1511 ;;; varref flag | |
1512 ;;; varbind pop-up-windows | |
1513 ;;; varref pop-up-windows | |
1514 ;;; not | |
1515 ;;; to | |
1516 ;;; varref flag | |
1517 ;;; dup | |
1518 ;;; varbind pop-up-windows | |
1519 ;;; not | |
1520 ;;; | |
440 | 1521 ;;; we break the program, because it will appear that pop-up-windows and |
428 | 1522 ;;; old-pop-ups are not EQ when really they are. So we have to know what |
1523 ;;; the BOOL variables are, and not perform this optimization on them. | |
1524 ;;; | |
1525 | |
1526 ;;; This used to hold a large list of boolean variables, which had to | |
1527 ;;; be updated every time a new DEFVAR_BOOL is added, making it very | |
1528 ;;; hard to maintain. Such a list is not necessary under XEmacs, | |
1529 ;;; where we can use `built-in-variable-type' to query for boolean | |
1530 ;;; variables. | |
1531 | |
1532 ;(defconst byte-boolean-vars | |
1297 | 1533 ; ...) |
428 | 1534 |
1535 (defun byte-optimize-lapcode (lap &optional for-effect) | |
1536 "Simple peephole optimizer. LAP is both modified and returned." | |
442 | 1537 (let (lap0 |
1538 lap1 | |
1539 lap2 | |
1540 variable-frequency | |
428 | 1541 (keep-going 'first-time) |
1542 (add-depth 0) | |
1543 rest tmp tmp2 tmp3 | |
1544 (side-effect-free (if byte-compile-delete-errors | |
1545 byte-compile-side-effect-free-ops | |
1546 byte-compile-side-effect-and-error-free-ops))) | |
1547 (while keep-going | |
1548 (or (eq keep-going 'first-time) | |
1549 (byte-compile-log-lap " ---- next pass")) | |
1550 (setq rest lap | |
1551 keep-going nil) | |
1552 (while rest | |
1553 (setq lap0 (car rest) | |
1554 lap1 (nth 1 rest) | |
1555 lap2 (nth 2 rest)) | |
1556 | |
1557 ;; You may notice that sequences like "dup varset discard" are | |
1558 ;; optimized but sequences like "dup varset TAG1: discard" are not. | |
1559 ;; You may be tempted to change this; resist that temptation. | |
1560 (cond ;; | |
1561 ;; <side-effect-free> pop --> <deleted> | |
1562 ;; ...including: | |
1563 ;; const-X pop --> <deleted> | |
1564 ;; varref-X pop --> <deleted> | |
1565 ;; dup pop --> <deleted> | |
1566 ;; | |
1567 ((and (eq 'byte-discard (car lap1)) | |
1568 (memq (car lap0) side-effect-free)) | |
1569 (setq keep-going t) | |
1570 (setq tmp (aref byte-stack+-info (symbol-value (car lap0)))) | |
1571 (setq rest (cdr rest)) | |
1572 (cond ((= tmp 1) | |
1573 (byte-compile-log-lap | |
1574 " %s discard\t-->\t<deleted>" lap0) | |
1575 (setq lap (delq lap0 (delq lap1 lap)))) | |
1576 ((= tmp 0) | |
1577 (byte-compile-log-lap | |
1578 " %s discard\t-->\t<deleted> discard" lap0) | |
1579 (setq lap (delq lap0 lap))) | |
1580 ((= tmp -1) | |
1581 (byte-compile-log-lap | |
1582 " %s discard\t-->\tdiscard discard" lap0) | |
1583 (setcar lap0 'byte-discard) | |
1584 (setcdr lap0 0)) | |
1585 ((error "Optimizer error: too much on the stack")))) | |
1586 ;; | |
1587 ;; goto*-X X: --> X: | |
1588 ;; | |
1589 ((and (memq (car lap0) byte-goto-ops) | |
1590 (eq (cdr lap0) lap1)) | |
1591 (cond ((eq (car lap0) 'byte-goto) | |
1592 (setq lap (delq lap0 lap)) | |
1593 (setq tmp "<deleted>")) | |
1594 ((memq (car lap0) byte-goto-always-pop-ops) | |
1595 (setcar lap0 (setq tmp 'byte-discard)) | |
1596 (setcdr lap0 0)) | |
1597 ((error "Depth conflict at tag %d" (nth 2 lap0)))) | |
1598 (and (memq byte-optimize-log '(t byte)) | |
1599 (byte-compile-log " (goto %s) %s:\t-->\t%s %s:" | |
1600 (nth 1 lap1) (nth 1 lap1) | |
1601 tmp (nth 1 lap1))) | |
1602 (setq keep-going t)) | |
1603 ;; | |
1604 ;; varset-X varref-X --> dup varset-X | |
1605 ;; varbind-X varref-X --> dup varbind-X | |
1606 ;; const/dup varset-X varref-X --> const/dup varset-X const/dup | |
1607 ;; const/dup varbind-X varref-X --> const/dup varbind-X const/dup | |
1608 ;; The latter two can enable other optimizations. | |
1609 ;; | |
1610 ((and (eq 'byte-varref (car lap2)) | |
1611 (eq (cdr lap1) (cdr lap2)) | |
1612 (memq (car lap1) '(byte-varset byte-varbind))) | |
1613 (if (and (setq tmp (eq (built-in-variable-type (car (cdr lap2))) | |
1614 'boolean)) | |
1615 (not (eq (car lap0) 'byte-constant))) | |
1616 nil | |
1617 (setq keep-going t) | |
1618 (if (memq (car lap0) '(byte-constant byte-dup)) | |
1619 (progn | |
1620 (setq tmp (if (or (not tmp) | |
1621 (memq (car (cdr lap0)) '(nil t))) | |
1622 (cdr lap0) | |
1623 (byte-compile-get-constant t))) | |
1624 (byte-compile-log-lap " %s %s %s\t-->\t%s %s %s" | |
1625 lap0 lap1 lap2 lap0 lap1 | |
1626 (cons (car lap0) tmp)) | |
1627 (setcar lap2 (car lap0)) | |
1628 (setcdr lap2 tmp)) | |
1629 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1) | |
1630 (setcar lap2 (car lap1)) | |
1631 (setcar lap1 'byte-dup) | |
1632 (setcdr lap1 0) | |
1633 ;; The stack depth gets locally increased, so we will | |
1634 ;; increase maxdepth in case depth = maxdepth here. | |
1635 ;; This can cause the third argument to byte-code to | |
1636 ;; be larger than necessary. | |
1637 (setq add-depth 1)))) | |
1638 ;; | |
1639 ;; dup varset-X discard --> varset-X | |
1640 ;; dup varbind-X discard --> varbind-X | |
1641 ;; (the varbind variant can emerge from other optimizations) | |
1642 ;; | |
1643 ((and (eq 'byte-dup (car lap0)) | |
1644 (eq 'byte-discard (car lap2)) | |
1645 (memq (car lap1) '(byte-varset byte-varbind))) | |
1646 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1) | |
1647 (setq keep-going t | |
1648 rest (cdr rest)) | |
1649 (setq lap (delq lap0 (delq lap2 lap)))) | |
1650 ;; | |
1651 ;; not goto-X-if-nil --> goto-X-if-non-nil | |
1652 ;; not goto-X-if-non-nil --> goto-X-if-nil | |
1653 ;; | |
1654 ;; it is wrong to do the same thing for the -else-pop variants. | |
1655 ;; | |
1656 ((and (eq 'byte-not (car lap0)) | |
1657 (or (eq 'byte-goto-if-nil (car lap1)) | |
1658 (eq 'byte-goto-if-not-nil (car lap1)))) | |
1659 (byte-compile-log-lap " not %s\t-->\t%s" | |
1660 lap1 | |
1661 (cons | |
1662 (if (eq (car lap1) 'byte-goto-if-nil) | |
1663 'byte-goto-if-not-nil | |
1664 'byte-goto-if-nil) | |
1665 (cdr lap1))) | |
1666 (setcar lap1 (if (eq (car lap1) 'byte-goto-if-nil) | |
1667 'byte-goto-if-not-nil | |
1668 'byte-goto-if-nil)) | |
1669 (setq lap (delq lap0 lap)) | |
1670 (setq keep-going t)) | |
1671 ;; | |
1672 ;; goto-X-if-nil goto-Y X: --> goto-Y-if-non-nil X: | |
1673 ;; goto-X-if-non-nil goto-Y X: --> goto-Y-if-nil X: | |
1674 ;; | |
1675 ;; it is wrong to do the same thing for the -else-pop variants. | |
440 | 1676 ;; |
428 | 1677 ((and (or (eq 'byte-goto-if-nil (car lap0)) |
1678 (eq 'byte-goto-if-not-nil (car lap0))) ; gotoX | |
1679 (eq 'byte-goto (car lap1)) ; gotoY | |
1680 (eq (cdr lap0) lap2)) ; TAG X | |
1681 (let ((inverse (if (eq 'byte-goto-if-nil (car lap0)) | |
1682 'byte-goto-if-not-nil 'byte-goto-if-nil))) | |
1683 (byte-compile-log-lap " %s %s %s:\t-->\t%s %s:" | |
1684 lap0 lap1 lap2 | |
1685 (cons inverse (cdr lap1)) lap2) | |
1686 (setq lap (delq lap0 lap)) | |
1687 (setcar lap1 inverse) | |
1688 (setq keep-going t))) | |
1689 ;; | |
1690 ;; const goto-if-* --> whatever | |
1691 ;; | |
1692 ((and (eq 'byte-constant (car lap0)) | |
1693 (memq (car lap1) byte-conditional-ops)) | |
1694 (cond ((if (or (eq (car lap1) 'byte-goto-if-nil) | |
1695 (eq (car lap1) 'byte-goto-if-nil-else-pop)) | |
1696 (car (cdr lap0)) | |
1697 (not (car (cdr lap0)))) | |
1698 (byte-compile-log-lap " %s %s\t-->\t<deleted>" | |
1699 lap0 lap1) | |
1700 (setq rest (cdr rest) | |
1701 lap (delq lap0 (delq lap1 lap)))) | |
1702 (t | |
1703 (if (memq (car lap1) byte-goto-always-pop-ops) | |
1704 (progn | |
1705 (byte-compile-log-lap " %s %s\t-->\t%s" | |
1706 lap0 lap1 (cons 'byte-goto (cdr lap1))) | |
1707 (setq lap (delq lap0 lap))) | |
1708 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 | |
1709 (cons 'byte-goto (cdr lap1)))) | |
1710 (setcar lap1 'byte-goto))) | |
1711 (setq keep-going t)) | |
1712 ;; | |
1713 ;; varref-X varref-X --> varref-X dup | |
1714 ;; varref-X [dup ...] varref-X --> varref-X [dup ...] dup | |
1715 ;; We don't optimize the const-X variations on this here, | |
1716 ;; because that would inhibit some goto optimizations; we | |
1717 ;; optimize the const-X case after all other optimizations. | |
1718 ;; | |
1719 ((and (eq 'byte-varref (car lap0)) | |
1720 (progn | |
1721 (setq tmp (cdr rest)) | |
1722 (while (eq (car (car tmp)) 'byte-dup) | |
1723 (setq tmp (cdr tmp))) | |
1724 t) | |
1725 (eq (cdr lap0) (cdr (car tmp))) | |
1726 (eq 'byte-varref (car (car tmp)))) | |
1727 (if (memq byte-optimize-log '(t byte)) | |
1728 (let ((str "")) | |
1729 (setq tmp2 (cdr rest)) | |
1730 (while (not (eq tmp tmp2)) | |
1731 (setq tmp2 (cdr tmp2) | |
1732 str (concat str " dup"))) | |
1733 (byte-compile-log-lap " %s%s %s\t-->\t%s%s dup" | |
1734 lap0 str lap0 lap0 str))) | |
1735 (setq keep-going t) | |
1736 (setcar (car tmp) 'byte-dup) | |
1737 (setcdr (car tmp) 0) | |
1738 (setq rest tmp)) | |
1739 ;; | |
1740 ;; TAG1: TAG2: --> TAG1: <deleted> | |
1741 ;; (and other references to TAG2 are replaced with TAG1) | |
1742 ;; | |
1743 ((and (eq (car lap0) 'TAG) | |
1744 (eq (car lap1) 'TAG)) | |
1745 (and (memq byte-optimize-log '(t byte)) | |
1746 (byte-compile-log " adjacent tags %d and %d merged" | |
1747 (nth 1 lap1) (nth 1 lap0))) | |
1748 (setq tmp3 lap) | |
1749 (while (setq tmp2 (rassq lap0 tmp3)) | |
1750 (setcdr tmp2 lap1) | |
1751 (setq tmp3 (cdr (memq tmp2 tmp3)))) | |
1752 (setq lap (delq lap0 lap) | |
1753 keep-going t)) | |
1754 ;; | |
1755 ;; unused-TAG: --> <deleted> | |
1756 ;; | |
1757 ((and (eq 'TAG (car lap0)) | |
1758 (not (rassq lap0 lap))) | |
1759 (and (memq byte-optimize-log '(t byte)) | |
1760 (byte-compile-log " unused tag %d removed" (nth 1 lap0))) | |
1761 (setq lap (delq lap0 lap) | |
1762 keep-going t)) | |
1763 ;; | |
1764 ;; goto ... --> goto <delete until TAG or end> | |
1765 ;; return ... --> return <delete until TAG or end> | |
1766 ;; | |
1767 ((and (memq (car lap0) '(byte-goto byte-return)) | |
1768 (not (memq (car lap1) '(TAG nil)))) | |
1769 (setq tmp rest) | |
1770 (let ((i 0) | |
1771 (opt-p (memq byte-optimize-log '(t lap))) | |
1772 str deleted) | |
1773 (while (and (setq tmp (cdr tmp)) | |
1774 (not (eq 'TAG (car (car tmp))))) | |
1775 (if opt-p (setq deleted (cons (car tmp) deleted) | |
1776 str (concat str " %s") | |
1777 i (1+ i)))) | |
1778 (if opt-p | |
440 | 1779 (let ((tagstr |
428 | 1780 (if (eq 'TAG (car (car tmp))) |
1781 (format "%d:" (car (cdr (car tmp)))) | |
1782 (or (car tmp) "")))) | |
1783 (if (< i 6) | |
1784 (apply 'byte-compile-log-lap-1 | |
1785 (concat " %s" str | |
1786 " %s\t-->\t%s <deleted> %s") | |
1787 lap0 | |
1788 (nconc (nreverse deleted) | |
1789 (list tagstr lap0 tagstr))) | |
1790 (byte-compile-log-lap | |
1791 " %s <%d unreachable op%s> %s\t-->\t%s <deleted> %s" | |
1792 lap0 i (if (= i 1) "" "s") | |
1793 tagstr lap0 tagstr)))) | |
1794 (rplacd rest tmp)) | |
1795 (setq keep-going t)) | |
1796 ;; | |
1797 ;; <safe-op> unbind --> unbind <safe-op> | |
1798 ;; (this may enable other optimizations.) | |
1799 ;; | |
1800 ((and (eq 'byte-unbind (car lap1)) | |
1801 (memq (car lap0) byte-after-unbind-ops)) | |
1802 (byte-compile-log-lap " %s %s\t-->\t%s %s" lap0 lap1 lap1 lap0) | |
1803 (setcar rest lap1) | |
1804 (setcar (cdr rest) lap0) | |
1805 (setq keep-going t)) | |
1806 ;; | |
1807 ;; varbind-X unbind-N --> discard unbind-(N-1) | |
1808 ;; save-excursion unbind-N --> unbind-(N-1) | |
1809 ;; save-restriction unbind-N --> unbind-(N-1) | |
1810 ;; | |
1811 ((and (eq 'byte-unbind (car lap1)) | |
1812 (memq (car lap0) '(byte-varbind byte-save-excursion | |
1813 byte-save-restriction)) | |
1814 (< 0 (cdr lap1))) | |
1815 (if (zerop (setcdr lap1 (1- (cdr lap1)))) | |
1816 (delq lap1 rest)) | |
1817 (if (eq (car lap0) 'byte-varbind) | |
1818 (setcar rest (cons 'byte-discard 0)) | |
1819 (setq lap (delq lap0 lap))) | |
1820 (byte-compile-log-lap " %s %s\t-->\t%s %s" | |
1821 lap0 (cons (car lap1) (1+ (cdr lap1))) | |
1822 (if (eq (car lap0) 'byte-varbind) | |
1823 (car rest) | |
1824 (car (cdr rest))) | |
1825 (if (and (/= 0 (cdr lap1)) | |
1826 (eq (car lap0) 'byte-varbind)) | |
1827 (car (cdr rest)) | |
1828 "")) | |
1829 (setq keep-going t)) | |
1830 ;; | |
1831 ;; goto*-X ... X: goto-Y --> goto*-Y | |
1832 ;; goto-X ... X: return --> return | |
1833 ;; | |
1834 ((and (memq (car lap0) byte-goto-ops) | |
1835 (memq (car (setq tmp (nth 1 (memq (cdr lap0) lap)))) | |
1836 '(byte-goto byte-return))) | |
1837 (cond ((and (not (eq tmp lap0)) | |
1838 (or (eq (car lap0) 'byte-goto) | |
1839 (eq (car tmp) 'byte-goto))) | |
1840 (byte-compile-log-lap " %s [%s]\t-->\t%s" | |
1841 (car lap0) tmp tmp) | |
1842 (if (eq (car tmp) 'byte-return) | |
1843 (setcar lap0 'byte-return)) | |
1844 (setcdr lap0 (cdr tmp)) | |
1845 (setq keep-going t)))) | |
1846 ;; | |
1847 ;; goto-*-else-pop X ... X: goto-if-* --> whatever | |
1848 ;; goto-*-else-pop X ... X: discard --> whatever | |
1849 ;; | |
1850 ((and (memq (car lap0) '(byte-goto-if-nil-else-pop | |
1851 byte-goto-if-not-nil-else-pop)) | |
1852 (memq (car (car (setq tmp (cdr (memq (cdr lap0) lap))))) | |
1853 (eval-when-compile | |
1854 (cons 'byte-discard byte-conditional-ops))) | |
1855 (not (eq lap0 (car tmp)))) | |
1856 (setq tmp2 (car tmp)) | |
1857 (setq tmp3 (assq (car lap0) '((byte-goto-if-nil-else-pop | |
1858 byte-goto-if-nil) | |
1859 (byte-goto-if-not-nil-else-pop | |
1860 byte-goto-if-not-nil)))) | |
1861 (if (memq (car tmp2) tmp3) | |
1862 (progn (setcar lap0 (car tmp2)) | |
1863 (setcdr lap0 (cdr tmp2)) | |
1864 (byte-compile-log-lap " %s-else-pop [%s]\t-->\t%s" | |
1865 (car lap0) tmp2 lap0)) | |
1866 ;; Get rid of the -else-pop's and jump one step further. | |
1867 (or (eq 'TAG (car (nth 1 tmp))) | |
1868 (setcdr tmp (cons (byte-compile-make-tag) | |
1869 (cdr tmp)))) | |
1870 (byte-compile-log-lap " %s [%s]\t-->\t%s <skip>" | |
1871 (car lap0) tmp2 (nth 1 tmp3)) | |
1872 (setcar lap0 (nth 1 tmp3)) | |
1873 (setcdr lap0 (nth 1 tmp))) | |
1874 (setq keep-going t)) | |
1875 ;; | |
1876 ;; const goto-X ... X: goto-if-* --> whatever | |
1877 ;; const goto-X ... X: discard --> whatever | |
1878 ;; | |
1879 ((and (eq (car lap0) 'byte-constant) | |
1880 (eq (car lap1) 'byte-goto) | |
1881 (memq (car (car (setq tmp (cdr (memq (cdr lap1) lap))))) | |
1882 (eval-when-compile | |
1883 (cons 'byte-discard byte-conditional-ops))) | |
1884 (not (eq lap1 (car tmp)))) | |
1885 (setq tmp2 (car tmp)) | |
1886 (cond ((memq (car tmp2) | |
1887 (if (null (car (cdr lap0))) | |
1888 '(byte-goto-if-nil byte-goto-if-nil-else-pop) | |
1889 '(byte-goto-if-not-nil | |
1890 byte-goto-if-not-nil-else-pop))) | |
1891 (byte-compile-log-lap " %s goto [%s]\t-->\t%s %s" | |
1892 lap0 tmp2 lap0 tmp2) | |
1893 (setcar lap1 (car tmp2)) | |
1894 (setcdr lap1 (cdr tmp2)) | |
1895 ;; Let next step fix the (const,goto-if*) sequence. | |
1896 (setq rest (cons nil rest))) | |
1897 (t | |
1898 ;; Jump one step further | |
1899 (byte-compile-log-lap | |
1900 " %s goto [%s]\t-->\t<deleted> goto <skip>" | |
1901 lap0 tmp2) | |
1902 (or (eq 'TAG (car (nth 1 tmp))) | |
1903 (setcdr tmp (cons (byte-compile-make-tag) | |
1904 (cdr tmp)))) | |
1905 (setcdr lap1 (car (cdr tmp))) | |
1906 (setq lap (delq lap0 lap)))) | |
1907 (setq keep-going t)) | |
1908 ;; | |
1909 ;; X: varref-Y ... varset-Y goto-X --> | |
1910 ;; X: varref-Y Z: ... dup varset-Y goto-Z | |
1911 ;; (varset-X goto-BACK, BACK: varref-X --> copy the varref down.) | |
1912 ;; (This is so usual for while loops that it is worth handling). | |
1913 ;; | |
1914 ((and (eq (car lap1) 'byte-varset) | |
1915 (eq (car lap2) 'byte-goto) | |
1916 (not (memq (cdr lap2) rest)) ;Backwards jump | |
1917 (eq (car (car (setq tmp (cdr (memq (cdr lap2) lap))))) | |
1918 'byte-varref) | |
1919 (eq (cdr (car tmp)) (cdr lap1)) | |
1920 (not (eq (built-in-variable-type (car (cdr lap1))) | |
1921 'boolean))) | |
1922 ;;(byte-compile-log-lap " Pulled %s to end of loop" (car tmp)) | |
1923 (let ((newtag (byte-compile-make-tag))) | |
1924 (byte-compile-log-lap | |
1925 " %s: %s ... %s %s\t-->\t%s: %s %s: ... %s %s %s" | |
1926 (nth 1 (cdr lap2)) (car tmp) | |
1927 lap1 lap2 | |
1928 (nth 1 (cdr lap2)) (car tmp) | |
1929 (nth 1 newtag) 'byte-dup lap1 | |
1930 (cons 'byte-goto newtag) | |
1931 ) | |
1932 (setcdr rest (cons (cons 'byte-dup 0) (cdr rest))) | |
1933 (setcdr tmp (cons (setcdr lap2 newtag) (cdr tmp)))) | |
1934 (setq add-depth 1) | |
1935 (setq keep-going t)) | |
1936 ;; | |
1937 ;; goto-X Y: ... X: goto-if*-Y --> goto-if-not-*-X+1 Y: | |
1938 ;; (This can pull the loop test to the end of the loop) | |
1939 ;; | |
1940 ((and (eq (car lap0) 'byte-goto) | |
1941 (eq (car lap1) 'TAG) | |
1942 (eq lap1 | |
1943 (cdr (car (setq tmp (cdr (memq (cdr lap0) lap)))))) | |
1944 (memq (car (car tmp)) | |
1945 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil | |
1946 byte-goto-if-nil-else-pop))) | |
1947 ;; (byte-compile-log-lap " %s %s, %s %s --> moved conditional" | |
1948 ;; lap0 lap1 (cdr lap0) (car tmp)) | |
1949 (let ((newtag (byte-compile-make-tag))) | |
1950 (byte-compile-log-lap | |
1951 "%s %s: ... %s: %s\t-->\t%s ... %s:" | |
1952 lap0 (nth 1 lap1) (nth 1 (cdr lap0)) (car tmp) | |
1953 (cons (cdr (assq (car (car tmp)) | |
1954 '((byte-goto-if-nil . byte-goto-if-not-nil) | |
1955 (byte-goto-if-not-nil . byte-goto-if-nil) | |
1956 (byte-goto-if-nil-else-pop . | |
1957 byte-goto-if-not-nil-else-pop) | |
1958 (byte-goto-if-not-nil-else-pop . | |
1959 byte-goto-if-nil-else-pop)))) | |
1960 newtag) | |
440 | 1961 |
428 | 1962 (nth 1 newtag) |
1963 ) | |
1964 (setcdr tmp (cons (setcdr lap0 newtag) (cdr tmp))) | |
1965 (if (eq (car (car tmp)) 'byte-goto-if-nil-else-pop) | |
1966 ;; We can handle this case but not the -if-not-nil case, | |
1967 ;; because we won't know which non-nil constant to push. | |
1968 (setcdr rest (cons (cons 'byte-constant | |
1969 (byte-compile-get-constant nil)) | |
1970 (cdr rest)))) | |
1971 (setcar lap0 (nth 1 (memq (car (car tmp)) | |
1972 '(byte-goto-if-nil-else-pop | |
1973 byte-goto-if-not-nil | |
1974 byte-goto-if-nil | |
1975 byte-goto-if-not-nil | |
1976 byte-goto byte-goto)))) | |
1977 ) | |
1978 (setq keep-going t)) | |
1979 ) | |
1980 (setq rest (cdr rest))) | |
1981 ) | |
1982 ;; Cleanup stage: | |
1983 ;; Rebuild byte-compile-constants / byte-compile-variables. | |
1984 ;; Simple optimizations that would inhibit other optimizations if they | |
1985 ;; were done in the optimizing loop, and optimizations which there is no | |
442 | 1986 ;; need to do more than once. |
428 | 1987 (setq byte-compile-constants nil |
442 | 1988 byte-compile-variables nil |
1989 variable-frequency (make-hash-table :test 'eq)) | |
428 | 1990 (setq rest lap) |
1991 (while rest | |
1992 (setq lap0 (car rest) | |
1993 lap1 (nth 1 rest)) | |
1297 | 1994 (if (memq (car lap0) byte-constref-ops) |
1995 (if (not (eq (car lap0) 'byte-constant)) | |
1996 (progn | |
1997 (incf (gethash (cdr lap0) variable-frequency 0)) | |
1998 (or (memq (cdr lap0) byte-compile-variables) | |
1999 (setq byte-compile-variables | |
2000 (cons (cdr lap0) byte-compile-variables)))) | |
2001 (or (memq (cdr lap0) byte-compile-constants) | |
2002 (setq byte-compile-constants (cons (cdr lap0) | |
2003 byte-compile-constants))))) | |
428 | 2004 (cond (;; |
442 | 2005 ;; const-C varset-X const-C --> const-C dup varset-X |
428 | 2006 ;; const-C varbind-X const-C --> const-C dup varbind-X |
2007 ;; | |
2008 (and (eq (car lap0) 'byte-constant) | |
2009 (eq (car (nth 2 rest)) 'byte-constant) | |
442 | 2010 (eq (cdr lap0) (cdr (nth 2 rest))) |
428 | 2011 (memq (car lap1) '(byte-varbind byte-varset))) |
2012 (byte-compile-log-lap " %s %s %s\t-->\t%s dup %s" | |
2013 lap0 lap1 lap0 lap0 lap1) | |
2014 (setcar (cdr (cdr rest)) (cons (car lap1) (cdr lap1))) | |
2015 (setcar (cdr rest) (cons 'byte-dup 0)) | |
2016 (setq add-depth 1)) | |
2017 ;; | |
2018 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup | |
2019 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup | |
2020 ;; | |
2021 ((memq (car lap0) '(byte-constant byte-varref)) | |
2022 (setq tmp rest | |
2023 tmp2 nil) | |
2024 (while (progn | |
2025 (while (eq 'byte-dup (car (car (setq tmp (cdr tmp)))))) | |
2026 (and (eq (cdr lap0) (cdr (car tmp))) | |
2027 (eq (car lap0) (car (car tmp))))) | |
2028 (setcar tmp (cons 'byte-dup 0)) | |
2029 (setq tmp2 t)) | |
2030 (if tmp2 | |
2031 (byte-compile-log-lap | |
2032 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0))) | |
2033 ;; | |
2034 ;; unbind-N unbind-M --> unbind-(N+M) | |
2035 ;; | |
2036 ((and (eq 'byte-unbind (car lap0)) | |
2037 (eq 'byte-unbind (car lap1))) | |
2038 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 | |
2039 (cons 'byte-unbind | |
2040 (+ (cdr lap0) (cdr lap1)))) | |
2041 (setq keep-going t) | |
2042 (setq lap (delq lap0 lap)) | |
2043 (setcdr lap1 (+ (cdr lap1) (cdr lap0)))) | |
2044 ) | |
2045 (setq rest (cdr rest))) | |
442 | 2046 ;; Since the first 6 entries of the compiled-function constants |
2047 ;; vector are most efficient for varref/set/bind ops, we sort by | |
2048 ;; reference count. This generates maximally space efficient and | |
2049 ;; pretty time-efficient byte-code. See `byte-compile-constants-vector'. | |
2050 (setq byte-compile-variables | |
2051 (sort byte-compile-variables | |
2052 #'(lambda (v1 v2) | |
2053 (< (gethash v1 variable-frequency) | |
2054 (gethash v2 variable-frequency))))) | |
2055 ;; Another hack - put the most used variable in position 6, for | |
2056 ;; better locality of reference with adjoining constants. | |
2057 (let ((tail (last byte-compile-variables 6))) | |
2058 (setq byte-compile-variables | |
2059 (append (nbutlast byte-compile-variables 6) | |
2060 (nreverse tail)))) | |
428 | 2061 (setq byte-compile-maxdepth (+ byte-compile-maxdepth add-depth))) |
2062 lap) | |
2063 | |
2064 (provide 'byte-optimize) | |
2065 | |
2066 | |
2067 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when this file compiles | |
2068 ;; itself, compile some of its most used recursive functions (at load time). | |
2069 ;; | |
2070 (eval-when-compile | |
2071 (or (compiled-function-p (symbol-function 'byte-optimize-form)) | |
2072 (assq 'byte-code (symbol-function 'byte-optimize-form)) | |
2073 (let ((byte-optimize nil) | |
2074 (byte-compile-warnings nil)) | |
2075 (mapcar | |
2076 #'(lambda (x) | |
2077 (or noninteractive (message "compiling %s..." x)) | |
2078 (byte-compile x) | |
2079 (or noninteractive (message "compiling %s...done" x))) | |
2080 '(byte-optimize-form | |
2081 byte-optimize-body | |
2082 byte-optimize-predicate | |
2083 byte-optimize-binary-predicate | |
2084 ;; Inserted some more than necessary, to speed it up. | |
2085 byte-optimize-form-code-walker | |
2086 byte-optimize-lapcode)))) | |
2087 nil) | |
2088 | |
1297 | 2089 ;; END SYNC WITH 20.7. |
2090 | |
428 | 2091 ;;; byte-optimize.el ends here |