annotate lisp/byte-optimize.el @ 434:9d177e8d4150 r21-2-25

Import from CVS: tag r21-2-25
author cvs
date Mon, 13 Aug 2007 11:30:53 +0200
parents 3ecd8885ac67
children 8de8e3f6228a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 ;;; byte-optimize.el --- the optimization passes of the emacs-lisp byte compiler.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3 ;;; Copyright (c) 1991, 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 ;; Author: Jamie Zawinski <jwz@jwz.org>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 ;; Keywords: internal
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 ;; This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 ;; XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 ;; under the terms of the GNU General Public License as published by
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 ;; any later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 ;; XEmacs is distributed in the hope that it will be useful, but
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 ;; General Public License for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 ;; along with XEmacs; see the file COPYING. If not, write to the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 ;; Boston, MA 02111-1307, USA.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 ;;; Synched up with: FSF 19.30.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 ;;; Commentary:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 ;; ========================================================================
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 ;; "No matter how hard you try, you can't make a racehorse out of a pig.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 ;; You can, however, make a faster pig."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 ;; Or, to put it another way, the emacs byte compiler is a VW Bug. This code
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 ;; makes it be a VW Bug with fuel injection and a turbocharger... You're
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 ;; still not going to make it go faster than 70 mph, but it might be easier
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 ;; to get it there.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 ;; TO DO:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 ;; (apply #'(lambda (x &rest y) ...) 1 (foo))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 ;; maintain a list of functions known not to access any global variables
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 ;; (actually, give them a 'dynamically-safe property) and then
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 ;; (let ( v1 v2 ... vM vN ) <...dynamically-safe...> ) ==>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 ;; (let ( v1 v2 ... vM ) vN <...dynamically-safe...> )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 ;; by recursing on this, we might be able to eliminate the entire let.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 ;; However certain variables should never have their bindings optimized
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 ;; away, because they affect everything.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 ;; (put 'debug-on-error 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 ;; (put 'debug-on-abort 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 ;; (put 'debug-on-next-call 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 ;; (put 'mocklisp-arguments 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 ;; (put 'inhibit-quit 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 ;; (put 'quit-flag 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 ;; (put 't 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 ;; (put 'nil 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 ;; possibly also
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 ;; (put 'gc-cons-threshold 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 ;; (put 'track-mouse 'binding-is-magic t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 ;; others?
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 ;; Simple defsubsts often produce forms like
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 ;; (let ((v1 (f1)) (v2 (f2)) ...)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 ;; (FN v1 v2 ...))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 ;; It would be nice if we could optimize this to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 ;; (FN (f1) (f2) ...)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 ;; but we can't unless FN is dynamically-safe (it might be dynamically
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 ;; referring to the bindings that the lambda arglist established.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 ;; One of the uncountable lossages introduced by dynamic scope...
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 ;; Maybe there should be a control-structure that says "turn on
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 ;; fast-and-loose type-assumptive optimizations here." Then when
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 ;; we see a form like (car foo) we can from then on assume that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 ;; the variable foo is of type cons, and optimize based on that.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 ;; But, this won't win much because of (you guessed it) dynamic
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 ;; scope. Anything down the stack could change the value.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 ;; (Another reason it doesn't work is that it is perfectly valid
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 ;; to call car with a null argument.) A better approach might
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 ;; be to allow type-specification of the form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 ;; (put 'foo 'arg-types '(float (list integer) dynamic))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 ;; (put 'foo 'result-type 'bool)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 ;; It should be possible to have these types checked to a certain
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 ;; degree.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 ;; collapse common subexpressions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 ;; It would be nice if redundant sequences could be factored out as well,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 ;; when they are known to have no side-effects:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 ;; (list (+ a b c) (+ a b c)) --> a b add c add dup list-2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 ;; but beware of traps like
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 ;; (cons (list x y) (list x y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 ;; Tail-recursion elimination is not really possible in Emacs Lisp.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 ;; Tail-recursion elimination is almost always impossible when all variables
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 ;; have dynamic scope, but given that the "return" byteop requires the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 ;; binding stack to be empty (rather than emptying it itself), there can be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 ;; no truly tail-recursive Emacs Lisp functions that take any arguments or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 ;; make any bindings.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 ;; Here is an example of an Emacs Lisp function which could safely be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 ;; byte-compiled tail-recursively:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 ;; (defun tail-map (fn list)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 ;; (cond (list
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 ;; (funcall fn (car list))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 ;; (tail-map fn (cdr list)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 ;; However, if there was even a single let-binding around the COND,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 ;; it could not be byte-compiled, because there would be an "unbind"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 ;; byte-op between the final "call" and "return." Adding a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 ;; Bunbind_all byteop would fix this.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 ;; (defun foo (x y z) ... (foo a b c))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 ;; ... (const foo) (varref a) (varref b) (varref c) (call 3) END: (return)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 ;; ... (varref a) (varbind x) (varref b) (varbind y) (varref c) (varbind z) (goto 0) END: (unbind-all) (return)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 ;; ... (varref a) (varset x) (varref b) (varset y) (varref c) (varset z) (goto 0) END: (return)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 ;; this also can be considered tail recursion:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 ;; ... (const foo) (varref a) (call 1) (goto X) ... X: (return)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 ;; could generalize this by doing the optimization
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 ;; (goto X) ... X: (return) --> (return)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 ;; But this doesn't solve all of the problems: although by doing tail-
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 ;; recursion elimination in this way, the call-stack does not grow, the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 ;; binding-stack would grow with each recursive step, and would eventually
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 ;; overflow. I don't believe there is any way around this without lexical
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 ;; scope.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 ;; Wouldn't it be nice if Emacs Lisp had lexical scope.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 ;; Idea: the form (lexical-scope) in a file means that the file may be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 ;; compiled lexically. This proclamation is file-local. Then, within
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 ;; that file, "let" would establish lexical bindings, and "let-dynamic"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 ;; would do things the old way. (Or we could use CL "declare" forms.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 ;; We'd have to notice defvars and defconsts, since those variables should
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 ;; always be dynamic, and attempting to do a lexical binding of them
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 ;; should simply do a dynamic binding instead.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 ;; But! We need to know about variables that were not necessarily defvarred
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 ;; in the file being compiled (doing a boundp check isn't good enough.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 ;; Fdefvar() would have to be modified to add something to the plist.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 ;; A major disadvantage of this scheme is that the interpreter and compiler
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 ;; would have different semantics for files compiled with (dynamic-scope).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 ;; Since this would be a file-local optimization, there would be no way to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 ;; modify the interpreter to obey this (unless the loader was hacked
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 ;; in some grody way, but that's a really bad idea.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 ;; HA! RMS removed the following paragraph from his version of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 ;; byte-optimize.el.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 ;; Really the Right Thing is to make lexical scope the default across
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 ;; the board, in the interpreter and compiler, and just FIX all of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 ;; the code that relies on dynamic scope of non-defvarred variables.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 ;; Other things to consider:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 ;; Associative math should recognize subcalls to identical function:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 ;;(disassemble #'(lambda (x) (+ (+ (foo) 1) (+ (bar) 2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 ;; This should generate the same as (1+ x) and (1- x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 ;;(disassemble #'(lambda (x) (cons (+ x 1) (- x 1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 ;; An awful lot of functions always return a non-nil value. If they're
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 ;; error free also they may act as true-constants.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 ;;(disassemble #'(lambda (x) (and (point) (foo))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 ;; When
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 ;; - all but one arguments to a function are constant
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 ;; - the non-constant argument is an if-expression (cond-expression?)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 ;; then the outer function can be distributed. If the guarding
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 ;; condition is side-effect-free [assignment-free] then the other
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 ;; arguments may be any expressions. Since, however, the code size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 ;; can increase this way they should be "simple". Compare:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 ;;(disassemble #'(lambda (x) (eq (if (point) 'a 'b) 'c)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 ;;(disassemble #'(lambda (x) (if (point) (eq 'a 'c) (eq 'b 'c))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 ;; (car (cons A B)) -> (progn B A)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 ;;(disassemble #'(lambda (x) (car (cons (foo) 42))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 ;; (cdr (cons A B)) -> (progn A B)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 ;;(disassemble #'(lambda (x) (cdr (cons 42 (foo)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 ;; (car (list A B ...)) -> (progn B ... A)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 ;;(disassemble #'(lambda (x) (car (list (foo) 42 (bar)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 ;; (cdr (list A B ...)) -> (progn A (list B ...))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 ;;(disassemble #'(lambda (x) (cdr (list 42 (foo) (bar)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 ;;; Code:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 (require 'byte-compile "bytecomp")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 (defun byte-compile-log-lap-1 (format &rest args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 (if (aref byte-code-vector 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 (error "The old version of the disassembler is loaded. Reload new-bytecomp as well."))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 (byte-compile-log-1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 (apply 'format format
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 (let (c a)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 (mapcar
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 #'(lambda (arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 (if (not (consp arg))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 (if (and (symbolp arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 (string-match "^byte-" (symbol-name arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 (intern (substring (symbol-name arg) 5))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 (if (integerp (setq c (car arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 (error "non-symbolic byte-op %s" c))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 (if (eq c 'TAG)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 (setq c arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 (setq a (cond ((memq c byte-goto-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 (car (cdr (cdr arg))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 ((memq c byte-constref-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 (car (cdr arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 (t (cdr arg))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 (setq c (symbol-name c))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 (if (string-match "^byte-." c)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 (setq c (intern (substring c 5)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 (if (eq c 'constant) (setq c 'const))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 (if (and (eq (cdr arg) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 (not (memq c '(unbind call const))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 c
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 (format "(%s %s)" c a))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 args)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 (defmacro byte-compile-log-lap (format-string &rest args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 (list 'and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 '(memq byte-optimize-log '(t byte))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 (cons 'byte-compile-log-lap-1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 (cons format-string args))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 ;;; byte-compile optimizers to support inlining
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 (put 'inline 'byte-optimizer 'byte-optimize-inline-handler)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 (defun byte-optimize-inline-handler (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 "byte-optimize-handler for the `inline' special-form."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 (cons
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 'progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 (mapcar
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 #'(lambda (sexp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 (let ((fn (car-safe sexp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 (if (and (symbolp fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 (or (cdr (assq fn byte-compile-function-environment))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 (and (fboundp fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 (not (or (cdr (assq fn byte-compile-macro-environment))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 (and (consp (setq fn (symbol-function fn)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 (eq (car fn) 'macro))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 (subrp fn))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 (byte-compile-inline-expand sexp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 sexp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 ;; Splice the given lap code into the current instruction stream.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 ;; If it has any labels in it, you're responsible for making sure there
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 ;; are no collisions, and that byte-compile-tag-number is reasonable
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 ;; after this is spliced in. The provided list is destroyed.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 (defun byte-inline-lapcode (lap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 (setq byte-compile-output (nconc (nreverse lap) byte-compile-output)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 (defun byte-compile-inline-expand (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 (let* ((name (car form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 (fn (or (cdr (assq name byte-compile-function-environment))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 (and (fboundp name) (symbol-function name)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 (if (null fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 (byte-compile-warn "attempt to inline %s before it was defined" name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 ;; else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 (if (and (consp fn) (eq (car fn) 'autoload))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 (load (nth 1 fn))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 (setq fn (or (cdr (assq name byte-compile-function-environment))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 (and (fboundp name) (symbol-function name))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 (if (and (consp fn) (eq (car fn) 'autoload))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 (error "file \"%s\" didn't define \"%s\"" (nth 1 fn) name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 (if (symbolp fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 (byte-compile-inline-expand (cons fn (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 (if (compiled-function-p fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 (fetch-bytecode fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 (cons (list 'lambda (compiled-function-arglist fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 (list 'byte-code
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 (compiled-function-instructions fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 (compiled-function-constants fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 (compiled-function-stack-depth fn)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 (if (not (eq (car fn) 'lambda)) (error "%s is not a lambda" name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 (cons fn (cdr form)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 ;;; ((lambda ...) ...)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 ;;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 (defun byte-compile-unfold-lambda (form &optional name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 (or name (setq name "anonymous lambda"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 (let ((lambda (car form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 (values (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 (if (compiled-function-p lambda)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 (setq lambda (list 'lambda (compiled-function-arglist lambda)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 (list 'byte-code
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 (compiled-function-instructions lambda)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 (compiled-function-constants lambda)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 (compiled-function-stack-depth lambda)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 (let ((arglist (nth 1 lambda))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 (body (cdr (cdr lambda)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 optionalp restp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 bindings)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 (if (and (stringp (car body)) (cdr body))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 (setq body (cdr body)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 (if (and (consp (car body)) (eq 'interactive (car (car body))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 (setq body (cdr body)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 (while arglist
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 (cond ((eq (car arglist) '&optional)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 ;; ok, I'll let this slide because funcall_lambda() does...
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 ;; (if optionalp (error "multiple &optional keywords in %s" name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 (if restp (error "&optional found after &rest in %s" name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 (if (null (cdr arglist))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 (error "nothing after &optional in %s" name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 (setq optionalp t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 ((eq (car arglist) '&rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 ;; ...but it is by no stretch of the imagination a reasonable
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 ;; thing that funcall_lambda() allows (&rest x y) and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 ;; (&rest x &optional y) in arglists.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 (if (null (cdr arglist))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 (error "nothing after &rest in %s" name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 (if (cdr (cdr arglist))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 (error "multiple vars after &rest in %s" name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 (setq restp t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 (restp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 (setq bindings (cons (list (car arglist)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 (and values (cons 'list values)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 bindings)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 values nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 ((and (not optionalp) (null values))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 (byte-compile-warn "attempt to open-code %s with too few arguments" name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 (setq arglist nil values 'too-few))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 (setq bindings (cons (list (car arglist) (car values))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 bindings)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 values (cdr values))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 (setq arglist (cdr arglist)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 (if values
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 (or (eq values 'too-few)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 (byte-compile-warn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 "attempt to open-code %s with too many arguments" name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 (let ((newform
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 (if bindings
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 (cons 'let (cons (nreverse bindings) body))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 (cons 'progn body))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 (byte-compile-log " %s\t==>\t%s" form newform)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 newform)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 ;;; implementing source-level optimizers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 (defun byte-optimize-form-code-walker (form for-effect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 ;; For normal function calls, We can just mapcar the optimizer the cdr. But
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 ;; we need to have special knowledge of the syntax of the special forms
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 ;; like let and defun (that's why they're special forms :-). (Actually,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 ;; the important aspect is that they are subrs that don't evaluate all of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 ;; their args.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 (let ((fn (car-safe form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 (cond ((not (consp form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 (if (not (and for-effect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 (or byte-compile-delete-errors
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 (not (symbolp form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 (eq form t))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 ((eq fn 'quote)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 (if (cdr (cdr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 (byte-compile-warn "malformed quote form: %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 (prin1-to-string form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 ;; map (quote nil) to nil to simplify optimizer logic.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 ;; map quoted constants to nil if for-effect (just because).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 (and (nth 1 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 (not for-effect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 ((or (compiled-function-p fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 (eq 'lambda (car-safe fn)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 (byte-compile-unfold-lambda form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 ((memq fn '(let let*))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 ;; recursively enter the optimizer for the bindings and body
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 ;; of a let or let*. This for depth-firstness: forms that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 ;; are more deeply nested are optimized first.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 (cons fn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 (cons
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 (mapcar
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 #'(lambda (binding)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 (if (symbolp binding)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 binding
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 (if (cdr (cdr binding))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 (byte-compile-warn "malformed let binding: %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 (prin1-to-string binding)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 (list (car binding)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 (byte-optimize-form (nth 1 binding) nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 (byte-optimize-body (cdr (cdr form)) for-effect))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 ((eq fn 'cond)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 (cons fn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 (mapcar
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 #'(lambda (clause)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 (if (consp clause)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 (cons
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 (byte-optimize-form (car clause) nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 (byte-optimize-body (cdr clause) for-effect))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 (byte-compile-warn "malformed cond form: %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 (prin1-to-string clause))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 clause))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 ((eq fn 'progn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 ;; as an extra added bonus, this simplifies (progn <x>) --> <x>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 (if (cdr (cdr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 (setq tmp (byte-optimize-body (cdr form) for-effect))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 (if (cdr tmp) (cons 'progn tmp) (car tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 (byte-optimize-form (nth 1 form) for-effect)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 ((eq fn 'prog1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 (if (cdr (cdr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 (cons 'prog1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 (cons (byte-optimize-form (nth 1 form) for-effect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 (byte-optimize-body (cdr (cdr form)) t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 (byte-optimize-form (nth 1 form) for-effect)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 ((eq fn 'prog2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 (cons 'prog2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 (cons (byte-optimize-form (nth 1 form) t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 (cons (byte-optimize-form (nth 2 form) for-effect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 (byte-optimize-body (cdr (cdr (cdr form))) t)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 ((memq fn '(save-excursion save-restriction save-current-buffer))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 ;; those subrs which have an implicit progn; it's not quite good
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 ;; enough to treat these like normal function calls.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 ;; This can turn (save-excursion ...) into (save-excursion) which
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 ;; will be optimized away in the lap-optimize pass.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 (cons fn (byte-optimize-body (cdr form) for-effect)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 ((eq fn 'with-output-to-temp-buffer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 ;; this is just like the above, except for the first argument.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 (cons fn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 (cons
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 (byte-optimize-form (nth 1 form) nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 (byte-optimize-body (cdr (cdr form)) for-effect))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 ((eq fn 'if)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 (cons fn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 (cons (byte-optimize-form (nth 1 form) nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 (cons
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 (byte-optimize-form (nth 2 form) for-effect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 (byte-optimize-body (nthcdr 3 form) for-effect)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 ((memq fn '(and or)) ; remember, and/or are control structures.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 ;; take forms off the back until we can't any more.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 ;; In the future it could conceivably be a problem that the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 ;; subexpressions of these forms are optimized in the reverse
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 ;; order, but it's ok for now.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 (if for-effect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 (let ((backwards (reverse (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 (while (and backwards
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 (null (setcar backwards
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 (byte-optimize-form (car backwards)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 for-effect))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 (setq backwards (cdr backwards)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 (if (and (cdr form) (null backwards))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 (byte-compile-log
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 " all subforms of %s called for effect; deleted" form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 (and backwards
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 (cons fn (nreverse backwards))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 (cons fn (mapcar 'byte-optimize-form (cdr form)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 ((eq fn 'interactive)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 (byte-compile-warn "misplaced interactive spec: %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 (prin1-to-string form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 ((memq fn '(defun defmacro function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 condition-case save-window-excursion))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 ;; These forms are compiled as constants or by breaking out
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 ;; all the subexpressions and compiling them separately.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 ((eq fn 'unwind-protect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 ;; the "protected" part of an unwind-protect is compiled (and thus
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 ;; optimized) as a top-level form, so don't do it here. But the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 ;; non-protected part has the same for-effect status as the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 ;; unwind-protect itself. (The protected part is always for effect,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 ;; but that isn't handled properly yet.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 (cons fn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 (cons (byte-optimize-form (nth 1 form) for-effect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 (cdr (cdr form)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 ((eq fn 'catch)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 ;; the body of a catch is compiled (and thus optimized) as a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 ;; top-level form, so don't do it here. The tag is never
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 ;; for-effect. The body should have the same for-effect status
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 ;; as the catch form itself, but that isn't handled properly yet.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 (cons fn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 (cons (byte-optimize-form (nth 1 form) nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 (cdr (cdr form)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 ;; If optimization is on, this is the only place that macros are
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 ;; expanded. If optimization is off, then macroexpansion happens
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 ;; in byte-compile-form. Otherwise, the macros are already expanded
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 ;; by the time that is reached.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 ((not (eq form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 (setq form (macroexpand form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 byte-compile-macro-environment))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 (byte-optimize-form form for-effect))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 ((not (symbolp fn))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 (or (eq 'mocklisp (car-safe fn)) ; ha!
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 (byte-compile-warn "%s is a malformed function"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 (prin1-to-string fn)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 ((and for-effect (setq tmp (get fn 'side-effect-free))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 (or byte-compile-delete-errors
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 (eq tmp 'error-free)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 (byte-compile-warn "%s called for effect"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 (prin1-to-string form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 (byte-compile-log " %s called for effect; deleted" fn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 ;; appending a nil here might not be necessary, but it can't hurt.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 (byte-optimize-form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 (cons 'progn (append (cdr form) '(nil))) t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 ;; Otherwise, no args can be considered to be for-effect,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 ;; even if the called function is for-effect, because we
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 ;; don't know anything about that function.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 (cons fn (mapcar 'byte-optimize-form (cdr form)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 (defun byte-optimize-form (form &optional for-effect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 "The source-level pass of the optimizer."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 ;; First, optimize all sub-forms of this one.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 (setq form (byte-optimize-form-code-walker form for-effect))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 ;; After optimizing all subforms, optimize this form until it doesn't
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 ;; optimize any further. This means that some forms will be passed through
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 ;; the optimizer many times, but that's necessary to make the for-effect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 ;; processing do as much as possible.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 (let (opt new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 (if (and (consp form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 (symbolp (car form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557 (or (and for-effect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 ;; we don't have any of these yet, but we might.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 (setq opt (get (car form) 'byte-for-effect-optimizer)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 (setq opt (get (car form) 'byte-optimizer)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 (not (eq form (setq new (funcall opt form)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 ;; (if (equal form new) (error "bogus optimizer -- %s" opt))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 (byte-compile-log " %s\t==>\t%s" form new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 (setq new (byte-optimize-form new for-effect))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566 new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 (defun byte-optimize-body (forms all-for-effect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 ;; Optimize the cdr of a progn or implicit progn; `forms' is a list of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 ;; forms, all but the last of which are optimized with the assumption that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 ;; they are being called for effect. The last is for-effect as well if
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 ;; all-for-effect is true. Returns a new list of forms.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 (let ((rest forms)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 (result nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 fe new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 (while rest
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 (setq fe (or all-for-effect (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 (setq new (and (car rest) (byte-optimize-form (car rest) fe)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 (if (or new (not fe))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582 (setq result (cons new result)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 (setq rest (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 (nreverse result)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 ;;; some source-level optimizers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 ;;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 ;;; when writing optimizers, be VERY careful that the optimizer returns
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 ;;; something not EQ to its argument if and ONLY if it has made a change.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 ;;; This implies that you cannot simply destructively modify the list;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 ;;; you must return something not EQ to it if you make an optimization.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 ;;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 ;;; It is now safe to optimize code such that it introduces new bindings.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 ;; I'd like this to be a defsubst, but let's not be self-referential...
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 (defmacro byte-compile-trueconstp (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 ;; Returns non-nil if FORM is a non-nil constant.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 `(cond ((consp ,form) (eq (car ,form) 'quote))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 ((not (symbolp ,form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 ((eq ,form t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 ((keywordp ,form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 ;; If the function is being called with constant numeric args,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 ;; evaluate as much as possible at compile-time. This optimizer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 ;; assumes that the function is associative, like + or *.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 (defun byte-optimize-associative-math (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 (let ((args nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 (constants nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 (rest (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 (while rest
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 (if (numberp (car rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 (setq constants (cons (car rest) constants))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 (setq args (cons (car rest) args)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 (setq rest (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 (if (cdr constants)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 (if args
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618 (list (car form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 (apply (car form) constants)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620 (if (cdr args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 (cons (car form) (nreverse args))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 (car args)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 (apply (car form) constants))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
626 ;; If the function is being called with constant numeric args,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
627 ;; evaluate as much as possible at compile-time. This optimizer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628 ;; assumes that the function satisfies
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
629 ;; (op x1 x2 ... xn) == (op ...(op (op x1 x2) x3) ...xn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630 ;; like - and /.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
631 (defun byte-optimize-nonassociative-math (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632 (if (or (not (numberp (car (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633 (not (numberp (car (cdr (cdr form))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
634 form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
635 (let ((constant (car (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
636 (rest (cdr (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
637 (while (numberp (car rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638 (setq constant (funcall (car form) constant (car rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
639 rest (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 (if rest
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641 (cons (car form) (cons constant rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 constant))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644 ;;(defun byte-optimize-associative-two-args-math (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 ;; (setq form (byte-optimize-associative-math form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 ;; (if (consp form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647 ;; (byte-optimize-two-args-left form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648 ;; form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 ;;(defun byte-optimize-nonassociative-two-args-math (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651 ;; (setq form (byte-optimize-nonassociative-math form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652 ;; (if (consp form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653 ;; (byte-optimize-two-args-right form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654 ;; form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 ;; jwz: (byte-optimize-approx-equal 0.0 0.0) was returning nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657 ;; in xemacs 19.15 because it used < instead of <=.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 (defun byte-optimize-approx-equal (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659 (<= (* (abs (- x y)) 100) (abs (+ x y))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
660
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
661 ;; Collect all the constants from FORM, after the STARTth arg,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 ;; and apply FUN to them to make one argument at the end.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 ;; For functions that can handle floats, that optimization
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664 ;; can be incorrect because reordering can cause an overflow
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
665 ;; that would otherwise be avoided by encountering an arg that is a float.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666 ;; We avoid this problem by (1) not moving float constants and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667 ;; (2) not moving anything if it would cause an overflow.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668 (defun byte-optimize-delay-constants-math (form start fun)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669 ;; Merge all FORM's constants from number START, call FUN on them
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670 ;; and put the result at the end.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671 (let ((rest (nthcdr (1- start) form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672 (orig form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 ;; t means we must check for overflow.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 (overflow (memq fun '(+ *))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 (while (cdr (setq rest (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676 (if (integerp (car rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 (let (constants)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678 (setq form (copy-sequence form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 rest (nthcdr (1- start) form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
680 (while (setq rest (cdr rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
681 (cond ((integerp (car rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
682 (setq constants (cons (car rest) constants))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
683 (setcar rest nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
684 ;; If necessary, check now for overflow
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
685 ;; that might be caused by reordering.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686 (if (and overflow
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
687 ;; We have overflow if the result of doing the arithmetic
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
688 ;; on floats is not even close to the result
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
689 ;; of doing it on integers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
690 (not (byte-optimize-approx-equal
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
691 (apply fun (mapcar 'float constants))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692 (float (apply fun constants)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693 (setq form orig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
694 (setq form (nconc (delq nil form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
695 (list (apply fun (nreverse constants)))))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
696 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
697
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
698 (defun byte-optimize-plus (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
699 (setq form (byte-optimize-delay-constants-math form 1 '+))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
700 (if (memq 0 form) (setq form (delq 0 (copy-sequence form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 ;;(setq form (byte-optimize-associative-two-args-math form))
434
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
702 (case (length (cdr form))
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
703 ((0)
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
704 (condition-case ()
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
705 (eval form)
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
706 (error form)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
707
434
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
708 ;; `add1' and `sub1' are a marginally fewer instructions
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
709 ;; than `plus' and `minus', so use them when possible.
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
710 ((2)
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
711 (cond
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
712 ((eq (nth 1 form) 1) `(1+ ,(nth 2 form))) ; (+ 1 x) --> (1+ x)
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
713 ((eq (nth 2 form) 1) `(1+ ,(nth 1 form))) ; (+ x 1) --> (1+ x)
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
714 ((eq (nth 1 form) -1) `(1- ,(nth 2 form))) ; (+ -1 x) --> (1- x)
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
715 ((eq (nth 2 form) -1) `(1- ,(nth 1 form))) ; (+ x -1) --> (1- x)
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
716 (t form)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
717
434
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
718 ;; It is not safe to delete the function entirely
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
719 ;; (actually, it would be safe if we know the sole arg
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
720 ;; is not a marker).
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
721 ;; ((null (cdr (cdr form))) (nth 1 form))
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
722 (t form)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
723
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
724 (defun byte-optimize-minus (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
725 ;; Put constants at the end, except the last constant.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726 (setq form (byte-optimize-delay-constants-math form 2 '+))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727 ;; Now only first and last element can be a number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
728 (let ((last (car (reverse (nthcdr 3 form)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
729 (cond ((eq 0 last)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
730 ;; (- x y ... 0) --> (- x y ...)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
731 (setq form (copy-sequence form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
732 (setcdr (cdr (cdr form)) (delq 0 (nthcdr 3 form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
733 ;; If form is (- CONST foo... CONST), merge first and last.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
734 ((and (numberp (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
735 (numberp last))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
736 (setq form (nconc (list '- (- (nth 1 form) last) (nth 2 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
737 (delq last (copy-sequence (nthcdr 3 form))))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738 (setq form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
739 ;;; It is not safe to delete the function entirely
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
740 ;;; (actually, it would be safe if we know the sole arg
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
741 ;;; is not a marker).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
742 ;;; (if (eq (nth 2 form) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
743 ;;; (nth 1 form) ; (- x 0) --> x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
744 (byte-optimize-predicate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745 (if (and (null (cdr (cdr (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
746 (eq (nth 1 form) 0)) ; (- 0 x) --> (- x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
747 (cons (car form) (cdr (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
748 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
749 ;;; )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
750 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
751
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
752 ;; `add1' and `sub1' are a marginally fewer instructions than `plus'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
753 ;; and `minus', so use them when possible.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
754 (cond ((and (null (nthcdr 3 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
755 (eq (nth 2 form) 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
756 (list '1- (nth 1 form))) ; (- x 1) --> (1- x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757 ((and (null (nthcdr 3 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758 (eq (nth 2 form) -1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 (list '1+ (nth 1 form))) ; (- x -1) --> (1+ x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
762 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
763
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
764 (defun byte-optimize-multiply (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
765 (setq form (byte-optimize-delay-constants-math form 1 '*))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
766 ;; If there is a constant in FORM, it is now the last element.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
767 (cond ((null (cdr form)) 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
768 ;;; It is not safe to delete the function entirely
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
769 ;;; (actually, it would be safe if we know the sole arg
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
770 ;;; is not a marker or if it appears in other arithmetic).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
771 ;;; ((null (cdr (cdr form))) (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
772 ((let ((last (car (reverse form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 (cond ((eq 0 last) (cons 'progn (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
774 ((eq 1 last) (delq 1 (copy-sequence form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775 ((eq -1 last) (list '- (delq -1 (copy-sequence form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776 ((and (eq 2 last)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777 (memq t (mapcar 'symbolp (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 (prog1 (setq form (delq 2 (copy-sequence form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779 (while (not (symbolp (car (setq form (cdr form))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
780 (setcar form (list '+ (car form) (car form)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
781 (form))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
782
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
783 (defun byte-optimize-divide (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784 (setq form (byte-optimize-delay-constants-math form 2 '*))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785 (let ((last (car (reverse (cdr (cdr form))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
786 (if (numberp last)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
787 (cond ((= (length form) 3)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
788 (if (and (numberp (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
789 (not (zerop last))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
790 (condition-case nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
791 (/ (nth 1 form) last)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
792 (error nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
793 (setq form (list 'progn (/ (nth 1 form) last)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
794 ((= last 1)
434
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
795 (setq form (butlast form)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
796 ((numberp (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
797 (setq form (cons (car form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
798 (cons (/ (nth 1 form) last)
434
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
799 (butlast (cdr (cdr form)))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
800 last nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
801 (cond
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
802 ;;; ((null (cdr (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
803 ;;; (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
804 ((eq (nth 1 form) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
805 (append '(progn) (cdr (cdr form)) '(0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
806 ((eq last -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
807 (list '- (if (nthcdr 3 form)
434
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
808 (butlast form)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
809 (nth 1 form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
810 (form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
811
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
812 (defun byte-optimize-logmumble (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
813 (setq form (byte-optimize-delay-constants-math form 1 (car form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
814 (byte-optimize-predicate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
815 (cond ((memq 0 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
816 (setq form (if (eq (car form) 'logand)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
817 (cons 'progn (cdr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
818 (delq 0 (copy-sequence form)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
819 ((and (eq (car-safe form) 'logior)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
820 (memq -1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
821 (cons 'progn (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
822 (form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
823
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
824
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
825 (defun byte-optimize-binary-predicate (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
826 (if (byte-compile-constp (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
827 (if (byte-compile-constp (nth 2 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
828 (condition-case ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
829 (list 'quote (eval form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
830 (error form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
831 ;; This can enable some lapcode optimizations.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
832 (list (car form) (nth 2 form) (nth 1 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
833 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
834
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
835 (defun byte-optimize-predicate (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
836 (let ((ok t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
837 (rest (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
838 (while (and rest ok)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
839 (setq ok (byte-compile-constp (car rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
840 rest (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
841 (if ok
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
842 (condition-case ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
843 (list 'quote (eval form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
844 (error form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
845 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
846
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
847 (defun byte-optimize-identity (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
848 (if (and (cdr form) (null (cdr (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
849 (nth 1 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
850 (byte-compile-warn "identity called with %d arg%s, but requires 1"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
851 (length (cdr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
852 (if (= 1 (length (cdr form))) "" "s"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
853 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
854
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
855 (put 'identity 'byte-optimizer 'byte-optimize-identity)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
856
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
857 (put '+ 'byte-optimizer 'byte-optimize-plus)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
858 (put '* 'byte-optimizer 'byte-optimize-multiply)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
859 (put '- 'byte-optimizer 'byte-optimize-minus)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
860 (put '/ 'byte-optimizer 'byte-optimize-divide)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
861 (put 'max 'byte-optimizer 'byte-optimize-associative-math)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
862 (put 'min 'byte-optimizer 'byte-optimize-associative-math)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
863
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
864 (put '= 'byte-optimizer 'byte-optimize-binary-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
865 (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
866 (put 'eql 'byte-optimizer 'byte-optimize-binary-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
867 (put 'equal 'byte-optimizer 'byte-optimize-binary-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
868 (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
869 (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
870
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
871 (put '< 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
872 (put '> 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
873 (put '<= 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
874 (put '>= 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
875 (put '1+ 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
876 (put '1- 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
877 (put 'not 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
878 (put 'null 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
879 (put 'memq 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
880 (put 'consp 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
881 (put 'listp 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
882 (put 'symbolp 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
883 (put 'stringp 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
884 (put 'string< 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
885 (put 'string-lessp 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
886
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
887 (put 'logand 'byte-optimizer 'byte-optimize-logmumble)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
888 (put 'logior 'byte-optimizer 'byte-optimize-logmumble)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
889 (put 'logxor 'byte-optimizer 'byte-optimize-logmumble)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
890 (put 'lognot 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
891
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
892 (put 'car 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
893 (put 'cdr 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
894 (put 'car-safe 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
895 (put 'cdr-safe 'byte-optimizer 'byte-optimize-predicate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
896
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
897
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
898 ;; I'm not convinced that this is necessary. Doesn't the optimizer loop
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
899 ;; take care of this? - Jamie
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
900 ;; I think this may some times be necessary to reduce eg. (quote 5) to 5,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
901 ;; so arithmetic optimizers recognize the numeric constant. - Hallvard
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
902 (put 'quote 'byte-optimizer 'byte-optimize-quote)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
903 (defun byte-optimize-quote (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
904 (if (or (consp (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
905 (and (symbolp (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
906 ;; XEmacs addition:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
907 (not (keywordp (nth 1 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
908 (not (memq (nth 1 form) '(nil t)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
909 form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
910 (nth 1 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
911
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
912 (defun byte-optimize-zerop (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
913 (cond ((numberp (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
914 (eval form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
915 (byte-compile-delete-errors
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
916 (list '= (nth 1 form) 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
917 (form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
918
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
919 (put 'zerop 'byte-optimizer 'byte-optimize-zerop)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
920
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
921 (defun byte-optimize-and (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
922 ;; Simplify if less than 2 args.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
923 ;; if there is a literal nil in the args to `and', throw it and following
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
924 ;; forms away, and surround the `and' with (progn ... nil).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
925 (cond ((null (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
926 ((memq nil form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
927 (list 'progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
928 (byte-optimize-and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
929 (prog1 (setq form (copy-sequence form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
930 (while (nth 1 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
931 (setq form (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
932 (setcdr form nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
933 nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
934 ((null (cdr (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
935 (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
936 ((byte-optimize-predicate form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
937
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
938 (defun byte-optimize-or (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
939 ;; Throw away nil's, and simplify if less than 2 args.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
940 ;; If there is a literal non-nil constant in the args to `or', throw away all
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
941 ;; following forms.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
942 (if (memq nil form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
943 (setq form (delq nil (copy-sequence form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
944 (let ((rest form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
945 (while (cdr (setq rest (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
946 (if (byte-compile-trueconstp (car rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
947 (setq form (copy-sequence form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
948 rest (setcdr (memq (car rest) form) nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
949 (if (cdr (cdr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
950 (byte-optimize-predicate form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
951 (nth 1 form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
952
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
953 (defun byte-optimize-cond (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
954 ;; if any clauses have a literal nil as their test, throw them away.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
955 ;; if any clause has a literal non-nil constant as its test, throw
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
956 ;; away all following clauses.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
957 (let (rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
958 ;; This must be first, to reduce (cond (t ...) (nil)) to (progn t ...)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
959 (while (setq rest (assq nil (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
960 (setq form (delq rest (copy-sequence form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
961 (if (memq nil (cdr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
962 (setq form (delq nil (copy-sequence form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
963 (setq rest form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
964 (while (setq rest (cdr rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
965 (cond ((byte-compile-trueconstp (car-safe (car rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
966 (cond ((eq rest (cdr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
967 (setq form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
968 (if (cdr (car rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
969 (if (cdr (cdr (car rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
970 (cons 'progn (cdr (car rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
971 (nth 1 (car rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
972 (car (car rest)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
973 ((cdr rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
974 (setq form (copy-sequence form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
975 (setcdr (memq (car rest) form) nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
976 (setq rest nil)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
977 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
978 ;; Turn (cond (( <x> )) ... ) into (or <x> (cond ... ))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
979 (if (eq 'cond (car-safe form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
980 (let ((clauses (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
981 (if (and (consp (car clauses))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
982 (null (cdr (car clauses))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
983 (list 'or (car (car clauses))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
984 (byte-optimize-cond
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
985 (cons (car form) (cdr (cdr form)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
986 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
987 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
988
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
989 (defun byte-optimize-if (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
990 ;; (if <true-constant> <then> <else...>) ==> <then>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
991 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
992 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
993 ;; (if <test> <then> nil) ==> (if <test> <then>)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
994 (let ((clause (nth 1 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
995 (cond ((byte-compile-trueconstp clause)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
996 (nth 2 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
997 ((null clause)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
998 (if (nthcdr 4 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
999 (cons 'progn (nthcdr 3 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1000 (nth 3 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1001 ((nth 2 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1002 (if (equal '(nil) (nthcdr 3 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1003 (list 'if clause (nth 2 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1004 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1005 ((or (nth 3 form) (nthcdr 4 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1006 (list 'if
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1007 ;; Don't make a double negative;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1008 ;; instead, take away the one that is there.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1009 (if (and (consp clause) (memq (car clause) '(not null))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1010 (= (length clause) 2)) ; (not xxxx) or (not (xxxx))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1011 (nth 1 clause)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1012 (list 'not clause))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1013 (if (nthcdr 4 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1014 (cons 'progn (nthcdr 3 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1015 (nth 3 form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1016 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1017 (list 'progn clause nil)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1018
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1019 (defun byte-optimize-while (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1020 (if (nth 1 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1021 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1022
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1023 (put 'and 'byte-optimizer 'byte-optimize-and)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1024 (put 'or 'byte-optimizer 'byte-optimize-or)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1025 (put 'cond 'byte-optimizer 'byte-optimize-cond)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1026 (put 'if 'byte-optimizer 'byte-optimize-if)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1027 (put 'while 'byte-optimizer 'byte-optimize-while)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1028
434
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
1029 ;; Remove any reason for avoiding `char-before'.
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
1030 (defun byte-optimize-char-before (form)
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
1031 `(char-after (1- ,(or (nth 1 form) '(point))) ,@(cdr (cdr form))))
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
1032
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
1033 (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
9d177e8d4150 Import from CVS: tag r21-2-25
cvs
parents: 428
diff changeset
1034
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1035 ;; byte-compile-negation-optimizer lives in bytecomp.el
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1036 ;(put '/= 'byte-optimizer 'byte-compile-negation-optimizer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1037 (put 'atom 'byte-optimizer 'byte-compile-negation-optimizer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1038 (put 'nlistp 'byte-optimizer 'byte-compile-negation-optimizer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1039
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1040
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1041 (defun byte-optimize-funcall (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1042 ;; (funcall '(lambda ...) ...) ==> ((lambda ...) ...)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1043 ;; (funcall 'foo ...) ==> (foo ...)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1044 (let ((fn (nth 1 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1045 (if (memq (car-safe fn) '(quote function))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1046 (cons (nth 1 fn) (cdr (cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1047 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1048
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1049 (defun byte-optimize-apply (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1050 ;; If the last arg is a literal constant, turn this into a funcall.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1051 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1052 (let ((fn (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1053 (last (nth (1- (length form)) form))) ; I think this really is fastest
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1054 (or (if (or (null last)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1055 (eq (car-safe last) 'quote))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1056 (if (listp (nth 1 last))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1057 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1058 (nconc (list 'funcall fn) butlast
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1059 (mapcar #'(lambda (x) (list 'quote x)) (nth 1 last))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1060 (byte-compile-warn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1061 "last arg to apply can't be a literal atom: %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1062 (prin1-to-string last))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1063 nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1064 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1065
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1066 (put 'funcall 'byte-optimizer 'byte-optimize-funcall)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1067 (put 'apply 'byte-optimizer 'byte-optimize-apply)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1068
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1069
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1070 (put 'let 'byte-optimizer 'byte-optimize-letX)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1071 (put 'let* 'byte-optimizer 'byte-optimize-letX)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1072 (defun byte-optimize-letX (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1073 (cond ((null (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1074 ;; No bindings
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1075 (cons 'progn (cdr (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1076 ((or (nth 2 form) (nthcdr 3 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1077 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1078 ;; The body is nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1079 ((eq (car form) 'let)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1080 (append '(progn) (mapcar 'car-safe (mapcar 'cdr-safe (nth 1 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1081 '(nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1082 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1083 (let ((binds (reverse (nth 1 form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1084 (list 'let* (reverse (cdr binds)) (nth 1 (car binds)) nil)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1085
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1086
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1087 (put 'nth 'byte-optimizer 'byte-optimize-nth)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1088 (defun byte-optimize-nth (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1089 (if (and (= (safe-length form) 3) (memq (nth 1 form) '(0 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1090 (list 'car (if (zerop (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1091 (nth 2 form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1092 (list 'cdr (nth 2 form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1093 (byte-optimize-predicate form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1094
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1095 (put 'nthcdr 'byte-optimizer 'byte-optimize-nthcdr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1096 (defun byte-optimize-nthcdr (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1097 (if (and (= (safe-length form) 3) (not (memq (nth 1 form) '(0 1 2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1098 (byte-optimize-predicate form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1099 (let ((count (nth 1 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1100 (setq form (nth 2 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1101 (while (>= (setq count (1- count)) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1102 (setq form (list 'cdr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1103 form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1104
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1105 ;;; enumerating those functions which need not be called if the returned
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1106 ;;; value is not used. That is, something like
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1107 ;;; (progn (list (something-with-side-effects) (yow))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1108 ;;; (foo))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1109 ;;; may safely be turned into
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1110 ;;; (progn (progn (something-with-side-effects) (yow))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1111 ;;; (foo))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1112 ;;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1113
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1114 ;;; I wonder if I missed any :-\)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1115 (let ((side-effect-free-fns
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1116 '(% * + - / /= 1+ 1- < <= = > >= abs acos append aref ash asin atan
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1117 assoc assq
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1118 boundp buffer-file-name buffer-local-variables buffer-modified-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1119 buffer-substring
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1120 capitalize car-less-than-car car cdr ceiling concat
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1121 ;; coordinates-in-window-p not in XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1122 copy-marker cos count-lines
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1123 default-boundp default-value documentation downcase
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1124 elt exp expt fboundp featurep
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1125 file-directory-p file-exists-p file-locked-p file-name-absolute-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1126 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1127 float floor format
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1128 get get-buffer get-buffer-window getenv get-file-buffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1129 ;; hash-table functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1130 make-hash-table copy-hash-table
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1131 gethash
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1132 hash-table-count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1133 hash-table-rehash-size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1134 hash-table-rehash-threshold
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1135 hash-table-size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1136 hash-table-test
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1137 hash-table-type
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1138 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1139 int-to-string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1140 length log log10 logand logb logior lognot logxor lsh
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1141 marker-buffer max member memq min mod
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1142 next-window nth nthcdr number-to-string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1143 parse-colon-path previous-window
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1144 radians-to-degrees rassq regexp-quote reverse round
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1145 sin sqrt string< string= string-equal string-lessp string-to-char
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1146 string-to-int string-to-number substring symbol-plist
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1147 tan upcase user-variable-p vconcat
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1148 ;; XEmacs change: window-edges -> window-pixel-edges
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1149 window-buffer window-dedicated-p window-pixel-edges window-height
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1150 window-hscroll window-minibuffer-p window-width
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1151 zerop
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1152 ;; functions defined by cl
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1153 oddp evenp plusp minusp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1154 abs expt signum last butlast ldiff
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1155 pairlis gcd lcm
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1156 isqrt floor* ceiling* truncate* round* mod* rem* subseq
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1157 list-length get* getf
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1158 ))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1159 (side-effect-and-error-free-fns
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1160 '(arrayp atom
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1161 bobp bolp buffer-end buffer-list buffer-size buffer-string bufferp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1162 car-safe case-table-p cdr-safe char-or-string-p char-table-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1163 characterp commandp cons
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1164 consolep console-live-p consp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1165 current-buffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1166 ;; XEmacs: extent functions, frame-live-p, various other stuff
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1167 devicep device-live-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1168 dot dot-marker eobp eolp eq eql equal eventp extentp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1169 extent-live-p floatp framep frame-live-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1170 get-largest-window get-lru-window
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1171 hash-table-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1172 identity ignore integerp integer-or-marker-p interactive-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1173 invocation-directory invocation-name
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1174 ;; keymapp may autoload in XEmacs, so not on this list!
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1175 list listp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1176 make-marker mark mark-marker markerp memory-limit minibuffer-window
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1177 ;; mouse-movement-p not in XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1178 natnump nlistp not null number-or-marker-p numberp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1179 one-window-p ;; overlayp not in XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1180 point point-marker point-min point-max processp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1181 range-table-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1182 selected-window sequencep stringp subrp symbolp syntax-table-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1183 user-full-name user-login-name user-original-login-name
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1184 user-real-login-name user-real-uid user-uid
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1185 vector vectorp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1186 window-configuration-p window-live-p windowp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1187 ;; Functions defined by cl
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1188 eql floatp-safe list* subst acons equalp random-state-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1189 copy-tree sublis
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1190 )))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1191 (dolist (fn side-effect-free-fns)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1192 (put fn 'side-effect-free t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1193 (dolist (fn side-effect-and-error-free-fns)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1194 (put fn 'side-effect-free 'error-free)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1195
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1196
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1197 (defun byte-compile-splice-in-already-compiled-code (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1198 ;; form is (byte-code "..." [...] n)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1199 (if (not (memq byte-optimize '(t lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1200 (byte-compile-normal-call form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1201 (byte-inline-lapcode
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1202 (byte-decompile-bytecode-1 (nth 1 form) (nth 2 form) t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1203 (setq byte-compile-maxdepth (max (+ byte-compile-depth (nth 3 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1204 byte-compile-maxdepth))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1205 (setq byte-compile-depth (1+ byte-compile-depth))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1206
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1207 (put 'byte-code 'byte-compile 'byte-compile-splice-in-already-compiled-code)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1208
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1209
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1210 (defconst byte-constref-ops
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1211 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1212
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1213 ;;; This function extracts the bitfields from variable-length opcodes.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1214 ;;; Originally defined in disass.el (which no longer uses it.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1215
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1216 (defun disassemble-offset ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1217 "Don't call this!"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1218 ;; fetch and return the offset for the current opcode.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1219 ;; return NIL if this opcode has no offset
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1220 ;; OP, PTR and BYTES are used and set dynamically
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1221 (defvar op)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1222 (defvar ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1223 (defvar bytes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1224 (cond ((< op byte-nth)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1225 (let ((tem (logand op 7)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1226 (setq op (logand op 248))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1227 (cond ((eq tem 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1228 (setq ptr (1+ ptr)) ;offset in next byte
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1229 ;; char-to-int to avoid downstream problems
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1230 ;; caused by chars appearing where ints are
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1231 ;; expected. In bytecode the bytes in the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1232 ;; opcode string are always interpreted as ints.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1233 (char-to-int (aref bytes ptr)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1234 ((eq tem 7)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1235 (setq ptr (1+ ptr)) ;offset in next 2 bytes
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1236 (+ (aref bytes ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1237 (progn (setq ptr (1+ ptr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1238 (lsh (aref bytes ptr) 8))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1239 (t tem)))) ;offset was in opcode
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1240 ((>= op byte-constant)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1241 (prog1 (- op byte-constant) ;offset in opcode
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1242 (setq op byte-constant)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1243 ((and (>= op byte-constant2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1244 (<= op byte-goto-if-not-nil-else-pop))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1245 (setq ptr (1+ ptr)) ;offset in next 2 bytes
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1246 (+ (aref bytes ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1247 (progn (setq ptr (1+ ptr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1248 (lsh (aref bytes ptr) 8))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1249 ;; XEmacs: this code was here before. FSF's first comparison
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1250 ;; is (>= op byte-listN). It appears that the rel-goto stuff
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1251 ;; does not exist in FSF 19.30. It doesn't exist in 19.28
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1252 ;; either, so I'm going to assume that this is an improvement
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1253 ;; on our part and leave it in. --ben
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1254 ((and (>= op byte-rel-goto)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1255 (<= op byte-insertN))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1256 (setq ptr (1+ ptr)) ;offset in next byte
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1257 ;; Use char-to-int to avoid downstream problems caused by
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1258 ;; chars appearing where ints are expected. In bytecode
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1259 ;; the bytes in the opcode string are always interpreted as
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1260 ;; ints.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1261 (char-to-int (aref bytes ptr)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1262
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1263
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1264 ;;; This de-compiler is used for inline expansion of compiled functions,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1265 ;;; and by the disassembler.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1266 ;;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1267 ;;; This list contains numbers, which are pc values,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1268 ;;; before each instruction.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1269 (defun byte-decompile-bytecode (bytes constvec)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1270 "Turns BYTECODE into lapcode, referring to CONSTVEC."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1271 (let ((byte-compile-constants nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1272 (byte-compile-variables nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1273 (byte-compile-tag-number 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1274 (byte-decompile-bytecode-1 bytes constvec)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1275
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1276 ;; As byte-decompile-bytecode, but updates
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1277 ;; byte-compile-{constants, variables, tag-number}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1278 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1279 ;; with `goto's destined for the end of the code.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1280 ;; That is for use by the compiler.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1281 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1282 ;; In that case, we put a pc value into the list
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1283 ;; before each insn (or its label).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1284 (defun byte-decompile-bytecode-1 (bytes constvec &optional make-spliceable)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1285 (let ((length (length bytes))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1286 (ptr 0) optr tags op offset
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1287 ;; tag unused
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1288 lap tmp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1289 endtag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1290 ;; (retcount 0) unused
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1291 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1292 (while (not (= ptr length))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1293 (or make-spliceable
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1294 (setq lap (cons ptr lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1295 (setq op (aref bytes ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1296 optr ptr
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1297 offset (disassemble-offset)) ; this does dynamic-scope magic
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1298 (setq op (aref byte-code-vector op))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1299 ;; XEmacs: the next line in FSF 19.30 reads
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1300 ;; (cond ((memq op byte-goto-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1301 ;; see the comment above about byte-rel-goto in XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1302 (cond ((or (memq op byte-goto-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1303 (cond ((memq op byte-rel-goto-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1304 (setq op (aref byte-code-vector
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1305 (- (symbol-value op)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1306 (- byte-rel-goto byte-goto))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1307 (setq offset (+ ptr (- offset 127)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1308 t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1309 ;; it's a pc
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1310 (setq offset
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1311 (cdr (or (assq offset tags)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1312 (car (setq tags
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1313 (cons (cons offset
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1314 (byte-compile-make-tag))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1315 tags)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1316 ((cond ((eq op 'byte-constant2) (setq op 'byte-constant) t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1317 ((memq op byte-constref-ops)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1318 (setq tmp (aref constvec offset)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1319 offset (if (eq op 'byte-constant)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1320 (byte-compile-get-constant tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1321 (or (assq tmp byte-compile-variables)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1322 (car (setq byte-compile-variables
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1323 (cons (list tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1324 byte-compile-variables)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1325 ((and make-spliceable
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1326 (eq op 'byte-return))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1327 (if (= ptr (1- length))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1328 (setq op nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1329 (setq offset (or endtag (setq endtag (byte-compile-make-tag)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1330 op 'byte-goto))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1331 ;; lap = ( [ (pc . (op . arg)) ]* )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1332 (setq lap (cons (cons optr (cons op (or offset 0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1333 lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1334 (setq ptr (1+ ptr)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1335 ;; take off the dummy nil op that we replaced a trailing "return" with.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1336 (let ((rest lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1337 (while rest
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1338 (cond ((numberp (car rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1339 ((setq tmp (assq (car (car rest)) tags))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1340 ;; this addr is jumped to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1341 (setcdr rest (cons (cons nil (cdr tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1342 (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1343 (setq tags (delq tmp tags))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1344 (setq rest (cdr rest))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1345 (setq rest (cdr rest))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1346 (if tags (error "optimizer error: missed tags %s" tags))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1347 (if (null (car (cdr (car lap))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1348 (setq lap (cdr lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1349 (if endtag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1350 (setq lap (cons (cons nil endtag) lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1351 ;; remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1352 (mapcar #'(lambda (elt) (if (numberp elt) elt (cdr elt)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1353 (nreverse lap))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1354
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1355
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1356 ;;; peephole optimizer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1357
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1358 (defconst byte-tagref-ops (cons 'TAG byte-goto-ops))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1359
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1360 (defconst byte-conditional-ops
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1361 '(byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1362 byte-goto-if-not-nil-else-pop))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1363
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1364 (defconst byte-after-unbind-ops
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1365 '(byte-constant byte-dup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1366 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-integerp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1367 byte-eq byte-equal byte-not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1368 byte-cons byte-list1 byte-list2 ; byte-list3 byte-list4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1369 byte-interactive-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1370 ;; How about other side-effect-free-ops? Is it safe to move an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1371 ;; error invocation (such as from nth) out of an unwind-protect?
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1372 "Byte-codes that can be moved past an unbind.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1373
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1374 (defconst byte-compile-side-effect-and-error-free-ops
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1375 '(byte-constant byte-dup byte-symbolp byte-consp byte-stringp byte-listp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1376 byte-integerp byte-numberp byte-eq byte-equal byte-not byte-car-safe
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1377 byte-cdr-safe byte-cons byte-list1 byte-list2 byte-point byte-point-max
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1378 byte-point-min byte-following-char byte-preceding-char
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1379 byte-current-column byte-eolp byte-eobp byte-bolp byte-bobp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1380 byte-current-buffer byte-interactive-p))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1381
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1382 (defconst byte-compile-side-effect-free-ops
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1383 (nconc
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1384 '(byte-varref byte-nth byte-memq byte-car byte-cdr byte-length byte-aref
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1385 byte-symbol-value byte-get byte-concat2 byte-concat3 byte-sub1 byte-add1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1386 byte-eqlsign byte-gtr byte-lss byte-leq byte-geq byte-diff byte-negate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1387 byte-plus byte-max byte-min byte-mult byte-char-after byte-char-syntax
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1388 byte-buffer-substring byte-string= byte-string< byte-nthcdr byte-elt
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1389 byte-member byte-assq byte-quo byte-rem)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1390 byte-compile-side-effect-and-error-free-ops))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1391
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1392 ;;; This piece of shit is because of the way DEFVAR_BOOL() variables work.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1393 ;;; Consider the code
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1394 ;;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1395 ;;; (defun foo (flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1396 ;;; (let ((old-pop-ups pop-up-windows)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1397 ;;; (pop-up-windows flag))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1398 ;;; (cond ((not (eq pop-up-windows old-pop-ups))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1399 ;;; (setq old-pop-ups pop-up-windows)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1400 ;;; ...))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1401 ;;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1402 ;;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1403 ;;; something else. But if we optimize
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1404 ;;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1405 ;;; varref flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1406 ;;; varbind pop-up-windows
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1407 ;;; varref pop-up-windows
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1408 ;;; not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1409 ;;; to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1410 ;;; varref flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1411 ;;; dup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1412 ;;; varbind pop-up-windows
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1413 ;;; not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1414 ;;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1415 ;;; we break the program, because it will appear that pop-up-windows and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1416 ;;; old-pop-ups are not EQ when really they are. So we have to know what
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1417 ;;; the BOOL variables are, and not perform this optimization on them.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1418 ;;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1419
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1420 ;;; This used to hold a large list of boolean variables, which had to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1421 ;;; be updated every time a new DEFVAR_BOOL is added, making it very
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1422 ;;; hard to maintain. Such a list is not necessary under XEmacs,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1423 ;;; where we can use `built-in-variable-type' to query for boolean
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1424 ;;; variables.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1425
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1426 ;(defconst byte-boolean-vars
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1427 ; '(abbrev-all-caps purify-flag find-file-compare-truenames
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1428 ; find-file-use-truenames delete-auto-save-files byte-metering-on
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1429 ; x-seppuku-on-epipe zmacs-regions zmacs-region-active-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1430 ; zmacs-region-stays atomic-extent-goto-char-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1431 ; suppress-early-error-handler-backtrace noninteractive
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1432 ; inhibit-early-packages inhibit-autoloads debug-paths
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1433 ; inhibit-site-lisp debug-on-quit debug-on-next-call
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1434 ; modifier-keys-are-sticky x-allow-sendevents
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1435 ; mswindows-dynamic-frame-resize focus-follows-mouse
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1436 ; inhibit-input-event-recording enable-multibyte-characters
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1437 ; disable-auto-save-when-buffer-shrinks
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1438 ; allow-deletion-of-last-visible-frame indent-tabs-mode
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1439 ; load-in-progress load-warn-when-source-newer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1440 ; load-warn-when-source-only load-ignore-elc-files
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1441 ; load-force-doc-strings fail-on-bucky-bit-character-escapes
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1442 ; popup-menu-titles menubar-show-keybindings completion-ignore-case
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1443 ; canna-empty-info canna-through-info canna-underline
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1444 ; canna-inhibit-hankakukana enable-multibyte-characters
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1445 ; re-short-flag x-handle-non-fully-specified-fonts
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1446 ; print-escape-newlines print-readably delete-exited-processes
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1447 ; windowed-process-io visible-bell no-redraw-on-reenter
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1448 ; cursor-in-echo-area inhibit-warning-display
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1449 ; column-number-start-at-one parse-sexp-ignore-comments
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1450 ; words-include-escapes scroll-on-clipped-lines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1451 ; "DEFVAR_BOOL variables. Giving these any non-nil value sets them to t.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1452 ;If this does not enumerate all DEFVAR_BOOL variables, the byte-optimizer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1453 ;may generate incorrect code.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1454
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1455 (defun byte-optimize-lapcode (lap &optional for-effect)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1456 "Simple peephole optimizer. LAP is both modified and returned."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1457 (let (lap0 ;; off0 unused
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1458 lap1 ;; off1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1459 lap2 ;; off2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1460 (keep-going 'first-time)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1461 (add-depth 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1462 rest tmp tmp2 tmp3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1463 (side-effect-free (if byte-compile-delete-errors
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1464 byte-compile-side-effect-free-ops
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1465 byte-compile-side-effect-and-error-free-ops)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1466 (while keep-going
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1467 (or (eq keep-going 'first-time)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1468 (byte-compile-log-lap " ---- next pass"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1469 (setq rest lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1470 keep-going nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1471 (while rest
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1472 (setq lap0 (car rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1473 lap1 (nth 1 rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1474 lap2 (nth 2 rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1475
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1476 ;; You may notice that sequences like "dup varset discard" are
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1477 ;; optimized but sequences like "dup varset TAG1: discard" are not.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1478 ;; You may be tempted to change this; resist that temptation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1479 (cond ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1480 ;; <side-effect-free> pop --> <deleted>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1481 ;; ...including:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1482 ;; const-X pop --> <deleted>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1483 ;; varref-X pop --> <deleted>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1484 ;; dup pop --> <deleted>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1485 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1486 ((and (eq 'byte-discard (car lap1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1487 (memq (car lap0) side-effect-free))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1488 (setq keep-going t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1489 (setq tmp (aref byte-stack+-info (symbol-value (car lap0))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1490 (setq rest (cdr rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1491 (cond ((= tmp 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1492 (byte-compile-log-lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1493 " %s discard\t-->\t<deleted>" lap0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1494 (setq lap (delq lap0 (delq lap1 lap))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1495 ((= tmp 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1496 (byte-compile-log-lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1497 " %s discard\t-->\t<deleted> discard" lap0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1498 (setq lap (delq lap0 lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1499 ((= tmp -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1500 (byte-compile-log-lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1501 " %s discard\t-->\tdiscard discard" lap0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1502 (setcar lap0 'byte-discard)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1503 (setcdr lap0 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1504 ((error "Optimizer error: too much on the stack"))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1505 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1506 ;; goto*-X X: --> X:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1507 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1508 ((and (memq (car lap0) byte-goto-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1509 (eq (cdr lap0) lap1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1510 (cond ((eq (car lap0) 'byte-goto)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1511 (setq lap (delq lap0 lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1512 (setq tmp "<deleted>"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1513 ((memq (car lap0) byte-goto-always-pop-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1514 (setcar lap0 (setq tmp 'byte-discard))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1515 (setcdr lap0 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1516 ((error "Depth conflict at tag %d" (nth 2 lap0))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1517 (and (memq byte-optimize-log '(t byte))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1518 (byte-compile-log " (goto %s) %s:\t-->\t%s %s:"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1519 (nth 1 lap1) (nth 1 lap1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1520 tmp (nth 1 lap1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1521 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1522 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1523 ;; varset-X varref-X --> dup varset-X
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1524 ;; varbind-X varref-X --> dup varbind-X
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1525 ;; const/dup varset-X varref-X --> const/dup varset-X const/dup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1526 ;; const/dup varbind-X varref-X --> const/dup varbind-X const/dup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1527 ;; The latter two can enable other optimizations.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1528 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1529 ((and (eq 'byte-varref (car lap2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1530 (eq (cdr lap1) (cdr lap2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1531 (memq (car lap1) '(byte-varset byte-varbind)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1532 (if (and (setq tmp (eq (built-in-variable-type (car (cdr lap2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1533 'boolean))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1534 (not (eq (car lap0) 'byte-constant)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1535 nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1536 (setq keep-going t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1537 (if (memq (car lap0) '(byte-constant byte-dup))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1538 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1539 (setq tmp (if (or (not tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1540 (memq (car (cdr lap0)) '(nil t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1541 (cdr lap0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1542 (byte-compile-get-constant t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1543 (byte-compile-log-lap " %s %s %s\t-->\t%s %s %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1544 lap0 lap1 lap2 lap0 lap1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1545 (cons (car lap0) tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1546 (setcar lap2 (car lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1547 (setcdr lap2 tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1548 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1549 (setcar lap2 (car lap1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1550 (setcar lap1 'byte-dup)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1551 (setcdr lap1 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1552 ;; The stack depth gets locally increased, so we will
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1553 ;; increase maxdepth in case depth = maxdepth here.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1554 ;; This can cause the third argument to byte-code to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1555 ;; be larger than necessary.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1556 (setq add-depth 1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1557 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1558 ;; dup varset-X discard --> varset-X
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1559 ;; dup varbind-X discard --> varbind-X
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1560 ;; (the varbind variant can emerge from other optimizations)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1561 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1562 ((and (eq 'byte-dup (car lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1563 (eq 'byte-discard (car lap2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1564 (memq (car lap1) '(byte-varset byte-varbind)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1565 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1566 (setq keep-going t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1567 rest (cdr rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1568 (setq lap (delq lap0 (delq lap2 lap))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1569 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1570 ;; not goto-X-if-nil --> goto-X-if-non-nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1571 ;; not goto-X-if-non-nil --> goto-X-if-nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1572 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1573 ;; it is wrong to do the same thing for the -else-pop variants.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1574 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1575 ((and (eq 'byte-not (car lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1576 (or (eq 'byte-goto-if-nil (car lap1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1577 (eq 'byte-goto-if-not-nil (car lap1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1578 (byte-compile-log-lap " not %s\t-->\t%s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1579 lap1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1580 (cons
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1581 (if (eq (car lap1) 'byte-goto-if-nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1582 'byte-goto-if-not-nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1583 'byte-goto-if-nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1584 (cdr lap1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1585 (setcar lap1 (if (eq (car lap1) 'byte-goto-if-nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1586 'byte-goto-if-not-nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1587 'byte-goto-if-nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1588 (setq lap (delq lap0 lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1589 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1590 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1591 ;; goto-X-if-nil goto-Y X: --> goto-Y-if-non-nil X:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1592 ;; goto-X-if-non-nil goto-Y X: --> goto-Y-if-nil X:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1593 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1594 ;; it is wrong to do the same thing for the -else-pop variants.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1595 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1596 ((and (or (eq 'byte-goto-if-nil (car lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1597 (eq 'byte-goto-if-not-nil (car lap0))) ; gotoX
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1598 (eq 'byte-goto (car lap1)) ; gotoY
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1599 (eq (cdr lap0) lap2)) ; TAG X
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1600 (let ((inverse (if (eq 'byte-goto-if-nil (car lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1601 'byte-goto-if-not-nil 'byte-goto-if-nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1602 (byte-compile-log-lap " %s %s %s:\t-->\t%s %s:"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1603 lap0 lap1 lap2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1604 (cons inverse (cdr lap1)) lap2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1605 (setq lap (delq lap0 lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1606 (setcar lap1 inverse)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1607 (setq keep-going t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1608 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1609 ;; const goto-if-* --> whatever
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1610 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1611 ((and (eq 'byte-constant (car lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1612 (memq (car lap1) byte-conditional-ops))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1613 (cond ((if (or (eq (car lap1) 'byte-goto-if-nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1614 (eq (car lap1) 'byte-goto-if-nil-else-pop))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1615 (car (cdr lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1616 (not (car (cdr lap0))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1617 (byte-compile-log-lap " %s %s\t-->\t<deleted>"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1618 lap0 lap1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1619 (setq rest (cdr rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1620 lap (delq lap0 (delq lap1 lap))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1621 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1622 (if (memq (car lap1) byte-goto-always-pop-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1623 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1624 (byte-compile-log-lap " %s %s\t-->\t%s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1625 lap0 lap1 (cons 'byte-goto (cdr lap1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1626 (setq lap (delq lap0 lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1627 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1628 (cons 'byte-goto (cdr lap1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1629 (setcar lap1 'byte-goto)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1630 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1631 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1632 ;; varref-X varref-X --> varref-X dup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1633 ;; varref-X [dup ...] varref-X --> varref-X [dup ...] dup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1634 ;; We don't optimize the const-X variations on this here,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1635 ;; because that would inhibit some goto optimizations; we
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1636 ;; optimize the const-X case after all other optimizations.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1637 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1638 ((and (eq 'byte-varref (car lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1639 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1640 (setq tmp (cdr rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1641 (while (eq (car (car tmp)) 'byte-dup)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1642 (setq tmp (cdr tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1643 t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1644 (eq (cdr lap0) (cdr (car tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1645 (eq 'byte-varref (car (car tmp))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1646 (if (memq byte-optimize-log '(t byte))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1647 (let ((str ""))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1648 (setq tmp2 (cdr rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1649 (while (not (eq tmp tmp2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1650 (setq tmp2 (cdr tmp2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1651 str (concat str " dup")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1652 (byte-compile-log-lap " %s%s %s\t-->\t%s%s dup"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1653 lap0 str lap0 lap0 str)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1654 (setq keep-going t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1655 (setcar (car tmp) 'byte-dup)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1656 (setcdr (car tmp) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1657 (setq rest tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1658 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1659 ;; TAG1: TAG2: --> TAG1: <deleted>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1660 ;; (and other references to TAG2 are replaced with TAG1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1661 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1662 ((and (eq (car lap0) 'TAG)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1663 (eq (car lap1) 'TAG))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1664 (and (memq byte-optimize-log '(t byte))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1665 (byte-compile-log " adjacent tags %d and %d merged"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1666 (nth 1 lap1) (nth 1 lap0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1667 (setq tmp3 lap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1668 (while (setq tmp2 (rassq lap0 tmp3))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1669 (setcdr tmp2 lap1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1670 (setq tmp3 (cdr (memq tmp2 tmp3))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1671 (setq lap (delq lap0 lap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1672 keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1673 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1674 ;; unused-TAG: --> <deleted>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1675 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1676 ((and (eq 'TAG (car lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1677 (not (rassq lap0 lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1678 (and (memq byte-optimize-log '(t byte))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1679 (byte-compile-log " unused tag %d removed" (nth 1 lap0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1680 (setq lap (delq lap0 lap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1681 keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1682 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1683 ;; goto ... --> goto <delete until TAG or end>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1684 ;; return ... --> return <delete until TAG or end>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1685 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1686 ((and (memq (car lap0) '(byte-goto byte-return))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1687 (not (memq (car lap1) '(TAG nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1688 (setq tmp rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1689 (let ((i 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1690 (opt-p (memq byte-optimize-log '(t lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1691 str deleted)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1692 (while (and (setq tmp (cdr tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1693 (not (eq 'TAG (car (car tmp)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1694 (if opt-p (setq deleted (cons (car tmp) deleted)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1695 str (concat str " %s")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1696 i (1+ i))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1697 (if opt-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1698 (let ((tagstr
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1699 (if (eq 'TAG (car (car tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1700 (format "%d:" (car (cdr (car tmp))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1701 (or (car tmp) ""))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1702 (if (< i 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1703 (apply 'byte-compile-log-lap-1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1704 (concat " %s" str
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1705 " %s\t-->\t%s <deleted> %s")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1706 lap0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1707 (nconc (nreverse deleted)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1708 (list tagstr lap0 tagstr)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1709 (byte-compile-log-lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1710 " %s <%d unreachable op%s> %s\t-->\t%s <deleted> %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1711 lap0 i (if (= i 1) "" "s")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1712 tagstr lap0 tagstr))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1713 (rplacd rest tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1714 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1715 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1716 ;; <safe-op> unbind --> unbind <safe-op>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1717 ;; (this may enable other optimizations.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1718 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1719 ((and (eq 'byte-unbind (car lap1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1720 (memq (car lap0) byte-after-unbind-ops))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1721 (byte-compile-log-lap " %s %s\t-->\t%s %s" lap0 lap1 lap1 lap0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1722 (setcar rest lap1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1723 (setcar (cdr rest) lap0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1724 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1725 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1726 ;; varbind-X unbind-N --> discard unbind-(N-1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1727 ;; save-excursion unbind-N --> unbind-(N-1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1728 ;; save-restriction unbind-N --> unbind-(N-1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1729 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1730 ((and (eq 'byte-unbind (car lap1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1731 (memq (car lap0) '(byte-varbind byte-save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1732 byte-save-restriction))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1733 (< 0 (cdr lap1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1734 (if (zerop (setcdr lap1 (1- (cdr lap1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1735 (delq lap1 rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1736 (if (eq (car lap0) 'byte-varbind)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1737 (setcar rest (cons 'byte-discard 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1738 (setq lap (delq lap0 lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1739 (byte-compile-log-lap " %s %s\t-->\t%s %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1740 lap0 (cons (car lap1) (1+ (cdr lap1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1741 (if (eq (car lap0) 'byte-varbind)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1742 (car rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1743 (car (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1744 (if (and (/= 0 (cdr lap1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1745 (eq (car lap0) 'byte-varbind))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1746 (car (cdr rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1747 ""))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1748 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1749 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1750 ;; goto*-X ... X: goto-Y --> goto*-Y
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1751 ;; goto-X ... X: return --> return
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1752 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1753 ((and (memq (car lap0) byte-goto-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1754 (memq (car (setq tmp (nth 1 (memq (cdr lap0) lap))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1755 '(byte-goto byte-return)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1756 (cond ((and (not (eq tmp lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1757 (or (eq (car lap0) 'byte-goto)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1758 (eq (car tmp) 'byte-goto)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1759 (byte-compile-log-lap " %s [%s]\t-->\t%s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1760 (car lap0) tmp tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1761 (if (eq (car tmp) 'byte-return)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1762 (setcar lap0 'byte-return))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1763 (setcdr lap0 (cdr tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1764 (setq keep-going t))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1765 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1766 ;; goto-*-else-pop X ... X: goto-if-* --> whatever
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1767 ;; goto-*-else-pop X ... X: discard --> whatever
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1768 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1769 ((and (memq (car lap0) '(byte-goto-if-nil-else-pop
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1770 byte-goto-if-not-nil-else-pop))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1771 (memq (car (car (setq tmp (cdr (memq (cdr lap0) lap)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1772 (eval-when-compile
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1773 (cons 'byte-discard byte-conditional-ops)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1774 (not (eq lap0 (car tmp))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1775 (setq tmp2 (car tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1776 (setq tmp3 (assq (car lap0) '((byte-goto-if-nil-else-pop
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1777 byte-goto-if-nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1778 (byte-goto-if-not-nil-else-pop
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1779 byte-goto-if-not-nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1780 (if (memq (car tmp2) tmp3)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1781 (progn (setcar lap0 (car tmp2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1782 (setcdr lap0 (cdr tmp2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1783 (byte-compile-log-lap " %s-else-pop [%s]\t-->\t%s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1784 (car lap0) tmp2 lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1785 ;; Get rid of the -else-pop's and jump one step further.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1786 (or (eq 'TAG (car (nth 1 tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1787 (setcdr tmp (cons (byte-compile-make-tag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1788 (cdr tmp))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1789 (byte-compile-log-lap " %s [%s]\t-->\t%s <skip>"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1790 (car lap0) tmp2 (nth 1 tmp3))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1791 (setcar lap0 (nth 1 tmp3))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1792 (setcdr lap0 (nth 1 tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1793 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1794 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1795 ;; const goto-X ... X: goto-if-* --> whatever
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1796 ;; const goto-X ... X: discard --> whatever
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1797 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1798 ((and (eq (car lap0) 'byte-constant)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1799 (eq (car lap1) 'byte-goto)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1800 (memq (car (car (setq tmp (cdr (memq (cdr lap1) lap)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1801 (eval-when-compile
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1802 (cons 'byte-discard byte-conditional-ops)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1803 (not (eq lap1 (car tmp))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1804 (setq tmp2 (car tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1805 (cond ((memq (car tmp2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1806 (if (null (car (cdr lap0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1807 '(byte-goto-if-nil byte-goto-if-nil-else-pop)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1808 '(byte-goto-if-not-nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1809 byte-goto-if-not-nil-else-pop)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1810 (byte-compile-log-lap " %s goto [%s]\t-->\t%s %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1811 lap0 tmp2 lap0 tmp2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1812 (setcar lap1 (car tmp2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1813 (setcdr lap1 (cdr tmp2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1814 ;; Let next step fix the (const,goto-if*) sequence.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1815 (setq rest (cons nil rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1816 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1817 ;; Jump one step further
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1818 (byte-compile-log-lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1819 " %s goto [%s]\t-->\t<deleted> goto <skip>"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1820 lap0 tmp2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1821 (or (eq 'TAG (car (nth 1 tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1822 (setcdr tmp (cons (byte-compile-make-tag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1823 (cdr tmp))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1824 (setcdr lap1 (car (cdr tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1825 (setq lap (delq lap0 lap))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1826 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1827 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1828 ;; X: varref-Y ... varset-Y goto-X -->
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1829 ;; X: varref-Y Z: ... dup varset-Y goto-Z
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1830 ;; (varset-X goto-BACK, BACK: varref-X --> copy the varref down.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1831 ;; (This is so usual for while loops that it is worth handling).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1832 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1833 ((and (eq (car lap1) 'byte-varset)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1834 (eq (car lap2) 'byte-goto)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1835 (not (memq (cdr lap2) rest)) ;Backwards jump
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1836 (eq (car (car (setq tmp (cdr (memq (cdr lap2) lap)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1837 'byte-varref)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1838 (eq (cdr (car tmp)) (cdr lap1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1839 (not (eq (built-in-variable-type (car (cdr lap1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1840 'boolean)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1841 ;;(byte-compile-log-lap " Pulled %s to end of loop" (car tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1842 (let ((newtag (byte-compile-make-tag)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1843 (byte-compile-log-lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1844 " %s: %s ... %s %s\t-->\t%s: %s %s: ... %s %s %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1845 (nth 1 (cdr lap2)) (car tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1846 lap1 lap2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1847 (nth 1 (cdr lap2)) (car tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1848 (nth 1 newtag) 'byte-dup lap1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1849 (cons 'byte-goto newtag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1850 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1851 (setcdr rest (cons (cons 'byte-dup 0) (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1852 (setcdr tmp (cons (setcdr lap2 newtag) (cdr tmp))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1853 (setq add-depth 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1854 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1855 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1856 ;; goto-X Y: ... X: goto-if*-Y --> goto-if-not-*-X+1 Y:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1857 ;; (This can pull the loop test to the end of the loop)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1858 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1859 ((and (eq (car lap0) 'byte-goto)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1860 (eq (car lap1) 'TAG)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1861 (eq lap1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1862 (cdr (car (setq tmp (cdr (memq (cdr lap0) lap))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1863 (memq (car (car tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1864 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1865 byte-goto-if-nil-else-pop)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1866 ;; (byte-compile-log-lap " %s %s, %s %s --> moved conditional"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1867 ;; lap0 lap1 (cdr lap0) (car tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1868 (let ((newtag (byte-compile-make-tag)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1869 (byte-compile-log-lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1870 "%s %s: ... %s: %s\t-->\t%s ... %s:"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1871 lap0 (nth 1 lap1) (nth 1 (cdr lap0)) (car tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1872 (cons (cdr (assq (car (car tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1873 '((byte-goto-if-nil . byte-goto-if-not-nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1874 (byte-goto-if-not-nil . byte-goto-if-nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1875 (byte-goto-if-nil-else-pop .
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1876 byte-goto-if-not-nil-else-pop)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1877 (byte-goto-if-not-nil-else-pop .
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1878 byte-goto-if-nil-else-pop))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1879 newtag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1880
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1881 (nth 1 newtag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1882 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1883 (setcdr tmp (cons (setcdr lap0 newtag) (cdr tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1884 (if (eq (car (car tmp)) 'byte-goto-if-nil-else-pop)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1885 ;; We can handle this case but not the -if-not-nil case,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1886 ;; because we won't know which non-nil constant to push.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1887 (setcdr rest (cons (cons 'byte-constant
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1888 (byte-compile-get-constant nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1889 (cdr rest))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1890 (setcar lap0 (nth 1 (memq (car (car tmp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1891 '(byte-goto-if-nil-else-pop
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1892 byte-goto-if-not-nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1893 byte-goto-if-nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1894 byte-goto-if-not-nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1895 byte-goto byte-goto))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1896 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1897 (setq keep-going t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1898 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1899 (setq rest (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1900 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1901 ;; Cleanup stage:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1902 ;; Rebuild byte-compile-constants / byte-compile-variables.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1903 ;; Simple optimizations that would inhibit other optimizations if they
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1904 ;; were done in the optimizing loop, and optimizations which there is no
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1905 ;; need to do more than once.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1906 (setq byte-compile-constants nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1907 byte-compile-variables nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1908 (setq rest lap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1909 (while rest
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1910 (setq lap0 (car rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1911 lap1 (nth 1 rest))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1912 (if (memq (car lap0) byte-constref-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1913 (if (eq (cdr lap0) 'byte-constant)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1914 (or (memq (cdr lap0) byte-compile-variables)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1915 (setq byte-compile-variables (cons (cdr lap0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1916 byte-compile-variables)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1917 (or (memq (cdr lap0) byte-compile-constants)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1918 (setq byte-compile-constants (cons (cdr lap0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1919 byte-compile-constants)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1920 (cond (;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1921 ;; const-C varset-X const-C --> const-C dup varset-X
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1922 ;; const-C varbind-X const-C --> const-C dup varbind-X
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1923 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1924 (and (eq (car lap0) 'byte-constant)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1925 (eq (car (nth 2 rest)) 'byte-constant)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1926 (eq (cdr lap0) (car (nth 2 rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1927 (memq (car lap1) '(byte-varbind byte-varset)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1928 (byte-compile-log-lap " %s %s %s\t-->\t%s dup %s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1929 lap0 lap1 lap0 lap0 lap1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1930 (setcar (cdr (cdr rest)) (cons (car lap1) (cdr lap1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1931 (setcar (cdr rest) (cons 'byte-dup 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1932 (setq add-depth 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1933 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1934 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1935 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1936 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1937 ((memq (car lap0) '(byte-constant byte-varref))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1938 (setq tmp rest
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1939 tmp2 nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1940 (while (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1941 (while (eq 'byte-dup (car (car (setq tmp (cdr tmp))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1942 (and (eq (cdr lap0) (cdr (car tmp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1943 (eq (car lap0) (car (car tmp)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1944 (setcar tmp (cons 'byte-dup 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1945 (setq tmp2 t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1946 (if tmp2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1947 (byte-compile-log-lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1948 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1949 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1950 ;; unbind-N unbind-M --> unbind-(N+M)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1951 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1952 ((and (eq 'byte-unbind (car lap0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1953 (eq 'byte-unbind (car lap1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1954 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1955 (cons 'byte-unbind
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1956 (+ (cdr lap0) (cdr lap1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1957 (setq keep-going t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1958 (setq lap (delq lap0 lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1959 (setcdr lap1 (+ (cdr lap1) (cdr lap0))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1960 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1961 (setq rest (cdr rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1962 (setq byte-compile-maxdepth (+ byte-compile-maxdepth add-depth)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1963 lap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1964
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1965 (provide 'byte-optimize)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1966
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1967
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1968 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when this file compiles
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1969 ;; itself, compile some of its most used recursive functions (at load time).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1970 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1971 (eval-when-compile
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1972 (or (compiled-function-p (symbol-function 'byte-optimize-form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1973 (assq 'byte-code (symbol-function 'byte-optimize-form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1974 (let ((byte-optimize nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1975 (byte-compile-warnings nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1976 (mapcar
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1977 #'(lambda (x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1978 (or noninteractive (message "compiling %s..." x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1979 (byte-compile x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1980 (or noninteractive (message "compiling %s...done" x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1981 '(byte-optimize-form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1982 byte-optimize-body
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1983 byte-optimize-predicate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1984 byte-optimize-binary-predicate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1985 ;; Inserted some more than necessary, to speed it up.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1986 byte-optimize-form-code-walker
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1987 byte-optimize-lapcode))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1988 nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1989
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1990 ;;; byte-optimize.el ends here