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