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