1 ;;; byte-opt.el --- the optimization passes of the emacs-lisp byte compiler -*- lexical-binding: t -*-
3 ;; Copyright (C) 1991, 1994, 2000-2017 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
7 ;; Maintainer: emacs-devel@gnu.org
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 defvared
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-lib
))
190 (defun byte-compile-log-lap-1 (format &rest args
)
191 ;; Newer byte codes for stack-ref make the slot 0 non-nil again.
192 ;; But the "old disassembler" is *really* ancient by now.
193 ;; (if (aref byte-code-vector 0)
194 ;; (error "The old version of the disassembler is loaded. Reload new-bytecomp as well"))
196 (apply #'format-message format
198 (mapcar (lambda (arg)
199 (if (not (consp arg
))
200 (if (and (symbolp arg
)
201 (string-match "^byte-" (symbol-name arg
)))
202 (intern (substring (symbol-name arg
) 5))
204 (if (integerp (setq c
(car arg
)))
205 (error "non-symbolic byte-op %s" c
))
208 (setq a
(cond ((memq c byte-goto-ops
)
209 (car (cdr (cdr arg
))))
210 ((memq c byte-constref-ops
)
213 (setq c
(symbol-name c
))
214 (if (string-match "^byte-." c
)
215 (setq c
(intern (substring c
5)))))
216 (if (eq c
'constant
) (setq c
'const
))
217 (if (and (eq (cdr arg
) 0)
218 (not (memq c
'(unbind call const
))))
220 (format "(%s %s)" c a
))))
223 (defmacro byte-compile-log-lap
(format-string &rest args
)
224 `(and (memq byte-optimize-log
'(t byte
))
225 (byte-compile-log-lap-1 ,format-string
,@args
)))
228 ;;; byte-compile optimizers to support inlining
230 (put 'inline
'byte-optimizer
'byte-optimize-inline-handler
)
232 (defun byte-optimize-inline-handler (form)
233 "byte-optimize-handler for the `inline' special-form."
237 (let ((f (car-safe sexp
)))
239 (or (cdr (assq f byte-compile-function-environment
))
240 (not (or (not (fboundp f
))
241 (cdr (assq f byte-compile-macro-environment
))
242 (and (consp (setq f
(symbol-function f
)))
245 (byte-compile-inline-expand sexp
)
249 (defun byte-compile-inline-expand (form)
250 (let* ((name (car form
))
251 (localfn (cdr (assq name byte-compile-function-environment
)))
252 (fn (or localfn
(symbol-function name
))))
254 (autoload-do-load fn
)
255 (setq fn
(or (symbol-function name
)
256 (cdr (assq name byte-compile-function-environment
)))))
259 (byte-compile-warn "attempt to inline `%s' before it was defined"
263 (error "File `%s' didn't define `%s'" (nth 1 fn
) name
))
264 ((and (pred symbolp
) (guard (not (eq fn t
)))) ;A function alias.
265 (byte-compile-inline-expand (cons fn
(cdr form
))))
266 ((pred byte-code-function-p
)
267 ;; (message "Inlining byte-code for %S!" name)
268 ;; The byte-code will be really inlined in byte-compile-unfold-bcf.
270 ((or `(lambda .
,_
) `(closure .
,_
))
271 (if (not (or (eq fn localfn
) ;From the same file => same mode.
272 (eq (car fn
) ;Same mode.
273 (if lexical-binding
'closure
'lambda
))))
274 ;; While byte-compile-unfold-bcf can inline dynbind byte-code into
275 ;; letbind byte-code (or any other combination for that matter), we
276 ;; can only inline dynbind source into dynbind source or letbind
277 ;; source into letbind source.
279 ;; We can of course byte-compile the inlined function
280 ;; first, and then inline its byte-code.
282 `(,(symbol-function name
) ,@(cdr form
)))
283 (let ((newfn (if (eq fn localfn
)
284 ;; If `fn' is from the same file, it has already
285 ;; been preprocessed!
287 (byte-compile-preprocess
288 (byte-compile--reify-function fn
)))))
289 (if (eq (car-safe newfn
) 'function
)
290 (byte-compile-unfold-lambda `(,(cadr newfn
) ,@(cdr form
)))
291 ;; This can happen because of macroexp-warn-and-return &co.
293 "Inlining closure %S failed" name
)
296 (_ ;; Give up on inlining.
299 ;; ((lambda ...) ...)
300 (defun byte-compile-unfold-lambda (form &optional name
)
301 ;; In lexical-binding mode, let and functions don't bind vars in the same way
302 ;; (let obey special-variable-p, but functions don't). But luckily, this
303 ;; doesn't matter here, because function's behavior is underspecified so it
304 ;; can safely be turned into a `let', even though the reverse is not true.
305 (or name
(setq name
"anonymous lambda"))
306 (let* ((lambda (car form
))
308 (arglist (nth 1 lambda
))
309 (body (cdr (cdr lambda
)))
312 (if (and (stringp (car body
)) (cdr body
))
313 (setq body
(cdr body
)))
314 (if (and (consp (car body
)) (eq 'interactive
(car (car body
))))
315 (setq body
(cdr body
)))
316 ;; FIXME: The checks below do not belong in an optimization phase.
318 (cond ((eq (car arglist
) '&optional
)
319 ;; ok, I'll let this slide because funcall_lambda() does...
320 ;; (if optionalp (error "multiple &optional keywords in %s" name))
321 (if restp
(error "&optional found after &rest in %s" name
))
322 (if (null (cdr arglist
))
323 (error "nothing after &optional in %s" name
))
325 ((eq (car arglist
) '&rest
)
326 ;; ...but it is by no stretch of the imagination a reasonable
327 ;; thing that funcall_lambda() allows (&rest x y) and
328 ;; (&rest x &optional y) in arglists.
329 (if (null (cdr arglist
))
330 (error "nothing after &rest in %s" name
))
331 (if (cdr (cdr arglist
))
332 (error "multiple vars after &rest in %s" name
))
335 (setq bindings
(cons (list (car arglist
)
336 (and values
(cons 'list values
)))
339 ((and (not optionalp
) (null values
))
340 (byte-compile-warn "attempt to open-code `%s' with too few arguments" name
)
341 (setq arglist nil values
'too-few
))
343 (setq bindings
(cons (list (car arglist
) (car values
))
345 values
(cdr values
))))
346 (setq arglist
(cdr arglist
)))
349 (or (eq values
'too-few
)
351 "attempt to open-code `%s' with too many arguments" name
))
354 ;; The following leads to infinite recursion when loading a
355 ;; file containing `(defsubst f () (f))', and then trying to
356 ;; byte-compile that file.
357 ;(setq body (mapcar 'byte-optimize-form body)))
361 (cons 'let
(cons (nreverse bindings
) body
))
362 (cons 'progn body
))))
363 (byte-compile-log " %s\t==>\t%s" form newform
)
367 ;;; implementing source-level optimizers
369 (defun byte-optimize-form-code-walker (form for-effect
)
371 ;; For normal function calls, We can just mapcar the optimizer the cdr. But
372 ;; we need to have special knowledge of the syntax of the special forms
373 ;; like let and defun (that's why they're special forms :-). (Actually,
374 ;; the important aspect is that they are subrs that don't evaluate all of
377 (let ((fn (car-safe form
))
379 (cond ((not (consp form
))
380 (if (not (and for-effect
381 (or byte-compile-delete-errors
387 (byte-compile-warn "malformed quote form: `%s'"
388 (prin1-to-string form
)))
389 ;; map (quote nil) to nil to simplify optimizer logic.
390 ;; map quoted constants to nil if for-effect (just because).
394 ((eq (car-safe fn
) 'lambda
)
395 (let ((newform (byte-compile-unfold-lambda form
)))
396 (if (eq newform form
)
397 ;; Some error occurred, avoid infinite recursion
399 (byte-optimize-form-code-walker newform for-effect
))))
400 ((eq (car-safe fn
) 'closure
) form
)
401 ((memq fn
'(let let
*))
402 ;; recursively enter the optimizer for the bindings and body
403 ;; of a let or let*. This for depth-firstness: forms that
404 ;; are more deeply nested are optimized first.
407 (mapcar (lambda (binding)
408 (if (symbolp binding
)
410 (if (cdr (cdr binding
))
411 (byte-compile-warn "malformed let binding: `%s'"
412 (prin1-to-string binding
)))
414 (byte-optimize-form (nth 1 binding
) nil
))))
416 (byte-optimize-body (cdr (cdr form
)) for-effect
))))
419 (mapcar (lambda (clause)
422 (byte-optimize-form (car clause
) nil
)
423 (byte-optimize-body (cdr clause
) for-effect
))
424 (byte-compile-warn "malformed cond form: `%s'"
425 (prin1-to-string clause
))
429 ;; As an extra added bonus, this simplifies (progn <x>) --> <x>.
431 (macroexp-progn (byte-optimize-body (cdr form
) for-effect
))
432 (byte-optimize-form (nth 1 form
) for-effect
)))
436 (cons (byte-optimize-form (nth 1 form
) for-effect
)
437 (byte-optimize-body (cdr (cdr form
)) t
)))
438 (byte-optimize-form (nth 1 form
) for-effect
)))
441 (cons (byte-optimize-form (nth 1 form
) t
)
442 (cons (byte-optimize-form (nth 2 form
) for-effect
)
443 (byte-optimize-body (cdr (cdr (cdr form
))) t
)))))
445 ((memq fn
'(save-excursion save-restriction save-current-buffer
))
446 ;; those subrs which have an implicit progn; it's not quite good
447 ;; enough to treat these like normal function calls.
448 ;; This can turn (save-excursion ...) into (save-excursion) which
449 ;; will be optimized away in the lap-optimize pass.
450 (cons fn
(byte-optimize-body (cdr form
) for-effect
)))
452 ((eq fn
'with-output-to-temp-buffer
)
453 ;; this is just like the above, except for the first argument.
456 (byte-optimize-form (nth 1 form
) nil
)
457 (byte-optimize-body (cdr (cdr form
)) for-effect
))))
460 (when (< (length form
) 3)
461 (byte-compile-warn "too few arguments for `if'"))
463 (cons (byte-optimize-form (nth 1 form
) nil
)
465 (byte-optimize-form (nth 2 form
) for-effect
)
466 (byte-optimize-body (nthcdr 3 form
) for-effect
)))))
468 ((memq fn
'(and or
)) ; Remember, and/or are control structures.
469 ;; Take forms off the back until we can't any more.
470 ;; In the future it could conceivably be a problem that the
471 ;; subexpressions of these forms are optimized in the reverse
472 ;; order, but it's ok for now.
474 (let ((backwards (reverse (cdr form
))))
475 (while (and backwards
476 (null (setcar backwards
477 (byte-optimize-form (car backwards
)
479 (setq backwards
(cdr backwards
)))
480 (if (and (cdr form
) (null backwards
))
482 " all subforms of %s called for effect; deleted" form
))
484 (cons fn
(nreverse (mapcar 'byte-optimize-form
486 (cons fn
(mapcar 'byte-optimize-form
(cdr form
)))))
488 ((eq fn
'interactive
)
489 (byte-compile-warn "misplaced interactive spec: `%s'"
490 (prin1-to-string form
))
494 ;; This forms is compiled as constant or by breaking out
495 ;; all the subexpressions and compiling them separately.
498 ((eq fn
'condition-case
)
499 (if byte-compile--use-old-handlers
500 ;; Will be optimized later.
502 `(condition-case ,(nth 1 form
) ;Not evaluated.
503 ,(byte-optimize-form (nth 2 form
) for-effect
)
504 ,@(mapcar (lambda (clause)
506 ,@(byte-optimize-body (cdr clause
) for-effect
)))
509 ((eq fn
'unwind-protect
)
510 ;; the "protected" part of an unwind-protect is compiled (and thus
511 ;; optimized) as a top-level form, so don't do it here. But the
512 ;; non-protected part has the same for-effect status as the
513 ;; unwind-protect itself. (The protected part is always for effect,
514 ;; but that isn't handled properly yet.)
516 (cons (byte-optimize-form (nth 1 form
) for-effect
)
521 (cons (byte-optimize-form (nth 1 form
) nil
)
522 (if byte-compile--use-old-handlers
523 ;; The body of a catch is compiled (and thus
524 ;; optimized) as a top-level form, so don't do it
527 (byte-optimize-body (cdr form
) for-effect
)))))
530 ;; Don't treat the args to `ignore' as being
531 ;; computed for effect. We want to avoid the warnings
532 ;; that might occur if they were treated that way.
533 ;; However, don't actually bother calling `ignore'.
534 `(prog1 nil .
,(mapcar 'byte-optimize-form
(cdr form
))))
536 ;; Needed as long as we run byte-optimize-form after cconv.
537 ((eq fn
'internal-make-closure
) form
)
539 ((byte-code-function-p fn
)
540 (cons fn
(mapcar #'byte-optimize-form
(cdr form
))))
543 (byte-compile-warn "`%s' is a malformed function"
544 (prin1-to-string fn
))
547 ((and for-effect
(setq tmp
(get fn
'side-effect-free
))
548 (or byte-compile-delete-errors
551 (byte-compile-warn "value returned from %s is unused"
552 (prin1-to-string form
))
554 (byte-compile-log " %s called for effect; deleted" fn
)
555 ;; appending a nil here might not be necessary, but it can't hurt.
557 (cons 'progn
(append (cdr form
) '(nil))) t
))
560 ;; Otherwise, no args can be considered to be for-effect,
561 ;; even if the called function is for-effect, because we
562 ;; don't know anything about that function.
563 (let ((args (mapcar #'byte-optimize-form
(cdr form
))))
564 (if (and (get fn
'pure
)
565 (byte-optimize-all-constp args
))
566 (list 'quote
(apply fn
(mapcar #'eval args
)))
569 (defun byte-optimize-all-constp (list)
570 "Non-nil if all elements of LIST satisfy `macroexp-const-p'."
572 (while (and list constant
)
573 (unless (macroexp-const-p (car list
))
575 (setq list
(cdr list
)))
578 (defun byte-optimize-form (form &optional for-effect
)
579 "The source-level pass of the optimizer."
581 ;; First, optimize all sub-forms of this one.
582 (setq form
(byte-optimize-form-code-walker form for-effect
))
584 ;; after optimizing all subforms, optimize this form until it doesn't
585 ;; optimize any further. This means that some forms will be passed through
586 ;; the optimizer many times, but that's necessary to make the for-effect
587 ;; processing do as much as possible.
590 (if (and (consp form
)
592 (or ;; (and for-effect
593 ;; ;; We don't have any of these yet, but we might.
594 ;; (setq opt (get (car form)
595 ;; 'byte-for-effect-optimizer)))
596 (setq opt
(function-get (car form
) 'byte-optimizer
)))
597 (not (eq form
(setq new
(funcall opt form
)))))
599 ;; (if (equal form new) (error "bogus optimizer -- %s" opt))
600 (byte-compile-log " %s\t==>\t%s" form new
)
601 (setq new
(byte-optimize-form new for-effect
))
606 (defun byte-optimize-body (forms all-for-effect
)
607 ;; Optimize the cdr of a progn or implicit progn; all forms is a list of
608 ;; forms, all but the last of which are optimized with the assumption that
609 ;; they are being called for effect. the last is for-effect as well if
610 ;; all-for-effect is true. returns a new list of forms.
615 (setq fe
(or all-for-effect
(cdr rest
)))
616 (setq new
(and (car rest
) (byte-optimize-form (car rest
) fe
)))
617 (if (or new
(not fe
))
618 (setq result
(cons new result
)))
619 (setq rest
(cdr rest
)))
623 ;; some source-level optimizers
625 ;; when writing optimizers, be VERY careful that the optimizer returns
626 ;; something not EQ to its argument if and ONLY if it has made a change.
627 ;; This implies that you cannot simply destructively modify the list;
628 ;; you must return something not EQ to it if you make an optimization.
630 ;; It is now safe to optimize code such that it introduces new bindings.
632 (defsubst byte-compile-trueconstp
(form)
633 "Return non-nil if FORM always evaluates to a non-nil value."
634 (while (eq (car-safe form
) 'progn
)
635 (setq form
(car (last (cdr form
)))))
639 ;; Can't use recursion in a defsubst.
640 ;; (`progn (byte-compile-trueconstp (car (last (cdr form)))))
642 ((not (symbolp form
)))
646 (defsubst byte-compile-nilconstp
(form)
647 "Return non-nil if FORM always evaluates to a nil value."
648 (while (eq (car-safe form
) 'progn
)
649 (setq form
(car (last (cdr form
)))))
652 (`quote
(null (cadr form
)))
653 ;; Can't use recursion in a defsubst.
654 ;; (`progn (byte-compile-nilconstp (car (last (cdr form)))))
656 ((not (symbolp form
)) nil
)
659 ;; If the function is being called with constant numeric args,
660 ;; evaluate as much as possible at compile-time. This optimizer
661 ;; assumes that the function is associative, like + or *.
662 (defun byte-optimize-associative-math (form)
667 (if (numberp (car rest
))
668 (setq constants
(cons (car rest
) constants
))
669 (setq args
(cons (car rest
) args
)))
670 (setq rest
(cdr rest
)))
674 (apply (car form
) constants
)
676 (cons (car form
) (nreverse args
))
678 (apply (car form
) constants
))
681 ;; If the function is being called with constant numeric args,
682 ;; evaluate as much as possible at compile-time. This optimizer
683 ;; assumes that the function satisfies
684 ;; (op x1 x2 ... xn) == (op ...(op (op x1 x2) x3) ...xn)
686 (defun byte-optimize-nonassociative-math (form)
687 (if (or (not (numberp (car (cdr form
))))
688 (not (numberp (car (cdr (cdr form
))))))
690 (let ((constant (car (cdr form
)))
691 (rest (cdr (cdr form
))))
692 (while (numberp (car rest
))
693 (setq constant
(funcall (car form
) constant
(car rest
))
696 (cons (car form
) (cons constant rest
))
699 ;;(defun byte-optimize-associative-two-args-math (form)
700 ;; (setq form (byte-optimize-associative-math form))
702 ;; (byte-optimize-two-args-left form)
705 ;;(defun byte-optimize-nonassociative-two-args-math (form)
706 ;; (setq form (byte-optimize-nonassociative-math form))
708 ;; (byte-optimize-two-args-right form)
711 (defun byte-optimize-approx-equal (x y
)
712 (<= (* (abs (- x y
)) 100) (abs (+ x y
))))
714 ;; Collect all the constants from FORM, after the STARTth arg,
715 ;; and apply FUN to them to make one argument at the end.
716 ;; For functions that can handle floats, that optimization
717 ;; can be incorrect because reordering can cause an overflow
718 ;; that would otherwise be avoided by encountering an arg that is a float.
719 ;; We avoid this problem by (1) not moving float constants and
720 ;; (2) not moving anything if it would cause an overflow.
721 (defun byte-optimize-delay-constants-math (form start fun
)
722 ;; Merge all FORM's constants from number START, call FUN on them
723 ;; and put the result at the end.
724 (let ((rest (nthcdr (1- start
) form
))
726 ;; t means we must check for overflow.
727 (overflow (memq fun
'(+ *))))
728 (while (cdr (setq rest
(cdr rest
)))
729 (if (integerp (car rest
))
731 (setq form
(copy-sequence form
)
732 rest
(nthcdr (1- start
) form
))
733 (while (setq rest
(cdr rest
))
734 (cond ((integerp (car rest
))
735 (setq constants
(cons (car rest
) constants
))
737 ;; If necessary, check now for overflow
738 ;; that might be caused by reordering.
740 ;; We have overflow if the result of doing the arithmetic
741 ;; on floats is not even close to the result
742 ;; of doing it on integers.
743 (not (byte-optimize-approx-equal
744 (apply fun
(mapcar 'float constants
))
745 (float (apply fun constants
)))))
747 (setq form
(nconc (delq nil form
)
748 (list (apply fun
(nreverse constants
)))))))))
751 (defsubst byte-compile-butlast
(form)
752 (nreverse (cdr (reverse form
))))
754 (defun byte-optimize-plus (form)
755 ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
756 ;;(setq form (byte-optimize-delay-constants-math form 1 '+))
757 (if (memq 0 form
) (setq form
(delq 0 (copy-sequence form
))))
758 ;; For (+ constants...), byte-optimize-predicate does the work.
759 (when (memq nil
(mapcar 'numberp
(cdr form
)))
761 ;; (+ x 1) --> (1+ x) and (+ x -1) --> (1- x).
762 ((and (= (length form
) 3)
763 (or (memq (nth 1 form
) '(1 -
1))
764 (memq (nth 2 form
) '(1 -
1))))
766 (if (memq (nth 1 form
) '(1 -
1))
767 (setq integer
(nth 1 form
) other
(nth 2 form
))
768 (setq integer
(nth 2 form
) other
(nth 1 form
)))
770 (list (if (eq integer
1) '1+ '1-
) other
))))
771 ;; Here, we could also do
772 ;; (+ x y ... 1) --> (1+ (+ x y ...))
773 ;; (+ x y ... -1) --> (1- (+ x y ...))
774 ;; The resulting bytecode is smaller, but is it faster? -- cyd
776 (byte-optimize-predicate form
))
778 (defun byte-optimize-minus (form)
779 ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
780 ;;(setq form (byte-optimize-delay-constants-math form 2 '+))
782 (when (and (nthcdr 3 form
)
783 (memq 0 (cddr form
)))
784 (setq form
(nconc (list (car form
) (cadr form
))
785 (delq 0 (copy-sequence (cddr form
)))))
786 ;; After the above, we must turn (- x) back into (- x 0)
788 (setq form
(nconc form
(list 0)))))
789 ;; For (- constants..), byte-optimize-predicate does the work.
790 (when (memq nil
(mapcar 'numberp
(cdr form
)))
792 ;; (- x 1) --> (1- x)
793 ((equal (nthcdr 2 form
) '(1))
794 (setq form
(list '1-
(nth 1 form
))))
795 ;; (- x -1) --> (1+ x)
796 ((equal (nthcdr 2 form
) '(-1))
797 (setq form
(list '1+ (nth 1 form
))))
799 ((and (eq (nth 1 form
) 0)
801 (setq form
(list '-
(nth 2 form
))))
802 ;; Here, we could also do
803 ;; (- x y ... 1) --> (1- (- x y ...))
804 ;; (- x y ... -1) --> (1+ (- x y ...))
805 ;; The resulting bytecode is smaller, but is it faster? -- cyd
807 (byte-optimize-predicate form
))
809 (defun byte-optimize-multiply (form)
810 (setq form
(byte-optimize-delay-constants-math form
1 '*))
811 ;; For (* constants..), byte-optimize-predicate does the work.
812 (when (memq nil
(mapcar 'numberp
(cdr form
)))
813 ;; After `byte-optimize-predicate', if there is a INTEGER constant
814 ;; in FORM, it is in the last element.
815 (let ((last (car (reverse (cdr form
)))))
817 ;; Would handling (* ... 0) here cause floating point errors?
819 ((eq 1 last
) (setq form
(byte-compile-butlast form
)))
821 (setq form
(list '-
(if (nthcdr 3 form
)
822 (byte-compile-butlast form
)
824 (byte-optimize-predicate form
))
826 (defun byte-optimize-divide (form)
827 (setq form
(byte-optimize-delay-constants-math form
2 '*))
828 ;; After `byte-optimize-predicate', if there is a INTEGER constant
829 ;; in FORM, it is in the last element.
830 (let ((last (car (reverse (cdr (cdr form
))))))
832 ;; Runtime error (leave it intact).
835 (memql 0.0 (cddr form
))))
836 ;; No constants in expression
837 ((not (numberp last
)))
838 ;; For (* constants..), byte-optimize-predicate does the work.
839 ((null (memq nil
(mapcar 'numberp
(cdr form
)))))
840 ;; (/ x y.. 1) --> (/ x y..)
841 ((and (eq last
1) (nthcdr 3 form
))
842 (setq form
(byte-compile-butlast form
)))
843 ;; (/ x -1), (/ x .. -1) --> (- x), (- (/ x ..))
845 (setq form
(list '-
(if (nthcdr 3 form
)
846 (byte-compile-butlast form
)
848 (byte-optimize-predicate form
))
850 (defun byte-optimize-logmumble (form)
851 (setq form
(byte-optimize-delay-constants-math form
1 (car form
)))
852 (byte-optimize-predicate
854 (setq form
(if (eq (car form
) 'logand
)
855 (cons 'progn
(cdr form
))
856 (delq 0 (copy-sequence form
)))))
857 ((and (eq (car-safe form
) 'logior
)
859 (cons 'progn
(cdr form
)))
863 (defun byte-optimize-binary-predicate (form)
865 ((or (not (macroexp-const-p (nth 1 form
)))
866 (nthcdr 3 form
)) ;; In case there are more than 2 args.
868 ((macroexp-const-p (nth 2 form
))
870 (list 'quote
(eval form
))
872 (t ;; This can enable some lapcode optimizations.
873 (list (car form
) (nth 2 form
) (nth 1 form
)))))
875 (defun byte-optimize-predicate (form)
879 (setq ok
(macroexp-const-p (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 (macroexp--const-symbol-p form
))))
949 (defun byte-optimize-and (form)
950 ;; Simplify if less than 2 args.
951 ;; if there is a literal nil in the args to `and', throw it and following
952 ;; forms away, and surround the `and' with (progn ... nil).
953 (cond ((null (cdr form
)))
957 (prog1 (setq form
(copy-sequence form
))
959 (setq form
(cdr form
)))
962 ((null (cdr (cdr form
)))
964 ((byte-optimize-predicate form
))))
966 (defun byte-optimize-or (form)
967 ;; Throw away nil's, and simplify if less than 2 args.
968 ;; If there is a literal non-nil constant in the args to `or', throw away all
971 (setq form
(delq nil
(copy-sequence form
))))
973 (while (cdr (setq rest
(cdr rest
)))
974 (if (byte-compile-trueconstp (car rest
))
975 (setq form
(copy-sequence form
)
976 rest
(setcdr (memq (car rest
) form
) nil
))))
978 (byte-optimize-predicate form
)
981 (defun byte-optimize-cond (form)
982 ;; if any clauses have a literal nil as their test, throw them away.
983 ;; if any clause has a literal non-nil constant as its test, throw
984 ;; away all following clauses.
986 ;; This must be first, to reduce (cond (t ...) (nil)) to (progn t ...)
987 (while (setq rest
(assq nil
(cdr form
)))
988 (setq form
(delq rest
(copy-sequence form
))))
989 (if (memq nil
(cdr form
))
990 (setq form
(delq nil
(copy-sequence form
))))
992 (while (setq rest
(cdr rest
))
993 (cond ((byte-compile-trueconstp (car-safe (car rest
)))
994 ;; This branch will always be taken: kill the subsequent ones.
995 (cond ((eq rest
(cdr form
)) ;First branch of `cond'.
996 (setq form
`(progn ,@(car rest
))))
998 (setq form
(copy-sequence form
))
999 (setcdr (memq (car rest
) form
) nil
)))
1001 ((and (consp (car rest
))
1002 (byte-compile-nilconstp (caar rest
)))
1003 ;; This branch will never be taken: kill its body.
1004 (setcdr (car rest
) nil
)))))
1006 ;; Turn (cond (( <x> )) ... ) into (or <x> (cond ... ))
1007 (if (eq 'cond
(car-safe form
))
1008 (let ((clauses (cdr form
)))
1009 (if (and (consp (car clauses
))
1010 (null (cdr (car clauses
))))
1011 (list 'or
(car (car clauses
))
1013 (cons (car form
) (cdr (cdr form
)))))
1017 (defun byte-optimize-if (form)
1018 ;; (if (progn <insts> <test>) <rest>) ==> (progn <insts> (if <test> <rest>))
1019 ;; (if <true-constant> <then> <else...>) ==> <then>
1020 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>)
1021 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>))
1022 ;; (if <test> <then> nil) ==> (if <test> <then>)
1023 (let ((clause (nth 1 form
)))
1024 (cond ((and (eq (car-safe clause
) 'progn
)
1025 ;; `clause' is a proper list.
1026 (null (cdr (last clause
))))
1027 (if (null (cddr clause
))
1028 ;; A trivial `progn'.
1029 (byte-optimize-if `(if ,(cadr clause
) ,@(nthcdr 2 form
)))
1030 (nconc (butlast clause
)
1033 `(if ,(car (last clause
)) ,@(nthcdr 2 form
)))))))
1034 ((byte-compile-trueconstp clause
)
1035 `(progn ,clause
,(nth 2 form
)))
1036 ((byte-compile-nilconstp clause
)
1037 `(progn ,clause
,@(nthcdr 3 form
)))
1039 (if (equal '(nil) (nthcdr 3 form
))
1040 (list 'if clause
(nth 2 form
))
1042 ((or (nth 3 form
) (nthcdr 4 form
))
1044 ;; Don't make a double negative;
1045 ;; instead, take away the one that is there.
1046 (if (and (consp clause
) (memq (car clause
) '(not null
))
1047 (= (length clause
) 2)) ; (not xxxx) or (not (xxxx))
1051 (cons 'progn
(nthcdr 3 form
))
1054 (list 'progn clause nil
)))))
1056 (defun byte-optimize-while (form)
1057 (when (< (length form
) 2)
1058 (byte-compile-warn "too few arguments for `while'"))
1062 (put 'and
'byte-optimizer
'byte-optimize-and
)
1063 (put 'or
'byte-optimizer
'byte-optimize-or
)
1064 (put 'cond
'byte-optimizer
'byte-optimize-cond
)
1065 (put 'if
'byte-optimizer
'byte-optimize-if
)
1066 (put 'while
'byte-optimizer
'byte-optimize-while
)
1068 ;; byte-compile-negation-optimizer lives in bytecomp.el
1069 (put '/= 'byte-optimizer
'byte-compile-negation-optimizer
)
1070 (put 'atom
'byte-optimizer
'byte-compile-negation-optimizer
)
1071 (put 'nlistp
'byte-optimizer
'byte-compile-negation-optimizer
)
1074 (defun byte-optimize-funcall (form)
1075 ;; (funcall (lambda ...) ...) ==> ((lambda ...) ...)
1076 ;; (funcall foo ...) ==> (foo ...)
1077 (let ((fn (nth 1 form
)))
1078 (if (memq (car-safe fn
) '(quote function
))
1079 (cons (nth 1 fn
) (cdr (cdr form
)))
1082 (defun byte-optimize-apply (form)
1083 ;; If the last arg is a literal constant, turn this into a funcall.
1084 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...).
1085 (let ((fn (nth 1 form
))
1086 (last (nth (1- (length form
)) form
))) ; I think this really is fastest
1087 (or (if (or (null last
)
1088 (eq (car-safe last
) 'quote
))
1089 (if (listp (nth 1 last
))
1090 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form
)))))))
1091 (nconc (list 'funcall fn
) butlast
1092 (mapcar (lambda (x) (list 'quote x
)) (nth 1 last
))))
1094 "last arg to apply can't be a literal atom: `%s'"
1095 (prin1-to-string last
))
1099 (put 'funcall
'byte-optimizer
'byte-optimize-funcall
)
1100 (put 'apply
'byte-optimizer
'byte-optimize-apply
)
1103 (put 'let
'byte-optimizer
'byte-optimize-letX
)
1104 (put 'let
* 'byte-optimizer
'byte-optimize-letX
)
1105 (defun byte-optimize-letX (form)
1106 (cond ((null (nth 1 form
))
1108 (cons 'progn
(cdr (cdr form
))))
1109 ((or (nth 2 form
) (nthcdr 3 form
))
1112 ((eq (car form
) 'let
)
1113 (append '(progn) (mapcar 'car-safe
(mapcar 'cdr-safe
(nth 1 form
)))
1116 (let ((binds (reverse (nth 1 form
))))
1117 (list 'let
* (reverse (cdr binds
)) (nth 1 (car binds
)) nil
)))))
1120 (put 'nth
'byte-optimizer
'byte-optimize-nth
)
1121 (defun byte-optimize-nth (form)
1122 (if (= (safe-length form
) 3)
1123 (if (memq (nth 1 form
) '(0 1))
1124 (list 'car
(if (zerop (nth 1 form
))
1126 (list 'cdr
(nth 2 form
))))
1127 (byte-optimize-predicate form
))
1130 (put 'nthcdr
'byte-optimizer
'byte-optimize-nthcdr
)
1131 (defun byte-optimize-nthcdr (form)
1132 (if (= (safe-length form
) 3)
1133 (if (memq (nth 1 form
) '(0 1 2))
1134 (let ((count (nth 1 form
)))
1135 (setq form
(nth 2 form
))
1136 (while (>= (setq count
(1- count
)) 0)
1137 (setq form
(list 'cdr form
)))
1139 (byte-optimize-predicate form
))
1142 ;; Fixme: delete-char -> delete-region (byte-coded)
1143 ;; optimize string-as-unibyte, string-as-multibyte, string-make-unibyte,
1144 ;; string-make-multibyte for constant args.
1146 (put 'set
'byte-optimizer
'byte-optimize-set
)
1147 (defun byte-optimize-set (form)
1148 (let ((var (car-safe (cdr-safe form
))))
1150 ((and (eq (car-safe var
) 'quote
) (consp (cdr var
)))
1151 `(setq ,(cadr var
) ,@(cddr form
)))
1152 ((and (eq (car-safe var
) 'make-local-variable
)
1153 (eq (car-safe (setq var
(car-safe (cdr var
)))) 'quote
)
1155 `(progn ,(cadr form
) (setq ,(cadr var
) ,@(cddr form
))))
1158 ;; enumerating those functions which need not be called if the returned
1159 ;; value is not used. That is, something like
1160 ;; (progn (list (something-with-side-effects) (yow))
1162 ;; may safely be turned into
1163 ;; (progn (progn (something-with-side-effects) (yow))
1165 ;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo.
1167 ;; Some of these functions have the side effect of allocating memory
1168 ;; and it would be incorrect to replace two calls with one.
1169 ;; But we don't try to do those kinds of optimizations,
1170 ;; so it is safe to list such functions here.
1171 ;; Some of these functions return values that depend on environment
1172 ;; state, so that constant folding them would be wrong,
1173 ;; but we don't do constant folding based on this list.
1175 ;; However, at present the only optimization we normally do
1176 ;; is delete calls that need not occur, and we only do that
1177 ;; with the error-free functions.
1179 ;; I wonder if I missed any :-\)
1180 (let ((side-effect-free-fns
1181 '(%
* + -
/ /= 1+ 1-
< <= = > >= abs acos append aref ash asin atan
1183 boundp buffer-file-name buffer-local-variables buffer-modified-p
1184 buffer-substring byte-code-function-p
1185 capitalize car-less-than-car car cdr ceiling char-after char-before
1186 char-equal char-to-string char-width compare-strings
1187 compare-window-configurations concat coordinates-in-window-p
1188 copy-alist copy-sequence copy-marker cos count-lines
1190 decode-time default-boundp default-value documentation downcase
1191 elt encode-char exp expt encode-time error-message-string
1192 fboundp fceiling featurep ffloor
1193 file-directory-p file-exists-p file-locked-p file-name-absolute-p
1194 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p
1195 float float-time floor format format-time-string frame-first-window
1196 frame-root-window frame-selected-window
1197 frame-visible-p fround ftruncate
1198 get gethash get-buffer get-buffer-window getenv get-file-buffer
1200 int-to-string intern-soft
1202 length local-variable-if-set-p local-variable-p log log10 logand
1203 logb logior lognot logxor lsh langinfo
1204 make-list make-string make-symbol marker-buffer max member memq min
1205 minibuffer-selected-window minibuffer-window
1206 mod multibyte-char-to-unibyte next-window nth nthcdr number-to-string
1207 parse-colon-path plist-get plist-member
1208 prefix-numeric-value previous-window prin1-to-string propertize
1210 radians-to-degrees rassq rassoc read-from-string regexp-quote
1211 region-beginning region-end reverse round
1212 sin sqrt string string
< string
= string-equal string-lessp string-to-char
1213 string-to-int string-to-number substring
1214 sxhash sxhash-equal sxhash-eq sxhash-eql
1215 symbol-function symbol-name symbol-plist symbol-value string-make-unibyte
1216 string-make-multibyte string-as-multibyte string-as-unibyte
1219 unibyte-char-to-multibyte upcase user-full-name
1220 user-login-name user-original-login-name custom-variable-p
1222 window-absolute-pixel-edges window-at window-body-height
1223 window-body-width window-buffer window-dedicated-p window-display-table
1224 window-combination-limit window-edges window-frame window-fringes
1225 window-height window-hscroll window-inside-edges
1226 window-inside-absolute-pixel-edges window-inside-pixel-edges
1227 window-left-child window-left-column window-margins window-minibuffer-p
1228 window-next-buffers window-next-sibling window-new-normal
1229 window-new-total window-normal-size window-parameter window-parameters
1230 window-parent window-pixel-edges window-point window-prev-buffers
1231 window-prev-sibling window-redisplay-end-trigger window-scroll-bars
1232 window-start window-text-height window-top-child window-top-line
1233 window-total-height window-total-width window-use-time window-vscroll
1234 window-width zerop
))
1235 (side-effect-and-error-free-fns
1237 bobp bolp bool-vector-p
1238 buffer-end buffer-list buffer-size buffer-string bufferp
1239 car-safe case-table-p cdr-safe char-or-string-p characterp
1240 charsetp commandp cons consp
1241 current-buffer current-global-map current-indentation
1242 current-local-map current-minor-mode-maps current-time
1243 current-time-string current-time-zone
1244 eobp eolp eq equal eventp
1245 floatp following-char framep
1246 get-largest-window get-lru-window
1248 identity ignore integerp integer-or-marker-p interactive-p
1249 invocation-directory invocation-name
1251 line-beginning-position line-end-position list listp
1252 make-marker mark mark-marker markerp max-char
1253 memory-limit minibuffer-window
1255 natnump nlistp not null number-or-marker-p numberp
1256 one-window-p overlayp
1257 point point-marker point-min point-max preceding-char primary-charset
1259 recent-keys recursion-depth
1260 safe-length selected-frame selected-window sequencep
1261 standard-case-table standard-syntax-table stringp subrp symbolp
1262 syntax-table syntax-table-p
1263 this-command-keys this-command-keys-vector this-single-command-keys
1264 this-single-command-raw-keys
1265 user-real-login-name user-real-uid user-uid
1266 vector vectorp visible-frame-list
1267 wholenump window-configuration-p window-live-p
1268 window-valid-p windowp
)))
1269 (while side-effect-free-fns
1270 (put (car side-effect-free-fns
) 'side-effect-free t
)
1271 (setq side-effect-free-fns
(cdr side-effect-free-fns
)))
1272 (while side-effect-and-error-free-fns
1273 (put (car side-effect-and-error-free-fns
) 'side-effect-free
'error-free
)
1274 (setq side-effect-and-error-free-fns
(cdr side-effect-and-error-free-fns
)))
1278 ;; pure functions are side-effect free functions whose values depend
1279 ;; only on their arguments. For these functions, calls with constant
1280 ;; arguments can be evaluated at compile time. This may shift run time
1281 ;; errors to compile time.
1284 '(concat symbol-name regexp-opt regexp-quote string-to-syntax
)))
1286 (put (car pure-fns
) 'pure t
)
1287 (setq pure-fns
(cdr pure-fns
)))
1290 (defconst byte-constref-ops
1291 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind
))
1293 ;; Used and set dynamically in byte-decompile-bytecode-1.
1294 (defvar bytedecomp-op
)
1295 (defvar bytedecomp-ptr
)
1297 ;; This function extracts the bitfields from variable-length opcodes.
1298 ;; Originally defined in disass.el (which no longer uses it.)
1299 (defun disassemble-offset (bytes)
1301 ;; Fetch and return the offset for the current opcode.
1302 ;; Return nil if this opcode has no offset.
1303 (cond ((< bytedecomp-op byte-pophandler
)
1304 (let ((tem (logand bytedecomp-op
7)))
1305 (setq bytedecomp-op
(logand bytedecomp-op
248))
1307 ;; Offset in next byte.
1308 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1309 (aref bytes bytedecomp-ptr
))
1311 ;; Offset in next 2 bytes.
1312 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1313 (+ (aref bytes bytedecomp-ptr
)
1314 (progn (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1315 (lsh (aref bytes bytedecomp-ptr
) 8))))
1316 (t tem
)))) ;Offset was in opcode.
1317 ((>= bytedecomp-op byte-constant
)
1318 (prog1 (- bytedecomp-op byte-constant
) ;Offset in opcode.
1319 (setq bytedecomp-op byte-constant
)))
1320 ((or (and (>= bytedecomp-op byte-constant2
)
1321 (<= bytedecomp-op byte-goto-if-not-nil-else-pop
))
1322 (memq bytedecomp-op
(eval-when-compile
1323 (list byte-stack-set2 byte-pushcatch
1324 byte-pushconditioncase
))))
1325 ;; Offset in next 2 bytes.
1326 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1327 (+ (aref bytes bytedecomp-ptr
)
1328 (progn (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1329 (lsh (aref bytes bytedecomp-ptr
) 8))))
1330 ((and (>= bytedecomp-op byte-listN
)
1331 (<= bytedecomp-op byte-discardN
))
1332 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
)) ;Offset in next byte.
1333 (aref bytes bytedecomp-ptr
))))
1335 (defvar byte-compile-tag-number
)
1337 ;; This de-compiler is used for inline expansion of compiled functions,
1338 ;; and by the disassembler.
1340 ;; This list contains numbers, which are pc values,
1341 ;; before each instruction.
1342 (defun byte-decompile-bytecode (bytes constvec
)
1343 "Turn BYTECODE into lapcode, referring to CONSTVEC."
1344 (let ((byte-compile-constants nil
)
1345 (byte-compile-variables nil
)
1346 (byte-compile-tag-number 0))
1347 (byte-decompile-bytecode-1 bytes constvec
)))
1349 ;; As byte-decompile-bytecode, but updates
1350 ;; byte-compile-{constants, variables, tag-number}.
1351 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced
1352 ;; with `goto's destined for the end of the code.
1353 ;; That is for use by the compiler.
1354 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler.
1355 ;; In that case, we put a pc value into the list
1356 ;; before each insn (or its label).
1357 (defun byte-decompile-bytecode-1 (bytes constvec
&optional make-spliceable
)
1358 (let ((length (length bytes
))
1359 (bytedecomp-ptr 0) optr tags bytedecomp-op offset
1360 lap tmp last-constant
)
1361 (while (not (= bytedecomp-ptr length
))
1363 (push bytedecomp-ptr lap
))
1364 (setq bytedecomp-op
(aref bytes bytedecomp-ptr
)
1366 ;; This uses dynamic-scope magic.
1367 offset
(disassemble-offset bytes
))
1368 (let ((opcode (aref byte-code-vector bytedecomp-op
)))
1370 (setq bytedecomp-op opcode
))
1371 (cond ((memq bytedecomp-op byte-goto-ops
)
1374 (cdr (or (assq offset tags
)
1375 (let ((new (cons offset
(byte-compile-make-tag))))
1378 ((cond ((eq bytedecomp-op
'byte-constant2
)
1379 (setq bytedecomp-op
'byte-constant
) t
)
1380 ((memq bytedecomp-op byte-constref-ops
)))
1381 (setq tmp
(if (>= offset
(length constvec
))
1382 (list 'out-of-range offset
)
1383 (aref constvec offset
))
1384 offset
(if (eq bytedecomp-op
'byte-constant
)
1385 (byte-compile-get-constant tmp
)
1386 (or (assq tmp byte-compile-variables
)
1387 (let ((new (list tmp
)))
1388 (push new byte-compile-variables
)
1391 ((eq bytedecomp-op
'byte-stack-set2
)
1392 (setq bytedecomp-op
'byte-stack-set
))
1393 ((and (eq bytedecomp-op
'byte-discardN
) (>= offset
#x80
))
1394 ;; The top bit of the operand for byte-discardN is a flag,
1395 ;; saying whether the top-of-stack is preserved. In
1396 ;; lapcode, we represent this by using a different opcode
1397 ;; (with the flag removed from the operand).
1398 (setq bytedecomp-op
'byte-discardN-preserve-tos
)
1399 (setq offset
(- offset
#x80
)))
1400 ((eq bytedecomp-op
'byte-switch
)
1401 (cl-assert (hash-table-p last-constant
) nil
1402 "byte-switch used without preceeding hash table")
1403 ;; We cannot use the original hash table referenced in the op,
1404 ;; so we create a copy of it, and replace the addresses with
1406 (let ((orig-table last-constant
))
1407 (cl-loop for e across constvec
1408 when
(eq e last-constant
)
1409 do
(setq last-constant
(copy-hash-table e
))
1411 ;; Replace all addresses with TAGs.
1412 (maphash #'(lambda (value tag
)
1414 (setq newtag
(byte-compile-make-tag))
1415 (push (cons tag newtag
) tags
)
1416 (puthash value newtag last-constant
)))
1418 ;; Replace the hash table referenced in the lapcode with our
1420 (cl-loop for el in-ref lap
1421 when
(and (listp el
) ;; make sure we're at the correct op
1422 (eq (nth 1 el
) 'byte-constant
)
1423 (eq (nth 2 el
) orig-table
))
1424 ;; Jump tables are never reused, so do this exactly
1426 do
(setf (nth 2 el
) last-constant
) and return nil
))))
1427 ;; lap = ( [ (pc . (op . arg)) ]* )
1428 (push (cons optr
(cons bytedecomp-op
(or offset
0)))
1430 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
)))
1433 (cond ((numberp (car rest
)))
1434 ((setq tmp
(assq (car (car rest
)) tags
))
1435 ;; This addr is jumped to.
1436 (setcdr rest
(cons (cons nil
(cdr tmp
))
1438 (setq tags
(delq tmp tags
))
1439 (setq rest
(cdr rest
))))
1440 (setq rest
(cdr rest
))))
1441 (if tags
(error "optimizer error: missed tags %s" tags
))
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-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 (defun byte-optimize-lapcode (lap &optional _for-effect
)
1520 "Simple peephole optimizer. LAP is both modified and returned.
1521 If FOR-EFFECT is non-nil, the return value is assumed to be of no importance."
1525 (keep-going 'first-time
)
1528 (side-effect-free (if byte-compile-delete-errors
1529 byte-compile-side-effect-free-ops
1530 byte-compile-side-effect-and-error-free-ops
)))
1532 (or (eq keep-going
'first-time
)
1533 (byte-compile-log-lap " ---- next pass"))
1537 (setq lap0
(car rest
)
1541 ;; You may notice that sequences like "dup varset discard" are
1542 ;; optimized but sequences like "dup varset TAG1: discard" are not.
1543 ;; You may be tempted to change this; resist that temptation.
1545 ;; <side-effect-free> pop --> <deleted>
1547 ;; const-X pop --> <deleted>
1548 ;; varref-X pop --> <deleted>
1549 ;; dup pop --> <deleted>
1551 ((and (eq 'byte-discard
(car lap1
))
1552 (memq (car lap0
) side-effect-free
))
1554 (setq tmp
(aref byte-stack
+-info
(symbol-value (car lap0
))))
1555 (setq rest
(cdr rest
))
1557 (byte-compile-log-lap
1558 " %s discard\t-->\t<deleted>" lap0
)
1559 (setq lap
(delq lap0
(delq lap1 lap
))))
1561 (byte-compile-log-lap
1562 " %s discard\t-->\t<deleted> discard" lap0
)
1563 (setq lap
(delq lap0 lap
)))
1565 (byte-compile-log-lap
1566 " %s discard\t-->\tdiscard discard" lap0
)
1567 (setcar lap0
'byte-discard
)
1569 ((error "Optimizer error: too much on the stack"))))
1571 ;; goto*-X X: --> X:
1573 ((and (memq (car lap0
) byte-goto-ops
)
1574 (eq (cdr lap0
) lap1
))
1575 (cond ((eq (car lap0
) 'byte-goto
)
1576 (setq lap
(delq lap0 lap
))
1577 (setq tmp
"<deleted>"))
1578 ((memq (car lap0
) byte-goto-always-pop-ops
)
1579 (setcar lap0
(setq tmp
'byte-discard
))
1581 ((error "Depth conflict at tag %d" (nth 2 lap0
))))
1582 (and (memq byte-optimize-log
'(t byte
))
1583 (byte-compile-log " (goto %s) %s:\t-->\t%s %s:"
1584 (nth 1 lap1
) (nth 1 lap1
)
1586 (setq keep-going t
))
1588 ;; varset-X varref-X --> dup varset-X
1589 ;; varbind-X varref-X --> dup varbind-X
1590 ;; const/dup varset-X varref-X --> const/dup varset-X const/dup
1591 ;; const/dup varbind-X varref-X --> const/dup varbind-X const/dup
1592 ;; The latter two can enable other optimizations.
1594 ;; For lexical variables, we could do the same
1595 ;; stack-set-X+1 stack-ref-X --> dup stack-set-X+2
1596 ;; but this is a very minor gain, since dup is stack-ref-0,
1597 ;; i.e. it's only better if X>5, and even then it comes
1598 ;; at the cost of an extra stack slot. Let's not bother.
1599 ((and (eq 'byte-varref
(car lap2
))
1600 (eq (cdr lap1
) (cdr lap2
))
1601 (memq (car lap1
) '(byte-varset byte-varbind
)))
1602 (if (and (setq tmp
(memq (car (cdr lap2
)) byte-boolean-vars
))
1603 (not (eq (car lap0
) 'byte-constant
)))
1606 (if (memq (car lap0
) '(byte-constant byte-dup
))
1608 (setq tmp
(if (or (not tmp
)
1609 (macroexp--const-symbol-p
1612 (byte-compile-get-constant t
)))
1613 (byte-compile-log-lap " %s %s %s\t-->\t%s %s %s"
1614 lap0 lap1 lap2 lap0 lap1
1615 (cons (car lap0
) tmp
))
1616 (setcar lap2
(car lap0
))
1618 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1
)
1619 (setcar lap2
(car lap1
))
1620 (setcar lap1
'byte-dup
)
1622 ;; The stack depth gets locally increased, so we will
1623 ;; increase maxdepth in case depth = maxdepth here.
1624 ;; This can cause the third argument to byte-code to
1625 ;; be larger than necessary.
1626 (setq add-depth
1))))
1628 ;; dup varset-X discard --> varset-X
1629 ;; dup varbind-X discard --> varbind-X
1630 ;; dup stack-set-X discard --> stack-set-X-1
1631 ;; (the varbind variant can emerge from other optimizations)
1633 ((and (eq 'byte-dup
(car lap0
))
1634 (eq 'byte-discard
(car lap2
))
1635 (memq (car lap1
) '(byte-varset byte-varbind
1637 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1
)
1640 (if (eq 'byte-stack-set
(car lap1
)) (cl-decf (cdr lap1
)))
1641 (setq lap
(delq lap0
(delq lap2 lap
))))
1643 ;; not goto-X-if-nil --> goto-X-if-non-nil
1644 ;; not goto-X-if-non-nil --> goto-X-if-nil
1646 ;; it is wrong to do the same thing for the -else-pop variants.
1648 ((and (eq 'byte-not
(car lap0
))
1649 (memq (car lap1
) '(byte-goto-if-nil byte-goto-if-not-nil
)))
1650 (byte-compile-log-lap " not %s\t-->\t%s"
1653 (if (eq (car lap1
) 'byte-goto-if-nil
)
1654 'byte-goto-if-not-nil
1657 (setcar lap1
(if (eq (car lap1
) 'byte-goto-if-nil
)
1658 'byte-goto-if-not-nil
1660 (setq lap
(delq lap0 lap
))
1661 (setq keep-going t
))
1663 ;; goto-X-if-nil goto-Y X: --> goto-Y-if-non-nil X:
1664 ;; goto-X-if-non-nil goto-Y X: --> goto-Y-if-nil X:
1666 ;; it is wrong to do the same thing for the -else-pop variants.
1668 ((and (memq (car lap0
)
1669 '(byte-goto-if-nil byte-goto-if-not-nil
)) ; gotoX
1670 (eq 'byte-goto
(car lap1
)) ; gotoY
1671 (eq (cdr lap0
) lap2
)) ; TAG X
1672 (let ((inverse (if (eq 'byte-goto-if-nil
(car lap0
))
1673 'byte-goto-if-not-nil
'byte-goto-if-nil
)))
1674 (byte-compile-log-lap " %s %s %s:\t-->\t%s %s:"
1676 (cons inverse
(cdr lap1
)) lap2
)
1677 (setq lap
(delq lap0 lap
))
1678 (setcar lap1 inverse
)
1679 (setq keep-going t
)))
1681 ;; const goto-if-* --> whatever
1683 ((and (eq 'byte-constant
(car lap0
))
1684 (memq (car lap1
) byte-conditional-ops
)
1685 ;; If the `byte-constant's cdr is not a cons cell, it has
1686 ;; to be an index into the constant pool); even though
1687 ;; it'll be a constant, that constant is not known yet
1688 ;; (it's typically a free variable of a closure, so will
1689 ;; only be known when the closure will be built at
1692 (cond ((if (memq (car lap1
) '(byte-goto-if-nil
1693 byte-goto-if-nil-else-pop
))
1695 (not (car (cdr lap0
))))
1696 (byte-compile-log-lap " %s %s\t-->\t<deleted>"
1698 (setq rest
(cdr rest
)
1699 lap
(delq lap0
(delq lap1 lap
))))
1701 (byte-compile-log-lap " %s %s\t-->\t%s"
1703 (cons 'byte-goto
(cdr lap1
)))
1704 (when (memq (car lap1
) byte-goto-always-pop-ops
)
1705 (setq lap
(delq lap0 lap
)))
1706 (setcar lap1
'byte-goto
)))
1707 (setq keep-going t
))
1709 ;; varref-X varref-X --> varref-X dup
1710 ;; varref-X [dup ...] varref-X --> varref-X [dup ...] dup
1711 ;; stackref-X [dup ...] stackref-X+N --> stackref-X [dup ...] dup
1712 ;; We don't optimize the const-X variations on this here,
1713 ;; because that would inhibit some goto optimizations; we
1714 ;; optimize the const-X case after all other optimizations.
1716 ((and (memq (car lap0
) '(byte-varref byte-stack-ref
))
1718 (setq tmp
(cdr rest
))
1720 (while (eq (car (car tmp
)) 'byte-dup
)
1721 (setq tmp2
(1+ tmp2
))
1722 (setq tmp
(cdr tmp
)))
1724 (eq (if (eq 'byte-stack-ref
(car lap0
))
1725 (+ tmp2
1 (cdr lap0
))
1728 (eq (car lap0
) (car (car tmp
))))
1729 (if (memq byte-optimize-log
'(t byte
))
1731 (setq tmp2
(cdr rest
))
1732 (while (not (eq tmp tmp2
))
1733 (setq tmp2
(cdr tmp2
)
1734 str
(concat str
" dup")))
1735 (byte-compile-log-lap " %s%s %s\t-->\t%s%s dup"
1736 lap0 str lap0 lap0 str
)))
1738 (setcar (car tmp
) 'byte-dup
)
1739 (setcdr (car tmp
) 0)
1742 ;; TAG1: TAG2: --> TAG1: <deleted>
1743 ;; (and other references to TAG2 are replaced with TAG1)
1745 ((and (eq (car lap0
) 'TAG
)
1746 (eq (car lap1
) 'TAG
))
1747 (and (memq byte-optimize-log
'(t byte
))
1748 (byte-compile-log " adjacent tags %d and %d merged"
1749 (nth 1 lap1
) (nth 1 lap0
)))
1751 (while (setq tmp2
(rassq lap0 tmp3
))
1753 (setq tmp3
(cdr (memq tmp2 tmp3
))))
1754 (setq lap
(delq lap0 lap
)
1756 ;; replace references to tag in jump tables, if any
1757 (dolist (table byte-compile-jump-tables
)
1759 (maphash #'(lambda (value tag
)
1760 (when (equal tag lap0
)
1761 ;; each tag occurs only once in the jump table
1762 (puthash value lap1 table
)
1763 (throw 'break nil
)))
1766 ;; unused-TAG: --> <deleted>
1768 ((and (eq 'TAG
(car lap0
))
1769 (not (rassq lap0 lap
))
1770 ;; make sure this tag isn't used in a jump-table
1771 (cl-loop for table in byte-compile-jump-tables
1772 when
(member lap0
(hash-table-values table
))
1773 return nil finally return t
))
1774 (and (memq byte-optimize-log
'(t byte
))
1775 (byte-compile-log " unused tag %d removed" (nth 1 lap0
)))
1776 (setq lap
(delq lap0 lap
)
1779 ;; goto ... --> goto <delete until TAG or end>
1780 ;; return ... --> return <delete until TAG or end>
1781 ;; (unless a jump-table is being used, where deleting may affect
1782 ;; other valid case bodies)
1784 ((and (memq (car lap0
) '(byte-goto byte-return
))
1785 (not (memq (car lap1
) '(TAG nil
)))
1786 ;; FIXME: Instead of deferring simply when jump-tables are
1787 ;; being used, keep a list of tags used for switch tags and
1788 ;; use them instead (see `byte-compile-inline-lapcode').
1789 (not byte-compile-jump-tables
))
1792 (opt-p (memq byte-optimize-log
'(t lap
)))
1794 (while (and (setq tmp
(cdr tmp
))
1795 (not (eq 'TAG
(car (car tmp
)))))
1796 (if opt-p
(setq deleted
(cons (car tmp
) deleted
)
1797 str
(concat str
" %s")
1801 (if (eq 'TAG
(car (car tmp
)))
1802 (format "%d:" (car (cdr (car tmp
))))
1803 (or (car tmp
) ""))))
1805 (apply 'byte-compile-log-lap-1
1807 " %s\t-->\t%s <deleted> %s")
1809 (nconc (nreverse deleted
)
1810 (list tagstr lap0 tagstr
)))
1811 (byte-compile-log-lap
1812 " %s <%d unreachable op%s> %s\t-->\t%s <deleted> %s"
1813 lap0 i
(if (= i
1) "" "s")
1814 tagstr lap0 tagstr
))))
1816 (setq keep-going t
))
1818 ;; <safe-op> unbind --> unbind <safe-op>
1819 ;; (this may enable other optimizations.)
1821 ((and (eq 'byte-unbind
(car lap1
))
1822 (memq (car lap0
) byte-after-unbind-ops
))
1823 (byte-compile-log-lap " %s %s\t-->\t%s %s" lap0 lap1 lap1 lap0
)
1825 (setcar (cdr rest
) lap0
)
1826 (setq keep-going t
))
1828 ;; varbind-X unbind-N --> discard unbind-(N-1)
1829 ;; save-excursion unbind-N --> unbind-(N-1)
1830 ;; save-restriction unbind-N --> unbind-(N-1)
1832 ((and (eq 'byte-unbind
(car lap1
))
1833 (memq (car lap0
) '(byte-varbind byte-save-excursion
1834 byte-save-restriction
))
1836 (if (zerop (setcdr lap1
(1- (cdr lap1
))))
1838 (if (eq (car lap0
) 'byte-varbind
)
1839 (setcar rest
(cons 'byte-discard
0))
1840 (setq lap
(delq lap0 lap
)))
1841 (byte-compile-log-lap " %s %s\t-->\t%s %s"
1842 lap0
(cons (car lap1
) (1+ (cdr lap1
)))
1843 (if (eq (car lap0
) 'byte-varbind
)
1846 (if (and (/= 0 (cdr lap1
))
1847 (eq (car lap0
) 'byte-varbind
))
1850 (setq keep-going t
))
1852 ;; goto*-X ... X: goto-Y --> goto*-Y
1853 ;; goto-X ... X: return --> return
1855 ((and (memq (car lap0
) byte-goto-ops
)
1856 (memq (car (setq tmp
(nth 1 (memq (cdr lap0
) lap
))))
1857 '(byte-goto byte-return
)))
1858 (cond ((and (not (eq tmp lap0
))
1859 (or (eq (car lap0
) 'byte-goto
)
1860 (eq (car tmp
) 'byte-goto
)))
1861 (byte-compile-log-lap " %s [%s]\t-->\t%s"
1863 (if (eq (car tmp
) 'byte-return
)
1864 (setcar lap0
'byte-return
))
1865 (setcdr lap0
(cdr tmp
))
1866 (setq keep-going t
))))
1868 ;; goto-*-else-pop X ... X: goto-if-* --> whatever
1869 ;; goto-*-else-pop X ... X: discard --> whatever
1871 ((and (memq (car lap0
) '(byte-goto-if-nil-else-pop
1872 byte-goto-if-not-nil-else-pop
))
1873 (memq (car (car (setq tmp
(cdr (memq (cdr lap0
) lap
)))))
1875 (cons 'byte-discard byte-conditional-ops
)))
1876 (not (eq lap0
(car tmp
))))
1877 (setq tmp2
(car tmp
))
1878 (setq tmp3
(assq (car lap0
) '((byte-goto-if-nil-else-pop
1880 (byte-goto-if-not-nil-else-pop
1881 byte-goto-if-not-nil
))))
1882 (if (memq (car tmp2
) tmp3
)
1883 (progn (setcar lap0
(car tmp2
))
1884 (setcdr lap0
(cdr tmp2
))
1885 (byte-compile-log-lap " %s-else-pop [%s]\t-->\t%s"
1886 (car lap0
) tmp2 lap0
))
1887 ;; Get rid of the -else-pop's and jump one step further.
1888 (or (eq 'TAG
(car (nth 1 tmp
)))
1889 (setcdr tmp
(cons (byte-compile-make-tag)
1891 (byte-compile-log-lap " %s [%s]\t-->\t%s <skip>"
1892 (car lap0
) tmp2
(nth 1 tmp3
))
1893 (setcar lap0
(nth 1 tmp3
))
1894 (setcdr lap0
(nth 1 tmp
)))
1895 (setq keep-going t
))
1897 ;; const goto-X ... X: goto-if-* --> whatever
1898 ;; const goto-X ... X: discard --> whatever
1900 ((and (eq (car lap0
) 'byte-constant
)
1901 (eq (car lap1
) 'byte-goto
)
1902 (memq (car (car (setq tmp
(cdr (memq (cdr lap1
) lap
)))))
1904 (cons 'byte-discard byte-conditional-ops
)))
1905 (not (eq lap1
(car tmp
))))
1906 (setq tmp2
(car tmp
))
1907 (cond ((when (consp (cdr lap0
))
1909 (if (null (car (cdr lap0
)))
1910 '(byte-goto-if-nil byte-goto-if-nil-else-pop
)
1911 '(byte-goto-if-not-nil
1912 byte-goto-if-not-nil-else-pop
))))
1913 (byte-compile-log-lap " %s goto [%s]\t-->\t%s %s"
1914 lap0 tmp2 lap0 tmp2
)
1915 (setcar lap1
(car tmp2
))
1916 (setcdr lap1
(cdr tmp2
))
1917 ;; Let next step fix the (const,goto-if*) sequence.
1918 (setq rest
(cons nil rest
))
1919 (setq keep-going t
))
1920 ((or (consp (cdr lap0
))
1921 (eq (car tmp2
) 'byte-discard
))
1922 ;; Jump one step further
1923 (byte-compile-log-lap
1924 " %s goto [%s]\t-->\t<deleted> goto <skip>"
1926 (or (eq 'TAG
(car (nth 1 tmp
)))
1927 (setcdr tmp
(cons (byte-compile-make-tag)
1929 (setcdr lap1
(car (cdr tmp
)))
1930 (setq lap
(delq lap0 lap
))
1931 (setq keep-going t
))))
1933 ;; X: varref-Y ... varset-Y goto-X -->
1934 ;; X: varref-Y Z: ... dup varset-Y goto-Z
1935 ;; (varset-X goto-BACK, BACK: varref-X --> copy the varref down.)
1936 ;; (This is so usual for while loops that it is worth handling).
1938 ;; Here again, we could do it for stack-ref/stack-set, but
1939 ;; that's replacing a stack-ref-Y with a stack-ref-0, which
1940 ;; is a very minor improvement (if any), at the cost of
1941 ;; more stack use and more byte-code. Let's not do it.
1943 ((and (eq (car lap1
) 'byte-varset
)
1944 (eq (car lap2
) 'byte-goto
)
1945 (not (memq (cdr lap2
) rest
)) ;Backwards jump
1946 (eq (car (car (setq tmp
(cdr (memq (cdr lap2
) lap
)))))
1948 (eq (cdr (car tmp
)) (cdr lap1
))
1949 (not (memq (car (cdr lap1
)) byte-boolean-vars
)))
1950 ;;(byte-compile-log-lap " Pulled %s to end of loop" (car tmp))
1951 (let ((newtag (byte-compile-make-tag)))
1952 (byte-compile-log-lap
1953 " %s: %s ... %s %s\t-->\t%s: %s %s: ... %s %s %s"
1954 (nth 1 (cdr lap2
)) (car tmp
)
1956 (nth 1 (cdr lap2
)) (car tmp
)
1957 (nth 1 newtag
) 'byte-dup lap1
1958 (cons 'byte-goto newtag
)
1960 (setcdr rest
(cons (cons 'byte-dup
0) (cdr rest
)))
1961 (setcdr tmp
(cons (setcdr lap2 newtag
) (cdr tmp
))))
1963 (setq keep-going t
))
1965 ;; goto-X Y: ... X: goto-if*-Y --> goto-if-not-*-X+1 Y:
1966 ;; (This can pull the loop test to the end of the loop)
1968 ((and (eq (car lap0
) 'byte-goto
)
1969 (eq (car lap1
) 'TAG
)
1971 (cdr (car (setq tmp
(cdr (memq (cdr lap0
) lap
))))))
1972 (memq (car (car tmp
))
1973 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
1974 byte-goto-if-nil-else-pop
)))
1975 ;; (byte-compile-log-lap " %s %s, %s %s --> moved conditional"
1976 ;; lap0 lap1 (cdr lap0) (car tmp))
1977 (let ((newtag (byte-compile-make-tag)))
1978 (byte-compile-log-lap
1979 "%s %s: ... %s: %s\t-->\t%s ... %s:"
1980 lap0
(nth 1 lap1
) (nth 1 (cdr lap0
)) (car tmp
)
1981 (cons (cdr (assq (car (car tmp
))
1982 '((byte-goto-if-nil . byte-goto-if-not-nil
)
1983 (byte-goto-if-not-nil . byte-goto-if-nil
)
1984 (byte-goto-if-nil-else-pop .
1985 byte-goto-if-not-nil-else-pop
)
1986 (byte-goto-if-not-nil-else-pop .
1987 byte-goto-if-nil-else-pop
))))
1992 (setcdr tmp
(cons (setcdr lap0 newtag
) (cdr tmp
)))
1993 (if (eq (car (car tmp
)) 'byte-goto-if-nil-else-pop
)
1994 ;; We can handle this case but not the -if-not-nil case,
1995 ;; because we won't know which non-nil constant to push.
1996 (setcdr rest
(cons (cons 'byte-constant
1997 (byte-compile-get-constant nil
))
1999 (setcar lap0
(nth 1 (memq (car (car tmp
))
2000 '(byte-goto-if-nil-else-pop
2001 byte-goto-if-not-nil
2003 byte-goto-if-not-nil
2004 byte-goto byte-goto
))))
2006 (setq keep-going t
))
2008 (setq rest
(cdr rest
)))
2011 ;; Rebuild byte-compile-constants / byte-compile-variables.
2012 ;; Simple optimizations that would inhibit other optimizations if they
2013 ;; were done in the optimizing loop, and optimizations which there is no
2014 ;; need to do more than once.
2015 (setq byte-compile-constants nil
2016 byte-compile-variables nil
)
2018 (byte-compile-log-lap " ---- final pass")
2020 (setq lap0
(car rest
)
2022 (if (memq (car lap0
) byte-constref-ops
)
2023 (if (memq (car lap0
) '(byte-constant byte-constant2
))
2024 (unless (memq (cdr lap0
) byte-compile-constants
)
2025 (setq byte-compile-constants
(cons (cdr lap0
)
2026 byte-compile-constants
)))
2027 (unless (memq (cdr lap0
) byte-compile-variables
)
2028 (setq byte-compile-variables
(cons (cdr lap0
)
2029 byte-compile-variables
)))))
2031 ;; const-C varset-X const-C --> const-C dup varset-X
2032 ;; const-C varbind-X const-C --> const-C dup varbind-X
2034 (and (eq (car lap0
) 'byte-constant
)
2035 (eq (car (nth 2 rest
)) 'byte-constant
)
2036 (eq (cdr lap0
) (cdr (nth 2 rest
)))
2037 (memq (car lap1
) '(byte-varbind byte-varset
)))
2038 (byte-compile-log-lap " %s %s %s\t-->\t%s dup %s"
2039 lap0 lap1 lap0 lap0 lap1
)
2040 (setcar (cdr (cdr rest
)) (cons (car lap1
) (cdr lap1
)))
2041 (setcar (cdr rest
) (cons 'byte-dup
0))
2044 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup
2045 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup
2047 ((memq (car lap0
) '(byte-constant byte-varref
))
2051 (while (eq 'byte-dup
(car (car (setq tmp
(cdr tmp
))))))
2052 (and (eq (cdr lap0
) (cdr (car tmp
)))
2053 (eq (car lap0
) (car (car tmp
)))))
2054 (setcar tmp
(cons 'byte-dup
0))
2057 (byte-compile-log-lap
2058 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0
)))
2060 ;; unbind-N unbind-M --> unbind-(N+M)
2062 ((and (eq 'byte-unbind
(car lap0
))
2063 (eq 'byte-unbind
(car lap1
)))
2064 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1
2066 (+ (cdr lap0
) (cdr lap1
))))
2067 (setq lap
(delq lap0 lap
))
2068 (setcdr lap1
(+ (cdr lap1
) (cdr lap0
))))
2071 ;; stack-set-M [discard/discardN ...] --> discardN-preserve-tos
2072 ;; stack-set-M [discard/discardN ...] --> discardN
2074 ((and (eq (car lap0
) 'byte-stack-set
)
2075 (memq (car lap1
) '(byte-discard byte-discardN
))
2077 ;; See if enough discard operations follow to expose or
2078 ;; destroy the value stored by the stack-set.
2079 (setq tmp
(cdr rest
))
2080 (setq tmp2
(1- (cdr lap0
)))
2082 (while (memq (car (car tmp
)) '(byte-discard byte-discardN
))
2084 (+ tmp3
(if (eq (car (car tmp
)) 'byte-discard
)
2087 (setq tmp
(cdr tmp
)))
2089 ;; Do the optimization.
2090 (setq lap
(delq lap0 lap
))
2093 ;; The value stored is the new TOS, so pop one more
2094 ;; value (to get rid of the old value) using the
2095 ;; TOS-preserving discard operator.
2096 'byte-discardN-preserve-tos
2097 ;; Otherwise, the value stored is lost, so just use a
2100 (setcdr lap1
(1+ tmp3
))
2101 (setcdr (cdr rest
) tmp
)
2102 (byte-compile-log-lap " %s [discard/discardN]...\t-->\t%s"
2106 ;; discard/discardN/discardN-preserve-tos-X discard/discardN-Y -->
2109 ((and (memq (car lap0
)
2110 '(byte-discard byte-discardN
2111 byte-discardN-preserve-tos
))
2112 (memq (car lap1
) '(byte-discard byte-discardN
)))
2113 (setq lap
(delq lap0 lap
))
2114 (byte-compile-log-lap
2115 " %s %s\t-->\t(discardN %s)"
2117 (+ (if (eq (car lap0
) 'byte-discard
) 1 (cdr lap0
))
2118 (if (eq (car lap1
) 'byte-discard
) 1 (cdr lap1
))))
2119 (setcdr lap1
(+ (if (eq (car lap0
) 'byte-discard
) 1 (cdr lap0
))
2120 (if (eq (car lap1
) 'byte-discard
) 1 (cdr lap1
))))
2121 (setcar lap1
'byte-discardN
))
2124 ;; discardN-preserve-tos-X discardN-preserve-tos-Y -->
2125 ;; discardN-preserve-tos-(X+Y)
2127 ((and (eq (car lap0
) 'byte-discardN-preserve-tos
)
2128 (eq (car lap1
) 'byte-discardN-preserve-tos
))
2129 (setq lap
(delq lap0 lap
))
2130 (setcdr lap1
(+ (cdr lap0
) (cdr lap1
)))
2131 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1
(car rest
)))
2134 ;; discardN-preserve-tos return --> return
2135 ;; dup return --> return
2136 ;; stack-set-N return --> return ; where N is TOS-1
2138 ((and (eq (car lap1
) 'byte-return
)
2139 (or (memq (car lap0
) '(byte-discardN-preserve-tos byte-dup
))
2140 (and (eq (car lap0
) 'byte-stack-set
)
2142 ;; The byte-code interpreter will pop the stack for us, so
2143 ;; we can just leave stuff on it.
2144 (setq lap
(delq lap0 lap
))
2145 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 lap1
))
2147 (setq rest
(cdr rest
)))
2148 (setq byte-compile-maxdepth
(+ byte-compile-maxdepth add-depth
)))
2154 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when this file compiles
2155 ;; itself, compile some of its most used recursive functions (at load time).
2158 (or (byte-code-function-p (symbol-function 'byte-optimize-form
))
2159 (assq 'byte-code
(symbol-function 'byte-optimize-form
))
2160 (let ((byte-optimize nil
)
2161 (byte-compile-warnings nil
))
2163 (or noninteractive
(message "compiling %s..." x
))
2165 (or noninteractive
(message "compiling %s...done" x
)))
2166 '(byte-optimize-form
2168 byte-optimize-predicate
2169 byte-optimize-binary-predicate
2170 ;; Inserted some more than necessary, to speed it up.
2171 byte-optimize-form-code-walker
2172 byte-optimize-lapcode
))))
2175 ;;; byte-opt.el ends here