comparison lisp/byte-optimize.el @ 209:41ff10fd062f r20-4b3

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