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