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