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