1 ;;; byte-opt.el --- the optimization passes of the emacs-lisp byte compiler -*- lexical-binding: t -*-
3 ;; Copyright (C) 1991, 1994, 2000-2013 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 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
))
189 (defun byte-compile-log-lap-1 (format &rest args
)
190 ;; Newer byte codes for stack-ref make the slot 0 non-nil again.
191 ;; But the "old disassembler" is *really* ancient by now.
192 ;; (if (aref byte-code-vector 0)
193 ;; (error "The old version of the disassembler is loaded. Reload new-bytecomp as well"))
195 (apply 'format format
197 (mapcar (lambda (arg)
198 (if (not (consp arg
))
199 (if (and (symbolp arg
)
200 (string-match "^byte-" (symbol-name arg
)))
201 (intern (substring (symbol-name arg
) 5))
203 (if (integerp (setq c
(car arg
)))
204 (error "non-symbolic byte-op %s" c
))
207 (setq a
(cond ((memq c byte-goto-ops
)
208 (car (cdr (cdr arg
))))
209 ((memq c byte-constref-ops
)
212 (setq c
(symbol-name c
))
213 (if (string-match "^byte-." c
)
214 (setq c
(intern (substring c
5)))))
215 (if (eq c
'constant
) (setq c
'const
))
216 (if (and (eq (cdr arg
) 0)
217 (not (memq c
'(unbind call const
))))
219 (format "(%s %s)" c a
))))
222 (defmacro byte-compile-log-lap
(format-string &rest args
)
223 `(and (memq byte-optimize-log
'(t byte
))
224 (byte-compile-log-lap-1 ,format-string
,@args
)))
227 ;;; byte-compile optimizers to support inlining
229 (put 'inline
'byte-optimizer
'byte-optimize-inline-handler
)
231 (defun byte-optimize-inline-handler (form)
232 "byte-optimize-handler for the `inline' special-form."
236 (let ((f (car-safe sexp
)))
238 (or (cdr (assq f byte-compile-function-environment
))
239 (not (or (not (fboundp f
))
240 (cdr (assq f byte-compile-macro-environment
))
241 (and (consp (setq f
(symbol-function f
)))
244 (byte-compile-inline-expand sexp
)
248 (defun byte-compile-inline-expand (form)
249 (let* ((name (car form
))
250 (localfn (cdr (assq name byte-compile-function-environment
)))
251 (fn (or localfn
(and (fboundp name
) (symbol-function name
)))))
253 (autoload-do-load fn
)
254 (setq fn
(or (and (fboundp name
) (symbol-function name
))
255 (cdr (assq name byte-compile-function-environment
)))))
258 (byte-compile-warn "attempt to inline `%s' before it was defined"
262 (error "File `%s' didn't define `%s'" (nth 1 fn
) name
))
263 ((and (pred symbolp
) (guard (not (eq fn t
)))) ;A function alias.
264 (byte-compile-inline-expand (cons fn
(cdr form
))))
265 ((pred byte-code-function-p
)
266 ;; (message "Inlining byte-code for %S!" name)
267 ;; The byte-code will be really inlined in byte-compile-unfold-bcf.
269 ((or `(lambda .
,_
) `(closure .
,_
))
270 (if (not (or (eq fn localfn
) ;From the same file => same mode.
271 (eq (car fn
) ;Same mode.
272 (if lexical-binding
'closure
'lambda
))))
273 ;; While byte-compile-unfold-bcf can inline dynbind byte-code into
274 ;; letbind byte-code (or any other combination for that matter), we
275 ;; can only inline dynbind source into dynbind source or letbind
276 ;; source into letbind source.
278 ;; We can of course byte-compile the inlined function
279 ;; first, and then inline its byte-code.
281 `(,(symbol-function name
) ,@(cdr form
)))
282 (let ((newfn (if (eq fn localfn
)
283 ;; If `fn' is from the same file, it has already
284 ;; been preprocessed!
286 (byte-compile-preprocess
287 (byte-compile--reify-function fn
)))))
288 (if (eq (car-safe newfn
) 'function
)
289 (byte-compile-unfold-lambda `(,(cadr newfn
) ,@(cdr form
)))
290 ;; This can happen because of macroexp-warn-and-return &co.
291 (byte-compile-log-warning
292 (format "Inlining closure %S failed" name
))
295 (t ;; Give up on inlining.
298 ;; ((lambda ...) ...)
299 (defun byte-compile-unfold-lambda (form &optional name
)
300 ;; In lexical-binding mode, let and functions don't bind vars in the same way
301 ;; (let obey special-variable-p, but functions don't). But luckily, this
302 ;; doesn't matter here, because function's behavior is underspecified so it
303 ;; can safely be turned into a `let', even though the reverse is not true.
304 (or name
(setq name
"anonymous lambda"))
305 (let ((lambda (car form
))
307 (let ((arglist (nth 1 lambda
))
308 (body (cdr (cdr lambda
)))
311 (if (and (stringp (car body
)) (cdr body
))
312 (setq body
(cdr body
)))
313 (if (and (consp (car body
)) (eq 'interactive
(car (car body
))))
314 (setq body
(cdr body
)))
315 ;; FIXME: The checks below do not belong in an optimization phase.
317 (cond ((eq (car arglist
) '&optional
)
318 ;; ok, I'll let this slide because funcall_lambda() does...
319 ;; (if optionalp (error "multiple &optional keywords in %s" name))
320 (if restp
(error "&optional found after &rest in %s" name
))
321 (if (null (cdr arglist
))
322 (error "nothing after &optional in %s" name
))
324 ((eq (car arglist
) '&rest
)
325 ;; ...but it is by no stretch of the imagination a reasonable
326 ;; thing that funcall_lambda() allows (&rest x y) and
327 ;; (&rest x &optional y) in arglists.
328 (if (null (cdr arglist
))
329 (error "nothing after &rest in %s" name
))
330 (if (cdr (cdr arglist
))
331 (error "multiple vars after &rest in %s" name
))
334 (setq bindings
(cons (list (car arglist
)
335 (and values
(cons 'list values
)))
338 ((and (not optionalp
) (null values
))
339 (byte-compile-warn "attempt to open-code `%s' with too few arguments" name
)
340 (setq arglist nil values
'too-few
))
342 (setq bindings
(cons (list (car arglist
) (car values
))
344 values
(cdr values
))))
345 (setq arglist
(cdr arglist
)))
348 (or (eq values
'too-few
)
350 "attempt to open-code `%s' with too many arguments" name
))
353 ;; The following leads to infinite recursion when loading a
354 ;; file containing `(defsubst f () (f))', and then trying to
355 ;; byte-compile that file.
356 ;(setq body (mapcar 'byte-optimize-form body)))
360 (cons 'let
(cons (nreverse bindings
) body
))
361 (cons 'progn body
))))
362 (byte-compile-log " %s\t==>\t%s" form newform
)
366 ;;; implementing source-level optimizers
368 (defun byte-optimize-form-code-walker (form for-effect
)
370 ;; For normal function calls, We can just mapcar the optimizer the cdr. But
371 ;; we need to have special knowledge of the syntax of the special forms
372 ;; like let and defun (that's why they're special forms :-). (Actually,
373 ;; the important aspect is that they are subrs that don't evaluate all of
376 (let ((fn (car-safe form
))
378 (cond ((not (consp form
))
379 (if (not (and for-effect
380 (or byte-compile-delete-errors
386 (byte-compile-warn "malformed quote form: `%s'"
387 (prin1-to-string form
)))
388 ;; map (quote nil) to nil to simplify optimizer logic.
389 ;; map quoted constants to nil if for-effect (just because).
393 ((eq 'lambda
(car-safe fn
))
394 (let ((newform (byte-compile-unfold-lambda form
)))
395 (if (eq newform form
)
396 ;; Some error occurred, avoid infinite recursion
398 (byte-optimize-form-code-walker newform for-effect
))))
399 ((memq fn
'(let let
*))
400 ;; recursively enter the optimizer for the bindings and body
401 ;; of a let or let*. This for depth-firstness: forms that
402 ;; are more deeply nested are optimized first.
405 (mapcar (lambda (binding)
406 (if (symbolp binding
)
408 (if (cdr (cdr binding
))
409 (byte-compile-warn "malformed let binding: `%s'"
410 (prin1-to-string binding
)))
412 (byte-optimize-form (nth 1 binding
) nil
))))
414 (byte-optimize-body (cdr (cdr form
)) for-effect
))))
417 (mapcar (lambda (clause)
420 (byte-optimize-form (car clause
) nil
)
421 (byte-optimize-body (cdr clause
) for-effect
))
422 (byte-compile-warn "malformed cond form: `%s'"
423 (prin1-to-string clause
))
427 ;; As an extra added bonus, this simplifies (progn <x>) --> <x>.
429 (macroexp-progn (byte-optimize-body (cdr form
) for-effect
))
430 (byte-optimize-form (nth 1 form
) for-effect
)))
434 (cons (byte-optimize-form (nth 1 form
) for-effect
)
435 (byte-optimize-body (cdr (cdr form
)) t
)))
436 (byte-optimize-form (nth 1 form
) for-effect
)))
439 (cons (byte-optimize-form (nth 1 form
) t
)
440 (cons (byte-optimize-form (nth 2 form
) for-effect
)
441 (byte-optimize-body (cdr (cdr (cdr form
))) t
)))))
443 ((memq fn
'(save-excursion save-restriction save-current-buffer
))
444 ;; those subrs which have an implicit progn; it's not quite good
445 ;; enough to treat these like normal function calls.
446 ;; This can turn (save-excursion ...) into (save-excursion) which
447 ;; will be optimized away in the lap-optimize pass.
448 (cons fn
(byte-optimize-body (cdr form
) for-effect
)))
450 ((eq fn
'with-output-to-temp-buffer
)
451 ;; this is just like the above, except for the first argument.
454 (byte-optimize-form (nth 1 form
) nil
)
455 (byte-optimize-body (cdr (cdr form
)) for-effect
))))
458 (when (< (length form
) 3)
459 (byte-compile-warn "too few arguments for `if'"))
461 (cons (byte-optimize-form (nth 1 form
) nil
)
463 (byte-optimize-form (nth 2 form
) for-effect
)
464 (byte-optimize-body (nthcdr 3 form
) for-effect
)))))
466 ((memq fn
'(and or
)) ; Remember, and/or are control structures.
467 ;; Take forms off the back until we can't any more.
468 ;; In the future it could conceivably be a problem that the
469 ;; subexpressions of these forms are optimized in the reverse
470 ;; order, but it's ok for now.
472 (let ((backwards (reverse (cdr form
))))
473 (while (and backwards
474 (null (setcar backwards
475 (byte-optimize-form (car backwards
)
477 (setq backwards
(cdr backwards
)))
478 (if (and (cdr form
) (null backwards
))
480 " all subforms of %s called for effect; deleted" form
))
482 (cons fn
(nreverse (mapcar 'byte-optimize-form
484 (cons fn
(mapcar 'byte-optimize-form
(cdr form
)))))
486 ((eq fn
'interactive
)
487 (byte-compile-warn "misplaced interactive spec: `%s'"
488 (prin1-to-string form
))
491 ((memq fn
'(function condition-case
))
492 ;; These forms are compiled as constants or by breaking out
493 ;; all the subexpressions and compiling them separately.
496 ((eq fn
'unwind-protect
)
497 ;; the "protected" part of an unwind-protect is compiled (and thus
498 ;; optimized) as a top-level form, so don't do it here. But the
499 ;; non-protected part has the same for-effect status as the
500 ;; unwind-protect itself. (The protected part is always for effect,
501 ;; but that isn't handled properly yet.)
503 (cons (byte-optimize-form (nth 1 form
) for-effect
)
507 ;; the body of a catch is compiled (and thus optimized) as a
508 ;; top-level form, so don't do it here. The tag is never
509 ;; for-effect. The body should have the same for-effect status
510 ;; as the catch form itself, but that isn't handled properly yet.
512 (cons (byte-optimize-form (nth 1 form
) nil
)
516 ;; Don't treat the args to `ignore' as being
517 ;; computed for effect. We want to avoid the warnings
518 ;; that might occur if they were treated that way.
519 ;; However, don't actually bother calling `ignore'.
520 `(prog1 nil .
,(mapcar 'byte-optimize-form
(cdr form
))))
522 ;; Needed as long as we run byte-optimize-form after cconv.
523 ((eq fn
'internal-make-closure
) form
)
525 ((byte-code-function-p fn
)
526 (cons fn
(mapcar #'byte-optimize-form
(cdr form
))))
529 (byte-compile-warn "`%s' is a malformed function"
530 (prin1-to-string fn
))
533 ((and for-effect
(setq tmp
(get fn
'side-effect-free
))
534 (or byte-compile-delete-errors
537 (byte-compile-warn "value returned from %s is unused"
538 (prin1-to-string form
))
540 (byte-compile-log " %s called for effect; deleted" fn
)
541 ;; appending a nil here might not be necessary, but it can't hurt.
543 (cons 'progn
(append (cdr form
) '(nil))) t
))
546 ;; Otherwise, no args can be considered to be for-effect,
547 ;; even if the called function is for-effect, because we
548 ;; don't know anything about that function.
549 (let ((args (mapcar #'byte-optimize-form
(cdr form
))))
550 (if (and (get fn
'pure
)
551 (byte-optimize-all-constp args
))
552 (list 'quote
(apply fn
(mapcar #'eval args
)))
555 (defun byte-optimize-all-constp (list)
556 "Non-nil if all elements of LIST satisfy `macroexp-const-p"
558 (while (and list constant
)
559 (unless (macroexp-const-p (car list
))
561 (setq list
(cdr list
)))
564 (defun byte-optimize-form (form &optional for-effect
)
565 "The source-level pass of the optimizer."
567 ;; First, optimize all sub-forms of this one.
568 (setq form
(byte-optimize-form-code-walker form for-effect
))
570 ;; after optimizing all subforms, optimize this form until it doesn't
571 ;; optimize any further. This means that some forms will be passed through
572 ;; the optimizer many times, but that's necessary to make the for-effect
573 ;; processing do as much as possible.
576 (if (and (consp form
)
578 (or ;; (and for-effect
579 ;; ;; We don't have any of these yet, but we might.
580 ;; (setq opt (get (car form)
581 ;; 'byte-for-effect-optimizer)))
582 (setq opt
(function-get (car form
) 'byte-optimizer
)))
583 (not (eq form
(setq new
(funcall opt form
)))))
585 ;; (if (equal form new) (error "bogus optimizer -- %s" opt))
586 (byte-compile-log " %s\t==>\t%s" form new
)
587 (setq new
(byte-optimize-form new for-effect
))
592 (defun byte-optimize-body (forms all-for-effect
)
593 ;; Optimize the cdr of a progn or implicit progn; all forms is a list of
594 ;; forms, all but the last of which are optimized with the assumption that
595 ;; they are being called for effect. the last is for-effect as well if
596 ;; all-for-effect is true. returns a new list of forms.
601 (setq fe
(or all-for-effect
(cdr rest
)))
602 (setq new
(and (car rest
) (byte-optimize-form (car rest
) fe
)))
603 (if (or new
(not fe
))
604 (setq result
(cons new result
)))
605 (setq rest
(cdr rest
)))
609 ;; some source-level optimizers
611 ;; when writing optimizers, be VERY careful that the optimizer returns
612 ;; something not EQ to its argument if and ONLY if it has made a change.
613 ;; This implies that you cannot simply destructively modify the list;
614 ;; you must return something not EQ to it if you make an optimization.
616 ;; It is now safe to optimize code such that it introduces new bindings.
618 (defsubst byte-compile-trueconstp
(form)
619 "Return non-nil if FORM always evaluates to a non-nil value."
620 (while (eq (car-safe form
) 'progn
)
621 (setq form
(car (last (cdr form
)))))
625 ;; Can't use recursion in a defsubst.
626 ;; (`progn (byte-compile-trueconstp (car (last (cdr form)))))
628 ((not (symbolp form
)))
632 (defsubst byte-compile-nilconstp
(form)
633 "Return non-nil if FORM always evaluates to a nil value."
634 (while (eq (car-safe form
) 'progn
)
635 (setq form
(car (last (cdr form
)))))
638 (`quote
(null (cadr form
)))
639 ;; Can't use recursion in a defsubst.
640 ;; (`progn (byte-compile-nilconstp (car (last (cdr form)))))
642 ((not (symbolp form
)) nil
)
645 ;; If the function is being called with constant numeric args,
646 ;; evaluate as much as possible at compile-time. This optimizer
647 ;; assumes that the function is associative, like + or *.
648 (defun byte-optimize-associative-math (form)
653 (if (numberp (car rest
))
654 (setq constants
(cons (car rest
) constants
))
655 (setq args
(cons (car rest
) args
)))
656 (setq rest
(cdr rest
)))
660 (apply (car form
) constants
)
662 (cons (car form
) (nreverse args
))
664 (apply (car form
) constants
))
667 ;; If the function is being called with constant numeric args,
668 ;; evaluate as much as possible at compile-time. This optimizer
669 ;; assumes that the function satisfies
670 ;; (op x1 x2 ... xn) == (op ...(op (op x1 x2) x3) ...xn)
672 (defun byte-optimize-nonassociative-math (form)
673 (if (or (not (numberp (car (cdr form
))))
674 (not (numberp (car (cdr (cdr form
))))))
676 (let ((constant (car (cdr form
)))
677 (rest (cdr (cdr form
))))
678 (while (numberp (car rest
))
679 (setq constant
(funcall (car form
) constant
(car rest
))
682 (cons (car form
) (cons constant rest
))
685 ;;(defun byte-optimize-associative-two-args-math (form)
686 ;; (setq form (byte-optimize-associative-math form))
688 ;; (byte-optimize-two-args-left form)
691 ;;(defun byte-optimize-nonassociative-two-args-math (form)
692 ;; (setq form (byte-optimize-nonassociative-math form))
694 ;; (byte-optimize-two-args-right form)
697 (defun byte-optimize-approx-equal (x y
)
698 (<= (* (abs (- x y
)) 100) (abs (+ x y
))))
700 ;; Collect all the constants from FORM, after the STARTth arg,
701 ;; and apply FUN to them to make one argument at the end.
702 ;; For functions that can handle floats, that optimization
703 ;; can be incorrect because reordering can cause an overflow
704 ;; that would otherwise be avoided by encountering an arg that is a float.
705 ;; We avoid this problem by (1) not moving float constants and
706 ;; (2) not moving anything if it would cause an overflow.
707 (defun byte-optimize-delay-constants-math (form start fun
)
708 ;; Merge all FORM's constants from number START, call FUN on them
709 ;; and put the result at the end.
710 (let ((rest (nthcdr (1- start
) form
))
712 ;; t means we must check for overflow.
713 (overflow (memq fun
'(+ *))))
714 (while (cdr (setq rest
(cdr rest
)))
715 (if (integerp (car rest
))
717 (setq form
(copy-sequence form
)
718 rest
(nthcdr (1- start
) form
))
719 (while (setq rest
(cdr rest
))
720 (cond ((integerp (car rest
))
721 (setq constants
(cons (car rest
) constants
))
723 ;; If necessary, check now for overflow
724 ;; that might be caused by reordering.
726 ;; We have overflow if the result of doing the arithmetic
727 ;; on floats is not even close to the result
728 ;; of doing it on integers.
729 (not (byte-optimize-approx-equal
730 (apply fun
(mapcar 'float constants
))
731 (float (apply fun constants
)))))
733 (setq form
(nconc (delq nil form
)
734 (list (apply fun
(nreverse constants
)))))))))
737 (defsubst byte-compile-butlast
(form)
738 (nreverse (cdr (reverse form
))))
740 (defun byte-optimize-plus (form)
741 ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
742 ;;(setq form (byte-optimize-delay-constants-math form 1 '+))
743 (if (memq 0 form
) (setq form
(delq 0 (copy-sequence form
))))
744 ;; For (+ constants...), byte-optimize-predicate does the work.
745 (when (memq nil
(mapcar 'numberp
(cdr form
)))
747 ;; (+ x 1) --> (1+ x) and (+ x -1) --> (1- x).
748 ((and (= (length form
) 3)
749 (or (memq (nth 1 form
) '(1 -
1))
750 (memq (nth 2 form
) '(1 -
1))))
752 (if (memq (nth 1 form
) '(1 -
1))
753 (setq integer
(nth 1 form
) other
(nth 2 form
))
754 (setq integer
(nth 2 form
) other
(nth 1 form
)))
756 (list (if (eq integer
1) '1+ '1-
) other
))))
757 ;; Here, we could also do
758 ;; (+ x y ... 1) --> (1+ (+ x y ...))
759 ;; (+ x y ... -1) --> (1- (+ x y ...))
760 ;; The resulting bytecode is smaller, but is it faster? -- cyd
762 (byte-optimize-predicate form
))
764 (defun byte-optimize-minus (form)
765 ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
766 ;;(setq form (byte-optimize-delay-constants-math form 2 '+))
768 (when (and (nthcdr 3 form
)
769 (memq 0 (cddr form
)))
770 (setq form
(nconc (list (car form
) (cadr form
))
771 (delq 0 (copy-sequence (cddr form
)))))
772 ;; After the above, we must turn (- x) back into (- x 0)
774 (setq form
(nconc form
(list 0)))))
775 ;; For (- constants..), byte-optimize-predicate does the work.
776 (when (memq nil
(mapcar 'numberp
(cdr form
)))
778 ;; (- x 1) --> (1- x)
779 ((equal (nthcdr 2 form
) '(1))
780 (setq form
(list '1-
(nth 1 form
))))
781 ;; (- x -1) --> (1+ x)
782 ((equal (nthcdr 2 form
) '(-1))
783 (setq form
(list '1+ (nth 1 form
))))
785 ((and (eq (nth 1 form
) 0)
787 (setq form
(list '-
(nth 2 form
))))
788 ;; Here, we could also do
789 ;; (- x y ... 1) --> (1- (- x y ...))
790 ;; (- x y ... -1) --> (1+ (- x y ...))
791 ;; The resulting bytecode is smaller, but is it faster? -- cyd
793 (byte-optimize-predicate form
))
795 (defun byte-optimize-multiply (form)
796 (setq form
(byte-optimize-delay-constants-math form
1 '*))
797 ;; For (* constants..), byte-optimize-predicate does the work.
798 (when (memq nil
(mapcar 'numberp
(cdr form
)))
799 ;; After `byte-optimize-predicate', if there is a INTEGER constant
800 ;; in FORM, it is in the last element.
801 (let ((last (car (reverse (cdr form
)))))
803 ;; Would handling (* ... 0) here cause floating point errors?
805 ((eq 1 last
) (setq form
(byte-compile-butlast form
)))
807 (setq form
(list '-
(if (nthcdr 3 form
)
808 (byte-compile-butlast form
)
810 (byte-optimize-predicate form
))
812 (defun byte-optimize-divide (form)
813 (setq form
(byte-optimize-delay-constants-math form
2 '*))
814 ;; After `byte-optimize-predicate', if there is a INTEGER constant
815 ;; in FORM, it is in the last element.
816 (let ((last (car (reverse (cdr (cdr form
))))))
818 ;; Runtime error (leave it intact).
821 (memql 0.0 (cddr form
))))
822 ;; No constants in expression
823 ((not (numberp last
)))
824 ;; For (* constants..), byte-optimize-predicate does the work.
825 ((null (memq nil
(mapcar 'numberp
(cdr form
)))))
826 ;; (/ x y.. 1) --> (/ x y..)
827 ((and (eq last
1) (nthcdr 3 form
))
828 (setq form
(byte-compile-butlast form
)))
829 ;; (/ x -1), (/ x .. -1) --> (- x), (- (/ x ..))
831 (setq form
(list '-
(if (nthcdr 3 form
)
832 (byte-compile-butlast form
)
834 (byte-optimize-predicate form
))
836 (defun byte-optimize-logmumble (form)
837 (setq form
(byte-optimize-delay-constants-math form
1 (car form
)))
838 (byte-optimize-predicate
840 (setq form
(if (eq (car form
) 'logand
)
841 (cons 'progn
(cdr form
))
842 (delq 0 (copy-sequence form
)))))
843 ((and (eq (car-safe form
) 'logior
)
845 (cons 'progn
(cdr form
)))
849 (defun byte-optimize-binary-predicate (form)
850 (if (macroexp-const-p (nth 1 form
))
851 (if (macroexp-const-p (nth 2 form
))
853 (list 'quote
(eval form
))
855 ;; This can enable some lapcode optimizations.
856 (list (car form
) (nth 2 form
) (nth 1 form
)))
859 (defun byte-optimize-predicate (form)
863 (setq ok
(macroexp-const-p (car rest
))
867 (list 'quote
(eval form
))
871 (defun byte-optimize-identity (form)
872 (if (and (cdr form
) (null (cdr (cdr form
))))
874 (byte-compile-warn "identity called with %d arg%s, but requires 1"
876 (if (= 1 (length (cdr form
))) "" "s"))
879 (put 'identity
'byte-optimizer
'byte-optimize-identity
)
881 (put '+ 'byte-optimizer
'byte-optimize-plus
)
882 (put '* 'byte-optimizer
'byte-optimize-multiply
)
883 (put '-
'byte-optimizer
'byte-optimize-minus
)
884 (put '/ 'byte-optimizer
'byte-optimize-divide
)
885 (put 'max
'byte-optimizer
'byte-optimize-associative-math
)
886 (put 'min
'byte-optimizer
'byte-optimize-associative-math
)
888 (put '= 'byte-optimizer
'byte-optimize-binary-predicate
)
889 (put 'eq
'byte-optimizer
'byte-optimize-binary-predicate
)
890 (put 'equal
'byte-optimizer
'byte-optimize-binary-predicate
)
891 (put 'string
= 'byte-optimizer
'byte-optimize-binary-predicate
)
892 (put 'string-equal
'byte-optimizer
'byte-optimize-binary-predicate
)
894 (put '< 'byte-optimizer
'byte-optimize-predicate
)
895 (put '> 'byte-optimizer
'byte-optimize-predicate
)
896 (put '<= 'byte-optimizer
'byte-optimize-predicate
)
897 (put '>= 'byte-optimizer
'byte-optimize-predicate
)
898 (put '1+ 'byte-optimizer
'byte-optimize-predicate
)
899 (put '1-
'byte-optimizer
'byte-optimize-predicate
)
900 (put 'not
'byte-optimizer
'byte-optimize-predicate
)
901 (put 'null
'byte-optimizer
'byte-optimize-predicate
)
902 (put 'memq
'byte-optimizer
'byte-optimize-predicate
)
903 (put 'consp
'byte-optimizer
'byte-optimize-predicate
)
904 (put 'listp
'byte-optimizer
'byte-optimize-predicate
)
905 (put 'symbolp
'byte-optimizer
'byte-optimize-predicate
)
906 (put 'stringp
'byte-optimizer
'byte-optimize-predicate
)
907 (put 'string
< 'byte-optimizer
'byte-optimize-predicate
)
908 (put 'string-lessp
'byte-optimizer
'byte-optimize-predicate
)
910 (put 'logand
'byte-optimizer
'byte-optimize-logmumble
)
911 (put 'logior
'byte-optimizer
'byte-optimize-logmumble
)
912 (put 'logxor
'byte-optimizer
'byte-optimize-logmumble
)
913 (put 'lognot
'byte-optimizer
'byte-optimize-predicate
)
915 (put 'car
'byte-optimizer
'byte-optimize-predicate
)
916 (put 'cdr
'byte-optimizer
'byte-optimize-predicate
)
917 (put 'car-safe
'byte-optimizer
'byte-optimize-predicate
)
918 (put 'cdr-safe
'byte-optimizer
'byte-optimize-predicate
)
921 ;; I'm not convinced that this is necessary. Doesn't the optimizer loop
922 ;; take care of this? - Jamie
923 ;; I think this may some times be necessary to reduce ie (quote 5) to 5,
924 ;; so arithmetic optimizers recognize the numeric constant. - Hallvard
925 (put 'quote
'byte-optimizer
'byte-optimize-quote
)
926 (defun byte-optimize-quote (form)
927 (if (or (consp (nth 1 form
))
928 (and (symbolp (nth 1 form
))
929 (not (macroexp--const-symbol-p form
))))
933 (defun byte-optimize-zerop (form)
934 (cond ((numberp (nth 1 form
))
936 (byte-compile-delete-errors
937 (list '= (nth 1 form
) 0))
940 (put 'zerop
'byte-optimizer
'byte-optimize-zerop
)
942 (defun byte-optimize-and (form)
943 ;; Simplify if less than 2 args.
944 ;; if there is a literal nil in the args to `and', throw it and following
945 ;; forms away, and surround the `and' with (progn ... nil).
946 (cond ((null (cdr form
)))
950 (prog1 (setq form
(copy-sequence form
))
952 (setq form
(cdr form
)))
955 ((null (cdr (cdr form
)))
957 ((byte-optimize-predicate form
))))
959 (defun byte-optimize-or (form)
960 ;; Throw away nil's, and simplify if less than 2 args.
961 ;; If there is a literal non-nil constant in the args to `or', throw away all
964 (setq form
(delq nil
(copy-sequence form
))))
966 (while (cdr (setq rest
(cdr rest
)))
967 (if (byte-compile-trueconstp (car rest
))
968 (setq form
(copy-sequence form
)
969 rest
(setcdr (memq (car rest
) form
) nil
))))
971 (byte-optimize-predicate form
)
974 (defun byte-optimize-cond (form)
975 ;; if any clauses have a literal nil as their test, throw them away.
976 ;; if any clause has a literal non-nil constant as its test, throw
977 ;; away all following clauses.
979 ;; This must be first, to reduce (cond (t ...) (nil)) to (progn t ...)
980 (while (setq rest
(assq nil
(cdr form
)))
981 (setq form
(delq rest
(copy-sequence form
))))
982 (if (memq nil
(cdr form
))
983 (setq form
(delq nil
(copy-sequence form
))))
985 (while (setq rest
(cdr rest
))
986 (cond ((byte-compile-trueconstp (car-safe (car rest
)))
987 ;; This branch will always be taken: kill the subsequent ones.
988 (cond ((eq rest
(cdr form
)) ;First branch of `cond'.
989 (setq form
`(progn ,@(car rest
))))
991 (setq form
(copy-sequence form
))
992 (setcdr (memq (car rest
) form
) nil
)))
994 ((and (consp (car rest
))
995 (byte-compile-nilconstp (caar rest
)))
996 ;; This branch will never be taken: kill its body.
997 (setcdr (car rest
) nil
)))))
999 ;; Turn (cond (( <x> )) ... ) into (or <x> (cond ... ))
1000 (if (eq 'cond
(car-safe form
))
1001 (let ((clauses (cdr form
)))
1002 (if (and (consp (car clauses
))
1003 (null (cdr (car clauses
))))
1004 (list 'or
(car (car clauses
))
1006 (cons (car form
) (cdr (cdr form
)))))
1010 (defun byte-optimize-if (form)
1011 ;; (if (progn <insts> <test>) <rest>) ==> (progn <insts> (if <test> <rest>))
1012 ;; (if <true-constant> <then> <else...>) ==> <then>
1013 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>)
1014 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>))
1015 ;; (if <test> <then> nil) ==> (if <test> <then>)
1016 (let ((clause (nth 1 form
)))
1017 (cond ((and (eq (car-safe clause
) 'progn
)
1018 ;; `clause' is a proper list.
1019 (null (cdr (last clause
))))
1020 (if (null (cddr clause
))
1021 ;; A trivial `progn'.
1022 (byte-optimize-if `(if ,(cadr clause
) ,@(nthcdr 2 form
)))
1023 (nconc (butlast clause
)
1026 `(if ,(car (last clause
)) ,@(nthcdr 2 form
)))))))
1027 ((byte-compile-trueconstp clause
)
1028 `(progn ,clause
,(nth 2 form
)))
1029 ((byte-compile-nilconstp clause
)
1030 `(progn ,clause
,@(nthcdr 3 form
)))
1032 (if (equal '(nil) (nthcdr 3 form
))
1033 (list 'if clause
(nth 2 form
))
1035 ((or (nth 3 form
) (nthcdr 4 form
))
1037 ;; Don't make a double negative;
1038 ;; instead, take away the one that is there.
1039 (if (and (consp clause
) (memq (car clause
) '(not null
))
1040 (= (length clause
) 2)) ; (not xxxx) or (not (xxxx))
1044 (cons 'progn
(nthcdr 3 form
))
1047 (list 'progn clause nil
)))))
1049 (defun byte-optimize-while (form)
1050 (when (< (length form
) 2)
1051 (byte-compile-warn "too few arguments for `while'"))
1055 (put 'and
'byte-optimizer
'byte-optimize-and
)
1056 (put 'or
'byte-optimizer
'byte-optimize-or
)
1057 (put 'cond
'byte-optimizer
'byte-optimize-cond
)
1058 (put 'if
'byte-optimizer
'byte-optimize-if
)
1059 (put 'while
'byte-optimizer
'byte-optimize-while
)
1061 ;; byte-compile-negation-optimizer lives in bytecomp.el
1062 (put '/= 'byte-optimizer
'byte-compile-negation-optimizer
)
1063 (put 'atom
'byte-optimizer
'byte-compile-negation-optimizer
)
1064 (put 'nlistp
'byte-optimizer
'byte-compile-negation-optimizer
)
1067 (defun byte-optimize-funcall (form)
1068 ;; (funcall (lambda ...) ...) ==> ((lambda ...) ...)
1069 ;; (funcall foo ...) ==> (foo ...)
1070 (let ((fn (nth 1 form
)))
1071 (if (memq (car-safe fn
) '(quote function
))
1072 (cons (nth 1 fn
) (cdr (cdr form
)))
1075 (defun byte-optimize-apply (form)
1076 ;; If the last arg is a literal constant, turn this into a funcall.
1077 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...).
1078 (let ((fn (nth 1 form
))
1079 (last (nth (1- (length form
)) form
))) ; I think this really is fastest
1080 (or (if (or (null last
)
1081 (eq (car-safe last
) 'quote
))
1082 (if (listp (nth 1 last
))
1083 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form
)))))))
1084 (nconc (list 'funcall fn
) butlast
1085 (mapcar (lambda (x) (list 'quote x
)) (nth 1 last
))))
1087 "last arg to apply can't be a literal atom: `%s'"
1088 (prin1-to-string last
))
1092 (put 'funcall
'byte-optimizer
'byte-optimize-funcall
)
1093 (put 'apply
'byte-optimizer
'byte-optimize-apply
)
1096 (put 'let
'byte-optimizer
'byte-optimize-letX
)
1097 (put 'let
* 'byte-optimizer
'byte-optimize-letX
)
1098 (defun byte-optimize-letX (form)
1099 (cond ((null (nth 1 form
))
1101 (cons 'progn
(cdr (cdr form
))))
1102 ((or (nth 2 form
) (nthcdr 3 form
))
1105 ((eq (car form
) 'let
)
1106 (append '(progn) (mapcar 'car-safe
(mapcar 'cdr-safe
(nth 1 form
)))
1109 (let ((binds (reverse (nth 1 form
))))
1110 (list 'let
* (reverse (cdr binds
)) (nth 1 (car binds
)) nil
)))))
1113 (put 'nth
'byte-optimizer
'byte-optimize-nth
)
1114 (defun byte-optimize-nth (form)
1115 (if (= (safe-length form
) 3)
1116 (if (memq (nth 1 form
) '(0 1))
1117 (list 'car
(if (zerop (nth 1 form
))
1119 (list 'cdr
(nth 2 form
))))
1120 (byte-optimize-predicate form
))
1123 (put 'nthcdr
'byte-optimizer
'byte-optimize-nthcdr
)
1124 (defun byte-optimize-nthcdr (form)
1125 (if (= (safe-length form
) 3)
1126 (if (memq (nth 1 form
) '(0 1 2))
1127 (let ((count (nth 1 form
)))
1128 (setq form
(nth 2 form
))
1129 (while (>= (setq count
(1- count
)) 0)
1130 (setq form
(list 'cdr form
)))
1132 (byte-optimize-predicate form
))
1135 ;; Fixme: delete-char -> delete-region (byte-coded)
1136 ;; optimize string-as-unibyte, string-as-multibyte, string-make-unibyte,
1137 ;; string-make-multibyte for constant args.
1139 (put 'set
'byte-optimizer
'byte-optimize-set
)
1140 (defun byte-optimize-set (form)
1141 (let ((var (car-safe (cdr-safe form
))))
1143 ((and (eq (car-safe var
) 'quote
) (consp (cdr var
)))
1144 `(setq ,(cadr var
) ,@(cddr form
)))
1145 ((and (eq (car-safe var
) 'make-local-variable
)
1146 (eq (car-safe (setq var
(car-safe (cdr var
)))) 'quote
)
1148 `(progn ,(cadr form
) (setq ,(cadr var
) ,@(cddr form
))))
1151 ;; enumerating those functions which need not be called if the returned
1152 ;; value is not used. That is, something like
1153 ;; (progn (list (something-with-side-effects) (yow))
1155 ;; may safely be turned into
1156 ;; (progn (progn (something-with-side-effects) (yow))
1158 ;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo.
1160 ;; Some of these functions have the side effect of allocating memory
1161 ;; and it would be incorrect to replace two calls with one.
1162 ;; But we don't try to do those kinds of optimizations,
1163 ;; so it is safe to list such functions here.
1164 ;; Some of these functions return values that depend on environment
1165 ;; state, so that constant folding them would be wrong,
1166 ;; but we don't do constant folding based on this list.
1168 ;; However, at present the only optimization we normally do
1169 ;; is delete calls that need not occur, and we only do that
1170 ;; with the error-free functions.
1172 ;; I wonder if I missed any :-\)
1173 (let ((side-effect-free-fns
1174 '(%
* + -
/ /= 1+ 1-
< <= = > >= abs acos append aref ash asin atan
1176 boundp buffer-file-name buffer-local-variables buffer-modified-p
1177 buffer-substring byte-code-function-p
1178 capitalize car-less-than-car car cdr ceiling char-after char-before
1179 char-equal char-to-string char-width compare-strings
1180 compare-window-configurations concat coordinates-in-window-p
1181 copy-alist copy-sequence copy-marker cos count-lines
1183 decode-time default-boundp default-value documentation downcase
1184 elt encode-char exp expt encode-time error-message-string
1185 fboundp fceiling featurep ffloor
1186 file-directory-p file-exists-p file-locked-p file-name-absolute-p
1187 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p
1188 float float-time floor format format-time-string frame-first-window
1189 frame-root-window frame-selected-window
1190 frame-visible-p fround ftruncate
1191 get gethash get-buffer get-buffer-window getenv get-file-buffer
1193 int-to-string intern-soft
1195 length local-variable-if-set-p local-variable-p log log10 logand
1196 logb logior lognot logxor lsh langinfo
1197 make-list make-string make-symbol marker-buffer max member memq min
1198 minibuffer-selected-window minibuffer-window
1199 mod multibyte-char-to-unibyte next-window nth nthcdr number-to-string
1200 parse-colon-path plist-get plist-member
1201 prefix-numeric-value previous-window prin1-to-string propertize
1203 radians-to-degrees rassq rassoc read-from-string regexp-quote
1204 region-beginning region-end reverse round
1205 sin sqrt string string
< string
= string-equal string-lessp string-to-char
1206 string-to-int string-to-number substring sxhash symbol-function
1207 symbol-name symbol-plist symbol-value string-make-unibyte
1208 string-make-multibyte string-as-multibyte string-as-unibyte
1211 unibyte-char-to-multibyte upcase user-full-name
1212 user-login-name user-original-login-name custom-variable-p
1214 window-absolute-pixel-edges window-at window-body-height
1215 window-body-width window-buffer window-dedicated-p window-display-table
1216 window-combination-limit window-edges window-frame window-fringes
1217 window-height window-hscroll window-inside-edges
1218 window-inside-absolute-pixel-edges window-inside-pixel-edges
1219 window-left-child window-left-column window-margins window-minibuffer-p
1220 window-next-buffers window-next-sibling window-new-normal
1221 window-new-total window-normal-size window-parameter window-parameters
1222 window-parent window-pixel-edges window-point window-prev-buffers
1223 window-prev-sibling window-redisplay-end-trigger window-scroll-bars
1224 window-start window-text-height window-top-child window-top-line
1225 window-total-height window-total-width window-use-time window-vscroll
1226 window-width zerop
))
1227 (side-effect-and-error-free-fns
1229 bobp bolp bool-vector-p
1230 buffer-end buffer-list buffer-size buffer-string bufferp
1231 car-safe case-table-p cdr-safe char-or-string-p characterp
1232 charsetp commandp cons consp
1233 current-buffer current-global-map current-indentation
1234 current-local-map current-minor-mode-maps current-time
1235 current-time-string current-time-zone
1236 eobp eolp eq equal eventp
1237 floatp following-char framep
1238 get-largest-window get-lru-window
1240 identity ignore integerp integer-or-marker-p interactive-p
1241 invocation-directory invocation-name
1243 line-beginning-position line-end-position list listp
1244 make-marker mark mark-marker markerp max-char
1245 memory-limit minibuffer-window
1247 natnump nlistp not null number-or-marker-p numberp
1248 one-window-p overlayp
1249 point point-marker point-min point-max preceding-char primary-charset
1251 recent-keys recursion-depth
1252 safe-length selected-frame selected-window sequencep
1253 standard-case-table standard-syntax-table stringp subrp symbolp
1254 syntax-table syntax-table-p
1255 this-command-keys this-command-keys-vector this-single-command-keys
1256 this-single-command-raw-keys
1257 user-real-login-name user-real-uid user-uid
1258 vector vectorp visible-frame-list
1259 wholenump window-configuration-p window-live-p
1260 window-valid-p windowp
)))
1261 (while side-effect-free-fns
1262 (put (car side-effect-free-fns
) 'side-effect-free t
)
1263 (setq side-effect-free-fns
(cdr side-effect-free-fns
)))
1264 (while side-effect-and-error-free-fns
1265 (put (car side-effect-and-error-free-fns
) 'side-effect-free
'error-free
)
1266 (setq side-effect-and-error-free-fns
(cdr side-effect-and-error-free-fns
)))
1270 ;; pure functions are side-effect free functions whose values depend
1271 ;; only on their arguments. For these functions, calls with constant
1272 ;; arguments can be evaluated at compile time. This may shift run time
1273 ;; errors to compile time.
1276 '(concat symbol-name regexp-opt regexp-quote string-to-syntax
)))
1278 (put (car pure-fns
) 'pure t
)
1279 (setq pure-fns
(cdr pure-fns
)))
1282 (defconst byte-constref-ops
1283 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind
))
1285 ;; Used and set dynamically in byte-decompile-bytecode-1.
1286 (defvar bytedecomp-op
)
1287 (defvar bytedecomp-ptr
)
1289 ;; This function extracts the bitfields from variable-length opcodes.
1290 ;; Originally defined in disass.el (which no longer uses it.)
1291 (defun disassemble-offset (bytes)
1293 ;; Fetch and return the offset for the current opcode.
1294 ;; Return nil if this opcode has no offset.
1295 (cond ((< bytedecomp-op byte-nth
)
1296 (let ((tem (logand bytedecomp-op
7)))
1297 (setq bytedecomp-op
(logand bytedecomp-op
248))
1299 ;; Offset in next byte.
1300 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1301 (aref bytes bytedecomp-ptr
))
1303 ;; Offset in next 2 bytes.
1304 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1305 (+ (aref bytes bytedecomp-ptr
)
1306 (progn (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1307 (lsh (aref bytes bytedecomp-ptr
) 8))))
1308 (t tem
)))) ;Offset was in opcode.
1309 ((>= bytedecomp-op byte-constant
)
1310 (prog1 (- bytedecomp-op byte-constant
) ;Offset in opcode.
1311 (setq bytedecomp-op byte-constant
)))
1312 ((or (and (>= bytedecomp-op byte-constant2
)
1313 (<= bytedecomp-op byte-goto-if-not-nil-else-pop
))
1314 (= bytedecomp-op byte-stack-set2
))
1315 ;; Offset in next 2 bytes.
1316 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1317 (+ (aref bytes bytedecomp-ptr
)
1318 (progn (setq bytedecomp-ptr
(1+ bytedecomp-ptr
))
1319 (lsh (aref bytes bytedecomp-ptr
) 8))))
1320 ((and (>= bytedecomp-op byte-listN
)
1321 (<= bytedecomp-op byte-discardN
))
1322 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
)) ;Offset in next byte.
1323 (aref bytes bytedecomp-ptr
))))
1325 (defvar byte-compile-tag-number
)
1327 ;; This de-compiler is used for inline expansion of compiled functions,
1328 ;; and by the disassembler.
1330 ;; This list contains numbers, which are pc values,
1331 ;; before each instruction.
1332 (defun byte-decompile-bytecode (bytes constvec
)
1333 "Turn BYTECODE into lapcode, referring to CONSTVEC."
1334 (let ((byte-compile-constants nil
)
1335 (byte-compile-variables nil
)
1336 (byte-compile-tag-number 0))
1337 (byte-decompile-bytecode-1 bytes constvec
)))
1339 ;; As byte-decompile-bytecode, but updates
1340 ;; byte-compile-{constants, variables, tag-number}.
1341 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced
1342 ;; with `goto's destined for the end of the code.
1343 ;; That is for use by the compiler.
1344 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler.
1345 ;; In that case, we put a pc value into the list
1346 ;; before each insn (or its label).
1347 (defun byte-decompile-bytecode-1 (bytes constvec
&optional make-spliceable
)
1348 (let ((length (length bytes
))
1349 (bytedecomp-ptr 0) optr tags bytedecomp-op offset
1351 (while (not (= bytedecomp-ptr length
))
1353 (push bytedecomp-ptr lap
))
1354 (setq bytedecomp-op
(aref bytes bytedecomp-ptr
)
1356 ;; This uses dynamic-scope magic.
1357 offset
(disassemble-offset bytes
))
1358 (let ((opcode (aref byte-code-vector bytedecomp-op
)))
1360 (setq bytedecomp-op opcode
))
1361 (cond ((memq bytedecomp-op byte-goto-ops
)
1364 (cdr (or (assq offset tags
)
1365 (let ((new (cons offset
(byte-compile-make-tag))))
1368 ((cond ((eq bytedecomp-op
'byte-constant2
)
1369 (setq bytedecomp-op
'byte-constant
) t
)
1370 ((memq bytedecomp-op byte-constref-ops
)))
1371 (setq tmp
(if (>= offset
(length constvec
))
1372 (list 'out-of-range offset
)
1373 (aref constvec offset
))
1374 offset
(if (eq bytedecomp-op
'byte-constant
)
1375 (byte-compile-get-constant tmp
)
1376 (or (assq tmp byte-compile-variables
)
1377 (let ((new (list tmp
)))
1378 (push new byte-compile-variables
)
1380 ((eq bytedecomp-op
'byte-stack-set2
)
1381 (setq bytedecomp-op
'byte-stack-set
))
1382 ((and (eq bytedecomp-op
'byte-discardN
) (>= offset
#x80
))
1383 ;; The top bit of the operand for byte-discardN is a flag,
1384 ;; saying whether the top-of-stack is preserved. In
1385 ;; lapcode, we represent this by using a different opcode
1386 ;; (with the flag removed from the operand).
1387 (setq bytedecomp-op
'byte-discardN-preserve-tos
)
1388 (setq offset
(- offset
#x80
))))
1389 ;; lap = ( [ (pc . (op . arg)) ]* )
1390 (push (cons optr
(cons bytedecomp-op
(or offset
0)))
1392 (setq bytedecomp-ptr
(1+ bytedecomp-ptr
)))
1395 (cond ((numberp (car rest
)))
1396 ((setq tmp
(assq (car (car rest
)) tags
))
1397 ;; This addr is jumped to.
1398 (setcdr rest
(cons (cons nil
(cdr tmp
))
1400 (setq tags
(delq tmp tags
))
1401 (setq rest
(cdr rest
))))
1402 (setq rest
(cdr rest
))))
1403 (if tags
(error "optimizer error: missed tags %s" tags
))
1404 ;; Remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* )
1405 (mapcar (function (lambda (elt)
1412 ;;; peephole optimizer
1414 (defconst byte-tagref-ops
(cons 'TAG byte-goto-ops
))
1416 (defconst byte-conditional-ops
1417 '(byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop
1418 byte-goto-if-not-nil-else-pop
))
1420 (defconst byte-after-unbind-ops
1421 '(byte-constant byte-dup
1422 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-integerp
1424 byte-cons byte-list1 byte-list2
; byte-list3 byte-list4
1426 ;; How about other side-effect-free-ops? Is it safe to move an
1427 ;; error invocation (such as from nth) out of an unwind-protect?
1428 ;; No, it is not, because the unwind-protect forms can alter
1429 ;; the inside of the object to which nth would apply.
1430 ;; For the same reason, byte-equal was deleted from this list.
1431 "Byte-codes that can be moved past an unbind.")
1433 (defconst byte-compile-side-effect-and-error-free-ops
1434 '(byte-constant byte-dup byte-symbolp byte-consp byte-stringp byte-listp
1435 byte-integerp byte-numberp byte-eq byte-equal byte-not byte-car-safe
1436 byte-cdr-safe byte-cons byte-list1 byte-list2 byte-point byte-point-max
1437 byte-point-min byte-following-char byte-preceding-char
1438 byte-current-column byte-eolp byte-eobp byte-bolp byte-bobp
1439 byte-current-buffer byte-stack-ref
))
1441 (defconst byte-compile-side-effect-free-ops
1443 '(byte-varref byte-nth byte-memq byte-car byte-cdr byte-length byte-aref
1444 byte-symbol-value byte-get byte-concat2 byte-concat3 byte-sub1 byte-add1
1445 byte-eqlsign byte-gtr byte-lss byte-leq byte-geq byte-diff byte-negate
1446 byte-plus byte-max byte-min byte-mult byte-char-after byte-char-syntax
1447 byte-buffer-substring byte-string
= byte-string
< byte-nthcdr byte-elt
1448 byte-member byte-assq byte-quo byte-rem
)
1449 byte-compile-side-effect-and-error-free-ops
))
1451 ;; This crock is because of the way DEFVAR_BOOL variables work.
1452 ;; Consider the code
1454 ;; (defun foo (flag)
1455 ;; (let ((old-pop-ups pop-up-windows)
1456 ;; (pop-up-windows flag))
1457 ;; (cond ((not (eq pop-up-windows old-pop-ups))
1458 ;; (setq old-pop-ups pop-up-windows)
1461 ;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is
1462 ;; something else. But if we optimize
1465 ;; varbind pop-up-windows
1466 ;; varref pop-up-windows
1471 ;; varbind pop-up-windows
1474 ;; we break the program, because it will appear that pop-up-windows and
1475 ;; old-pop-ups are not EQ when really they are. So we have to know what
1476 ;; the BOOL variables are, and not perform this optimization on them.
1478 ;; The variable `byte-boolean-vars' is now primitive and updated
1479 ;; automatically by DEFVAR_BOOL.
1481 (defun byte-optimize-lapcode (lap &optional _for-effect
)
1482 "Simple peephole optimizer. LAP is both modified and returned.
1483 If FOR-EFFECT is non-nil, the return value is assumed to be of no importance."
1487 (keep-going 'first-time
)
1490 (side-effect-free (if byte-compile-delete-errors
1491 byte-compile-side-effect-free-ops
1492 byte-compile-side-effect-and-error-free-ops
)))
1494 (or (eq keep-going
'first-time
)
1495 (byte-compile-log-lap " ---- next pass"))
1499 (setq lap0
(car rest
)
1503 ;; You may notice that sequences like "dup varset discard" are
1504 ;; optimized but sequences like "dup varset TAG1: discard" are not.
1505 ;; You may be tempted to change this; resist that temptation.
1507 ;; <side-effect-free> pop --> <deleted>
1509 ;; const-X pop --> <deleted>
1510 ;; varref-X pop --> <deleted>
1511 ;; dup pop --> <deleted>
1513 ((and (eq 'byte-discard
(car lap1
))
1514 (memq (car lap0
) side-effect-free
))
1516 (setq tmp
(aref byte-stack
+-info
(symbol-value (car lap0
))))
1517 (setq rest
(cdr rest
))
1519 (byte-compile-log-lap
1520 " %s discard\t-->\t<deleted>" lap0
)
1521 (setq lap
(delq lap0
(delq lap1 lap
))))
1523 (byte-compile-log-lap
1524 " %s discard\t-->\t<deleted> discard" lap0
)
1525 (setq lap
(delq lap0 lap
)))
1527 (byte-compile-log-lap
1528 " %s discard\t-->\tdiscard discard" lap0
)
1529 (setcar lap0
'byte-discard
)
1531 ((error "Optimizer error: too much on the stack"))))
1533 ;; goto*-X X: --> X:
1535 ((and (memq (car lap0
) byte-goto-ops
)
1536 (eq (cdr lap0
) lap1
))
1537 (cond ((eq (car lap0
) 'byte-goto
)
1538 (setq lap
(delq lap0 lap
))
1539 (setq tmp
"<deleted>"))
1540 ((memq (car lap0
) byte-goto-always-pop-ops
)
1541 (setcar lap0
(setq tmp
'byte-discard
))
1543 ((error "Depth conflict at tag %d" (nth 2 lap0
))))
1544 (and (memq byte-optimize-log
'(t byte
))
1545 (byte-compile-log " (goto %s) %s:\t-->\t%s %s:"
1546 (nth 1 lap1
) (nth 1 lap1
)
1548 (setq keep-going t
))
1550 ;; varset-X varref-X --> dup varset-X
1551 ;; varbind-X varref-X --> dup varbind-X
1552 ;; const/dup varset-X varref-X --> const/dup varset-X const/dup
1553 ;; const/dup varbind-X varref-X --> const/dup varbind-X const/dup
1554 ;; The latter two can enable other optimizations.
1556 ;; For lexical variables, we could do the same
1557 ;; stack-set-X+1 stack-ref-X --> dup stack-set-X+2
1558 ;; but this is a very minor gain, since dup is stack-ref-0,
1559 ;; i.e. it's only better if X>5, and even then it comes
1560 ;; at the cost of an extra stack slot. Let's not bother.
1561 ((and (eq 'byte-varref
(car lap2
))
1562 (eq (cdr lap1
) (cdr lap2
))
1563 (memq (car lap1
) '(byte-varset byte-varbind
)))
1564 (if (and (setq tmp
(memq (car (cdr lap2
)) byte-boolean-vars
))
1565 (not (eq (car lap0
) 'byte-constant
)))
1568 (if (memq (car lap0
) '(byte-constant byte-dup
))
1570 (setq tmp
(if (or (not tmp
)
1571 (macroexp--const-symbol-p
1574 (byte-compile-get-constant t
)))
1575 (byte-compile-log-lap " %s %s %s\t-->\t%s %s %s"
1576 lap0 lap1 lap2 lap0 lap1
1577 (cons (car lap0
) tmp
))
1578 (setcar lap2
(car lap0
))
1580 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1
)
1581 (setcar lap2
(car lap1
))
1582 (setcar lap1
'byte-dup
)
1584 ;; The stack depth gets locally increased, so we will
1585 ;; increase maxdepth in case depth = maxdepth here.
1586 ;; This can cause the third argument to byte-code to
1587 ;; be larger than necessary.
1588 (setq add-depth
1))))
1590 ;; dup varset-X discard --> varset-X
1591 ;; dup varbind-X discard --> varbind-X
1592 ;; dup stack-set-X discard --> stack-set-X-1
1593 ;; (the varbind variant can emerge from other optimizations)
1595 ((and (eq 'byte-dup
(car lap0
))
1596 (eq 'byte-discard
(car lap2
))
1597 (memq (car lap1
) '(byte-varset byte-varbind
1599 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1
)
1602 (if (eq 'byte-stack-set
(car lap1
)) (cl-decf (cdr lap1
)))
1603 (setq lap
(delq lap0
(delq lap2 lap
))))
1605 ;; not goto-X-if-nil --> goto-X-if-non-nil
1606 ;; not goto-X-if-non-nil --> goto-X-if-nil
1608 ;; it is wrong to do the same thing for the -else-pop variants.
1610 ((and (eq 'byte-not
(car lap0
))
1611 (memq (car lap1
) '(byte-goto-if-nil byte-goto-if-not-nil
)))
1612 (byte-compile-log-lap " not %s\t-->\t%s"
1615 (if (eq (car lap1
) 'byte-goto-if-nil
)
1616 'byte-goto-if-not-nil
1619 (setcar lap1
(if (eq (car lap1
) 'byte-goto-if-nil
)
1620 'byte-goto-if-not-nil
1622 (setq lap
(delq lap0 lap
))
1623 (setq keep-going t
))
1625 ;; goto-X-if-nil goto-Y X: --> goto-Y-if-non-nil X:
1626 ;; goto-X-if-non-nil goto-Y X: --> goto-Y-if-nil X:
1628 ;; it is wrong to do the same thing for the -else-pop variants.
1630 ((and (memq (car lap0
)
1631 '(byte-goto-if-nil byte-goto-if-not-nil
)) ; gotoX
1632 (eq 'byte-goto
(car lap1
)) ; gotoY
1633 (eq (cdr lap0
) lap2
)) ; TAG X
1634 (let ((inverse (if (eq 'byte-goto-if-nil
(car lap0
))
1635 'byte-goto-if-not-nil
'byte-goto-if-nil
)))
1636 (byte-compile-log-lap " %s %s %s:\t-->\t%s %s:"
1638 (cons inverse
(cdr lap1
)) lap2
)
1639 (setq lap
(delq lap0 lap
))
1640 (setcar lap1 inverse
)
1641 (setq keep-going t
)))
1643 ;; const goto-if-* --> whatever
1645 ((and (eq 'byte-constant
(car lap0
))
1646 (memq (car lap1
) byte-conditional-ops
)
1647 ;; If the `byte-constant's cdr is not a cons cell, it has
1648 ;; to be an index into the constant pool); even though
1649 ;; it'll be a constant, that constant is not known yet
1650 ;; (it's typically a free variable of a closure, so will
1651 ;; only be known when the closure will be built at
1654 (cond ((if (memq (car lap1
) '(byte-goto-if-nil
1655 byte-goto-if-nil-else-pop
))
1657 (not (car (cdr lap0
))))
1658 (byte-compile-log-lap " %s %s\t-->\t<deleted>"
1660 (setq rest
(cdr rest
)
1661 lap
(delq lap0
(delq lap1 lap
))))
1663 (byte-compile-log-lap " %s %s\t-->\t%s"
1665 (cons 'byte-goto
(cdr lap1
)))
1666 (when (memq (car lap1
) byte-goto-always-pop-ops
)
1667 (setq lap
(delq lap0 lap
)))
1668 (setcar lap1
'byte-goto
)))
1669 (setq keep-going t
))
1671 ;; varref-X varref-X --> varref-X dup
1672 ;; varref-X [dup ...] varref-X --> varref-X [dup ...] dup
1673 ;; stackref-X [dup ...] stackref-X+N --> stackref-X [dup ...] dup
1674 ;; We don't optimize the const-X variations on this here,
1675 ;; because that would inhibit some goto optimizations; we
1676 ;; optimize the const-X case after all other optimizations.
1678 ((and (memq (car lap0
) '(byte-varref byte-stack-ref
))
1680 (setq tmp
(cdr rest
))
1682 (while (eq (car (car tmp
)) 'byte-dup
)
1683 (setq tmp2
(1+ tmp2
))
1684 (setq tmp
(cdr tmp
)))
1686 (eq (if (eq 'byte-stack-ref
(car lap0
))
1687 (+ tmp2
1 (cdr lap0
))
1690 (eq (car lap0
) (car (car tmp
))))
1691 (if (memq byte-optimize-log
'(t byte
))
1693 (setq tmp2
(cdr rest
))
1694 (while (not (eq tmp tmp2
))
1695 (setq tmp2
(cdr tmp2
)
1696 str
(concat str
" dup")))
1697 (byte-compile-log-lap " %s%s %s\t-->\t%s%s dup"
1698 lap0 str lap0 lap0 str
)))
1700 (setcar (car tmp
) 'byte-dup
)
1701 (setcdr (car tmp
) 0)
1704 ;; TAG1: TAG2: --> TAG1: <deleted>
1705 ;; (and other references to TAG2 are replaced with TAG1)
1707 ((and (eq (car lap0
) 'TAG
)
1708 (eq (car lap1
) 'TAG
))
1709 (and (memq byte-optimize-log
'(t byte
))
1710 (byte-compile-log " adjacent tags %d and %d merged"
1711 (nth 1 lap1
) (nth 1 lap0
)))
1713 (while (setq tmp2
(rassq lap0 tmp3
))
1715 (setq tmp3
(cdr (memq tmp2 tmp3
))))
1716 (setq lap
(delq lap0 lap
)
1719 ;; unused-TAG: --> <deleted>
1721 ((and (eq 'TAG
(car lap0
))
1722 (not (rassq lap0 lap
)))
1723 (and (memq byte-optimize-log
'(t byte
))
1724 (byte-compile-log " unused tag %d removed" (nth 1 lap0
)))
1725 (setq lap
(delq lap0 lap
)
1728 ;; goto ... --> goto <delete until TAG or end>
1729 ;; return ... --> return <delete until TAG or end>
1731 ((and (memq (car lap0
) '(byte-goto byte-return
))
1732 (not (memq (car lap1
) '(TAG nil
))))
1735 (opt-p (memq byte-optimize-log
'(t lap
)))
1737 (while (and (setq tmp
(cdr tmp
))
1738 (not (eq 'TAG
(car (car tmp
)))))
1739 (if opt-p
(setq deleted
(cons (car tmp
) deleted
)
1740 str
(concat str
" %s")
1744 (if (eq 'TAG
(car (car tmp
)))
1745 (format "%d:" (car (cdr (car tmp
))))
1746 (or (car tmp
) ""))))
1748 (apply 'byte-compile-log-lap-1
1750 " %s\t-->\t%s <deleted> %s")
1752 (nconc (nreverse deleted
)
1753 (list tagstr lap0 tagstr
)))
1754 (byte-compile-log-lap
1755 " %s <%d unreachable op%s> %s\t-->\t%s <deleted> %s"
1756 lap0 i
(if (= i
1) "" "s")
1757 tagstr lap0 tagstr
))))
1759 (setq keep-going t
))
1761 ;; <safe-op> unbind --> unbind <safe-op>
1762 ;; (this may enable other optimizations.)
1764 ((and (eq 'byte-unbind
(car lap1
))
1765 (memq (car lap0
) byte-after-unbind-ops
))
1766 (byte-compile-log-lap " %s %s\t-->\t%s %s" lap0 lap1 lap1 lap0
)
1768 (setcar (cdr rest
) lap0
)
1769 (setq keep-going t
))
1771 ;; varbind-X unbind-N --> discard unbind-(N-1)
1772 ;; save-excursion unbind-N --> unbind-(N-1)
1773 ;; save-restriction unbind-N --> unbind-(N-1)
1775 ((and (eq 'byte-unbind
(car lap1
))
1776 (memq (car lap0
) '(byte-varbind byte-save-excursion
1777 byte-save-restriction
))
1779 (if (zerop (setcdr lap1
(1- (cdr lap1
))))
1781 (if (eq (car lap0
) 'byte-varbind
)
1782 (setcar rest
(cons 'byte-discard
0))
1783 (setq lap
(delq lap0 lap
)))
1784 (byte-compile-log-lap " %s %s\t-->\t%s %s"
1785 lap0
(cons (car lap1
) (1+ (cdr lap1
)))
1786 (if (eq (car lap0
) 'byte-varbind
)
1789 (if (and (/= 0 (cdr lap1
))
1790 (eq (car lap0
) 'byte-varbind
))
1793 (setq keep-going t
))
1795 ;; goto*-X ... X: goto-Y --> goto*-Y
1796 ;; goto-X ... X: return --> return
1798 ((and (memq (car lap0
) byte-goto-ops
)
1799 (memq (car (setq tmp
(nth 1 (memq (cdr lap0
) lap
))))
1800 '(byte-goto byte-return
)))
1801 (cond ((and (not (eq tmp lap0
))
1802 (or (eq (car lap0
) 'byte-goto
)
1803 (eq (car tmp
) 'byte-goto
)))
1804 (byte-compile-log-lap " %s [%s]\t-->\t%s"
1806 (if (eq (car tmp
) 'byte-return
)
1807 (setcar lap0
'byte-return
))
1808 (setcdr lap0
(cdr tmp
))
1809 (setq keep-going t
))))
1811 ;; goto-*-else-pop X ... X: goto-if-* --> whatever
1812 ;; goto-*-else-pop X ... X: discard --> whatever
1814 ((and (memq (car lap0
) '(byte-goto-if-nil-else-pop
1815 byte-goto-if-not-nil-else-pop
))
1816 (memq (car (car (setq tmp
(cdr (memq (cdr lap0
) lap
)))))
1818 (cons 'byte-discard byte-conditional-ops
)))
1819 (not (eq lap0
(car tmp
))))
1820 (setq tmp2
(car tmp
))
1821 (setq tmp3
(assq (car lap0
) '((byte-goto-if-nil-else-pop
1823 (byte-goto-if-not-nil-else-pop
1824 byte-goto-if-not-nil
))))
1825 (if (memq (car tmp2
) tmp3
)
1826 (progn (setcar lap0
(car tmp2
))
1827 (setcdr lap0
(cdr tmp2
))
1828 (byte-compile-log-lap " %s-else-pop [%s]\t-->\t%s"
1829 (car lap0
) tmp2 lap0
))
1830 ;; Get rid of the -else-pop's and jump one step further.
1831 (or (eq 'TAG
(car (nth 1 tmp
)))
1832 (setcdr tmp
(cons (byte-compile-make-tag)
1834 (byte-compile-log-lap " %s [%s]\t-->\t%s <skip>"
1835 (car lap0
) tmp2
(nth 1 tmp3
))
1836 (setcar lap0
(nth 1 tmp3
))
1837 (setcdr lap0
(nth 1 tmp
)))
1838 (setq keep-going t
))
1840 ;; const goto-X ... X: goto-if-* --> whatever
1841 ;; const goto-X ... X: discard --> whatever
1843 ((and (eq (car lap0
) 'byte-constant
)
1844 (eq (car lap1
) 'byte-goto
)
1845 (memq (car (car (setq tmp
(cdr (memq (cdr lap1
) lap
)))))
1847 (cons 'byte-discard byte-conditional-ops
)))
1848 (not (eq lap1
(car tmp
))))
1849 (setq tmp2
(car tmp
))
1850 (cond ((when (consp (cdr lap0
))
1852 (if (null (car (cdr lap0
)))
1853 '(byte-goto-if-nil byte-goto-if-nil-else-pop
)
1854 '(byte-goto-if-not-nil
1855 byte-goto-if-not-nil-else-pop
))))
1856 (byte-compile-log-lap " %s goto [%s]\t-->\t%s %s"
1857 lap0 tmp2 lap0 tmp2
)
1858 (setcar lap1
(car tmp2
))
1859 (setcdr lap1
(cdr tmp2
))
1860 ;; Let next step fix the (const,goto-if*) sequence.
1861 (setq rest
(cons nil rest
))
1862 (setq keep-going t
))
1863 ((or (consp (cdr lap0
))
1864 (eq (car tmp2
) 'byte-discard
))
1865 ;; Jump one step further
1866 (byte-compile-log-lap
1867 " %s goto [%s]\t-->\t<deleted> goto <skip>"
1869 (or (eq 'TAG
(car (nth 1 tmp
)))
1870 (setcdr tmp
(cons (byte-compile-make-tag)
1872 (setcdr lap1
(car (cdr tmp
)))
1873 (setq lap
(delq lap0 lap
))
1874 (setq keep-going t
))))
1876 ;; X: varref-Y ... varset-Y goto-X -->
1877 ;; X: varref-Y Z: ... dup varset-Y goto-Z
1878 ;; (varset-X goto-BACK, BACK: varref-X --> copy the varref down.)
1879 ;; (This is so usual for while loops that it is worth handling).
1881 ;; Here again, we could do it for stack-ref/stack-set, but
1882 ;; that's replacing a stack-ref-Y with a stack-ref-0, which
1883 ;; is a very minor improvement (if any), at the cost of
1884 ;; more stack use and more byte-code. Let's not do it.
1886 ((and (eq (car lap1
) 'byte-varset
)
1887 (eq (car lap2
) 'byte-goto
)
1888 (not (memq (cdr lap2
) rest
)) ;Backwards jump
1889 (eq (car (car (setq tmp
(cdr (memq (cdr lap2
) lap
)))))
1891 (eq (cdr (car tmp
)) (cdr lap1
))
1892 (not (memq (car (cdr lap1
)) byte-boolean-vars
)))
1893 ;;(byte-compile-log-lap " Pulled %s to end of loop" (car tmp))
1894 (let ((newtag (byte-compile-make-tag)))
1895 (byte-compile-log-lap
1896 " %s: %s ... %s %s\t-->\t%s: %s %s: ... %s %s %s"
1897 (nth 1 (cdr lap2
)) (car tmp
)
1899 (nth 1 (cdr lap2
)) (car tmp
)
1900 (nth 1 newtag
) 'byte-dup lap1
1901 (cons 'byte-goto newtag
)
1903 (setcdr rest
(cons (cons 'byte-dup
0) (cdr rest
)))
1904 (setcdr tmp
(cons (setcdr lap2 newtag
) (cdr tmp
))))
1906 (setq keep-going t
))
1908 ;; goto-X Y: ... X: goto-if*-Y --> goto-if-not-*-X+1 Y:
1909 ;; (This can pull the loop test to the end of the loop)
1911 ((and (eq (car lap0
) 'byte-goto
)
1912 (eq (car lap1
) 'TAG
)
1914 (cdr (car (setq tmp
(cdr (memq (cdr lap0
) lap
))))))
1915 (memq (car (car tmp
))
1916 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
1917 byte-goto-if-nil-else-pop
)))
1918 ;; (byte-compile-log-lap " %s %s, %s %s --> moved conditional"
1919 ;; lap0 lap1 (cdr lap0) (car tmp))
1920 (let ((newtag (byte-compile-make-tag)))
1921 (byte-compile-log-lap
1922 "%s %s: ... %s: %s\t-->\t%s ... %s:"
1923 lap0
(nth 1 lap1
) (nth 1 (cdr lap0
)) (car tmp
)
1924 (cons (cdr (assq (car (car tmp
))
1925 '((byte-goto-if-nil . byte-goto-if-not-nil
)
1926 (byte-goto-if-not-nil . byte-goto-if-nil
)
1927 (byte-goto-if-nil-else-pop .
1928 byte-goto-if-not-nil-else-pop
)
1929 (byte-goto-if-not-nil-else-pop .
1930 byte-goto-if-nil-else-pop
))))
1935 (setcdr tmp
(cons (setcdr lap0 newtag
) (cdr tmp
)))
1936 (if (eq (car (car tmp
)) 'byte-goto-if-nil-else-pop
)
1937 ;; We can handle this case but not the -if-not-nil case,
1938 ;; because we won't know which non-nil constant to push.
1939 (setcdr rest
(cons (cons 'byte-constant
1940 (byte-compile-get-constant nil
))
1942 (setcar lap0
(nth 1 (memq (car (car tmp
))
1943 '(byte-goto-if-nil-else-pop
1944 byte-goto-if-not-nil
1946 byte-goto-if-not-nil
1947 byte-goto byte-goto
))))
1949 (setq keep-going t
))
1951 (setq rest
(cdr rest
)))
1954 ;; Rebuild byte-compile-constants / byte-compile-variables.
1955 ;; Simple optimizations that would inhibit other optimizations if they
1956 ;; were done in the optimizing loop, and optimizations which there is no
1957 ;; need to do more than once.
1958 (setq byte-compile-constants nil
1959 byte-compile-variables nil
)
1961 (byte-compile-log-lap " ---- final pass")
1963 (setq lap0
(car rest
)
1965 (if (memq (car lap0
) byte-constref-ops
)
1966 (if (memq (car lap0
) '(byte-constant byte-constant2
))
1967 (unless (memq (cdr lap0
) byte-compile-constants
)
1968 (setq byte-compile-constants
(cons (cdr lap0
)
1969 byte-compile-constants
)))
1970 (unless (memq (cdr lap0
) byte-compile-variables
)
1971 (setq byte-compile-variables
(cons (cdr lap0
)
1972 byte-compile-variables
)))))
1974 ;; const-C varset-X const-C --> const-C dup varset-X
1975 ;; const-C varbind-X const-C --> const-C dup varbind-X
1977 (and (eq (car lap0
) 'byte-constant
)
1978 (eq (car (nth 2 rest
)) 'byte-constant
)
1979 (eq (cdr lap0
) (cdr (nth 2 rest
)))
1980 (memq (car lap1
) '(byte-varbind byte-varset
)))
1981 (byte-compile-log-lap " %s %s %s\t-->\t%s dup %s"
1982 lap0 lap1 lap0 lap0 lap1
)
1983 (setcar (cdr (cdr rest
)) (cons (car lap1
) (cdr lap1
)))
1984 (setcar (cdr rest
) (cons 'byte-dup
0))
1987 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup
1988 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup
1990 ((memq (car lap0
) '(byte-constant byte-varref
))
1994 (while (eq 'byte-dup
(car (car (setq tmp
(cdr tmp
))))))
1995 (and (eq (cdr lap0
) (cdr (car tmp
)))
1996 (eq (car lap0
) (car (car tmp
)))))
1997 (setcar tmp
(cons 'byte-dup
0))
2000 (byte-compile-log-lap
2001 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0
)))
2003 ;; unbind-N unbind-M --> unbind-(N+M)
2005 ((and (eq 'byte-unbind
(car lap0
))
2006 (eq 'byte-unbind
(car lap1
)))
2007 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1
2009 (+ (cdr lap0
) (cdr lap1
))))
2010 (setq lap
(delq lap0 lap
))
2011 (setcdr lap1
(+ (cdr lap1
) (cdr lap0
))))
2014 ;; stack-set-M [discard/discardN ...] --> discardN-preserve-tos
2015 ;; stack-set-M [discard/discardN ...] --> discardN
2017 ((and (eq (car lap0
) 'byte-stack-set
)
2018 (memq (car lap1
) '(byte-discard byte-discardN
))
2020 ;; See if enough discard operations follow to expose or
2021 ;; destroy the value stored by the stack-set.
2022 (setq tmp
(cdr rest
))
2023 (setq tmp2
(1- (cdr lap0
)))
2025 (while (memq (car (car tmp
)) '(byte-discard byte-discardN
))
2027 (+ tmp3
(if (eq (car (car tmp
)) 'byte-discard
)
2030 (setq tmp
(cdr tmp
)))
2032 ;; Do the optimization.
2033 (setq lap
(delq lap0 lap
))
2036 ;; The value stored is the new TOS, so pop one more
2037 ;; value (to get rid of the old value) using the
2038 ;; TOS-preserving discard operator.
2039 'byte-discardN-preserve-tos
2040 ;; Otherwise, the value stored is lost, so just use a
2043 (setcdr lap1
(1+ tmp3
))
2044 (setcdr (cdr rest
) tmp
)
2045 (byte-compile-log-lap " %s [discard/discardN]...\t-->\t%s"
2049 ;; discard/discardN/discardN-preserve-tos-X discard/discardN-Y -->
2052 ((and (memq (car lap0
)
2053 '(byte-discard byte-discardN
2054 byte-discardN-preserve-tos
))
2055 (memq (car lap1
) '(byte-discard byte-discardN
)))
2056 (setq lap
(delq lap0 lap
))
2057 (byte-compile-log-lap
2058 " %s %s\t-->\t(discardN %s)"
2060 (+ (if (eq (car lap0
) 'byte-discard
) 1 (cdr lap0
))
2061 (if (eq (car lap1
) 'byte-discard
) 1 (cdr lap1
))))
2062 (setcdr lap1
(+ (if (eq (car lap0
) 'byte-discard
) 1 (cdr lap0
))
2063 (if (eq (car lap1
) 'byte-discard
) 1 (cdr lap1
))))
2064 (setcar lap1
'byte-discardN
))
2067 ;; discardN-preserve-tos-X discardN-preserve-tos-Y -->
2068 ;; discardN-preserve-tos-(X+Y)
2070 ((and (eq (car lap0
) 'byte-discardN-preserve-tos
)
2071 (eq (car lap1
) 'byte-discardN-preserve-tos
))
2072 (setq lap
(delq lap0 lap
))
2073 (setcdr lap1
(+ (cdr lap0
) (cdr lap1
)))
2074 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1
(car rest
)))
2077 ;; discardN-preserve-tos return --> return
2078 ;; dup return --> return
2079 ;; stack-set-N return --> return ; where N is TOS-1
2081 ((and (eq (car lap1
) 'byte-return
)
2082 (or (memq (car lap0
) '(byte-discardN-preserve-tos byte-dup
))
2083 (and (eq (car lap0
) 'byte-stack-set
)
2085 ;; The byte-code interpreter will pop the stack for us, so
2086 ;; we can just leave stuff on it.
2087 (setq lap
(delq lap0 lap
))
2088 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 lap1
))
2090 (setq rest
(cdr rest
)))
2091 (setq byte-compile-maxdepth
(+ byte-compile-maxdepth add-depth
)))
2097 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when this file compiles
2098 ;; itself, compile some of its most used recursive functions (at load time).
2101 (or (byte-code-function-p (symbol-function 'byte-optimize-form
))
2102 (assq 'byte-code
(symbol-function 'byte-optimize-form
))
2103 (let ((byte-optimize nil
)
2104 (byte-compile-warnings nil
))
2106 (or noninteractive
(message "compiling %s..." x
))
2108 (or noninteractive
(message "compiling %s...done" x
)))
2109 '(byte-optimize-form
2111 byte-optimize-predicate
2112 byte-optimize-binary-predicate
2113 ;; Inserted some more than necessary, to speed it up.
2114 byte-optimize-form-code-walker
2115 byte-optimize-lapcode
))))
2118 ;;; byte-opt.el ends here