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 ;; "Replay" the operations: we used to just do
252 ;; (setq byte-compile-output (nconc (nreverse lap) byte-compile-output))
253 ;; but that fails to update byte-compile-depth, so we had to assume
254 ;; that `lap' ends up adding exactly 1 element to the stack. This
255 ;; happens to be true for byte-code generated by bytecomp.el without
256 ;; lexical-binding, but it's not true in general, and it's not true for
257 ;; code output by bytecomp.el with lexical-binding.
260 ((eq (car op) 'TAG) (byte-compile-out-tag op))
261 ((memq (car op) byte-goto-ops) (byte-compile-goto (car op) (cdr op)))
262 (t (byte-compile-out (car op) (cdr op))))))
264 (defun byte-compile-inline-expand (form)
265 (let* ((name (car form))
266 (fn (or (cdr (assq name byte-compile-function-environment))
267 (and (fboundp name) (symbol-function name)))))
270 (byte-compile-warn "attempt to inline `%s' before it was defined"
274 (when (and (consp fn) (eq (car fn) 'autoload))
276 (setq fn (or (and (fboundp name) (symbol-function name))
277 (cdr (assq name byte-compile-function-environment)))))
278 (if (and (consp fn) (eq (car fn) 'autoload))
279 (error "File `%s' didn't define `%s'" (nth 1 fn) name))
281 ((and (symbolp fn) (not (eq fn t))) ;A function alias.
282 (byte-compile-inline-expand (cons fn (cdr form))))
283 ((and (byte-code-function-p fn)
284 ;; FIXME: This works to inline old-style-byte-codes into
285 ;; old-style-byte-codes, but not mixed cases (not sure
286 ;; about new-style into new-style).
287 (not lexical-binding)
288 (not (and (>= (length fn) 7)
289 (aref fn 6)))) ;6 = COMPILED_PUSH_ARGS
290 ;; (message "Inlining %S byte-code" name)
292 (let ((string (aref fn 1)))
293 ;; Isn't it an error for `string' not to be unibyte?? --stef
294 (if (fboundp 'string-as-unibyte)
295 (setq string (string-as-unibyte string)))
296 ;; `byte-compile-splice-in-already-compiled-code'
297 ;; takes care of inlining the body.
298 (cons `(lambda ,(aref fn 0)
299 (byte-code ,string ,(aref fn 2) ,(aref fn 3)))
301 ((eq (car-safe fn) 'lambda)
302 (macroexpand-all (cons fn (cdr form))
303 byte-compile-macro-environment))
304 (t ;; Give up on inlining.
307 ;; ((lambda ...) ...)
308 (defun byte-compile-unfold-lambda (form &optional name)
309 (or name (setq name "anonymous lambda"))
310 (let ((lambda (car form))
312 (if (byte-code-function-p lambda)
313 (setq lambda (list 'lambda (aref lambda 0)
314 (list 'byte-code (aref lambda 1)
315 (aref lambda 2) (aref lambda 3)))))
316 (let ((arglist (nth 1 lambda))
317 (body (cdr (cdr lambda)))
320 (if (and (stringp (car body)) (cdr body))
321 (setq body (cdr body)))
322 (if (and (consp (car body)) (eq 'interactive (car (car body))))
323 (setq body (cdr body)))
325 (cond ((eq (car arglist) '&optional)
326 ;; ok, I'll let this slide because funcall_lambda() does...
327 ;; (if optionalp (error "multiple &optional keywords in %s" name))
328 (if restp (error "&optional found after &rest in %s" name))
329 (if (null (cdr arglist))
330 (error "nothing after &optional in %s" name))
332 ((eq (car arglist) '&rest)
333 ;; ...but it is by no stretch of the imagination a reasonable
334 ;; thing that funcall_lambda() allows (&rest x y) and
335 ;; (&rest x &optional y) in arglists.
336 (if (null (cdr arglist))
337 (error "nothing after &rest in %s" name))
338 (if (cdr (cdr arglist))
339 (error "multiple vars after &rest in %s" name))
342 (setq bindings (cons (list (car arglist)
343 (and values (cons 'list values)))
346 ((and (not optionalp) (null values))
347 (byte-compile-warn "attempt to open-code `%s' with too few arguments" name)
348 (setq arglist nil values 'too-few))
350 (setq bindings (cons (list (car arglist) (car values))
352 values (cdr values))))
353 (setq arglist (cdr arglist)))
356 (or (eq values 'too-few)
358 "attempt to open-code `%s' with too many arguments" name))
361 ;; The following leads to infinite recursion when loading a
362 ;; file containing `(defsubst f () (f))', and then trying to
363 ;; byte-compile that file.
364 ;(setq body (mapcar 'byte-optimize-form body)))
368 (cons 'let (cons (nreverse bindings) body))
369 (cons 'progn body))))
370 (byte-compile-log " %s\t==>\t%s" form newform)
374 ;;; implementing source-level optimizers
376 (defun byte-optimize-form-code-walker (form for-effect)
378 ;; For normal function calls, We can just mapcar the optimizer the cdr. But
379 ;; we need to have special knowledge of the syntax of the special forms
380 ;; like let and defun (that's why they're special forms :-). (Actually,
381 ;; the important aspect is that they are subrs that don't evaluate all of
384 (let ((fn (car-safe form))
386 (cond ((not (consp form))
387 (if (not (and for-effect
388 (or byte-compile-delete-errors
394 (byte-compile-warn "malformed quote form: `%s'"
395 (prin1-to-string form)))
396 ;; map (quote nil) to nil to simplify optimizer logic.
397 ;; map quoted constants to nil if for-effect (just because).
401 ((or (byte-code-function-p fn)
402 (eq 'lambda (car-safe fn)))
403 (let ((newform (byte-compile-unfold-lambda form)))
404 (if (eq newform form)
405 ;; Some error occurred, avoid infinite recursion
407 (byte-optimize-form-code-walker newform for-effect))))
408 ((memq fn '(let let*))
409 ;; recursively enter the optimizer for the bindings and body
410 ;; of a let or let*. This for depth-firstness: forms that
411 ;; are more deeply nested are optimized first.
414 (mapcar (lambda (binding)
415 (if (symbolp binding)
417 (if (cdr (cdr binding))
418 (byte-compile-warn "malformed let binding: `%s'"
419 (prin1-to-string binding)))
421 (byte-optimize-form (nth 1 binding) nil))))
423 (byte-optimize-body (cdr (cdr form)) for-effect))))
426 (mapcar (lambda (clause)
429 (byte-optimize-form (car clause) nil)
430 (byte-optimize-body (cdr clause) for-effect))
431 (byte-compile-warn "malformed cond form: `%s'"
432 (prin1-to-string clause))
436 ;; as an extra added bonus, this simplifies (progn <x>) --> <x>
439 (setq tmp (byte-optimize-body (cdr form) for-effect))
440 (if (cdr tmp) (cons 'progn tmp) (car tmp)))
441 (byte-optimize-form (nth 1 form) for-effect)))
445 (cons (byte-optimize-form (nth 1 form) for-effect)
446 (byte-optimize-body (cdr (cdr form)) t)))
447 (byte-optimize-form (nth 1 form) for-effect)))
450 (cons (byte-optimize-form (nth 1 form) t)
451 (cons (byte-optimize-form (nth 2 form) for-effect)
452 (byte-optimize-body (cdr (cdr (cdr form))) t)))))
454 ((memq fn '(save-excursion save-restriction save-current-buffer))
455 ;; those subrs which have an implicit progn; it's not quite good
456 ;; enough to treat these like normal function calls.
457 ;; This can turn (save-excursion ...) into (save-excursion) which
458 ;; will be optimized away in the lap-optimize pass.
459 (cons fn (byte-optimize-body (cdr form) for-effect)))
461 ((eq fn 'with-output-to-temp-buffer)
462 ;; this is just like the above, except for the first argument.
465 (byte-optimize-form (nth 1 form) nil)
466 (byte-optimize-body (cdr (cdr form)) for-effect))))
469 (when (< (length form) 3)
470 (byte-compile-warn "too few arguments for `if'"))
472 (cons (byte-optimize-form (nth 1 form) nil)
474 (byte-optimize-form (nth 2 form) for-effect)
475 (byte-optimize-body (nthcdr 3 form) for-effect)))))
477 ((memq fn '(and or)) ; remember, and/or are control structures.
478 ;; take forms off the back until we can't any more.
479 ;; In the future it could conceivably be a problem that the
480 ;; subexpressions of these forms are optimized in the reverse
481 ;; order, but it's ok for now.
483 (let ((backwards (reverse (cdr form))))
484 (while (and backwards
485 (null (setcar backwards
486 (byte-optimize-form (car backwards)
488 (setq backwards (cdr backwards)))
489 (if (and (cdr form) (null backwards))
491 " all subforms of %s called for effect; deleted" form))
493 (cons fn (nreverse (mapcar 'byte-optimize-form backwards)))))
494 (cons fn (mapcar 'byte-optimize-form (cdr form)))))
496 ((eq fn 'interactive)
497 (byte-compile-warn "misplaced interactive spec: `%s'"
498 (prin1-to-string form))
501 ((memq fn '(defun defmacro function
502 condition-case save-window-excursion))
503 ;; These forms are compiled as constants or by breaking out
504 ;; all the subexpressions and compiling them separately.
507 ((eq fn 'unwind-protect)
508 ;; the "protected" part of an unwind-protect is compiled (and thus
509 ;; optimized) as a top-level form, so don't do it here. But the
510 ;; non-protected part has the same for-effect status as the
511 ;; unwind-protect itself. (The protected part is always for effect,
512 ;; but that isn't handled properly yet.)
514 (cons (byte-optimize-form (nth 1 form) for-effect)
518 ;; the body of a catch is compiled (and thus optimized) as a
519 ;; top-level form, so don't do it here. The tag is never
520 ;; for-effect. The body should have the same for-effect status
521 ;; as the catch form itself, but that isn't handled properly yet.
523 (cons (byte-optimize-form (nth 1 form) nil)
527 ;; Don't treat the args to `ignore' as being
528 ;; computed for effect. We want to avoid the warnings
529 ;; that might occur if they were treated that way.
530 ;; However, don't actually bother calling `ignore'.
531 `(prog1 nil . ,(mapcar 'byte-optimize-form (cdr form))))
533 ;; If optimization is on, this is the only place that macros are
534 ;; expanded. If optimization is off, then macroexpansion happens
535 ;; in byte-compile-form. Otherwise, the macros are already expanded
536 ;; by the time that is reached.
538 (setq form (macroexpand form
539 byte-compile-macro-environment))))
540 (byte-optimize-form form for-effect))
542 ;; Support compiler macros as in cl.el.
543 ((and (fboundp 'compiler-macroexpand)
544 (symbolp (car-safe form))
545 (get (car-safe form) 'cl-compiler-macro)
548 (setq form (compiler-macroexpand form))))))
549 (byte-optimize-form form for-effect))
552 (byte-compile-warn "`%s' is a malformed function"
553 (prin1-to-string fn))
556 ((and for-effect (setq tmp (get fn 'side-effect-free))
557 (or byte-compile-delete-errors
559 ;; Detect the expansion of (pop foo).
560 ;; There is no need to compile the call to `car' there.
562 (eq (car-safe (cadr form)) 'prog1)
563 (let ((var (cadr (cadr form)))
564 (last (nth 2 (cadr form))))
566 (null (nthcdr 3 (cadr form)))
567 (eq (car-safe last) 'setq)
569 (eq (car-safe (nth 2 last)) 'cdr)
570 (eq (cadr (nth 2 last)) var))))
572 (byte-compile-warn "value returned from %s is unused"
573 (prin1-to-string form))
575 (byte-compile-log " %s called for effect; deleted" fn)
576 ;; appending a nil here might not be necessary, but it can't hurt.
578 (cons 'progn (append (cdr form) '(nil))) t))
581 ;; Otherwise, no args can be considered to be for-effect,
582 ;; even if the called function is for-effect, because we
583 ;; don't know anything about that function.
584 (let ((args (mapcar #'byte-optimize-form (cdr form))))
585 (if (and (get fn 'pure)
586 (byte-optimize-all-constp args))
587 (list 'quote (apply fn (mapcar #'eval args)))
590 (defun byte-optimize-all-constp (list)
591 "Non-nil if all elements of LIST satisfy `byte-compile-constp'."
593 (while (and list constant)
594 (unless (byte-compile-constp (car list))
596 (setq list (cdr list)))
599 (defun byte-optimize-form (form &optional for-effect)
600 "The source-level pass of the optimizer."
602 ;; First, optimize all sub-forms of this one.
603 (setq form (byte-optimize-form-code-walker form for-effect))
605 ;; after optimizing all subforms, optimize this form until it doesn't
606 ;; optimize any further. This means that some forms will be passed through
607 ;; the optimizer many times, but that's necessary to make the for-effect
608 ;; processing do as much as possible.
611 (if (and (consp form)
614 ;; we don't have any of these yet, but we might.
615 (setq opt (get (car form) 'byte-for-effect-optimizer)))
616 (setq opt (get (car form) 'byte-optimizer)))
617 (not (eq form (setq new (funcall opt form)))))
619 ;; (if (equal form new) (error "bogus optimizer -- %s" opt))
620 (byte-compile-log " %s\t==>\t%s" form new)
621 (setq new (byte-optimize-form new for-effect))
626 (defun byte-optimize-body (forms all-for-effect)
627 ;; optimize the cdr of a progn or implicit progn; all forms is a list of
628 ;; forms, all but the last of which are optimized with the assumption that
629 ;; they are being called for effect. the last is for-effect as well if
630 ;; all-for-effect is true. returns a new list of forms.
635 (setq fe (or all-for-effect (cdr rest)))
636 (setq new (and (car rest) (byte-optimize-form (car rest) fe)))
637 (if (or new (not fe))
638 (setq result (cons new result)))
639 (setq rest (cdr rest)))
643 ;; some source-level optimizers
645 ;; when writing optimizers, be VERY careful that the optimizer returns
646 ;; something not EQ to its argument if and ONLY if it has made a change.
647 ;; This implies that you cannot simply destructively modify the list;
648 ;; you must return something not EQ to it if you make an optimization.
650 ;; It is now safe to optimize code such that it introduces new bindings.
652 (defsubst byte-compile-trueconstp (form)
653 "Return non-nil if FORM always evaluates to a non-nil value."
654 (while (eq (car-safe form) 'progn)
655 (setq form (car (last (cdr form)))))
659 ;; Can't use recursion in a defsubst.
660 ;; (progn (byte-compile-trueconstp (car (last (cdr form)))))
662 ((not (symbolp form)))
666 (defsubst byte-compile-nilconstp (form)
667 "Return non-nil if FORM always evaluates to a nil value."
668 (while (eq (car-safe form) 'progn)
669 (setq form (car (last (cdr form)))))
672 (quote (null (cadr form)))
673 ;; Can't use recursion in a defsubst.
674 ;; (progn (byte-compile-nilconstp (car (last (cdr form)))))
676 ((not (symbolp form)) nil)
679 ;; If the function is being called with constant numeric args,
680 ;; evaluate as much as possible at compile-time. This optimizer
681 ;; assumes that the function is associative, like + or *.
682 (defun byte-optimize-associative-math (form)
687 (if (numberp (car rest))
688 (setq constants (cons (car rest) constants))
689 (setq args (cons (car rest) args)))
690 (setq rest (cdr rest)))
694 (apply (car form) constants)
696 (cons (car form) (nreverse args))
698 (apply (car form) constants))
701 ;; If the function is being called with constant numeric args,
702 ;; evaluate as much as possible at compile-time. This optimizer
703 ;; assumes that the function satisfies
704 ;; (op x1 x2 ... xn) == (op ...(op (op x1 x2) x3) ...xn)
706 (defun byte-optimize-nonassociative-math (form)
707 (if (or (not (numberp (car (cdr form))))
708 (not (numberp (car (cdr (cdr form))))))
710 (let ((constant (car (cdr form)))
711 (rest (cdr (cdr form))))
712 (while (numberp (car rest))
713 (setq constant (funcall (car form) constant (car rest))
716 (cons (car form) (cons constant rest))
719 ;;(defun byte-optimize-associative-two-args-math (form)
720 ;; (setq form (byte-optimize-associative-math form))
722 ;; (byte-optimize-two-args-left form)
725 ;;(defun byte-optimize-nonassociative-two-args-math (form)
726 ;; (setq form (byte-optimize-nonassociative-math form))
728 ;; (byte-optimize-two-args-right form)
731 (defun byte-optimize-approx-equal (x y)
732 (<= (* (abs (- x y)) 100) (abs (+ x y))))
734 ;; Collect all the constants from FORM, after the STARTth arg,
735 ;; and apply FUN to them to make one argument at the end.
736 ;; For functions that can handle floats, that optimization
737 ;; can be incorrect because reordering can cause an overflow
738 ;; that would otherwise be avoided by encountering an arg that is a float.
739 ;; We avoid this problem by (1) not moving float constants and
740 ;; (2) not moving anything if it would cause an overflow.
741 (defun byte-optimize-delay-constants-math (form start fun)
742 ;; Merge all FORM's constants from number START, call FUN on them
743 ;; and put the result at the end.
744 (let ((rest (nthcdr (1- start) form))
746 ;; t means we must check for overflow.
747 (overflow (memq fun '(+ *))))
748 (while (cdr (setq rest (cdr rest)))
749 (if (integerp (car rest))
751 (setq form (copy-sequence form)
752 rest (nthcdr (1- start) form))
753 (while (setq rest (cdr rest))
754 (cond ((integerp (car rest))
755 (setq constants (cons (car rest) constants))
757 ;; If necessary, check now for overflow
758 ;; that might be caused by reordering.
760 ;; We have overflow if the result of doing the arithmetic
761 ;; on floats is not even close to the result
762 ;; of doing it on integers.
763 (not (byte-optimize-approx-equal
764 (apply fun (mapcar 'float constants))
765 (float (apply fun constants)))))
767 (setq form (nconc (delq nil form)
768 (list (apply fun (nreverse constants)))))))))
771 (defsubst byte-compile-butlast (form)
772 (nreverse (cdr (reverse form))))
774 (defun byte-optimize-plus (form)
775 ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
776 ;;(setq form (byte-optimize-delay-constants-math form 1 '+))
777 (if (memq 0 form) (setq form (delq 0 (copy-sequence form))))
778 ;; For (+ constants...), byte-optimize-predicate does the work.
779 (when (memq nil (mapcar 'numberp (cdr form)))
781 ;; (+ x 1) --> (1+ x) and (+ x -1) --> (1- x).
782 ((and (= (length form) 3)
783 (or (memq (nth 1 form) '(1 -1))
784 (memq (nth 2 form) '(1 -1))))
786 (if (memq (nth 1 form) '(1 -1))
787 (setq integer (nth 1 form) other (nth 2 form))
788 (setq integer (nth 2 form) other (nth 1 form)))
790 (list (if (eq integer 1) '1+ '1-) other))))
791 ;; Here, we could also do
792 ;; (+ x y ... 1) --> (1+ (+ x y ...))
793 ;; (+ x y ... -1) --> (1- (+ x y ...))
794 ;; The resulting bytecode is smaller, but is it faster? -- cyd
796 (byte-optimize-predicate form))
798 (defun byte-optimize-minus (form)
799 ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
800 ;;(setq form (byte-optimize-delay-constants-math form 2 '+))
802 (when (and (nthcdr 3 form)
803 (memq 0 (cddr form)))
804 (setq form (nconc (list (car form) (cadr form))
805 (delq 0 (copy-sequence (cddr form)))))
806 ;; After the above, we must turn (- x) back into (- x 0)
808 (setq form (nconc form (list 0)))))
809 ;; For (- constants..), byte-optimize-predicate does the work.
810 (when (memq nil (mapcar 'numberp (cdr form)))
812 ;; (- x 1) --> (1- x)
813 ((equal (nthcdr 2 form) '(1))
814 (setq form (list '1- (nth 1 form))))
815 ;; (- x -1) --> (1+ x)
816 ((equal (nthcdr 2 form) '(-1))
817 (setq form (list '1+ (nth 1 form))))
819 ((and (eq (nth 1 form) 0)
821 (setq form (list '- (nth 2 form))))
822 ;; Here, we could also do
823 ;; (- x y ... 1) --> (1- (- x y ...))
824 ;; (- x y ... -1) --> (1+ (- x y ...))
825 ;; The resulting bytecode is smaller, but is it faster? -- cyd
827 (byte-optimize-predicate form))
829 (defun byte-optimize-multiply (form)
830 (setq form (byte-optimize-delay-constants-math form 1 '*))
831 ;; For (* constants..), byte-optimize-predicate does the work.
832 (when (memq nil (mapcar 'numberp (cdr form)))
833 ;; After `byte-optimize-predicate', if there is a INTEGER constant
834 ;; in FORM, it is in the last element.
835 (let ((last (car (reverse (cdr form)))))
837 ;; Would handling (* ... 0) here cause floating point errors?
839 ((eq 1 last) (setq form (byte-compile-butlast form)))
841 (setq form (list '- (if (nthcdr 3 form)
842 (byte-compile-butlast form)
844 (byte-optimize-predicate form))
846 (defun byte-optimize-divide (form)
847 (setq form (byte-optimize-delay-constants-math form 2 '*))
848 ;; After `byte-optimize-predicate', if there is a INTEGER constant
849 ;; in FORM, it is in the last element.
850 (let ((last (car (reverse (cdr (cdr form))))))
852 ;; Runtime error (leave it intact).
855 (memql 0.0 (cddr form))))
856 ;; No constants in expression
857 ((not (numberp last)))
858 ;; For (* constants..), byte-optimize-predicate does the work.
859 ((null (memq nil (mapcar 'numberp (cdr form)))))
860 ;; (/ x y.. 1) --> (/ x y..)
861 ((and (eq last 1) (nthcdr 3 form))
862 (setq form (byte-compile-butlast form)))
863 ;; (/ x -1), (/ x .. -1) --> (- x), (- (/ x ..))
865 (setq form (list '- (if (nthcdr 3 form)
866 (byte-compile-butlast form)
868 (byte-optimize-predicate form))
870 (defun byte-optimize-logmumble (form)
871 (setq form (byte-optimize-delay-constants-math form 1 (car form)))
872 (byte-optimize-predicate
874 (setq form (if (eq (car form) 'logand)
875 (cons 'progn (cdr form))
876 (delq 0 (copy-sequence form)))))
877 ((and (eq (car-safe form) 'logior)
879 (cons 'progn (cdr form)))
883 (defun byte-optimize-binary-predicate (form)
884 (if (byte-compile-constp (nth 1 form))
885 (if (byte-compile-constp (nth 2 form))
887 (list 'quote (eval form))
889 ;; This can enable some lapcode optimizations.
890 (list (car form) (nth 2 form) (nth 1 form)))
893 (defun byte-optimize-predicate (form)
897 (setq ok (byte-compile-constp (car rest))
901 (list 'quote (eval form))
905 (defun byte-optimize-identity (form)
906 (if (and (cdr form) (null (cdr (cdr form))))
908 (byte-compile-warn "identity called with %d arg%s, but requires 1"
910 (if (= 1 (length (cdr form))) "" "s"))
913 (put 'identity 'byte-optimizer 'byte-optimize-identity)
915 (put '+ 'byte-optimizer 'byte-optimize-plus)
916 (put '* 'byte-optimizer 'byte-optimize-multiply)
917 (put '- 'byte-optimizer 'byte-optimize-minus)
918 (put '/ 'byte-optimizer 'byte-optimize-divide)
919 (put 'max 'byte-optimizer 'byte-optimize-associative-math)
920 (put 'min 'byte-optimizer 'byte-optimize-associative-math)
922 (put '= 'byte-optimizer 'byte-optimize-binary-predicate)
923 (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate)
924 (put 'equal 'byte-optimizer 'byte-optimize-binary-predicate)
925 (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate)
926 (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate)
928 (put '< 'byte-optimizer 'byte-optimize-predicate)
929 (put '> 'byte-optimizer 'byte-optimize-predicate)
930 (put '<= 'byte-optimizer 'byte-optimize-predicate)
931 (put '>= 'byte-optimizer 'byte-optimize-predicate)
932 (put '1+ 'byte-optimizer 'byte-optimize-predicate)
933 (put '1- 'byte-optimizer 'byte-optimize-predicate)
934 (put 'not 'byte-optimizer 'byte-optimize-predicate)
935 (put 'null 'byte-optimizer 'byte-optimize-predicate)
936 (put 'memq 'byte-optimizer 'byte-optimize-predicate)
937 (put 'consp 'byte-optimizer 'byte-optimize-predicate)
938 (put 'listp 'byte-optimizer 'byte-optimize-predicate)
939 (put 'symbolp 'byte-optimizer 'byte-optimize-predicate)
940 (put 'stringp 'byte-optimizer 'byte-optimize-predicate)
941 (put 'string< 'byte-optimizer 'byte-optimize-predicate)
942 (put 'string-lessp 'byte-optimizer 'byte-optimize-predicate)
944 (put 'logand 'byte-optimizer 'byte-optimize-logmumble)
945 (put 'logior 'byte-optimizer 'byte-optimize-logmumble)
946 (put 'logxor 'byte-optimizer 'byte-optimize-logmumble)
947 (put 'lognot 'byte-optimizer 'byte-optimize-predicate)
949 (put 'car 'byte-optimizer 'byte-optimize-predicate)
950 (put 'cdr 'byte-optimizer 'byte-optimize-predicate)
951 (put 'car-safe 'byte-optimizer 'byte-optimize-predicate)
952 (put 'cdr-safe 'byte-optimizer 'byte-optimize-predicate)
955 ;; I'm not convinced that this is necessary. Doesn't the optimizer loop
956 ;; take care of this? - Jamie
957 ;; I think this may some times be necessary to reduce ie (quote 5) to 5,
958 ;; so arithmetic optimizers recognize the numeric constant. - Hallvard
959 (put 'quote 'byte-optimizer 'byte-optimize-quote)
960 (defun byte-optimize-quote (form)
961 (if (or (consp (nth 1 form))
962 (and (symbolp (nth 1 form))
963 (not (byte-compile-const-symbol-p form))))
967 (defun byte-optimize-zerop (form)
968 (cond ((numberp (nth 1 form))
970 (byte-compile-delete-errors
971 (list '= (nth 1 form) 0))
974 (put 'zerop 'byte-optimizer 'byte-optimize-zerop)
976 (defun byte-optimize-and (form)
977 ;; Simplify if less than 2 args.
978 ;; if there is a literal nil in the args to `and', throw it and following
979 ;; forms away, and surround the `and' with (progn ... nil).
980 (cond ((null (cdr form)))
984 (prog1 (setq form (copy-sequence form))
986 (setq form (cdr form)))
989 ((null (cdr (cdr form)))
991 ((byte-optimize-predicate form))))
993 (defun byte-optimize-or (form)
994 ;; Throw away nil's, and simplify if less than 2 args.
995 ;; If there is a literal non-nil constant in the args to `or', throw away all
998 (setq form (delq nil (copy-sequence form))))
1000 (while (cdr (setq rest (cdr rest)))
1001 (if (byte-compile-trueconstp (car rest))
1002 (setq form (copy-sequence form)
1003 rest (setcdr (memq (car rest) form) nil))))
1004 (if (cdr (cdr form))
1005 (byte-optimize-predicate form)
1008 (defun byte-optimize-cond (form)
1009 ;; if any clauses have a literal nil as their test, throw them away.
1010 ;; if any clause has a literal non-nil constant as its test, throw
1011 ;; away all following clauses.
1013 ;; This must be first, to reduce (cond (t ...) (nil)) to (progn t ...)
1014 (while (setq rest (assq nil (cdr form)))
1015 (setq form (delq rest (copy-sequence form))))
1016 (if (memq nil (cdr form))
1017 (setq form (delq nil (copy-sequence form))))
1019 (while (setq rest (cdr rest))
1020 (cond ((byte-compile-trueconstp (car-safe (car rest)))
1021 ;; This branch will always be taken: kill the subsequent ones.
1022 (cond ((eq rest (cdr form)) ;First branch of `cond'.
1023 (setq form `(progn ,@(car rest))))
1025 (setq form (copy-sequence form))
1026 (setcdr (memq (car rest) form) nil)))
1028 ((and (consp (car rest))
1029 (byte-compile-nilconstp (caar rest)))
1030 ;; This branch will never be taken: kill its body.
1031 (setcdr (car rest) nil)))))
1033 ;; Turn (cond (( <x> )) ... ) into (or <x> (cond ... ))
1034 (if (eq 'cond (car-safe form))
1035 (let ((clauses (cdr form)))
1036 (if (and (consp (car clauses))
1037 (null (cdr (car clauses))))
1038 (list 'or (car (car clauses))
1040 (cons (car form) (cdr (cdr form)))))
1044 (defun byte-optimize-if (form)
1045 ;; (if (progn <insts> <test>) <rest>) ==> (progn <insts> (if <test> <rest>))
1046 ;; (if <true-constant> <then> <else...>) ==> <then>
1047 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>)
1048 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>))
1049 ;; (if <test> <then> nil) ==> (if <test> <then>)
1050 (let ((clause (nth 1 form)))
1051 (cond ((and (eq (car-safe clause) 'progn)
1052 ;; `clause' is a proper list.
1053 (null (cdr (last clause))))
1054 (if (null (cddr clause))
1055 ;; A trivial `progn'.
1056 (byte-optimize-if `(if ,(cadr clause) ,@(nthcdr 2 form)))
1057 (nconc (butlast clause)
1060 `(if ,(car (last clause)) ,@(nthcdr 2 form)))))))
1061 ((byte-compile-trueconstp clause)
1062 `(progn ,clause ,(nth 2 form)))
1063 ((byte-compile-nilconstp clause)
1064 `(progn ,clause ,@(nthcdr 3 form)))
1066 (if (equal '(nil) (nthcdr 3 form))
1067 (list 'if clause (nth 2 form))
1069 ((or (nth 3 form) (nthcdr 4 form))
1071 ;; Don't make a double negative;
1072 ;; instead, take away the one that is there.
1073 (if (and (consp clause) (memq (car clause) '(not null))
1074 (= (length clause) 2)) ; (not xxxx) or (not (xxxx))
1078 (cons 'progn (nthcdr 3 form))
1081 (list 'progn clause nil)))))
1083 (defun byte-optimize-while (form)
1084 (when (< (length form) 2)
1085 (byte-compile-warn "too few arguments for `while'"))
1089 (put 'and 'byte-optimizer 'byte-optimize-and)
1090 (put 'or 'byte-optimizer 'byte-optimize-or)
1091 (put 'cond 'byte-optimizer 'byte-optimize-cond)
1092 (put 'if 'byte-optimizer 'byte-optimize-if)
1093 (put 'while 'byte-optimizer 'byte-optimize-while)
1095 ;; byte-compile-negation-optimizer lives in bytecomp.el
1096 (put '/= 'byte-optimizer 'byte-compile-negation-optimizer)
1097 (put 'atom 'byte-optimizer 'byte-compile-negation-optimizer)
1098 (put 'nlistp 'byte-optimizer 'byte-compile-negation-optimizer)
1101 (defun byte-optimize-funcall (form)
1102 ;; (funcall (lambda ...) ...) ==> ((lambda ...) ...)
1103 ;; (funcall foo ...) ==> (foo ...)
1104 (let ((fn (nth 1 form)))
1105 (if (memq (car-safe fn) '(quote function))
1106 (cons (nth 1 fn) (cdr (cdr form)))
1109 (defun byte-optimize-apply (form)
1110 ;; If the last arg is a literal constant, turn this into a funcall.
1111 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...).
1112 (let ((fn (nth 1 form))
1113 (last (nth (1- (length form)) form))) ; I think this really is fastest
1114 (or (if (or (null last)
1115 (eq (car-safe last) 'quote))
1116 (if (listp (nth 1 last))
1117 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form)))))))
1118 (nconc (list 'funcall fn) butlast
1119 (mapcar (lambda (x) (list 'quote x)) (nth 1 last))))
1121 "last arg to apply can't be a literal atom: `%s'"
1122 (prin1-to-string last))
1126 (put 'funcall 'byte-optimizer 'byte-optimize-funcall)
1127 (put 'apply 'byte-optimizer 'byte-optimize-apply)
1130 (put 'let 'byte-optimizer 'byte-optimize-letX)
1131 (put 'let* 'byte-optimizer 'byte-optimize-letX)
1132 (defun byte-optimize-letX (form)
1133 (cond ((null (nth 1 form))
1135 (cons 'progn (cdr (cdr form))))
1136 ((or (nth 2 form) (nthcdr 3 form))
1139 ((eq (car form) 'let)
1140 (append '(progn) (mapcar 'car-safe (mapcar 'cdr-safe (nth 1 form)))
1143 (let ((binds (reverse (nth 1 form))))
1144 (list 'let* (reverse (cdr binds)) (nth 1 (car binds)) nil)))))
1147 (put 'nth 'byte-optimizer 'byte-optimize-nth)
1148 (defun byte-optimize-nth (form)
1149 (if (= (safe-length form) 3)
1150 (if (memq (nth 1 form) '(0 1))
1151 (list 'car (if (zerop (nth 1 form))
1153 (list 'cdr (nth 2 form))))
1154 (byte-optimize-predicate form))
1157 (put 'nthcdr 'byte-optimizer 'byte-optimize-nthcdr)
1158 (defun byte-optimize-nthcdr (form)
1159 (if (= (safe-length form) 3)
1160 (if (memq (nth 1 form) '(0 1 2))
1161 (let ((count (nth 1 form)))
1162 (setq form (nth 2 form))
1163 (while (>= (setq count (1- count)) 0)
1164 (setq form (list 'cdr form)))
1166 (byte-optimize-predicate form))
1169 ;; Fixme: delete-char -> delete-region (byte-coded)
1170 ;; optimize string-as-unibyte, string-as-multibyte, string-make-unibyte,
1171 ;; string-make-multibyte for constant args.
1173 (put 'featurep 'byte-optimizer 'byte-optimize-featurep)
1174 (defun byte-optimize-featurep (form)
1175 ;; Emacs-21's byte-code doesn't run under XEmacs or SXEmacs anyway, so we
1176 ;; can safely optimize away this test.
1177 (if (member (cdr-safe form) '(((quote xemacs)) ((quote sxemacs))))
1179 (if (member (cdr-safe form) '(((quote emacs))))
1183 (put 'set 'byte-optimizer 'byte-optimize-set)
1184 (defun byte-optimize-set (form)
1185 (let ((var (car-safe (cdr-safe form))))
1187 ((and (eq (car-safe var) 'quote) (consp (cdr var)))
1188 `(setq ,(cadr var) ,@(cddr form)))
1189 ((and (eq (car-safe var) 'make-local-variable)
1190 (eq (car-safe (setq var (car-safe (cdr var)))) 'quote)
1192 `(progn ,(cadr form) (setq ,(cadr var) ,@(cddr form))))
1195 ;; enumerating those functions which need not be called if the returned
1196 ;; value is not used. That is, something like
1197 ;; (progn (list (something-with-side-effects) (yow))
1199 ;; may safely be turned into
1200 ;; (progn (progn (something-with-side-effects) (yow))
1202 ;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo.
1204 ;; Some of these functions have the side effect of allocating memory
1205 ;; and it would be incorrect to replace two calls with one.
1206 ;; But we don't try to do those kinds of optimizations,
1207 ;; so it is safe to list such functions here.
1208 ;; Some of these functions return values that depend on environment
1209 ;; state, so that constant folding them would be wrong,
1210 ;; but we don't do constant folding based on this list.
1212 ;; However, at present the only optimization we normally do
1213 ;; is delete calls that need not occur, and we only do that
1214 ;; with the error-free functions.
1216 ;; I wonder if I missed any :-\)
1217 (let ((side-effect-free-fns
1218 '(% * + - / /= 1+ 1- < <= = > >= abs acos append aref ash asin atan
1220 boundp buffer-file-name buffer-local-variables buffer-modified-p
1221 buffer-substring byte-code-function-p
1222 capitalize car-less-than-car car cdr ceiling char-after char-before
1223 char-equal char-to-string char-width
1224 compare-strings concat coordinates-in-window-p
1225 copy-alist copy-sequence copy-marker cos count-lines
1227 decode-time default-boundp default-value documentation downcase
1228 elt encode-char exp expt encode-time error-message-string
1229 fboundp fceiling featurep ffloor
1230 file-directory-p file-exists-p file-locked-p file-name-absolute-p
1231 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p
1232 float float-time floor format format-time-string frame-visible-p
1234 get gethash get-buffer get-buffer-window getenv get-file-buffer
1236 int-to-string intern-soft
1238 length local-variable-if-set-p local-variable-p log log10 logand
1239 logb logior lognot logxor lsh langinfo
1240 make-list make-string make-symbol
1241 marker-buffer max member memq min mod multibyte-char-to-unibyte
1242 next-window nth nthcdr number-to-string
1243 parse-colon-path plist-get plist-member
1244 prefix-numeric-value previous-window prin1-to-string propertize
1246 radians-to-degrees rassq rassoc read-from-string regexp-quote
1247 region-beginning region-end reverse round
1248 sin sqrt string string< string= string-equal string-lessp string-to-char
1249 string-to-int string-to-number substring sxhash symbol-function
1250 symbol-name symbol-plist symbol-value string-make-unibyte
1251 string-make-multibyte string-as-multibyte string-as-unibyte
1254 unibyte-char-to-multibyte upcase user-full-name
1255 user-login-name user-original-login-name user-variable-p
1257 window-buffer window-dedicated-p window-edges window-height
1258 window-hscroll window-minibuffer-p window-width
1260 (side-effect-and-error-free-fns
1262 bobp bolp bool-vector-p
1263 buffer-end buffer-list buffer-size buffer-string bufferp
1264 car-safe case-table-p cdr-safe char-or-string-p characterp
1265 charsetp commandp cons consp
1266 current-buffer current-global-map current-indentation
1267 current-local-map current-minor-mode-maps current-time
1268 current-time-string current-time-zone
1269 eobp eolp eq equal eventp
1270 floatp following-char framep
1271 get-largest-window get-lru-window
1273 identity ignore integerp integer-or-marker-p interactive-p
1274 invocation-directory invocation-name
1276 line-beginning-position line-end-position list listp
1277 make-marker mark mark-marker markerp max-char
1278 memory-limit minibuffer-window
1280 natnump nlistp not null number-or-marker-p numberp
1281 one-window-p overlayp
1282 point point-marker point-min point-max preceding-char primary-charset
1284 recent-keys recursion-depth
1285 safe-length selected-frame selected-window sequencep
1286 standard-case-table standard-syntax-table stringp subrp symbolp
1287 syntax-table syntax-table-p
1288 this-command-keys this-command-keys-vector this-single-command-keys
1289 this-single-command-raw-keys
1290 user-real-login-name user-real-uid user-uid
1291 vector vectorp visible-frame-list
1292 wholenump window-configuration-p window-live-p windowp)))
1293 (while side-effect-free-fns
1294 (put (car side-effect-free-fns) 'side-effect-free t)
1295 (setq side-effect-free-fns (cdr side-effect-free-fns)))
1296 (while side-effect-and-error-free-fns
1297 (put (car side-effect-and-error-free-fns) 'side-effect-free 'error-free)
1298 (setq side-effect-and-error-free-fns (cdr side-effect-and-error-free-fns)))
1302 ;; pure functions are side-effect free functions whose values depend
1303 ;; only on their arguments. For these functions, calls with constant
1304 ;; arguments can be evaluated at compile time. This may shift run time
1305 ;; errors to compile time.
1308 '(concat symbol-name regexp-opt regexp-quote string-to-syntax)))
1310 (put (car pure-fns) 'pure t)
1311 (setq pure-fns (cdr pure-fns)))
1314 (defun byte-compile-splice-in-already-compiled-code (form)
1315 ;; form is (byte-code "..." [...] n)
1316 (if (not (memq byte-optimize '(t lap)))
1317 (byte-compile-normal-call form)
1318 (byte-inline-lapcode
1319 (byte-decompile-bytecode-1 (nth 1 form) (nth 2 form) t))))
1321 (put 'byte-code 'byte-compile 'byte-compile-splice-in-already-compiled-code)
1324 (defconst byte-constref-ops
1325 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind))
1327 ;; This function extracts the bitfields from variable-length opcodes.
1328 ;; Originally defined in disass.el (which no longer uses it.)
1330 (defun disassemble-offset ()
1332 ;; fetch and return the offset for the current opcode.
1333 ;; return nil if this opcode has no offset
1334 ;; Used and set dynamically in byte-decompile-bytecode-1.
1335 (defvar bytedecomp-op)
1336 (defvar bytedecomp-ptr)
1337 (defvar bytedecomp-bytes)
1338 (cond ((< bytedecomp-op byte-nth)
1339 (let ((tem (logand bytedecomp-op 7)))
1340 (setq bytedecomp-op (logand bytedecomp-op 248))
1342 ;; Offset in next byte.
1343 (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1344 (aref bytedecomp-bytes bytedecomp-ptr))
1346 ;; Offset in next 2 bytes.
1347 (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1348 (+ (aref bytedecomp-bytes bytedecomp-ptr)
1349 (progn (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1350 (lsh (aref bytedecomp-bytes bytedecomp-ptr) 8))))
1351 (t tem)))) ;offset was in opcode
1352 ((>= bytedecomp-op byte-constant)
1353 (prog1 (- bytedecomp-op byte-constant) ;offset in opcode
1354 (setq bytedecomp-op byte-constant)))
1355 ((or (and (>= bytedecomp-op byte-constant2)
1356 (<= bytedecomp-op byte-goto-if-not-nil-else-pop))
1357 (= bytedecomp-op byte-stack-set2))
1358 ;; Offset in next 2 bytes.
1359 (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1360 (+ (aref bytedecomp-bytes bytedecomp-ptr)
1361 (progn (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1362 (lsh (aref bytedecomp-bytes bytedecomp-ptr) 8))))
1363 ((and (>= bytedecomp-op byte-listN)
1364 (<= bytedecomp-op byte-discardN))
1365 (setq bytedecomp-ptr (1+ bytedecomp-ptr)) ;offset in next byte
1366 (aref bytedecomp-bytes bytedecomp-ptr))))
1369 ;; This de-compiler is used for inline expansion of compiled functions,
1370 ;; and by the disassembler.
1372 ;; This list contains numbers, which are pc values,
1373 ;; before each instruction.
1374 (defun byte-decompile-bytecode (bytes constvec)
1375 "Turn BYTECODE into lapcode, referring to CONSTVEC."
1376 (let ((byte-compile-constants nil)
1377 (byte-compile-variables nil)
1378 (byte-compile-tag-number 0))
1379 (byte-decompile-bytecode-1 bytes constvec)))
1381 ;; As byte-decompile-bytecode, but updates
1382 ;; byte-compile-{constants, variables, tag-number}.
1383 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced
1384 ;; with `goto's destined for the end of the code.
1385 ;; That is for use by the compiler.
1386 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler.
1387 ;; In that case, we put a pc value into the list
1388 ;; before each insn (or its label).
1389 (defun byte-decompile-bytecode-1 (bytedecomp-bytes constvec
1390 &optional make-spliceable)
1391 (let ((length (length bytedecomp-bytes))
1392 (bytedecomp-ptr 0) optr tags bytedecomp-op offset
1395 (while (not (= bytedecomp-ptr length))
1397 (setq lap (cons bytedecomp-ptr lap)))
1398 (setq bytedecomp-op (aref bytedecomp-bytes bytedecomp-ptr)
1400 offset (disassemble-offset)) ; this does dynamic-scope magic
1401 (setq bytedecomp-op (aref byte-code-vector bytedecomp-op))
1402 (cond ((memq bytedecomp-op byte-goto-ops)
1405 (cdr (or (assq offset tags)
1408 (byte-compile-make-tag))
1410 ((cond ((eq bytedecomp-op 'byte-constant2)
1411 (setq bytedecomp-op 'byte-constant) t)
1412 ((memq bytedecomp-op byte-constref-ops)))
1413 (setq tmp (if (>= offset (length constvec))
1414 (list 'out-of-range offset)
1415 (aref constvec offset))
1416 offset (if (eq bytedecomp-op 'byte-constant)
1417 (byte-compile-get-constant tmp)
1418 (or (assq tmp byte-compile-variables)
1419 (car (setq byte-compile-variables
1421 byte-compile-variables)))))))
1422 ((and make-spliceable
1423 (eq bytedecomp-op 'byte-return))
1424 (if (= bytedecomp-ptr (1- length))
1425 (setq bytedecomp-op nil)
1426 (setq offset (or endtag (setq endtag (byte-compile-make-tag)))
1427 bytedecomp-op 'byte-goto)))
1428 ((eq bytedecomp-op 'byte-stack-set2)
1429 (setq bytedecomp-op 'byte-stack-set))
1430 ((and (eq bytedecomp-op 'byte-discardN) (>= offset #x80))
1431 ;; The top bit of the operand for byte-discardN is a flag,
1432 ;; saying whether the top-of-stack is preserved. In
1433 ;; lapcode, we represent this by using a different opcode
1434 ;; (with the flag removed from the operand).
1435 (setq bytedecomp-op 'byte-discardN-preserve-tos)
1436 (setq offset (- offset #x80))))
1437 ;; lap = ( [ (pc . (op . arg)) ]* )
1438 (setq lap (cons (cons optr (cons bytedecomp-op (or offset 0)))
1440 (setq bytedecomp-ptr (1+ bytedecomp-ptr)))
1441 ;; take off the dummy nil op that we replaced a trailing "return" with.
1444 (cond ((numberp (car rest)))
1445 ((setq tmp (assq (car (car rest)) tags))
1446 ;; this addr is jumped to
1447 (setcdr rest (cons (cons nil (cdr tmp))
1449 (setq tags (delq tmp tags))
1450 (setq rest (cdr rest))))
1451 (setq rest (cdr rest))))
1452 (if tags (error "optimizer error: missed tags %s" tags))
1453 (if (null (car (cdr (car lap))))
1454 (setq lap (cdr lap)))
1456 (setq lap (cons (cons nil endtag) lap)))
1457 ;; remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* )
1458 (mapcar (function (lambda (elt)
1465 ;;; peephole optimizer
1467 (defconst byte-tagref-ops (cons 'TAG byte-goto-ops))
1469 (defconst byte-conditional-ops
1470 '(byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop
1471 byte-goto-if-not-nil-else-pop))
1473 (defconst byte-after-unbind-ops
1474 '(byte-constant byte-dup
1475 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-integerp
1477 byte-cons byte-list1 byte-list2 ; byte-list3 byte-list4
1479 ;; How about other side-effect-free-ops? Is it safe to move an
1480 ;; error invocation (such as from nth) out of an unwind-protect?
1481 ;; No, it is not, because the unwind-protect forms can alter
1482 ;; the inside of the object to which nth would apply.
1483 ;; For the same reason, byte-equal was deleted from this list.
1484 "Byte-codes that can be moved past an unbind.")
1486 (defconst byte-compile-side-effect-and-error-free-ops
1487 '(byte-constant byte-dup byte-symbolp byte-consp byte-stringp byte-listp
1488 byte-integerp byte-numberp byte-eq byte-equal byte-not byte-car-safe
1489 byte-cdr-safe byte-cons byte-list1 byte-list2 byte-point byte-point-max
1490 byte-point-min byte-following-char byte-preceding-char
1491 byte-current-column byte-eolp byte-eobp byte-bolp byte-bobp
1492 byte-current-buffer byte-interactive-p byte-stack-ref))
1494 (defconst byte-compile-side-effect-free-ops
1496 '(byte-varref byte-nth byte-memq byte-car byte-cdr byte-length byte-aref
1497 byte-symbol-value byte-get byte-concat2 byte-concat3 byte-sub1 byte-add1
1498 byte-eqlsign byte-gtr byte-lss byte-leq byte-geq byte-diff byte-negate
1499 byte-plus byte-max byte-min byte-mult byte-char-after byte-char-syntax
1500 byte-buffer-substring byte-string= byte-string< byte-nthcdr byte-elt
1501 byte-member byte-assq byte-quo byte-rem)
1502 byte-compile-side-effect-and-error-free-ops))
1504 ;; This crock is because of the way DEFVAR_BOOL variables work.
1505 ;; Consider the code
1507 ;; (defun foo (flag)
1508 ;; (let ((old-pop-ups pop-up-windows)
1509 ;; (pop-up-windows flag))
1510 ;; (cond ((not (eq pop-up-windows old-pop-ups))
1511 ;; (setq old-pop-ups pop-up-windows)
1514 ;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is
1515 ;; something else. But if we optimize
1518 ;; varbind pop-up-windows
1519 ;; varref pop-up-windows
1524 ;; varbind pop-up-windows
1527 ;; we break the program, because it will appear that pop-up-windows and
1528 ;; old-pop-ups are not EQ when really they are. So we have to know what
1529 ;; the BOOL variables are, and not perform this optimization on them.
1531 ;; The variable `byte-boolean-vars' is now primitive and updated
1532 ;; automatically by DEFVAR_BOOL.
1534 (defmacro byte-opt-update-stack-params (stack-adjust stack-depth lap0 rest lap)
1535 "...macro used by byte-optimize-lapcode..."
1537 (byte-compile-log-lap "Before %s [depth = %s]" ,lap0 ,stack-depth)
1538 (cond ((eq (car ,lap0) 'TAG)
1539 ;; A tag can encode the expected stack depth.
1541 ;; First, check to see if our notion of the current stack
1542 ;; depth agrees with this tag. We don't check at the
1543 ;; beginning of the function, because the presence of
1544 ;; lexical arguments means the first tag will have a
1546 (when (and (not (eq ,rest ,lap)) ; not at first insn
1547 ,stack-depth ; not just after a goto
1548 (not (= (cddr ,lap0) ,stack-depth)))
1549 (error "Compiler error: optimizer is confused about %s:
1550 %s != %s at lapcode %s" ',stack-depth (cddr ,lap0) ,stack-depth ,lap0))
1551 ;; Now set out current depth from this tag
1552 (setq ,stack-depth (cddr ,lap0)))
1553 (setq ,stack-adjust 0))
1554 ((memq (car ,lap0) '(byte-goto byte-return))
1555 ;; These insns leave us in an unknown state
1556 (setq ,stack-adjust nil))
1558 ;; Not a no-op, set ,stack-adjust for lap0. ,stack-adjust will
1559 ;; be added to ,stack-depth at the end of the loop, so any code
1560 ;; that modifies the instruction sequence must adjust this too.
1562 (byte-compile-stack-adjustment (car ,lap0) (cdr ,lap0)))))
1563 (byte-compile-log-lap "Before %s [depth => %s, adj = %s]" ,lap0 ,stack-depth ,stack-adjust)
1566 (defun byte-optimize-lapcode (lap &optional for-effect)
1567 "Simple peephole optimizer. LAP is both modified and returned.
1568 If FOR-EFFECT is non-nil, the return value is assumed to be of no importance."
1574 (initial-stack-depth
1575 (if (and lap (eq (car (car lap)) 'TAG))
1576 (cdr (cdr (car lap)))
1578 (keep-going 'first-time)
1581 (side-effect-free (if byte-compile-delete-errors
1582 byte-compile-side-effect-free-ops
1583 byte-compile-side-effect-and-error-free-ops)))
1585 (or (eq keep-going 'first-time)
1586 (byte-compile-log-lap " ---- next pass"))
1588 stack-depth initial-stack-depth
1591 (setq lap0 (car rest)
1595 (byte-opt-update-stack-params stack-adjust stack-depth lap0 rest lap)
1597 ;; You may notice that sequences like "dup varset discard" are
1598 ;; optimized but sequences like "dup varset TAG1: discard" are not.
1599 ;; You may be tempted to change this; resist that temptation.
1601 ;; <side-effect-free> pop --> <deleted>
1603 ;; const-X pop --> <deleted>
1604 ;; varref-X pop --> <deleted>
1605 ;; dup pop --> <deleted>
1607 ((and (eq 'byte-discard (car lap1))
1608 (memq (car lap0) side-effect-free))
1610 (setq rest (cdr rest))
1611 (cond ((= stack-adjust 1)
1612 (byte-compile-log-lap
1613 " %s discard\t-->\t<deleted>" lap0)
1614 (setq lap (delq lap0 (delq lap1 lap))))
1616 (byte-compile-log-lap
1617 " %s discard\t-->\t<deleted> discard" lap0)
1618 (setq lap (delq lap0 lap)))
1619 ((= stack-adjust -1)
1620 (byte-compile-log-lap
1621 " %s discard\t-->\tdiscard discard" lap0)
1622 (setcar lap0 'byte-discard)
1624 ((error "Optimizer error: too much on the stack")))
1625 (setq stack-adjust (1- stack-adjust)))
1627 ;; goto*-X X: --> X:
1629 ((and (memq (car lap0) byte-goto-ops)
1630 (eq (cdr lap0) lap1))
1631 (cond ((eq (car lap0) 'byte-goto)
1632 (setq lap (delq lap0 lap))
1633 (setq tmp "<deleted>"))
1634 ((memq (car lap0) byte-goto-always-pop-ops)
1635 (setcar lap0 (setq tmp 'byte-discard))
1637 ((error "Depth conflict at tag %d" (nth 2 lap0))))
1638 (and (memq byte-optimize-log '(t byte))
1639 (byte-compile-log " (goto %s) %s:\t-->\t%s %s:"
1640 (nth 1 lap1) (nth 1 lap1)
1642 (setq keep-going t))
1644 ;; varset-X varref-X --> dup varset-X
1645 ;; varbind-X varref-X --> dup varbind-X
1646 ;; const/dup varset-X varref-X --> const/dup varset-X const/dup
1647 ;; const/dup varbind-X varref-X --> const/dup varbind-X const/dup
1648 ;; The latter two can enable other optimizations.
1650 ((or (and (eq 'byte-varref (car lap2))
1651 (eq (cdr lap1) (cdr lap2))
1652 (memq (car lap1) '(byte-varset byte-varbind)))
1653 (and (eq (car lap2) 'byte-stack-ref)
1654 (eq (car lap1) 'byte-stack-set)
1655 (eq (cdr lap1) (cdr lap2))))
1656 (if (and (eq 'byte-varref (car lap2))
1657 (setq tmp (memq (car (cdr lap2)) byte-boolean-vars))
1658 (not (eq (car lap0) 'byte-constant)))
1661 (if (memq (car lap0) '(byte-constant byte-dup))
1663 (setq tmp (if (or (not tmp)
1664 (byte-compile-const-symbol-p
1667 (byte-compile-get-constant t)))
1668 (byte-compile-log-lap " %s %s %s\t-->\t%s %s %s"
1669 lap0 lap1 lap2 lap0 lap1
1670 (cons (car lap0) tmp))
1671 (setcar lap2 (car lap0))
1673 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1)
1674 (setcar lap2 (car lap1))
1675 (setcar lap1 'byte-dup)
1677 ;; The stack depth gets locally increased, so we will
1678 ;; increase maxdepth in case depth = maxdepth here.
1679 ;; This can cause the third argument to byte-code to
1680 ;; be larger than necessary.
1681 (setq add-depth 1))))
1683 ;; dup varset-X discard --> varset-X
1684 ;; dup varbind-X discard --> varbind-X
1685 ;; (the varbind variant can emerge from other optimizations)
1687 ((and (eq 'byte-dup (car lap0))
1688 (eq 'byte-discard (car lap2))
1689 (memq (car lap1) '(byte-varset byte-varbind byte-stack-set)))
1690 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1)
1694 (setq lap (delq lap0 (delq lap2 lap))))
1696 ;; not goto-X-if-nil --> goto-X-if-non-nil
1697 ;; not goto-X-if-non-nil --> goto-X-if-nil
1699 ;; it is wrong to do the same thing for the -else-pop variants.
1701 ((and (eq 'byte-not (car lap0))
1702 (or (eq 'byte-goto-if-nil (car lap1))
1703 (eq 'byte-goto-if-not-nil (car lap1))))
1704 (byte-compile-log-lap " not %s\t-->\t%s"
1707 (if (eq (car lap1) 'byte-goto-if-nil)
1708 'byte-goto-if-not-nil
1711 (setcar lap1 (if (eq (car lap1) 'byte-goto-if-nil)
1712 'byte-goto-if-not-nil
1714 (setq lap (delq lap0 lap))
1718 ;; goto-X-if-nil goto-Y X: --> goto-Y-if-non-nil X:
1719 ;; goto-X-if-non-nil goto-Y X: --> goto-Y-if-nil X:
1721 ;; it is wrong to do the same thing for the -else-pop variants.
1723 ((and (or (eq 'byte-goto-if-nil (car lap0))
1724 (eq 'byte-goto-if-not-nil (car lap0))) ; gotoX
1725 (eq 'byte-goto (car lap1)) ; gotoY
1726 (eq (cdr lap0) lap2)) ; TAG X
1727 (let ((inverse (if (eq 'byte-goto-if-nil (car lap0))
1728 'byte-goto-if-not-nil 'byte-goto-if-nil)))
1729 (byte-compile-log-lap " %s %s %s:\t-->\t%s %s:"
1731 (cons inverse (cdr lap1)) lap2)
1732 (setq lap (delq lap0 lap)
1734 (setcar lap1 inverse)
1735 (setq keep-going t)))
1737 ;; const goto-if-* --> whatever
1739 ((and (eq 'byte-constant (car lap0))
1740 (memq (car lap1) byte-conditional-ops))
1741 (cond ((if (or (eq (car lap1) 'byte-goto-if-nil)
1742 (eq (car lap1) 'byte-goto-if-nil-else-pop))
1744 (not (car (cdr lap0))))
1745 (byte-compile-log-lap " %s %s\t-->\t<deleted>"
1747 (setq rest (cdr rest)
1748 lap (delq lap0 (delq lap1 lap))))
1750 (byte-compile-log-lap " %s %s\t-->\t%s"
1752 (cons 'byte-goto (cdr lap1)))
1753 (when (memq (car lap1) byte-goto-always-pop-ops)
1754 (setq lap (delq lap0 lap)))
1755 (setcar lap1 'byte-goto)))
1759 ;; varref-X varref-X --> varref-X dup
1760 ;; varref-X [dup ...] varref-X --> varref-X [dup ...] dup
1761 ;; We don't optimize the const-X variations on this here,
1762 ;; because that would inhibit some goto optimizations; we
1763 ;; optimize the const-X case after all other optimizations.
1765 ((and (memq (car lap0) '(byte-varref byte-stack-ref))
1767 (setq tmp (cdr rest) tmp2 0)
1768 (while (eq (car (car tmp)) 'byte-dup)
1769 (setq tmp (cdr tmp) tmp2 (1+ tmp2)))
1771 (eq (car lap0) (car (car tmp)))
1772 (eq (cdr lap0) (cdr (car tmp))))
1773 (if (memq byte-optimize-log '(t byte))
1775 (setq tmp2 (cdr rest))
1776 (while (not (eq tmp tmp2))
1777 (setq tmp2 (cdr tmp2)
1778 str (concat str " dup")))
1779 (byte-compile-log-lap " %s%s %s\t-->\t%s%s dup"
1780 lap0 str lap0 lap0 str)))
1782 (setcar (car tmp) 'byte-dup)
1783 (setcdr (car tmp) 0)
1785 stack-adjust (+ 2 tmp2)))
1787 ;; TAG1: TAG2: --> TAG1: <deleted>
1788 ;; (and other references to TAG2 are replaced with TAG1)
1790 ((and (eq (car lap0) 'TAG)
1791 (eq (car lap1) 'TAG))
1792 (and (memq byte-optimize-log '(t byte))
1793 (byte-compile-log " adjacent tags %d and %d merged"
1794 (nth 1 lap1) (nth 1 lap0)))
1796 (while (setq tmp2 (rassq lap0 tmp3))
1798 (setq tmp3 (cdr (memq tmp2 tmp3))))
1799 (setq lap (delq lap0 lap)
1802 ;; unused-TAG: --> <deleted>
1804 ((and (eq 'TAG (car lap0))
1805 (not (rassq lap0 lap)))
1806 (and (memq byte-optimize-log '(t byte))
1807 (byte-compile-log " unused tag %d removed" (nth 1 lap0)))
1808 (setq lap (delq lap0 lap)
1811 ;; goto ... --> goto <delete until TAG or end>
1812 ;; return ... --> return <delete until TAG or end>
1814 ((and (memq (car lap0) '(byte-goto byte-return))
1815 (not (memq (car lap1) '(TAG nil))))
1818 (opt-p (memq byte-optimize-log '(t lap)))
1820 (while (and (setq tmp (cdr tmp))
1821 (not (eq 'TAG (car (car tmp)))))
1822 (if opt-p (setq deleted (cons (car tmp) deleted)
1823 str (concat str " %s")
1827 (if (eq 'TAG (car (car tmp)))
1828 (format "%d:" (car (cdr (car tmp))))
1829 (or (car tmp) ""))))
1831 (apply 'byte-compile-log-lap-1
1833 " %s\t-->\t%s <deleted> %s")
1835 (nconc (nreverse deleted)
1836 (list tagstr lap0 tagstr)))
1837 (byte-compile-log-lap
1838 " %s <%d unreachable op%s> %s\t-->\t%s <deleted> %s"
1839 lap0 i (if (= i 1) "" "s")
1840 tagstr lap0 tagstr))))
1842 (setq keep-going t))
1844 ;; <safe-op> unbind --> unbind <safe-op>
1845 ;; (this may enable other optimizations.)
1847 ((and (eq 'byte-unbind (car lap1))
1848 (memq (car lap0) byte-after-unbind-ops))
1849 (byte-compile-log-lap " %s %s\t-->\t%s %s" lap0 lap1 lap1 lap0)
1851 (setcar (cdr rest) lap0)
1855 ;; varbind-X unbind-N --> discard unbind-(N-1)
1856 ;; save-excursion unbind-N --> unbind-(N-1)
1857 ;; save-restriction unbind-N --> unbind-(N-1)
1859 ((and (eq 'byte-unbind (car lap1))
1860 (memq (car lap0) '(byte-varbind byte-save-excursion
1861 byte-save-restriction))
1863 (if (zerop (setcdr lap1 (1- (cdr lap1))))
1865 (if (eq (car lap0) 'byte-varbind)
1866 (setcar rest (cons 'byte-discard 0))
1867 (setq lap (delq lap0 lap)))
1868 (byte-compile-log-lap " %s %s\t-->\t%s %s"
1869 lap0 (cons (car lap1) (1+ (cdr lap1)))
1870 (if (eq (car lap0) 'byte-varbind)
1873 (if (and (/= 0 (cdr lap1))
1874 (eq (car lap0) 'byte-varbind))
1877 (setq keep-going t))
1879 ;; stack-ref-N --> dup ; where N is TOS
1881 ((and stack-depth (eq (car lap0) 'byte-stack-ref)
1882 (= (cdr lap0) (1- stack-depth)))
1883 (setcar lap0 'byte-dup)
1885 (setq keep-going t))
1887 ;; goto*-X ... X: goto-Y --> goto*-Y
1888 ;; goto-X ... X: return --> return
1890 ((and (memq (car lap0) byte-goto-ops)
1891 (memq (car (setq tmp (nth 1 (memq (cdr lap0) lap))))
1892 '(byte-goto byte-return)))
1893 (cond ((and (not (eq tmp lap0))
1894 (or (eq (car lap0) 'byte-goto)
1895 (eq (car tmp) 'byte-goto)))
1896 (byte-compile-log-lap " %s [%s]\t-->\t%s"
1898 (if (eq (car tmp) 'byte-return)
1899 (setcar lap0 'byte-return))
1900 (setcdr lap0 (cdr tmp))
1901 (setq keep-going t))))
1903 ;; goto-*-else-pop X ... X: goto-if-* --> whatever
1904 ;; goto-*-else-pop X ... X: discard --> whatever
1906 ((and (memq (car lap0) '(byte-goto-if-nil-else-pop
1907 byte-goto-if-not-nil-else-pop))
1908 (memq (car (car (setq tmp (cdr (memq (cdr lap0) lap)))))
1910 (cons 'byte-discard byte-conditional-ops)))
1911 (not (eq lap0 (car tmp))))
1912 (setq tmp2 (car tmp))
1913 (setq tmp3 (assq (car lap0) '((byte-goto-if-nil-else-pop
1915 (byte-goto-if-not-nil-else-pop
1916 byte-goto-if-not-nil))))
1917 (if (memq (car tmp2) tmp3)
1918 (progn (setcar lap0 (car tmp2))
1919 (setcdr lap0 (cdr tmp2))
1920 (byte-compile-log-lap " %s-else-pop [%s]\t-->\t%s"
1921 (car lap0) tmp2 lap0))
1922 ;; Get rid of the -else-pop's and jump one step further.
1923 (or (eq 'TAG (car (nth 1 tmp)))
1924 (setcdr tmp (cons (byte-compile-make-tag)
1926 (byte-compile-log-lap " %s [%s]\t-->\t%s <skip>"
1927 (car lap0) tmp2 (nth 1 tmp3))
1928 (setcar lap0 (nth 1 tmp3))
1929 (setcdr lap0 (nth 1 tmp)))
1930 (setq keep-going t))
1932 ;; const goto-X ... X: goto-if-* --> whatever
1933 ;; const goto-X ... X: discard --> whatever
1935 ((and (eq (car lap0) 'byte-constant)
1936 (eq (car lap1) 'byte-goto)
1937 (memq (car (car (setq tmp (cdr (memq (cdr lap1) lap)))))
1939 (cons 'byte-discard byte-conditional-ops)))
1940 (not (eq lap1 (car tmp))))
1941 (setq tmp2 (car tmp))
1942 (cond ((memq (car tmp2)
1943 (if (null (car (cdr lap0)))
1944 '(byte-goto-if-nil byte-goto-if-nil-else-pop)
1945 '(byte-goto-if-not-nil
1946 byte-goto-if-not-nil-else-pop)))
1947 (byte-compile-log-lap " %s goto [%s]\t-->\t%s %s"
1948 lap0 tmp2 lap0 tmp2)
1949 (setcar lap1 (car tmp2))
1950 (setcdr lap1 (cdr tmp2))
1951 ;; Let next step fix the (const,goto-if*) sequence.
1952 (setq rest (cons nil rest)))
1954 ;; Jump one step further
1955 (byte-compile-log-lap
1956 " %s goto [%s]\t-->\t<deleted> goto <skip>"
1958 (or (eq 'TAG (car (nth 1 tmp)))
1959 (setcdr tmp (cons (byte-compile-make-tag)
1961 (setcdr lap1 (car (cdr tmp)))
1962 (setq lap (delq lap0 lap))))
1966 ;; X: varref-Y ... varset-Y goto-X -->
1967 ;; X: varref-Y Z: ... dup varset-Y goto-Z
1968 ;; (varset-X goto-BACK, BACK: varref-X --> copy the varref down.)
1969 ;; (This is so usual for while loops that it is worth handling).
1971 ((and (memq (car lap1) '(byte-varset byte-stack-set))
1972 (eq (car lap2) 'byte-goto)
1973 (not (memq (cdr lap2) rest)) ;Backwards jump
1974 (eq (car (car (setq tmp (cdr (memq (cdr lap2) lap)))))
1975 (if (eq (car lap1) 'byte-varset) 'byte-varref 'byte-stack-ref))
1976 (eq (cdr (car tmp)) (cdr lap1))
1977 (not (and (eq (car lap1) 'byte-varref)
1978 (memq (car (cdr lap1)) byte-boolean-vars))))
1979 ;;(byte-compile-log-lap " Pulled %s to end of loop" (car tmp))
1980 (let ((newtag (byte-compile-make-tag)))
1981 (byte-compile-log-lap
1982 " %s: %s ... %s %s\t-->\t%s: %s %s: ... %s %s %s"
1983 (nth 1 (cdr lap2)) (car tmp)
1985 (nth 1 (cdr lap2)) (car tmp)
1986 (nth 1 newtag) 'byte-dup lap1
1987 (cons 'byte-goto newtag)
1989 (setcdr rest (cons (cons 'byte-dup 0) (cdr rest)))
1990 (setcdr tmp (cons (setcdr lap2 newtag) (cdr tmp))))
1992 (setq keep-going t))
1994 ;; goto-X Y: ... X: goto-if*-Y --> goto-if-not-*-X+1 Y:
1995 ;; (This can pull the loop test to the end of the loop)
1997 ((and (eq (car lap0) 'byte-goto)
1998 (eq (car lap1) 'TAG)
2000 (cdr (car (setq tmp (cdr (memq (cdr lap0) lap))))))
2001 (memq (car (car tmp))
2002 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
2003 byte-goto-if-nil-else-pop)))
2004 ;; (byte-compile-log-lap " %s %s, %s %s --> moved conditional"
2005 ;; lap0 lap1 (cdr lap0) (car tmp))
2006 (let ((newtag (byte-compile-make-tag)))
2007 (byte-compile-log-lap
2008 "%s %s: ... %s: %s\t-->\t%s ... %s:"
2009 lap0 (nth 1 lap1) (nth 1 (cdr lap0)) (car tmp)
2010 (cons (cdr (assq (car (car tmp))
2011 '((byte-goto-if-nil . byte-goto-if-not-nil)
2012 (byte-goto-if-not-nil . byte-goto-if-nil)
2013 (byte-goto-if-nil-else-pop .
2014 byte-goto-if-not-nil-else-pop)
2015 (byte-goto-if-not-nil-else-pop .
2016 byte-goto-if-nil-else-pop))))
2021 (setcdr tmp (cons (setcdr lap0 newtag) (cdr tmp)))
2022 (if (eq (car (car tmp)) 'byte-goto-if-nil-else-pop)
2023 ;; We can handle this case but not the -if-not-nil case,
2024 ;; because we won't know which non-nil constant to push.
2025 (setcdr rest (cons (cons 'byte-constant
2026 (byte-compile-get-constant nil))
2028 (setcar lap0 (nth 1 (memq (car (car tmp))
2029 '(byte-goto-if-nil-else-pop
2030 byte-goto-if-not-nil
2032 byte-goto-if-not-nil
2033 byte-goto byte-goto))))
2036 stack-adjust (and (not (eq (car lap0) 'byte-goto)) -1)))
2040 (and stack-depth stack-adjust (+ stack-depth stack-adjust)))
2041 (setq rest (cdr rest)))
2045 ;; Rebuild byte-compile-constants / byte-compile-variables.
2046 ;; Simple optimizations that would inhibit other optimizations if they
2047 ;; were done in the optimizing loop, and optimizations which there is no
2048 ;; need to do more than once.
2049 (setq byte-compile-constants nil
2050 byte-compile-variables nil)
2052 stack-depth initial-stack-depth)
2053 (byte-compile-log-lap " ---- final pass")
2055 (setq lap0 (car rest)
2057 (byte-opt-update-stack-params stack-adjust stack-depth lap0 rest lap)
2058 (if (memq (car lap0) byte-constref-ops)
2059 (if (or (eq (car lap0) 'byte-constant)
2060 (eq (car lap0) 'byte-constant2))
2061 (unless (memq (cdr lap0) byte-compile-constants)
2062 (setq byte-compile-constants (cons (cdr lap0)
2063 byte-compile-constants)))
2064 (unless (memq (cdr lap0) byte-compile-variables)
2065 (setq byte-compile-variables (cons (cdr lap0)
2066 byte-compile-variables)))))
2068 ;; const-C varset-X const-C --> const-C dup varset-X
2069 ;; const-C varbind-X const-C --> const-C dup varbind-X
2071 (and (eq (car lap0) 'byte-constant)
2072 (eq (car (nth 2 rest)) 'byte-constant)
2073 (eq (cdr lap0) (cdr (nth 2 rest)))
2074 (memq (car lap1) '(byte-varbind byte-varset)))
2075 (byte-compile-log-lap " %s %s %s\t-->\t%s dup %s"
2076 lap0 lap1 lap0 lap0 lap1)
2077 (setcar (cdr (cdr rest)) (cons (car lap1) (cdr lap1)))
2078 (setcar (cdr rest) (cons 'byte-dup 0))
2081 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup
2082 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup
2084 ((memq (car lap0) '(byte-constant byte-varref))
2088 (while (eq 'byte-dup (car (car (setq tmp (cdr tmp))))))
2089 (and (eq (cdr lap0) (cdr (car tmp)))
2090 (eq (car lap0) (car (car tmp)))))
2091 (setcar tmp (cons 'byte-dup 0))
2094 (byte-compile-log-lap
2095 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0)))
2097 ;; unbind-N unbind-M --> unbind-(N+M)
2099 ((and (eq 'byte-unbind (car lap0))
2100 (eq 'byte-unbind (car lap1)))
2101 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1
2103 (+ (cdr lap0) (cdr lap1))))
2104 (setq lap (delq lap0 lap))
2105 (setcdr lap1 (+ (cdr lap1) (cdr lap0))))
2108 ;; stack-set-M [discard/discardN ...] --> discardN-preserve-tos
2109 ;; stack-set-M [discard/discardN ...] --> discardN
2111 ((and stack-depth ;Make sure we know the stack depth.
2112 (eq (car lap0) 'byte-stack-set)
2113 (memq (car lap1) '(byte-discard byte-discardN))
2115 ;; See if enough discard operations follow to expose or
2116 ;; destroy the value stored by the stack-set.
2117 (setq tmp (cdr rest))
2118 (setq tmp2 (- stack-depth 2 (cdr lap0)))
2120 (while (memq (car (car tmp)) '(byte-discard byte-discardN))
2121 (if (eq (car (car tmp)) 'byte-discard)
2122 (setq tmp3 (1+ tmp3))
2123 (setq tmp3 (+ tmp3 (cdr (car tmp)))))
2124 (setq tmp (cdr tmp)))
2126 ;; Do the optimization
2127 (setq lap (delq lap0 lap))
2128 (cond ((= tmp2 tmp3)
2129 ;; The value stored is the new TOS, so pop one more value
2130 ;; (to get rid of the old value) using the TOS-preserving
2131 ;; discard operator.
2132 (setcar lap1 'byte-discardN-preserve-tos)
2133 (setcdr lap1 (1+ tmp3)))
2135 ;; Otherwise, the value stored is lost, so just use a
2137 (setcar lap1 'byte-discardN)
2138 (setcdr lap1 tmp3)))
2139 (setcdr (cdr rest) tmp)
2140 (setq stack-adjust 0)
2141 (byte-compile-log-lap " %s [discard/discardN]...\t-->\t%s"
2145 ;; discard/discardN/discardN-preserve-tos-X discard/discardN-Y -->
2148 ((and (memq (car lap0)
2151 byte-discardN-preserve-tos))
2152 (memq (car lap1) '(byte-discard byte-discardN)))
2153 (setq lap (delq lap0 lap))
2154 (byte-compile-log-lap
2155 " %s %s\t-->\t(discardN %s)"
2157 (+ (if (eq (car lap0) 'byte-discard) 1 (cdr lap0))
2158 (if (eq (car lap1) 'byte-discard) 1 (cdr lap1))))
2159 (setcdr lap1 (+ (if (eq (car lap0) 'byte-discard) 1 (cdr lap0))
2160 (if (eq (car lap1) 'byte-discard) 1 (cdr lap1))))
2161 (setcar lap1 'byte-discardN)
2162 (setq stack-adjust 0))
2165 ;; discardN-preserve-tos-X discardN-preserve-tos-Y -->
2166 ;; discardN-preserve-tos-(X+Y)
2168 ((and (eq (car lap0) 'byte-discardN-preserve-tos)
2169 (eq (car lap1) 'byte-discardN-preserve-tos))
2170 (setq lap (delq lap0 lap))
2171 (setcdr lap1 (+ (cdr lap0) (cdr lap1)))
2172 (setq stack-adjust 0)
2173 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 (car rest)))
2176 ;; discardN-preserve-tos return --> return
2177 ;; dup return --> return
2178 ;; stack-set-N return --> return ; where N is TOS-1
2180 ((and stack-depth ;Make sure we know the stack depth.
2181 (eq (car lap1) 'byte-return)
2182 (or (memq (car lap0) '(byte-discardN-preserve-tos byte-dup))
2183 (and (eq (car lap0) 'byte-stack-set)
2184 (= (cdr lap0) (- stack-depth 2)))))
2185 ;; the byte-code interpreter will pop the stack for us, so
2186 ;; we can just leave stuff on it
2187 (setq lap (delq lap0 lap))
2188 (setq stack-adjust 0)
2189 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 lap1))
2192 ;; dup stack-set-N return --> return ; where N is TOS
2194 ((and stack-depth ;Make sure we know the stack depth.
2195 (eq (car lap0) 'byte-dup)
2196 (eq (car lap1) 'byte-stack-set)
2197 (eq (car (car (cdr (cdr rest)))) 'byte-return)
2198 (= (cdr lap1) (1- stack-depth)))
2199 (setq lap (delq lap0 (delq lap1 lap)))
2200 (setq rest (cdr rest))
2201 (setq stack-adjust 0)
2202 (byte-compile-log-lap " dup %s return\t-->\treturn" lap1))
2206 (and stack-depth stack-adjust (+ stack-depth stack-adjust)))
2207 (setq rest (cdr rest)))
2209 (setq byte-compile-maxdepth (+ byte-compile-maxdepth add-depth)))
2215 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when this file compiles
2216 ;; itself, compile some of its most used recursive functions (at load time).
2219 (or (byte-code-function-p (symbol-function 'byte-optimize-form))
2220 (assq 'byte-code (symbol-function 'byte-optimize-form))
2221 (let ((byte-optimize nil)
2222 (byte-compile-warnings nil))
2224 (or noninteractive (message "compiling %s..." x))
2226 (or noninteractive (message "compiling %s...done" x)))
2227 '(byte-optimize-form
2229 byte-optimize-predicate
2230 byte-optimize-binary-predicate
2231 ;; Inserted some more than necessary, to speed it up.
2232 byte-optimize-form-code-walker
2233 byte-optimize-lapcode))))
2236 ;;; byte-opt.el ends here