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