1 ;;; byte-opt.el --- the optimization passes of the emacs-lisp byte compiler
3 ;; Copyright (C) 1991, 1994, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
11 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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."
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
40 ;; (apply (lambda (x &rest y) ...) 1 (foo))
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)
57 ;; (put 'gc-cons-threshold 'binding-is-magic t)
58 ;; (put 'track-mouse 'binding-is-magic t)
61 ;; Simple defsubsts often produce forms like
62 ;; (let ((v1 (f1)) (v2 (f2)) ...)
64 ;; It would be nice if we could optimize this to
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...
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
84 ;; collapse common subexpressions
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))
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
99 ;; Here is an example of an Emacs Lisp function which could safely be
100 ;; byte-compiled tail-recursively:
102 ;; (defun tail-map (fn list)
104 ;; (funcall fn (car list))
105 ;; (tail-map fn (cdr list)))))
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.
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)
117 ;; this also can be considered tail recursion:
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)
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
129 ;; Wouldn't it be nice if Emacs Lisp had lexical scope.
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.
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.)
148 ;; Other things to consider:
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)
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.
158 ;; (disassemble (lambda (x) (and (point) (foo))))
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:
167 ;; (disassemble (lambda (x) (eq (if (point) 'a 'b) 'c)))
168 ;; (disassemble (lambda (x) (if (point) (eq 'a 'c) (eq 'b 'c))))
170 ;; ;; (car (cons A B)) -> (prog1 A B)
171 ;; (disassemble (lambda (x) (car (cons (foo) 42))))
173 ;; ;; (cdr (cons A B)) -> (progn A B)
174 ;; (disassemble (lambda (x) (cdr (cons 42 (foo)))))
176 ;; ;; (car (list A B ...)) -> (prog1 A B ...)
177 ;; (disassemble (lambda (x) (car (list (foo) 42 (bar)))))
179 ;; ;; (cdr (list A B ...)) -> (progn A (list B ...))
180 ;; (disassemble (lambda (x) (cdr (list 42 (foo) (bar)))))
186 (eval-when-compile (require 'cl
))
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"))
192 (apply 'format format
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))
200 (if (integerp (setq c
(car arg
)))
201 (error "non-symbolic byte-op %s" c
))
204 (setq a
(cond ((memq c byte-goto-ops
)
205 (car (cdr (cdr arg
))))
206 ((memq c byte-constref-ops
)
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
))))
216 (format "(%s %s)" c a
))))
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
)))
224 ;;; byte-compile optimizers to support inlining
226 (put 'inline
'byte-optimizer
'byte-optimize-inline-handler
)
228 (defun byte-optimize-inline-handler (form)
229 "byte-optimize-handler for the `inline' special-form."
233 (let ((f (car-safe sexp
)))
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
)))
241 (byte-compile-inline-expand sexp
)
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
)))
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
)))))
259 (byte-compile-warn "attempt to inline `%s' before it was defined"
263 (when (and (consp fn
) (eq (car fn
) 'autoload
))
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
)
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)))
283 (if (eq (car-safe fn
) 'lambda
)
284 (macroexpand-all (cons fn
(cdr form
))
285 byte-compile-macro-environment
)
286 ;; Give up on inlining.
289 ;; ((lambda ...) ...)
290 (defun byte-compile-unfold-lambda (form &optional name
)
291 (or name
(setq name
"anonymous lambda"))
292 (let ((lambda (car 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
)))
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
)))
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
))
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
))
324 (setq bindings
(cons (list (car arglist
)
325 (and values
(cons 'list values
)))
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
))
332 (setq bindings
(cons (list (car arglist
) (car values
))
334 values
(cdr values
))))
335 (setq arglist
(cdr arglist
)))
338 (or (eq values
'too-few
)
340 "attempt to open-code `%s' with too many arguments" name
))
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)))
350 (cons 'let
(cons (nreverse bindings
) body
))
351 (cons 'progn body
))))
352 (byte-compile-log " %s\t==>\t%s" form newform
)
356 ;;; implementing source-level optimizers
358 (defun byte-optimize-form-code-walker (form for-effect
)
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
366 (let ((fn (car-safe form
))
368 (cond ((not (consp form
))
369 (if (not (and for-effect
370 (or byte-compile-delete-errors
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).
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
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.
396 (mapcar (lambda (binding)
397 (if (symbolp binding
)
399 (if (cdr (cdr binding
))
400 (byte-compile-warn "malformed let binding: `%s'"
401 (prin1-to-string binding
)))
403 (byte-optimize-form (nth 1 binding
) nil
))))
405 (byte-optimize-body (cdr (cdr form
)) for-effect
))))
408 (mapcar (lambda (clause)
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
))
418 ;; as an extra added bonus, this simplifies (progn <x>) --> <x>
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
)))
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
)))
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
)))))
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
)))
443 ((eq fn
'with-output-to-temp-buffer
)
444 ;; this is just like the above, except for the first argument.
447 (byte-optimize-form (nth 1 form
) nil
)
448 (byte-optimize-body (cdr (cdr form
)) for-effect
))))
451 (when (< (length form
) 3)
452 (byte-compile-warn "too few arguments for `if'"))
454 (cons (byte-optimize-form (nth 1 form
) nil
)
456 (byte-optimize-form (nth 2 form
) for-effect
)
457 (byte-optimize-body (nthcdr 3 form
) for-effect
)))))
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.
465 (let ((backwards (reverse (cdr form
))))
466 (while (and backwards
467 (null (setcar backwards
468 (byte-optimize-form (car backwards
)
470 (setq backwards
(cdr backwards
)))
471 (if (and (cdr form
) (null backwards
))
473 " all subforms of %s called for effect; deleted" form
))
475 (cons fn
(nreverse (mapcar 'byte-optimize-form backwards
)))))
476 (cons fn
(mapcar 'byte-optimize-form
(cdr form
)))))
478 ((eq fn
'interactive
)
479 (byte-compile-warn "misplaced interactive spec: `%s'"
480 (prin1-to-string form
))
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.
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.)
496 (cons (byte-optimize-form (nth 1 form
) for-effect
)
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.
505 (cons (byte-optimize-form (nth 1 form
) nil
)
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
))))
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.
520 (setq form
(macroexpand form
521 byte-compile-macro-environment
))))
522 (byte-optimize-form form for-effect
))
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
)
530 (setq form
(compiler-macroexpand form
))))))
531 (byte-optimize-form form for-effect
))
534 (byte-compile-warn "`%s' is a malformed function"
535 (prin1-to-string fn
))
538 ((and for-effect
(setq tmp
(get fn
'side-effect-free
))
539 (or byte-compile-delete-errors
541 ;; Detect the expansion of (pop foo).
542 ;; There is no need to compile the call to `car' there.
544 (eq (car-safe (cadr form
)) 'prog1
)
545 (let ((var (cadr (cadr form
)))
546 (last (nth 2 (cadr form
))))
548 (null (nthcdr 3 (cadr form
)))
549 (eq (car-safe last
) 'setq
)
551 (eq (car-safe (nth 2 last
)) 'cdr
)
552 (eq (cadr (nth 2 last
)) var
))))
554 (byte-compile-warn "value returned from %s is unused"
555 (prin1-to-string form
))
557 (byte-compile-log " %s called for effect; deleted" fn
)
558 ;; appending a nil here might not be necessary, but it can't hurt.
560 (cons 'progn
(append (cdr form
) '(nil))) 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
)))
572 (defun byte-optimize-all-constp (list)
573 "Non-nil if all elements of LIST satisfy `byte-compile-constp'."
575 (while (and list constant
)
576 (unless (byte-compile-constp (car list
))
578 (setq list
(cdr list
)))
581 (defun byte-optimize-form (form &optional for-effect
)
582 "The source-level pass of the optimizer."
584 ;; First, optimize all sub-forms of this one.
585 (setq form
(byte-optimize-form-code-walker form for-effect
))
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.
593 (if (and (consp form
)
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
)))))
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
))
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.
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
)))
625 ;; some source-level optimizers
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.
632 ;; It is now safe to optimize code such that it introduces new bindings.
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
)))))
641 ;; Can't use recursion in a defsubst.
642 ;; (progn (byte-compile-trueconstp (car (last (cdr form)))))
644 ((not (symbolp form
)))
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
)))))
654 (quote (null (cadr form
)))
655 ;; Can't use recursion in a defsubst.
656 ;; (progn (byte-compile-nilconstp (car (last (cdr form)))))
658 ((not (symbolp form
)) nil
)
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)
669 (if (numberp (car rest
))
670 (setq constants
(cons (car rest
) constants
))
671 (setq args
(cons (car rest
) args
)))
672 (setq rest
(cdr rest
)))
676 (apply (car form
) constants
)
678 (cons (car form
) (nreverse args
))
680 (apply (car form
) constants
))
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)
688 (defun byte-optimize-nonassociative-math (form)
689 (if (or (not (numberp (car (cdr form
))))
690 (not (numberp (car (cdr (cdr 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
))
698 (cons (car form
) (cons constant rest
))
701 ;;(defun byte-optimize-associative-two-args-math (form)
702 ;; (setq form (byte-optimize-associative-math form))
704 ;; (byte-optimize-two-args-left form)
707 ;;(defun byte-optimize-nonassociative-two-args-math (form)
708 ;; (setq form (byte-optimize-nonassociative-math form))
710 ;; (byte-optimize-two-args-right form)
713 (defun byte-optimize-approx-equal (x y
)
714 (<= (* (abs (- x y
)) 100) (abs (+ x y
))))
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
))
728 ;; t means we must check for overflow.
729 (overflow (memq fun
'(+ *))))
730 (while (cdr (setq rest
(cdr rest
)))
731 (if (integerp (car rest
))
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
))
739 ;; If necessary, check now for overflow
740 ;; that might be caused by reordering.
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
)))))
749 (setq form
(nconc (delq nil form
)
750 (list (apply fun
(nreverse constants
)))))))))
753 (defsubst byte-compile-butlast
(form)
754 (nreverse (cdr (reverse form
))))
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
)))
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))))
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
)))
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
778 (byte-optimize-predicate form
))
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 '+))
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)
790 (setq form
(nconc form
(list 0)))))
791 ;; For (- constants..), byte-optimize-predicate does the work.
792 (when (memq nil
(mapcar 'numberp
(cdr form
)))
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
))))
801 ((and (eq (nth 1 form
) 0)
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
809 (byte-optimize-predicate form
))
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
)))))
819 ;; Would handling (* ... 0) here cause floating point errors?
821 ((eq 1 last
) (setq form
(byte-compile-butlast form
)))
823 (setq form
(list '-
(if (nthcdr 3 form
)
824 (byte-compile-butlast form
)
826 (byte-optimize-predicate form
))
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
))))))
834 ;; Runtime error (leave it intact).
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 ..))
847 (setq form
(list '-
(if (nthcdr 3 form
)
848 (byte-compile-butlast form
)
850 (byte-optimize-predicate form
))
852 (defun byte-optimize-logmumble (form)
853 (setq form
(byte-optimize-delay-constants-math form
1 (car form
)))
854 (byte-optimize-predicate
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
)
861 (cons 'progn
(cdr form
)))
865 (defun byte-optimize-binary-predicate (form)
866 (if (byte-compile-constp (nth 1 form
))
867 (if (byte-compile-constp (nth 2 form
))
869 (list 'quote
(eval form
))
871 ;; This can enable some lapcode optimizations.
872 (list (car form
) (nth 2 form
) (nth 1 form
)))
875 (defun byte-optimize-predicate (form)
879 (setq ok
(byte-compile-constp (car rest
))
883 (list 'quote
(eval form
))
887 (defun byte-optimize-identity (form)
888 (if (and (cdr form
) (null (cdr (cdr form
))))
890 (byte-compile-warn "identity called with %d arg%s, but requires 1"
892 (if (= 1 (length (cdr form
))) "" "s"))
895 (put 'identity
'byte-optimizer
'byte-optimize-identity
)
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
)
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
)
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
)
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
)
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
)
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
))))
949 (defun byte-optimize-zerop (form)
950 (cond ((numberp (nth 1 form
))
952 (byte-compile-delete-errors
953 (list '= (nth 1 form
) 0))
956 (put 'zerop
'byte-optimizer
'byte-optimize-zerop
)
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
)))
966 (prog1 (setq form
(copy-sequence form
))
968 (setq form
(cdr form
)))
971 ((null (cdr (cdr form
)))
973 ((byte-optimize-predicate form
))))
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
980 (setq form
(delq nil
(copy-sequence 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
))))
987 (byte-optimize-predicate form
)
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.
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
))))
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
))))
1007 (setq form
(copy-sequence form
))
1008 (setcdr (memq (car rest
) form
) 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
)))))
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
))
1022 (cons (car form
) (cdr (cdr form
)))))
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
)
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
)))
1048 (if (equal '(nil) (nthcdr 3 form
))
1049 (list 'if clause
(nth 2 form
))
1051 ((or (nth 3 form
) (nthcdr 4 form
))
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))
1060 (cons 'progn
(nthcdr 3 form
))
1063 (list 'progn clause nil
)))))
1065 (defun byte-optimize-while (form)
1066 (when (< (length form
) 2)
1067 (byte-compile-warn "too few arguments for `while'"))
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
)
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
)
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
)))
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
))))
1103 "last arg to apply can't be a literal atom: `%s'"
1104 (prin1-to-string last
))
1108 (put 'funcall
'byte-optimizer
'byte-optimize-funcall
)
1109 (put 'apply
'byte-optimizer
'byte-optimize-apply
)
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
))
1117 (cons 'progn
(cdr (cdr form
))))
1118 ((or (nth 2 form
) (nthcdr 3 form
))
1121 ((eq (car form
) 'let
)
1122 (append '(progn) (mapcar 'car-safe
(mapcar 'cdr-safe
(nth 1 form
)))
1125 (let ((binds (reverse (nth 1 form
))))
1126 (list 'let
* (reverse (cdr binds
)) (nth 1 (car binds
)) nil
)))))
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
))
1135 (list 'cdr
(nth 2 form
))))
1136 (byte-optimize-predicate form
))
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
)))
1148 (byte-optimize-predicate form
))
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.
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
))))
1161 (if (member (cdr-safe form
) '(((quote emacs
))))
1165 (put 'set
'byte-optimizer
'byte-optimize-set
)
1166 (defun byte-optimize-set (form)
1167 (let ((var (car-safe (cdr-safe form
))))
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
)
1174 `(progn ,(cadr form
) (setq ,(cadr var
) ,@(cddr form
))))
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))
1181 ;; may safely be turned into
1182 ;; (progn (progn (something-with-side-effects) (yow))
1184 ;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo.
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.
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.
1198 ;; I wonder if I missed any :-\)
1199 (let ((side-effect-free-fns
1200 '(%
* + -
/ /= 1+ 1-
< <= = > >= abs acos append aref ash asin atan
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
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
1216 get gethash get-buffer get-buffer-window getenv get-file-buffer
1218 int-to-string intern-soft
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
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
1236 unibyte-char-to-multibyte upcase user-full-name
1237 user-login-name user-original-login-name user-variable-p
1239 window-buffer window-dedicated-p window-edges window-height
1240 window-hscroll window-minibuffer-p window-width
1242 (side-effect-and-error-free-fns
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
1255 identity ignore integerp integer-or-marker-p interactive-p
1256 invocation-directory invocation-name
1258 line-beginning-position line-end-position list listp
1259 make-marker mark mark-marker markerp max-char
1260 memory-limit minibuffer-window
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
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
)))
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.
1290 '(concat symbol-name regexp-opt regexp-quote string-to-syntax
)))
1292 (put (car pure-fns
) 'pure t
)
1293 (setq pure-fns
(cdr pure-fns
)))
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
))))
1306 (put 'byte-code
'byte-compile
'byte-compile-splice-in-already-compiled-code
)
1309 (defconst byte-constref-ops
1310 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind
))
1312 ;; This function extracts the bitfields from variable-length opcodes.
1313 ;; Originally defined in disass.el (which no longer uses it.)
1315 (defun disassemble-offset ()
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))
1327 ;; Offset in next byte.
1328 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1329 (aref bytedecomp-bytes bytedecomp-ptr
))
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
))))
1354 ;; This de-compiler is used for inline expansion of compiled functions,
1355 ;; and by the disassembler.
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
)))
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
1380 (while (not (= bytedecomp-ptr length
))
1382 (setq lap
(cons bytedecomp-ptr lap
)))
1383 (setq bytedecomp-op
(aref bytedecomp-bytes 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
)
1390 (cdr (or (assq offset tags
)
1393 (byte-compile-make-tag))
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
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)))
1425 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
)))
1426 ;; take off the dummy nil op that we replaced a trailing "return" with.
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
))
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
)))
1441 (setq lap
(cons (cons nil endtag
) lap
)))
1442 ;; remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* )
1443 (mapcar (function (lambda (elt)
1450 ;;; peephole optimizer
1452 (defconst byte-tagref-ops
(cons 'TAG byte-goto-ops
))
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
))
1458 (defconst byte-after-unbind-ops
1459 '(byte-constant byte-dup
1460 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-integerp
1462 byte-cons byte-list1 byte-list2
; byte-list3 byte-list4
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.")
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
))
1479 (defconst byte-compile-side-effect-free-ops
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
)
1487 byte-compile-side-effect-and-error-free-ops
))
1489 ;; This crock is because of the way DEFVAR_BOOL variables work.
1490 ;; Consider the code
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)
1499 ;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is
1500 ;; something else. But if we optimize
1503 ;; varbind pop-up-windows
1504 ;; varref pop-up-windows
1509 ;; varbind pop-up-windows
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.
1516 ;; The variable `byte-boolean-vars' is now primitive and updated
1517 ;; automatically by DEFVAR_BOOL.
1519 (defmacro byte-opt-update-stack-params
(stack-adjust stack-depth lap0 rest lap
)
1520 "...macro used by byte-optimize-lapcode..."
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.
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
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
))
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.
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
)
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."
1559 (initial-stack-depth
1560 (if (and lap
(eq (car (car lap
)) 'TAG
))
1561 (cdr (cdr (car lap
)))
1563 (keep-going 'first-time
)
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
)))
1570 (or (eq keep-going
'first-time
)
1571 (byte-compile-log-lap " ---- next pass"))
1573 stack-depth initial-stack-depth
1576 (setq lap0
(car rest
)
1580 (byte-opt-update-stack-params stack-adjust stack-depth lap0 rest lap
)
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.
1586 ;; <side-effect-free> pop --> <deleted>
1588 ;; const-X pop --> <deleted>
1589 ;; varref-X pop --> <deleted>
1590 ;; dup pop --> <deleted>
1592 ((and (eq 'byte-discard
(car lap1
))
1593 (memq (car lap0
) side-effect-free
))
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
))))
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
)
1609 ((error "Optimizer error: too much on the stack")))
1610 (setq stack-adjust
(1- stack-adjust
)))
1612 ;; goto*-X X: --> X:
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
))
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
)
1627 (setq keep-going t
))
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.
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
)))
1646 (if (memq (car lap0
) '(byte-constant byte-dup
))
1648 (setq tmp
(if (or (not tmp
)
1649 (byte-compile-const-symbol-p
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
))
1658 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1
)
1659 (setcar lap2
(car lap1
))
1660 (setcar lap1
'byte-dup
)
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))))
1668 ;; dup varset-X discard --> varset-X
1669 ;; dup varbind-X discard --> varbind-X
1670 ;; (the varbind variant can emerge from other optimizations)
1672 ((and (eq 'byte-dup
(car lap0
))
1673 (eq 'byte-discard
(car lap2
))
1674 (memq (car lap1
) '(byte-varset byte-varbind byte-stack-set
)))
1675 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1
)
1679 (setq lap
(delq lap0
(delq lap2 lap
))))
1681 ;; not goto-X-if-nil --> goto-X-if-non-nil
1682 ;; not goto-X-if-non-nil --> goto-X-if-nil
1684 ;; it is wrong to do the same thing for the -else-pop variants.
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"
1692 (if (eq (car lap1
) 'byte-goto-if-nil
)
1693 'byte-goto-if-not-nil
1696 (setcar lap1
(if (eq (car lap1
) 'byte-goto-if-nil
)
1697 'byte-goto-if-not-nil
1699 (setq lap
(delq lap0 lap
))
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:
1706 ;; it is wrong to do the same thing for the -else-pop variants.
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:"
1716 (cons inverse
(cdr lap1
)) lap2
)
1717 (setq lap
(delq lap0 lap
)
1719 (setcar lap1 inverse
)
1720 (setq keep-going t
)))
1722 ;; const goto-if-* --> whatever
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
))
1729 (not (car (cdr lap0
))))
1730 (byte-compile-log-lap " %s %s\t-->\t<deleted>"
1732 (setq rest
(cdr rest
)
1733 lap
(delq lap0
(delq lap1 lap
))))
1735 (byte-compile-log-lap " %s %s\t-->\t%s"
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
)))
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.
1750 ((and (memq (car lap0
) '(byte-varref byte-stack-ref
))
1752 (setq tmp
(cdr rest
) tmp2
0)
1753 (while (eq (car (car tmp
)) 'byte-dup
)
1754 (setq tmp
(cdr tmp
) tmp2
(1+ tmp2
)))
1756 (eq (car lap0
) (car (car tmp
)))
1757 (eq (cdr lap0
) (cdr (car tmp
))))
1758 (if (memq byte-optimize-log
'(t byte
))
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
)))
1767 (setcar (car tmp
) 'byte-dup
)
1768 (setcdr (car tmp
) 0)
1770 stack-adjust
(+ 2 tmp2
)))
1772 ;; TAG1: TAG2: --> TAG1: <deleted>
1773 ;; (and other references to TAG2 are replaced with TAG1)
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
)))
1781 (while (setq tmp2
(rassq lap0 tmp3
))
1783 (setq tmp3
(cdr (memq tmp2 tmp3
))))
1784 (setq lap
(delq lap0 lap
)
1787 ;; unused-TAG: --> <deleted>
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
)
1796 ;; goto ... --> goto <delete until TAG or end>
1797 ;; return ... --> return <delete until TAG or end>
1799 ((and (memq (car lap0
) '(byte-goto byte-return
))
1800 (not (memq (car lap1
) '(TAG nil
))))
1803 (opt-p (memq byte-optimize-log
'(t lap
)))
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")
1812 (if (eq 'TAG
(car (car tmp
)))
1813 (format "%d:" (car (cdr (car tmp
))))
1814 (or (car tmp
) ""))))
1816 (apply 'byte-compile-log-lap-1
1818 " %s\t-->\t%s <deleted> %s")
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
))))
1827 (setq keep-going t
))
1829 ;; <safe-op> unbind --> unbind <safe-op>
1830 ;; (this may enable other optimizations.)
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
)
1836 (setcar (cdr rest
) lap0
)
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)
1844 ((and (eq 'byte-unbind
(car lap1
))
1845 (memq (car lap0
) '(byte-varbind byte-save-excursion
1846 byte-save-restriction
))
1848 (if (zerop (setcdr lap1
(1- (cdr lap1
))))
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
)
1858 (if (and (/= 0 (cdr lap1
))
1859 (eq (car lap0
) 'byte-varbind
))
1862 (setq keep-going t
))
1864 ;; stack-ref-N --> dup ; where N is TOS
1866 ((and stack-depth
(eq (car lap0
) 'byte-stack-ref
)
1867 (= (cdr lap0
) (1- stack-depth
)))
1868 (setcar lap0
'byte-dup
)
1870 (setq keep-going t
))
1872 ;; goto*-X ... X: goto-Y --> goto*-Y
1873 ;; goto-X ... X: return --> return
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"
1883 (if (eq (car tmp
) 'byte-return
)
1884 (setcar lap0
'byte-return
))
1885 (setcdr lap0
(cdr tmp
))
1886 (setq keep-going t
))))
1888 ;; goto-*-else-pop X ... X: goto-if-* --> whatever
1889 ;; goto-*-else-pop X ... X: discard --> whatever
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
)))))
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
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)
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
))
1917 ;; const goto-X ... X: goto-if-* --> whatever
1918 ;; const goto-X ... X: discard --> whatever
1920 ((and (eq (car lap0
) 'byte-constant
)
1921 (eq (car lap1
) 'byte-goto
)
1922 (memq (car (car (setq tmp
(cdr (memq (cdr lap1
) lap
)))))
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
)))
1939 ;; Jump one step further
1940 (byte-compile-log-lap
1941 " %s goto [%s]\t-->\t<deleted> goto <skip>"
1943 (or (eq 'TAG
(car (nth 1 tmp
)))
1944 (setcdr tmp
(cons (byte-compile-make-tag)
1946 (setcdr lap1
(car (cdr tmp
)))
1947 (setq lap
(delq lap0 lap
))))
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).
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
)
1970 (nth 1 (cdr lap2
)) (car tmp
)
1971 (nth 1 newtag
) 'byte-dup lap1
1972 (cons 'byte-goto newtag
)
1974 (setcdr rest
(cons (cons 'byte-dup
0) (cdr rest
)))
1975 (setcdr tmp
(cons (setcdr lap2 newtag
) (cdr tmp
))))
1977 (setq keep-going t
))
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)
1982 ((and (eq (car lap0
) 'byte-goto
)
1983 (eq (car lap1
) 'TAG
)
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
))))
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
))
2013 (setcar lap0
(nth 1 (memq (car (car tmp
))
2014 '(byte-goto-if-nil-else-pop
2015 byte-goto-if-not-nil
2017 byte-goto-if-not-nil
2018 byte-goto byte-goto
))))
2021 stack-adjust
(and (not (eq (car lap0
) 'byte-goto
)) -
1)))
2025 (and stack-depth stack-adjust
(+ stack-depth stack-adjust
)))
2026 (setq rest
(cdr rest
)))
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
)
2037 stack-depth initial-stack-depth
)
2038 (byte-compile-log-lap " ---- final pass")
2040 (setq lap0
(car 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
)))))
2053 ;; const-C varset-X const-C --> const-C dup varset-X
2054 ;; const-C varbind-X const-C --> const-C dup varbind-X
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))
2066 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup
2067 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup
2069 ((memq (car lap0
) '(byte-constant byte-varref
))
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))
2079 (byte-compile-log-lap
2080 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0
)))
2082 ;; unbind-N unbind-M --> unbind-(N+M)
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
2088 (+ (cdr lap0
) (cdr lap1
))))
2089 (setq lap
(delq lap0 lap
))
2090 (setcdr lap1
(+ (cdr lap1
) (cdr lap0
))))
2093 ;; stack-set-M [discard/discardN ...] --> discardN-preserve-tos
2094 ;; stack-set-M [discard/discardN ...] --> discardN
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
))
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
)))
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
)))
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
)))
2120 ;; Otherwise, the value stored is lost, so just use a
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"
2130 ;; discard/discardN/discardN-preserve-tos-X discard/discardN-Y -->
2133 ((and (memq (car lap0
)
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)"
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))
2150 ;; discardN-preserve-tos-X discardN-preserve-tos-Y -->
2151 ;; discardN-preserve-tos-(X+Y)
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
)))
2161 ;; discardN-preserve-tos return --> return
2162 ;; dup return --> return
2163 ;; stack-set-N return --> return ; where N is TOS-1
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
))
2177 ;; dup stack-set-N return --> return ; where N is TOS
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
))
2191 (and stack-depth stack-adjust
(+ stack-depth stack-adjust
)))
2192 (setq rest
(cdr rest
)))
2194 (setq byte-compile-maxdepth
(+ byte-compile-maxdepth add-depth
)))
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).
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
))
2209 (or noninteractive
(message "compiling %s..." x
))
2211 (or noninteractive
(message "compiling %s...done" x
)))
2212 '(byte-optimize-form
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
))))
2221 ;;; byte-opt.el ends here