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