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