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