1 ;;; byte-optimize.el --- the optimization passes of the emacs-lisp byte compiler.
3 ;;; Copyright (c) 1991, 1994 Free Software Foundation, Inc.
5 ;; Authors: Jamie Zawinski <jwz@jwz.org>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
7 ;; Martin Buchholz <martin@xemacs.org>
10 ;; This file is part of SXEmacs.
12 ;; SXEmacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; SXEmacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Synched up with: FSF 20.7 except where marked.
26 ;;; [[ Synched up with: FSF 20.7. ]]
27 ;;; DO NOT PUT IN AN INVALID SYNC MESSAGE WHEN YOU DO A PARTIAL SYNC. --ben
29 ;; BEGIN SYNC WITH 20.7.
33 ;; ========================================================================
34 ;; "No matter how hard you try, you can't make a racehorse out of a pig.
35 ;; You can, however, make a faster pig."
37 ;; Or, to put it another way, the emacs byte compiler is a VW Bug. This code
38 ;; makes it be a VW Bug with fuel injection and a turbocharger... You're
39 ;; still not going to make it go faster than 70 mph, but it might be easier
45 ;; (apply #'(lambda (x &rest y) ...) 1 (foo))
47 ;; maintain a list of functions known not to access any global variables
48 ;; (actually, give them a 'dynamically-safe property) and then
49 ;; (let ( v1 v2 ... vM vN ) <...dynamically-safe...> ) ==>
50 ;; (let ( v1 v2 ... vM ) vN <...dynamically-safe...> )
51 ;; by recursing on this, we might be able to eliminate the entire let.
52 ;; However certain variables should never have their bindings optimized
53 ;; away, because they affect everything.
54 ;; (put 'debug-on-error 'binding-is-magic t)
55 ;; (put 'debug-on-abort 'binding-is-magic t)
56 ;; (put 'debug-on-next-call 'binding-is-magic t)
57 ;; (put 'mocklisp-arguments 'binding-is-magic t)
58 ;; (put 'inhibit-quit 'binding-is-magic t)
59 ;; (put 'quit-flag 'binding-is-magic t)
60 ;; (put 't 'binding-is-magic t)
61 ;; (put 'nil 'binding-is-magic t)
63 ;; (put 'gc-cons-threshold 'binding-is-magic t)
64 ;; (put 'track-mouse 'binding-is-magic t)
67 ;; Simple defsubsts often produce forms like
68 ;; (let ((v1 (f1)) (v2 (f2)) ...)
70 ;; It would be nice if we could optimize this to
72 ;; but we can't unless FN is dynamically-safe (it might be dynamically
73 ;; referring to the bindings that the lambda arglist established.)
74 ;; One of the uncountable lossages introduced by dynamic scope...
76 ;; Maybe there should be a control-structure that says "turn on
77 ;; fast-and-loose type-assumptive optimizations here." Then when
78 ;; we see a form like (car foo) we can from then on assume that
79 ;; the variable foo is of type cons, and optimize based on that.
80 ;; But, this won't win much because of (you guessed it) dynamic
81 ;; scope. Anything down the stack could change the value.
82 ;; (Another reason it doesn't work is that it is perfectly valid
83 ;; to call car with a null argument.) A better approach might
84 ;; be to allow type-specification of the form
85 ;; (put 'foo 'arg-types '(float (list integer) dynamic))
86 ;; (put 'foo 'result-type 'bool)
87 ;; It should be possible to have these types checked to a certain
90 ;; collapse common subexpressions
92 ;; It would be nice if redundant sequences could be factored out as well,
93 ;; when they are known to have no side-effects:
94 ;; (list (+ a b c) (+ a b c)) --> a b add c add dup list-2
95 ;; but beware of traps like
96 ;; (cons (list x y) (list x y))
98 ;; Tail-recursion elimination is not really possible in Emacs Lisp.
99 ;; Tail-recursion elimination is almost always impossible when all variables
100 ;; have dynamic scope, but given that the "return" byteop requires the
101 ;; binding stack to be empty (rather than emptying it itself), there can be
102 ;; no truly tail-recursive Emacs Lisp functions that take any arguments or
103 ;; make any bindings.
105 ;; Here is an example of an Emacs Lisp function which could safely be
106 ;; byte-compiled tail-recursively:
108 ;; (defun tail-map (fn list)
110 ;; (funcall fn (car list))
111 ;; (tail-map fn (cdr list)))))
113 ;; However, if there was even a single let-binding around the COND,
114 ;; it could not be byte-compiled, because there would be an "unbind"
115 ;; byte-op between the final "call" and "return." Adding a
116 ;; Bunbind_all byteop would fix this.
118 ;; (defun foo (x y z) ... (foo a b c))
119 ;; ... (const foo) (varref a) (varref b) (varref c) (call 3) END: (return)
120 ;; ... (varref a) (varbind x) (varref b) (varbind y) (varref c) (varbind z) (goto 0) END: (unbind-all) (return)
121 ;; ... (varref a) (varset x) (varref b) (varset y) (varref c) (varset z) (goto 0) END: (return)
123 ;; this also can be considered tail recursion:
125 ;; ... (const foo) (varref a) (call 1) (goto X) ... X: (return)
126 ;; could generalize this by doing the optimization
127 ;; (goto X) ... X: (return) --> (return)
129 ;; But this doesn't solve all of the problems: although by doing tail-
130 ;; recursion elimination in this way, the call-stack does not grow, the
131 ;; binding-stack would grow with each recursive step, and would eventually
132 ;; overflow. I don't believe there is any way around this without lexical
135 ;; Wouldn't it be nice if Emacs Lisp had lexical scope.
137 ;; Idea: the form (lexical-scope) in a file means that the file may be
138 ;; compiled lexically. This proclamation is file-local. Then, within
139 ;; that file, "let" would establish lexical bindings, and "let-dynamic"
140 ;; would do things the old way. (Or we could use CL "declare" forms.)
141 ;; We'd have to notice defvars and defconsts, since those variables should
142 ;; always be dynamic, and attempting to do a lexical binding of them
143 ;; should simply do a dynamic binding instead.
144 ;; But! We need to know about variables that were not necessarily defvarred
145 ;; in the file being compiled (doing a boundp check isn't good enough.)
146 ;; Fdefvar() would have to be modified to add something to the plist.
148 ;; A major disadvantage of this scheme is that the interpreter and compiler
149 ;; would have different semantics for files compiled with (dynamic-scope).
150 ;; Since this would be a file-local optimization, there would be no way to
151 ;; modify the interpreter to obey this (unless the loader was hacked
152 ;; in some grody way, but that's a really bad idea.)
154 ;; HA! RMS removed the following paragraph from his version of
157 ;; Really the Right Thing is to make lexical scope the default across
158 ;; the board, in the interpreter and compiler, and just FIX all of
159 ;; the code that relies on dynamic scope of non-defvarred variables.
161 ;; Other things to consider:
163 ;; Associative math should recognize subcalls to identical function:
164 ;;(disassemble #'(lambda (x) (+ (+ (foo) 1) (+ (bar) 2))))
165 ;; This should generate the same as (1+ x) and (1- x)
167 ;;(disassemble #'(lambda (x) (cons (+ x 1) (- x 1))))
168 ;; An awful lot of functions always return a non-nil value. If they're
169 ;; error free also they may act as true-constants.
171 ;;(disassemble #'(lambda (x) (and (point) (foo))))
173 ;; - all but one arguments to a function are constant
174 ;; - the non-constant argument is an if-expression (cond-expression?)
175 ;; then the outer function can be distributed. If the guarding
176 ;; condition is side-effect-free [assignment-free] then the other
177 ;; arguments may be any expressions. Since, however, the code size
178 ;; can increase this way they should be "simple". Compare:
180 ;;(disassemble #'(lambda (x) (eq (if (point) 'a 'b) 'c)))
181 ;;(disassemble #'(lambda (x) (if (point) (eq 'a 'c) (eq 'b 'c))))
183 ;; (car (cons A B)) -> (prog1 A B)
184 ;;(disassemble #'(lambda (x) (car (cons (foo) 42))))
186 ;; (cdr (cons A B)) -> (progn A B)
187 ;;(disassemble #'(lambda (x) (cdr (cons 42 (foo)))))
189 ;; (car (list A B ...)) -> (prog1 A ... B)
190 ;;(disassemble #'(lambda (x) (car (list (foo) 42 (bar)))))
192 ;; (cdr (list A B ...)) -> (progn A (list B ...))
193 ;;(disassemble #'(lambda (x) (cdr (list 42 (foo) (bar)))))
198 (require 'byte-compile "bytecomp")
200 (defun byte-compile-log-lap-1 (format &rest args)
201 (if (aref byte-code-vector 0)
202 (error "The old version of the disassembler is loaded. Reload new-bytecomp as well."))
204 (apply 'format format
208 (if (not (consp arg))
209 (if (and (symbolp arg)
210 (string-match "^byte-" (symbol-name arg)))
211 (intern (substring (symbol-name arg) 5))
213 (if (integerp (setq c (car arg)))
214 (error "non-symbolic byte-op %s" c))
217 (setq a (cond ((memq c byte-goto-ops)
218 (car (cdr (cdr arg))))
219 ((memq c byte-constref-ops)
222 (setq c (symbol-name c))
223 (if (string-match "^byte-." c)
224 (setq c (intern (substring c 5)))))
225 (if (eq c 'constant) (setq c 'const))
226 (if (and (eq (cdr arg) 0)
227 (not (memq c '(unbind call const))))
229 (format "(%s %s)" c a))))
232 (defmacro byte-compile-log-lap (format-string &rest args)
234 '(memq byte-optimize-log '(t byte))
235 (cons 'byte-compile-log-lap-1
236 (cons format-string args))))
239 ;;; byte-compile optimizers to support inlining
241 (put 'inline 'byte-optimizer 'byte-optimize-inline-handler)
243 (defun byte-optimize-inline-handler (form)
244 "byte-optimize-handler for the `inline' special-form."
249 (let ((fn (car-safe sexp)))
250 (if (and (symbolp fn)
251 (or (cdr (assq fn byte-compile-function-environment))
253 (not (or (cdr (assq fn byte-compile-macro-environment))
254 (and (consp (setq fn (symbol-function fn)))
255 (eq (car fn) 'macro))
257 (byte-compile-inline-expand sexp)
262 ;; Splice the given lap code into the current instruction stream.
263 ;; If it has any labels in it, you're responsible for making sure there
264 ;; are no collisions, and that byte-compile-tag-number is reasonable
265 ;; after this is spliced in. The provided list is destroyed.
266 (defun byte-inline-lapcode (lap)
267 (setq byte-compile-output (nconc (nreverse lap) byte-compile-output)))
270 (defun byte-compile-inline-expand (form)
271 (let* ((name (car form))
272 (fn (or (cdr (assq name byte-compile-function-environment))
273 (and (fboundp name) (symbol-function name)))))
276 (byte-compile-warn "attempt to inline %s before it was defined" name)
279 (if (and (consp fn) (eq (car fn) 'autoload))
282 (setq fn (or (cdr (assq name byte-compile-function-environment))
283 (and (fboundp name) (symbol-function name))))))
284 (if (and (consp fn) (eq (car fn) 'autoload))
285 (error "file \"%s\" didn't define \"%s\"" (nth 1 fn) name))
287 (byte-compile-inline-expand (cons fn (cdr form)))
288 (if (compiled-function-p fn)
291 (cons (list 'lambda (compiled-function-arglist fn)
293 (compiled-function-instructions fn)
294 (compiled-function-constants fn)
295 (compiled-function-stack-depth fn)))
297 (if (eq (car-safe fn) 'lambda)
299 ;; Give up on inlining.
302 ;;; ((lambda ...) ...)
304 (defun byte-compile-unfold-lambda (form &optional name)
305 (or name (setq name "anonymous lambda"))
306 (let ((lambda (car form))
308 (if (compiled-function-p lambda)
309 (setq lambda (list 'lambda (compiled-function-arglist lambda)
311 (compiled-function-instructions lambda)
312 (compiled-function-constants lambda)
313 (compiled-function-stack-depth lambda)))))
314 (let ((arglist (nth 1 lambda))
315 (body (cdr (cdr lambda)))
318 (if (and (stringp (car body)) (cdr body))
319 (setq body (cdr body)))
320 (if (and (consp (car body)) (eq 'interactive (car (car body))))
321 (setq body (cdr body)))
323 (cond ((eq (car arglist) '&optional)
324 ;; ok, I'll let this slide because funcall_lambda() does...
325 ;; (if optionalp (error "multiple &optional keywords in %s" name))
326 (if restp (error "&optional found after &rest in %s" name))
327 (if (null (cdr arglist))
328 (error "nothing after &optional in %s" name))
330 ((eq (car arglist) '&rest)
331 ;; ...but it is by no stretch of the imagination a reasonable
332 ;; thing that funcall_lambda() allows (&rest x y) and
333 ;; (&rest x &optional y) in arglists.
334 (if (null (cdr arglist))
335 (error "nothing after &rest in %s" name))
336 (if (cdr (cdr arglist))
337 (error "multiple vars after &rest in %s" name))
340 (setq bindings (cons (list (car arglist)
341 (and values (cons 'list values)))
344 ((and (not optionalp) (null values))
345 (byte-compile-warn "attempt to open-code %s with too few arguments" name)
346 (setq arglist nil values 'too-few))
348 (setq bindings (cons (list (car arglist) (car values))
350 values (cdr values))))
351 (setq arglist (cdr arglist)))
354 (or (eq values 'too-few)
356 "attempt to open-code %s with too many arguments" name))
358 ;; This line, introduced in v1.10, can cause an infinite
359 ;; recursion when inlining recursive defsubst's
360 ; (setq body (mapcar 'byte-optimize-form body))
363 (cons 'let (cons (nreverse bindings) body))
364 (cons 'progn body))))
365 (byte-compile-log " %s\t==>\t%s" form newform)
369 ;;; implementing source-level optimizers
371 (defun byte-optimize-form-code-walker (form for-effect)
373 ;; For normal function calls, We can just mapcar the optimizer the cdr. But
374 ;; we need to have special knowledge of the syntax of the special forms
375 ;; like let and defun (that's why they're special forms :-). (Actually,
376 ;; the important aspect is that they are subrs that don't evaluate all of
379 (let ((fn (car-safe form))
381 (cond ((not (consp form))
382 (if (not (and for-effect
383 (or byte-compile-delete-errors
389 (byte-compile-warn "malformed quote form: %s"
390 (prin1-to-string form)))
391 ;; map (quote nil) to nil to simplify optimizer logic.
392 ;; map quoted constants to nil if for-effect (just because).
396 ((or (compiled-function-p fn)
397 (eq 'lambda (car-safe fn)))
398 (byte-compile-unfold-lambda form))
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.
407 (if (symbolp binding)
409 (if (cdr (cdr binding))
410 (byte-compile-warn "malformed let binding: %s"
411 (prin1-to-string binding)))
413 (byte-optimize-form (nth 1 binding) nil))))
415 (byte-optimize-body (cdr (cdr form)) for-effect))))
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>
432 (setq tmp (byte-optimize-body (cdr form) for-effect))
433 (if (cdr tmp) (cons 'progn tmp) (car tmp)))
434 (byte-optimize-form (nth 1 form) for-effect)))
438 (cons (byte-optimize-form (nth 1 form) for-effect)
439 (byte-optimize-body (cdr (cdr form)) t)))
440 (byte-optimize-form (nth 1 form) for-effect)))
443 (cons (byte-optimize-form (nth 1 form) t)
444 (cons (byte-optimize-form (nth 2 form) for-effect)
445 (byte-optimize-body (cdr (cdr (cdr form))) t)))))
447 ((memq fn '(save-excursion save-restriction save-current-buffer))
448 ;; those subrs which have an implicit progn; it's not quite good
449 ;; enough to treat these like normal function calls.
450 ;; This can turn (save-excursion ...) into (save-excursion) which
451 ;; will be optimized away in the lap-optimize pass.
452 (cons fn (byte-optimize-body (cdr form) for-effect)))
454 ((eq fn 'with-output-to-temp-buffer)
455 ;; this is just like the above, except for the first argument.
458 (byte-optimize-form (nth 1 form) nil)
459 (byte-optimize-body (cdr (cdr form)) for-effect))))
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 ;; Now optimize the rest of the forms. We need the return
485 ;; values. We already did the car.
487 (mapcar 'byte-optimize-form (cdr backwards))))
488 (cons fn (nreverse backwards)))
489 (cons fn (mapcar 'byte-optimize-form (cdr form)))))
491 ((eq fn 'interactive)
492 (byte-compile-warn "misplaced interactive spec: %s"
493 (prin1-to-string form))
496 ((memq fn '(defun defmacro function
497 condition-case save-window-excursion))
498 ;; These forms are compiled as constants or by breaking out
499 ;; all the subexpressions and compiling them separately.
502 ((eq fn 'unwind-protect)
503 ;; the "protected" part of an unwind-protect is compiled (and thus
504 ;; optimized) as a top-level form, so don't do it here. But the
505 ;; non-protected part has the same for-effect status as the
506 ;; unwind-protect itself. (The protected part is always for effect,
507 ;; but that isn't handled properly yet.)
509 (cons (byte-optimize-form (nth 1 form) for-effect)
513 ;; the body of a catch is compiled (and thus optimized) as a
514 ;; top-level form, so don't do it here. The tag is never
515 ;; for-effect. The body should have the same for-effect status
516 ;; as the catch form itself, but that isn't handled properly yet.
518 (cons (byte-optimize-form (nth 1 form) nil)
521 ;; If optimization is on, this is the only place that macros are
522 ;; expanded. If optimization is off, then macroexpansion happens
523 ;; in byte-compile-form. Otherwise, the macros are already expanded
524 ;; by the time that is reached.
526 (setq form (macroexpand form
527 byte-compile-macro-environment))))
528 (byte-optimize-form form for-effect))
530 ;; Support compiler macros as in cl.el.
531 ((and (fboundp 'compiler-macroexpand)
532 (symbolp (car-safe form))
533 (get (car-safe form) 'cl-compiler-macro)
535 (setq form (compiler-macroexpand form)))))
536 (byte-optimize-form form for-effect))
539 (or (eq 'mocklisp (car-safe fn)) ; ha!
540 (byte-compile-warn "%s is a malformed function"
541 (prin1-to-string fn)))
544 ((and for-effect (setq tmp (get fn 'side-effect-free))
545 (or byte-compile-delete-errors
548 (byte-compile-warn "%s called for effect"
549 (prin1-to-string form))
551 (byte-compile-log " %s called for effect; deleted" fn)
552 ;; appending a nil here might not be necessary, but it can't hurt.
554 (cons 'progn (append (cdr form) '(nil))) t))
557 ;; Otherwise, no args can be considered to be for-effect,
558 ;; even if the called function is for-effect, because we
559 ;; don't know anything about that function.
560 (cons fn (mapcar 'byte-optimize-form (cdr form)))))))
563 (defun byte-optimize-form (form &optional for-effect)
564 "The source-level pass of the optimizer."
566 ;; First, optimize all sub-forms of this one.
567 (setq form (byte-optimize-form-code-walker form for-effect))
569 ;; After optimizing all subforms, optimize this form until it doesn't
570 ;; optimize any further. This means that some forms will be passed through
571 ;; the optimizer many times, but that's necessary to make the for-effect
572 ;; processing do as much as possible.
575 (if (and (consp form)
578 ;; we don't have any of these yet, but we might.
579 (setq opt (get (car form) 'byte-for-effect-optimizer)))
580 (setq opt (get (car form) 'byte-optimizer)))
581 (not (eq form (setq new (funcall opt form)))))
583 ;; (if (equal form new) (error "bogus optimizer -- %s" opt))
584 (byte-compile-log " %s\t==>\t%s" form new)
585 (setq new (byte-optimize-form new for-effect))
590 (defun byte-optimize-body (forms all-for-effect)
591 ;; Optimize the cdr of a progn or implicit progn; `forms' is a list of
592 ;; forms, all but the last of which are optimized with the assumption that
593 ;; they are being called for effect. The last is for-effect as well if
594 ;; all-for-effect is true. Returns a new list of forms.
599 (setq fe (or all-for-effect (cdr rest)))
600 (setq new (and (car rest) (byte-optimize-form (car rest) fe)))
601 (if (or new (not fe))
602 (setq result (cons new result)))
603 (setq rest (cdr rest)))
607 ;;; some source-level optimizers
609 ;;; when writing optimizers, be VERY careful that the optimizer returns
610 ;;; something not EQ to its argument if and ONLY if it has made a change.
611 ;;; This implies that you cannot simply destructively modify the list;
612 ;;; you must return something not EQ to it if you make an optimization.
614 ;;; It is now safe to optimize code such that it introduces new bindings.
616 ;; I'd like this to be a defsubst, but let's not be self-referential...
617 (defmacro byte-compile-trueconstp (form)
618 ;; Returns non-nil if FORM is a non-nil constant.
619 `(cond ((consp ,form) (eq (car ,form) 'quote))
620 ((not (symbolp ,form)))
624 ;; If the function is being called with constant numeric args,
625 ;; evaluate as much as possible at compile-time. This optimizer
626 ;; assumes that the function is associative, like + or *.
627 (defun byte-optimize-associative-math (form)
632 (if (numberp (car rest))
633 (setq constants (cons (car rest) constants))
634 (setq args (cons (car rest) args)))
635 (setq rest (cdr rest)))
639 (apply (car form) constants)
641 (cons (car form) (nreverse args))
643 (apply (car form) constants))
646 ;; If the function is being called with constant numeric args,
647 ;; evaluate as much as possible at compile-time. This optimizer
648 ;; assumes that the function satisfies
649 ;; (op x1 x2 ... xn) == (op ...(op (op x1 x2) x3) ...xn)
651 (defun byte-optimize-nonassociative-math (form)
652 (if (or (not (numberp (car (cdr form))))
653 (not (numberp (car (cdr (cdr form))))))
655 (let ((constant (car (cdr form)))
656 (rest (cdr (cdr form))))
657 (while (numberp (car rest))
658 (setq constant (funcall (car form) constant (car rest))
661 (cons (car form) (cons constant rest))
664 ;;(defun byte-optimize-associative-two-args-math (form)
665 ;; (setq form (byte-optimize-associative-math form))
667 ;; (byte-optimize-two-args-left form)
670 ;;(defun byte-optimize-nonassociative-two-args-math (form)
671 ;; (setq form (byte-optimize-nonassociative-math form))
673 ;; (byte-optimize-two-args-right form)
676 ;; jwz: (byte-optimize-approx-equal 0.0 0.0) was returning nil
677 ;; in xemacs 19.15 because it used < instead of <=.
678 (defun byte-optimize-approx-equal (x y)
679 (<= (* (abs (- x y)) 100) (abs (+ x y))))
681 ;; Collect all the constants from FORM, after the STARTth arg,
682 ;; and apply FUN to them to make one argument at the end.
683 ;; For functions that can handle floats, that optimization
684 ;; can be incorrect because reordering can cause an overflow
685 ;; that would otherwise be avoided by encountering an arg that is a float.
686 ;; We avoid this problem by (1) not moving float constants and
687 ;; (2) not moving anything if it would cause an overflow.
689 ;; This idea is going to be extended in case we have some
690 ;; multi-precision library on-board. Overflows are rare in that case,
691 ;; but since there is no distinguishing syntax for mpfr, mpf and
692 ;; emacs-floats, we will have problems to re-identify a printed real
693 ;; number representation. The worst thing that may happen is to lose
694 ;; precision, that is why we attempt to treat any incoming real number
695 ;; as bigfr, if provided, we fallback to bigf, if provided, and if
696 ;; that does not help we fallback to float.
699 ;; Byte-compiled code with special number types is not readable by
700 ;; SXEmacsen which do not have an mp spine.
701 ;; Therefore always tag their usage using (featurep 'ent)
704 (defun byte-optimize-delay-constants-math (form start fun)
705 ;; Merge all FORM's constants from number START, call FUN on them
706 ;; and put the result at the end.
707 (let ((rest (nthcdr (1- start) form))
709 ;; t means we must check for overflow.
710 (overflow (memq fun '(+ *))))
711 (while (cdr (setq rest (cdr rest)))
712 (if (if (featurep 'ent)
714 (integerp (car rest)))
716 (setq form (copy-sequence form)
717 rest (nthcdr (1- start) form))
718 (while (setq rest (cdr rest))
719 (cond ((and (featurep 'ent)
720 (rationalp (car rest)))
721 (setq constants (cons (car rest) constants))
723 ((integerp (car rest))
724 (setq constants (cons (car rest) constants))
736 ((featurep 'lisp-float-type)
739 ;; shit ... what to do now?
740 ;;(segmentation-fault)
744 ((and (or (featurep 'bigc)
746 (complexp (car rest)))
747 (setq constants (cons (car rest) constants))
749 ((and (featurep 'resclass)
750 (declare-fboundp (residue-class-p (car rest))))
751 (setq constants (cons (car rest) constants))
753 ;; If necessary, check now for overflow
754 ;; that might be caused by reordering.
756 ;; We have overflow if the result of doing the arithmetic
757 ;; on floats is not even close to the result
758 ;; of doing it on integers.
759 (not (featurep '(or bigz bigq bigf bigfr bigc bigg resclass)))
760 ;; This assumption, of course, is not valid if we
762 (not (byte-optimize-approx-equal
763 (apply fun (mapcar 'float constants))
764 (float (apply fun constants)))))
766 (setq form (nconc (delq nil form)
767 (list (apply fun (nreverse constants)))))))))
770 ;; END SYNC WITH 20.7.
772 ;;; It is not safe to optimize calls to arithmetic ops with one arg
773 ;;; away entirely (actually, it would be safe if we know the sole arg
774 ;;; is not a marker or if it appears in other arithmetic).
776 ;;; But this degree of paranoia is normally unjustified, so optimize unless
777 ;;; the user has done (declaim (optimize (safety 3))). See bytecomp.el.
779 (defun byte-optimize-plus (form)
780 (byte-optimize-predicate (byte-optimize-delay-constants-math form 1 '+)))
782 (defun byte-optimize-multiply (form)
783 (setq form (byte-optimize-delay-constants-math form 1 '*))
784 ;; If there is a constant integer in FORM, it is now the last element.
786 (case (car (last form))
787 ;; (* x y 0) --> (progn x y 0)
788 (0 (cons 'progn (cdr form)))
789 (t (byte-optimize-predicate form))))
791 (defun byte-optimize-minus (form)
792 ;; Put constants at the end, except the first arg.
793 (setq form (byte-optimize-delay-constants-math form 2 '+))
794 ;; Now only the first and last args can be integers.
795 (let ((last (car (last (nthcdr 3 form)))))
797 ;; If form is (- CONST foo... CONST), merge first and last.
798 ((and (numberp (nth 1 form)) (numberp last))
799 (decf (nth 1 form) last)
808 (3 `(- ,(nth 2 form)))
809 ;; (- 0 x y ...) --> (- (- x) y ...)
810 (t `(- (- ,(nth 2 form)) ,@(nthcdr 3 form)))))
812 (t (byte-optimize-predicate form)))))
814 (defun byte-optimize-divide (form)
815 ;; Put constants at the end, except the first arg.
816 (setq form (byte-optimize-delay-constants-math form 2 '*))
817 ;; Now only the first and last args can be integers.
818 (let ((last (car (last (nthcdr 3 form)))))
820 ;; If form is (/ CONST foo... CONST), merge first and last.
821 ((and (numberp (nth 1 form)) (numberp last))
824 (cons (/ (nth 1 form) last)
825 (butlast (cdr (cdr form)))))
828 ;; (/ 0 x y) --> (progn x y 0)
830 (append '(progn) (cdr (cdr form)) '(0)))
832 ;; We don't have to check for divide-by-zero because `/' does.
833 (t (byte-optimize-predicate form)))))
835 ;; BEGIN SYNC WITH 20.7.
837 (defun byte-optimize-logmumble (form)
838 (setq form (byte-optimize-delay-constants-math form 1 (car form)))
839 (byte-optimize-predicate
841 (setq form (if (eq (car form) 'logand)
842 (cons 'progn (cdr form))
843 (delq 0 (copy-sequence form)))))
844 ((and (eq (car-safe form) 'logior)
846 (cons 'progn (cdr form)))
850 (defun byte-optimize-binary-predicate (form)
851 (if (byte-compile-constp (nth 1 form))
852 (if (byte-compile-constp (nth 2 form))
854 (list 'quote (eval form))
856 ;; This can enable some lapcode optimizations.
857 (list (car form) (nth 2 form) (nth 1 form)))
860 (defun byte-optimize-predicate (form)
864 (setq ok (byte-compile-constp (car rest))
868 (list 'quote (eval form))
870 (byte-compile-warn "evaluating %s: %s" form err)
874 (defun byte-optimize-identity (form)
875 (if (and (cdr form) (null (cdr (cdr form))))
877 (byte-compile-warn "identity called with %d arg%s, but requires 1"
879 (if (= 1 (length (cdr form))) "" "s"))
882 (defun byte-optimize-car (form)
883 (let ((arg (cadr form)))
885 ((and (byte-compile-trueconstp arg)
886 (not (and (consp arg)
887 (eq (car arg) 'quote)
888 (listp (cadr arg)))))
890 "taking car of a constant: %s" arg)
892 ((and (eq (car-safe arg) 'cons)
894 `(prog1 ,(nth 1 arg) ,(nth 2 arg)))
895 ((eq (car-safe arg) 'list)
896 `(prog1 ,@(cdr arg)))
898 (byte-optimize-predicate form)))))
900 (defun byte-optimize-cdr (form)
901 (let ((arg (cadr form)))
903 ((and (byte-compile-trueconstp arg)
904 (not (and (consp arg)
905 (eq (car arg) 'quote)
906 (listp (cadr arg)))))
908 "taking cdr of a constant: %s" arg)
910 ((and (eq (car-safe arg) 'cons)
912 `(progn ,(nth 1 arg) ,(nth 2 arg)))
913 ((eq (car-safe arg) 'list)
914 (if (> (length arg) 2)
915 `(progn ,(cadr arg) (list ,@(cddr arg)))
918 (byte-optimize-predicate form)))))
920 (put 'identity 'byte-optimizer 'byte-optimize-identity)
922 (put '+ 'byte-optimizer 'byte-optimize-plus)
923 (put '* 'byte-optimizer 'byte-optimize-multiply)
924 (put '- 'byte-optimizer 'byte-optimize-minus)
925 (put '/ 'byte-optimizer 'byte-optimize-divide)
926 (put '% 'byte-optimizer 'byte-optimize-predicate)
927 (put 'max 'byte-optimizer 'byte-optimize-associative-math)
928 (put 'min 'byte-optimizer 'byte-optimize-associative-math)
930 (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate)
931 (put 'eql 'byte-optimizer 'byte-optimize-binary-predicate)
932 (put 'equal 'byte-optimizer 'byte-optimize-binary-predicate)
933 (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate)
934 (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate)
936 (put '= 'byte-optimizer 'byte-optimize-predicate)
937 (put '< 'byte-optimizer 'byte-optimize-predicate)
938 (put '> 'byte-optimizer 'byte-optimize-predicate)
939 (put '<= 'byte-optimizer 'byte-optimize-predicate)
940 (put '>= 'byte-optimizer 'byte-optimize-predicate)
941 (put '1+ 'byte-optimizer 'byte-optimize-predicate)
942 (put '1- 'byte-optimizer 'byte-optimize-predicate)
943 (put 'not 'byte-optimizer 'byte-optimize-predicate)
944 (put 'null 'byte-optimizer 'byte-optimize-predicate)
945 (put 'memq 'byte-optimizer 'byte-optimize-predicate)
946 (put 'consp 'byte-optimizer 'byte-optimize-predicate)
947 (put 'listp 'byte-optimizer 'byte-optimize-predicate)
948 (put 'symbolp 'byte-optimizer 'byte-optimize-predicate)
949 (put 'stringp 'byte-optimizer 'byte-optimize-predicate)
950 (put 'string< 'byte-optimizer 'byte-optimize-predicate)
951 (put 'string-lessp 'byte-optimizer 'byte-optimize-predicate)
952 (put 'length 'byte-optimizer 'byte-optimize-predicate)
954 (put 'logand 'byte-optimizer 'byte-optimize-logmumble)
955 (put 'logior 'byte-optimizer 'byte-optimize-logmumble)
956 (put 'logxor 'byte-optimizer 'byte-optimize-logmumble)
957 (put 'lognot 'byte-optimizer 'byte-optimize-predicate)
959 (put 'car 'byte-optimizer 'byte-optimize-car)
960 (put 'cdr 'byte-optimizer 'byte-optimize-cdr)
961 (put 'car-safe 'byte-optimizer 'byte-optimize-predicate)
962 (put 'cdr-safe 'byte-optimizer 'byte-optimize-predicate)
965 ;; I'm not convinced that this is necessary. Doesn't the optimizer loop
966 ;; take care of this? - Jamie
967 ;; I think this may some times be necessary to reduce eg. (quote 5) to 5,
968 ;; so arithmetic optimizers recognize the numeric constant. - Hallvard
969 (put 'quote 'byte-optimizer 'byte-optimize-quote)
970 (defun byte-optimize-quote (form)
971 (if (or (consp (nth 1 form))
972 (and (symbolp (nth 1 form))
974 (not (keywordp (nth 1 form)))
975 (not (memq (nth 1 form) '(nil t)))))
979 (defun byte-optimize-zerop (form)
980 (cond ((numberp (nth 1 form))
983 ;; we cannot compare to 0 anymore, since there are coercion
984 ;; issues and even non-comparable types
986 (byte-compile-delete-errors
987 (list '= (nth 1 form) 0))
990 (put 'zerop 'byte-optimizer 'byte-optimize-zerop)
992 (defun byte-optimize-and (form)
993 ;; Simplify if less than 2 args.
994 ;; if there is a literal nil in the args to `and', throw it and following
995 ;; forms away, and surround the `and' with (progn ... nil).
996 (cond ((null (cdr form)))
1000 (prog1 (setq form (copy-sequence form))
1002 (setq form (cdr form)))
1005 ((null (cdr (cdr form)))
1007 ((byte-optimize-predicate form))))
1009 (defun byte-optimize-or (form)
1010 ;; Throw away nil's, and simplify if less than 2 args.
1011 ;; If there is a literal non-nil constant in the args to `or', throw away all
1014 (setq form (delq nil (copy-sequence form))))
1016 (while (cdr (setq rest (cdr rest)))
1017 (if (byte-compile-trueconstp (car rest))
1018 (setq form (copy-sequence form)
1019 rest (setcdr (memq (car rest) form) nil))))
1020 (if (cdr (cdr form))
1021 (byte-optimize-predicate form)
1024 ;; END SYNC WITH 20.7.
1026 ;;; For the byte optimizer, `cond' is just overly sweet syntactic sugar.
1027 ;;; So we rewrite (cond ...) in terms of `if' and `or',
1028 ;;; which are easier to optimize.
1029 (defun byte-optimize-cond (form)
1030 (byte-optimize-cond-1 (cdr form)))
1032 (defun byte-optimize-cond-1 (clauses)
1034 ((null clauses) nil)
1035 ((consp (car clauses))
1037 (case (length (car clauses))
1038 (1 `(or ,(nth 0 (car clauses))))
1039 (2 `(if ,(nth 0 (car clauses)) ,(nth 1 (car clauses))))
1040 (t `(if ,(nth 0 (car clauses)) (progn ,@(cdr (car clauses))))))
1041 (when (cdr clauses) (list (byte-optimize-cond-1 (cdr clauses))))))
1042 (t (error "malformed cond clause %s" (car clauses)))))
1044 ;; BEGIN SYNC WITH 20.7.
1046 (defun byte-optimize-if (form)
1047 ;; (if <true-constant> <then> <else...>) ==> <then>
1048 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>)
1049 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>))
1050 ;; (if <test> <then> nil) ==> (if <test> <then>)
1051 (let ((clause (nth 1 form)))
1052 (cond ((byte-compile-trueconstp clause)
1056 (cons 'progn (nthcdr 3 form))
1059 (if (equal '(nil) (nthcdr 3 form))
1060 (list 'if clause (nth 2 form))
1062 ((or (nth 3 form) (nthcdr 4 form))
1064 ;; Don't make a double negative;
1065 ;; instead, take away the one that is there.
1066 (if (and (consp clause) (memq (car clause) '(not null))
1067 (= (length clause) 2)) ; (not xxxx) or (not (xxxx))
1071 (cons 'progn (nthcdr 3 form))
1074 (list 'progn clause nil)))))
1076 (defun byte-optimize-while (form)
1080 (put 'and 'byte-optimizer 'byte-optimize-and)
1081 (put 'or 'byte-optimizer 'byte-optimize-or)
1082 (put 'cond 'byte-optimizer 'byte-optimize-cond)
1083 (put 'if 'byte-optimizer 'byte-optimize-if)
1084 (put 'while 'byte-optimizer 'byte-optimize-while)
1086 ;; The supply of bytecodes is small and constrained by backward compatibility.
1087 ;; Several functions have byte-coded versions and hence are very efficient.
1088 ;; Related functions which can be expressed in terms of the byte-coded
1089 ;; ones should be transformed into bytecoded calls for efficiency.
1090 ;; This is especially the case for functions with a backward- and
1091 ;; forward- version, but with a bytecode only for the forward one.
1093 ;; Some programmers have hand-optimized calls like (backward-char)
1094 ;; into the call (forward-char -1).
1095 ;; But it's so much nicer for the byte-compiler to do this automatically!
1097 ;; (char-before) ==> (char-after (1- (point)))
1098 (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
1099 (defun byte-optimize-char-before (form)
1102 ((null (nth 1 form))
1104 ((equal '(point) (nth 1 form))
1106 (t `(1- (or ,(nth 1 form) (point)))))
1107 ,@(cdr (cdr form))))
1109 ;; (backward-char n) ==> (forward-char (- n))
1110 (put 'backward-char 'byte-optimizer 'byte-optimize-backward-char)
1111 (defun byte-optimize-backward-char (form)
1113 ,(typecase (nth 1 form)
1115 (integer (- (nth 1 form)))
1116 (t `(- (or ,(nth 1 form) 1))))
1117 ,@(cdr (cdr form))))
1119 ;; (backward-word n) ==> (forward-word (- n))
1120 (put 'backward-word 'byte-optimizer 'byte-optimize-backward-word)
1121 (defun byte-optimize-backward-word (form)
1123 ,(typecase (nth 1 form)
1125 (integer (- (nth 1 form)))
1126 (t `(- (or ,(nth 1 form) 1))))
1127 ,@(cdr (cdr form))))
1129 ;; The following would be a valid optimization of the above kind, but
1130 ;; the gain in performance is very small, since the saved funcall is
1131 ;; counterbalanced by the necessity of adding a bytecode for (point).
1133 ;; Also, users are more likely to have modified the behavior of
1134 ;; delete-char via advice or some similar mechanism. This is much
1135 ;; less of a problem for the previous functions because it wouldn't
1136 ;; make sense to modify the behaviour of `backward-char' without also
1137 ;; modifying `forward-char', for example.
1139 ;; (delete-char n) ==> (delete-region (point) (+ (point) n))
1140 ;; (put 'delete-char 'byte-optimizer 'byte-optimize-delete-char)
1141 ;; (defun byte-optimize-delete-char (form)
1142 ;; (case (length (cdr form))
1143 ;; (0 `(delete-region (point) (1+ (point))))
1144 ;; (1 `(delete-region (point) (+ (point) ,(nth 1 form))))
1147 ;; byte-compile-negation-optimizer lives in bytecomp.el
1148 ;(put '/= 'byte-optimizer 'byte-compile-negation-optimizer)
1149 (put 'atom 'byte-optimizer 'byte-compile-negation-optimizer)
1150 (put 'nlistp 'byte-optimizer 'byte-compile-negation-optimizer)
1152 (defun byte-optimize-funcall (form)
1153 ;; (funcall '(lambda ...) ...) ==> ((lambda ...) ...)
1154 ;; (funcall 'foo ...) ==> (foo ...)
1155 (let ((fn (nth 1 form)))
1156 (if (memq (car-safe fn) '(quote function))
1157 (cons (nth 1 fn) (cdr (cdr form)))
1160 (defun byte-optimize-apply (form)
1161 ;; If the last arg is a literal constant, turn this into a funcall.
1162 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...).
1163 (let ((fn (nth 1 form))
1164 (last (nth (1- (length form)) form))) ; I think this really is fastest
1165 (or (if (or (null last)
1166 (eq (car-safe last) 'quote))
1167 (if (listp (nth 1 last))
1168 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form)))))))
1169 (nconc (list 'funcall fn) butlast
1170 (mapcar #'(lambda (x) (list 'quote x)) (nth 1 last))))
1172 "last arg to apply can't be a literal atom: %s"
1173 (prin1-to-string last))
1177 (put 'funcall 'byte-optimizer 'byte-optimize-funcall)
1178 (put 'apply 'byte-optimizer 'byte-optimize-apply)
1181 (put 'let 'byte-optimizer 'byte-optimize-letX)
1182 (put 'let* 'byte-optimizer 'byte-optimize-letX)
1183 (defun byte-optimize-letX (form)
1184 (cond ((null (nth 1 form))
1186 (cons 'progn (cdr (cdr form))))
1187 ((or (nth 2 form) (nthcdr 3 form))
1190 ((eq (car form) 'let)
1191 (append '(progn) (mapcar 'car-safe (mapcar 'cdr-safe (nth 1 form)))
1194 (let ((binds (reverse (nth 1 form))))
1195 (list 'let* (reverse (cdr binds)) (nth 1 (car binds)) nil)))))
1198 (put 'nth 'byte-optimizer 'byte-optimize-nth)
1199 (defun byte-optimize-nth (form)
1200 (if (and (= (safe-length form) 3) (memq (nth 1 form) '(0 1)))
1201 (list 'car (if (zerop (nth 1 form))
1203 (list 'cdr (nth 2 form))))
1204 (byte-optimize-predicate form)))
1206 (put 'nthcdr 'byte-optimizer 'byte-optimize-nthcdr)
1207 (defun byte-optimize-nthcdr (form)
1208 (if (and (= (safe-length form) 3) (not (memq (nth 1 form) '(0 1 2))))
1209 (byte-optimize-predicate form)
1210 (let ((count (nth 1 form)))
1211 (setq form (nth 2 form))
1212 (while (>= (setq count (1- count)) 0)
1213 (setq form (list 'cdr form)))
1216 (put 'concat 'byte-optimizer 'byte-optimize-concat)
1217 (defun byte-optimize-concat (form)
1218 (let ((args (cdr form))
1220 (while (and args constant)
1221 (or (byte-compile-constp (car args))
1222 (setq constant nil))
1223 (setq args (cdr args)))
1229 ;;; enumerating those functions which need not be called if the returned
1230 ;;; value is not used. That is, something like
1231 ;;; (progn (list (something-with-side-effects) (yow))
1233 ;;; may safely be turned into
1234 ;;; (progn (progn (something-with-side-effects) (yow))
1236 ;;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo.
1238 ;;; I wonder if I missed any :-\)
1239 (let ((side-effect-free-fns
1240 '(% * + - / /= 1+ 1- < <= = > >= abs acos append aref ash asin atan
1242 boundp buffer-file-name buffer-local-variables buffer-modified-p
1244 capitalize car-less-than-car car cdr ceiling concat
1245 ;; coordinates-in-window-p not in XEmacs
1246 copy-marker cos count-lines
1247 default-boundp default-value documentation downcase
1248 elt exp expt fboundp featurep
1249 file-directory-p file-exists-p file-locked-p file-name-absolute-p
1250 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p
1252 get get-buffer get-buffer-window getenv get-file-buffer
1253 ;; hash-table functions
1254 make-hash-table copy-hash-table
1257 hash-table-rehash-size
1258 hash-table-rehash-threshold
1264 length log log10 logand logb logior lognot logxor lsh
1265 marker-buffer max member memq min mod
1266 next-window nth nthcdr number-to-string
1267 parse-colon-path plist-get previous-window
1268 radians-to-degrees rassq regexp-quote reverse round
1269 sin sqrt string< string= string-equal string-lessp string-to-char
1270 string-to-int string-to-number substring symbol-plist
1271 tan upcase user-variable-p vconcat
1272 ;; XEmacs change: window-edges -> window-pixel-edges
1273 window-buffer window-dedicated-p window-pixel-edges window-height
1274 window-hscroll window-minibuffer-p window-width
1276 ;; functions defined by cl
1277 oddp evenp plusp minusp
1278 abs expt signum last butlast ldiff
1280 isqrt floor* ceiling* truncate* round* mod* rem* subseq
1283 (side-effect-and-error-free-fns
1285 bobp bolp buffer-end buffer-list buffer-size buffer-string bufferp
1286 car-safe case-table-p cdr-safe char-or-string-p char-table-p
1287 characterp commandp cons
1288 consolep console-live-p consp
1290 ;; XEmacs: extent functions, frame-live-p, various other stuff
1291 devicep device-live-p
1292 dot dot-marker eobp eolp eq eql equal eventp extentp
1293 extent-live-p floatp framep frame-live-p
1294 get-largest-window get-lru-window
1296 identity ignore integerp integer-or-marker-p interactive-p
1297 invocation-directory invocation-name
1299 make-marker mark mark-marker markerp memory-limit minibuffer-window
1300 ;; mouse-movement-p not in XEmacs
1301 natnump nlistp not null number-or-marker-p numberp
1302 one-window-p ;; overlayp not in XEmacs
1303 point point-marker point-min point-max processp
1305 selected-window sequencep stringp subrp symbolp syntax-table-p
1306 user-full-name user-login-name user-original-login-name
1307 user-real-login-name user-real-uid user-uid
1309 window-configuration-p window-live-p windowp
1310 ;; Functions defined by cl
1311 eql floatp-safe list* subst acons equalp random-state-p
1314 (dolist (fn side-effect-free-fns)
1315 (put fn 'side-effect-free t))
1316 (dolist (fn side-effect-and-error-free-fns)
1317 (put fn 'side-effect-free 'error-free)))
1320 (defun byte-compile-splice-in-already-compiled-code (form)
1321 ;; form is (byte-code "..." [...] n)
1322 (if (not (memq byte-optimize '(t byte)))
1323 (byte-compile-normal-call form)
1324 (byte-inline-lapcode
1325 (byte-decompile-bytecode-1 (nth 1 form) (nth 2 form) t))
1326 (setq byte-compile-maxdepth (max (+ byte-compile-depth (nth 3 form))
1327 byte-compile-maxdepth))
1328 (setq byte-compile-depth (1+ byte-compile-depth))))
1330 (put 'byte-code 'byte-compile 'byte-compile-splice-in-already-compiled-code)
1333 (defconst byte-constref-ops
1334 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind))
1336 ;;; This function extracts the bitfields from variable-length opcodes.
1337 ;;; Originally defined in disass.el (which no longer uses it.)
1339 (defun disassemble-offset ()
1341 ;; fetch and return the offset for the current opcode.
1342 ;; return NIL if this opcode has no offset
1343 ;; OP, PTR and BYTES are used and set dynamically
1344 (declare (special op ptr bytes))
1345 (cond ((< op byte-nth)
1346 (let ((tem (logand op 7)))
1347 (setq op (logand op 248))
1349 (setq ptr (1+ ptr)) ;offset in next byte
1350 ;; char-to-int to avoid downstream problems
1351 ;; caused by chars appearing where ints are
1352 ;; expected. In bytecode the bytes in the
1353 ;; opcode string are always interpreted as ints.
1354 (char-to-int (aref bytes ptr)))
1356 (setq ptr (1+ ptr)) ;offset in next 2 bytes
1358 (progn (setq ptr (1+ ptr))
1359 (lsh (aref bytes ptr) 8))))
1360 (t tem)))) ;offset was in opcode
1361 ((>= op byte-constant)
1362 (prog1 (- op byte-constant) ;offset in opcode
1363 (setq op byte-constant)))
1364 ((and (>= op byte-constant2)
1365 (<= op byte-goto-if-not-nil-else-pop))
1366 (setq ptr (1+ ptr)) ;offset in next 2 bytes
1368 (progn (setq ptr (1+ ptr))
1369 (lsh (aref bytes ptr) 8))))
1370 ;; XEmacs: this code was here before. FSF's first comparison
1371 ;; is (>= op byte-listN). It appears that the rel-goto stuff
1372 ;; does not exist in FSF 19.30. It doesn't exist in 19.28
1373 ;; either, so I'm going to assume that this is an improvement
1374 ;; on our part and leave it in. --ben
1375 ((and (>= op byte-rel-goto)
1376 (<= op byte-insertN))
1377 (setq ptr (1+ ptr)) ;offset in next byte
1378 ;; Use char-to-int to avoid downstream problems caused by
1379 ;; chars appearing where ints are expected. In bytecode
1380 ;; the bytes in the opcode string are always interpreted as
1382 (char-to-int (aref bytes ptr)))))
1385 ;;; This de-compiler is used for inline expansion of compiled functions,
1386 ;;; and by the disassembler.
1388 ;;; This list contains numbers, which are pc values,
1389 ;;; before each instruction.
1390 (defun byte-decompile-bytecode (bytes constvec)
1391 "Turns BYTECODE into lapcode, referring to CONSTVEC."
1392 (let ((byte-compile-constants nil)
1393 (byte-compile-variables nil)
1394 (byte-compile-tag-number 0))
1395 (byte-decompile-bytecode-1 bytes constvec)))
1397 ;; As byte-decompile-bytecode, but updates
1398 ;; byte-compile-{constants, variables, tag-number}.
1399 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced
1400 ;; with `goto's destined for the end of the code.
1401 ;; That is for use by the compiler.
1402 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler.
1403 ;; In that case, we put a pc value into the list
1404 ;; before each insn (or its label).
1405 (defun byte-decompile-bytecode-1 (bytes constvec &optional make-spliceable)
1406 (let ((length (length bytes))
1407 (ptr 0) optr tags op offset
1411 ;; (retcount 0) unused
1413 (while (not (= ptr length))
1415 (setq lap (cons ptr lap)))
1416 (setq op (aref bytes ptr)
1418 offset (disassemble-offset)) ; this does dynamic-scope magic
1419 (setq op (aref byte-code-vector op))
1420 ;; XEmacs: the next line in FSF 19.30 reads
1421 ;; (cond ((memq op byte-goto-ops)
1422 ;; see the comment above about byte-rel-goto in XEmacs.
1423 (cond ((or (memq op byte-goto-ops)
1424 (cond ((memq op byte-rel-goto-ops)
1425 (setq op (aref byte-code-vector
1426 (- (symbol-value op)
1427 (- byte-rel-goto byte-goto))))
1428 (setq offset (+ ptr (- offset 127)))
1432 (cdr (or (assq offset tags)
1435 (byte-compile-make-tag))
1437 ((cond ((eq op 'byte-constant2) (setq op 'byte-constant) t)
1438 ((memq op byte-constref-ops)))
1439 (setq tmp (if (>= offset (length constvec))
1440 (list 'out-of-range offset)
1441 (aref constvec offset))
1442 offset (if (eq op 'byte-constant)
1443 (byte-compile-get-constant tmp)
1444 (or (assq tmp byte-compile-variables)
1445 (car (setq byte-compile-variables
1447 byte-compile-variables)))))))
1448 ((and make-spliceable
1449 (eq op 'byte-return))
1450 (if (= ptr (1- length))
1452 (setq offset (or endtag (setq endtag (byte-compile-make-tag)))
1454 ;; lap = ( [ (pc . (op . arg)) ]* )
1455 (setq lap (cons (cons optr (cons op (or offset 0)))
1457 (setq ptr (1+ ptr)))
1458 ;; take off the dummy nil op that we replaced a trailing "return" with.
1461 (cond ((numberp (car rest)))
1462 ((setq tmp (assq (car (car rest)) tags))
1463 ;; this addr is jumped to
1464 (setcdr rest (cons (cons nil (cdr tmp))
1466 (setq tags (delq tmp tags))
1467 (setq rest (cdr rest))))
1468 (setq rest (cdr rest))))
1469 (if tags (error "optimizer error: missed tags %s" tags))
1470 (if (null (car (cdr (car lap))))
1471 (setq lap (cdr lap)))
1473 (setq lap (cons (cons nil endtag) lap)))
1474 ;; remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* )
1475 (mapcar #'(lambda (elt) (if (numberp elt) elt (cdr elt)))
1479 ;;; peephole optimizer
1481 (defconst byte-tagref-ops (cons 'TAG byte-goto-ops))
1483 (defconst byte-conditional-ops
1484 '(byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop
1485 byte-goto-if-not-nil-else-pop))
1487 (defconst byte-after-unbind-ops
1488 '(byte-constant byte-dup
1489 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-integerp
1491 byte-cons byte-list1 byte-list2 ; byte-list3 byte-list4
1493 ;; How about other side-effect-free-ops? Is it safe to move an
1494 ;; error invocation (such as from nth) out of an unwind-protect?
1495 ;; No, it is not, because the unwind-protect forms can alter
1496 ;; the inside of the object to which nth would apply.
1497 ;; For the same reason, byte-equal was deleted from this list.
1498 "Byte-codes that can be moved past an unbind.")
1500 (defconst byte-compile-side-effect-and-error-free-ops
1501 '(byte-constant byte-dup byte-symbolp byte-consp byte-stringp byte-listp
1502 byte-integerp byte-numberp byte-eq byte-equal byte-not byte-car-safe
1503 byte-cdr-safe byte-cons byte-list1 byte-list2 byte-point byte-point-max
1504 byte-point-min byte-following-char byte-preceding-char
1505 byte-current-column byte-eolp byte-eobp byte-bolp byte-bobp
1506 byte-current-buffer byte-interactive-p))
1508 (defconst byte-compile-side-effect-free-ops
1510 '(byte-varref byte-nth byte-memq byte-car byte-cdr byte-length byte-aref
1511 byte-symbol-value byte-get byte-concat2 byte-concat3 byte-sub1 byte-add1
1512 byte-eqlsign byte-gtr byte-lss byte-leq byte-geq byte-diff byte-negate
1513 byte-plus byte-max byte-min byte-mult byte-char-after byte-char-syntax
1514 byte-buffer-substring byte-string= byte-string< byte-nthcdr byte-elt
1515 byte-member byte-assq byte-quo byte-rem)
1516 byte-compile-side-effect-and-error-free-ops))
1518 ;;; This piece of shit is because of the way DEFVAR_BOOL() variables work.
1519 ;;; Consider the code
1521 ;;; (defun foo (flag)
1522 ;;; (let ((old-pop-ups pop-up-windows)
1523 ;;; (pop-up-windows flag))
1524 ;;; (cond ((not (eq pop-up-windows old-pop-ups))
1525 ;;; (setq old-pop-ups pop-up-windows)
1528 ;;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is
1529 ;;; something else. But if we optimize