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