Mercurial > hg > xemacs-beta
annotate lisp/byte-optimize.el @ 5652:cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
lisp/ChangeLog addition:
2012-05-01 Aidan Kehoe <kehoea@parhasard.net>
Avoid #'delq in core code, for the sake of style and a (very
slightly) smaller binary.
* behavior.el (disable-behavior):
* behavior.el (compute-behavior-group-children):
* buff-menu.el (buffers-tab-items):
* byte-optimize.el (byte-optimize-delay-constants-math):
* byte-optimize.el (byte-optimize-logmumble):
* byte-optimize.el (byte-decompile-bytecode-1):
* byte-optimize.el (byte-optimize-lapcode):
* bytecomp.el:
* bytecomp.el (byte-compile-arglist-warn):
* bytecomp.el (byte-compile-warn-about-unresolved-functions):
* bytecomp.el (byte-compile-lambda):
* bytecomp.el (byte-compile-out-toplevel):
* bytecomp.el (byte-compile-insert):
* bytecomp.el (byte-compile-defalias-warn):
* cl-macs.el (cl-upcase-arg):
* cl-macs.el (cl-transform-lambda):
* cl-macs.el (cl-do-proclaim):
* cl-macs.el (defstruct):
* cl-macs.el (cl-make-type-test):
* cl-macs.el (define-compiler-macro):
* cl-macs.el (delete-duplicates):
* cus-edit.el (widget-face-value-delete):
* cus-edit.el (face-history):
* easymenu.el (easy-menu-remove):
* files.el (files-fetch-hook-value):
* files.el (file-expand-wildcards):
* font-lock.el (font-lock-update-removed-keyword-alist):
* font-lock.el (font-lock-remove-keywords):
* frame.el (frame-initialize):
* frame.el (frame-notice-user-settings):
* frame.el (set-frame-font):
* frame.el (delete-other-frames):
* frame.el (get-frame-for-buffer-noselect):
* gnuserv.el (gnuserv-kill-buffer-function):
* gnuserv.el (gnuserv-check-device):
* gnuserv.el (gnuserv-kill-client):
* gnuserv.el (gnuserv-buffer-done-1):
* gtk-font-menu.el (gtk-reset-device-font-menus):
* gutter-items.el (buffers-tab-items):
* gutter.el (set-gutter-element-visible-p):
* info.el (Info-find-file-node):
* info.el (Info-history-add):
* info.el (Info-build-annotation-completions):
* info.el (Info-index):
* info.el (Info-reannotate-node):
* itimer.el (delete-itimer):
* itimer.el (start-itimer):
* lib-complete.el (lib-complete:cache-completions):
* loadhist.el (unload-feature):
* menubar-items.el (build-buffers-menu-internal):
* menubar.el (delete-menu-item):
* menubar.el (relabel-menu-item):
* msw-font-menu.el (mswindows-reset-device-font-menus):
* mule/make-coding-system.el (fixed-width-generate-helper):
* next-error.el (next-error-find-buffer):
* obsolete.el:
* obsolete.el (find-non-ascii-charset-string):
* obsolete.el (find-non-ascii-charset-region):
* occur.el (multi-occur-by-filename-regexp):
* occur.el (occur-1):
* packages.el (packages-package-hierarchy-directory-names):
* packages.el (package-get-key-1):
* process.el (setenv):
* simple.el (undo):
* simple.el (handle-pre-motion-command-current-command-is-motion):
* sound.el (load-sound-file):
* wid-edit.el (widget-field-value-delete):
* wid-edit.el (widget-checklist-match-inline):
* wid-edit.el (widget-checklist-match-find):
* wid-edit.el (widget-editable-list-delete-at):
* wid-edit.el (widget-editable-list-entry-create):
* window.el (quit-window):
* x-font-menu.el (x-reset-device-font-menus-core):
1. Replace (delq nil (mapcar ....)) with analogous (mapcan ...)
forms; this is in non-dumped files, it was done previously in
dumped files.
2. Replace (delq FOO (copy-sequence BAR)) with (remove* FOO BAR),
where #'eq and #'eql are equivalent
3. Replace (delq FOO BAR) with (delete* FOO BAR), where FOO is not
a non-fixnum number. Saves a little space in the dumped file
(since the compiler macro adds :test #'eq to the delete* call if
it's not clear that FOO is not a non-fixnum number).
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Tue, 01 May 2012 16:17:42 +0100 |
parents | ae2fdb1fd9e0 |
children | e9c3fe82127d |
rev | line source |
---|---|
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 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
12 ;; XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
13 ;; under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
14 ;; Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
15 ;; option) any later version. |
428 | 16 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
20 ;; for more details. |
428 | 21 |
22 ;; You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
428 | 24 |
1297 | 25 ;;; Synched up with: FSF 20.7 except where marked. |
26 ;;; [[ Synched up with: FSF 20.7. ]] | |
27 ;;; DO NOT PUT IN AN INVALID SYNC MESSAGE WHEN YOU DO A PARTIAL SYNC. --ben | |
28 | |
29 ;; BEGIN SYNC WITH 20.7. | |
428 | 30 |
31 ;;; Commentary: | |
32 | |
33 ;; ======================================================================== | |
34 ;; "No matter how hard you try, you can't make a racehorse out of a pig. | |
35 ;; You can, however, make a faster pig." | |
36 ;; | |
37 ;; Or, to put it another way, the emacs byte compiler is a VW Bug. This code | |
440 | 38 ;; makes it be a VW Bug with fuel injection and a turbocharger... You're |
428 | 39 ;; still not going to make it go faster than 70 mph, but it might be easier |
40 ;; to get it there. | |
41 ;; | |
42 | |
43 ;; TO DO: | |
44 ;; | |
45 ;; (apply #'(lambda (x &rest y) ...) 1 (foo)) | |
46 ;; | |
47 ;; maintain a list of functions known not to access any global variables | |
48 ;; (actually, give them a 'dynamically-safe property) and then | |
49 ;; (let ( v1 v2 ... vM vN ) <...dynamically-safe...> ) ==> | |
50 ;; (let ( v1 v2 ... vM ) vN <...dynamically-safe...> ) | |
51 ;; by recursing on this, we might be able to eliminate the entire let. | |
52 ;; However certain variables should never have their bindings optimized | |
53 ;; away, because they affect everything. | |
54 ;; (put 'debug-on-error 'binding-is-magic t) | |
55 ;; (put 'debug-on-abort 'binding-is-magic t) | |
56 ;; (put 'debug-on-next-call 'binding-is-magic t) | |
57 ;; (put 'inhibit-quit 'binding-is-magic t) | |
58 ;; (put 'quit-flag 'binding-is-magic t) | |
59 ;; (put 't 'binding-is-magic t) | |
60 ;; (put 'nil 'binding-is-magic t) | |
61 ;; possibly also | |
62 ;; (put 'gc-cons-threshold 'binding-is-magic t) | |
63 ;; (put 'track-mouse 'binding-is-magic t) | |
64 ;; others? | |
65 ;; | |
66 ;; Simple defsubsts often produce forms like | |
67 ;; (let ((v1 (f1)) (v2 (f2)) ...) | |
68 ;; (FN v1 v2 ...)) | |
440 | 69 ;; It would be nice if we could optimize this to |
428 | 70 ;; (FN (f1) (f2) ...) |
71 ;; but we can't unless FN is dynamically-safe (it might be dynamically | |
72 ;; referring to the bindings that the lambda arglist established.) | |
73 ;; One of the uncountable lossages introduced by dynamic scope... | |
74 ;; | |
440 | 75 ;; Maybe there should be a control-structure that says "turn on |
428 | 76 ;; fast-and-loose type-assumptive optimizations here." Then when |
77 ;; we see a form like (car foo) we can from then on assume that | |
78 ;; the variable foo is of type cons, and optimize based on that. | |
440 | 79 ;; But, this won't win much because of (you guessed it) dynamic |
428 | 80 ;; scope. Anything down the stack could change the value. |
81 ;; (Another reason it doesn't work is that it is perfectly valid | |
82 ;; to call car with a null argument.) A better approach might | |
83 ;; be to allow type-specification of the form | |
84 ;; (put 'foo 'arg-types '(float (list integer) dynamic)) | |
85 ;; (put 'foo 'result-type 'bool) | |
86 ;; It should be possible to have these types checked to a certain | |
87 ;; degree. | |
88 ;; | |
89 ;; collapse common subexpressions | |
90 ;; | |
91 ;; It would be nice if redundant sequences could be factored out as well, | |
92 ;; when they are known to have no side-effects: | |
93 ;; (list (+ a b c) (+ a b c)) --> a b add c add dup list-2 | |
94 ;; but beware of traps like | |
95 ;; (cons (list x y) (list x y)) | |
96 ;; | |
97 ;; Tail-recursion elimination is not really possible in Emacs Lisp. | |
98 ;; Tail-recursion elimination is almost always impossible when all variables | |
99 ;; have dynamic scope, but given that the "return" byteop requires the | |
100 ;; binding stack to be empty (rather than emptying it itself), there can be | |
101 ;; no truly tail-recursive Emacs Lisp functions that take any arguments or | |
102 ;; make any bindings. | |
103 ;; | |
104 ;; Here is an example of an Emacs Lisp function which could safely be | |
105 ;; byte-compiled tail-recursively: | |
106 ;; | |
107 ;; (defun tail-map (fn list) | |
108 ;; (cond (list | |
109 ;; (funcall fn (car list)) | |
110 ;; (tail-map fn (cdr list))))) | |
111 ;; | |
112 ;; However, if there was even a single let-binding around the COND, | |
113 ;; it could not be byte-compiled, because there would be an "unbind" | |
440 | 114 ;; byte-op between the final "call" and "return." Adding a |
428 | 115 ;; Bunbind_all byteop would fix this. |
116 ;; | |
117 ;; (defun foo (x y z) ... (foo a b c)) | |
118 ;; ... (const foo) (varref a) (varref b) (varref c) (call 3) END: (return) | |
119 ;; ... (varref a) (varbind x) (varref b) (varbind y) (varref c) (varbind z) (goto 0) END: (unbind-all) (return) | |
120 ;; ... (varref a) (varset x) (varref b) (varset y) (varref c) (varset z) (goto 0) END: (return) | |
121 ;; | |
122 ;; this also can be considered tail recursion: | |
123 ;; | |
124 ;; ... (const foo) (varref a) (call 1) (goto X) ... X: (return) | |
125 ;; could generalize this by doing the optimization | |
126 ;; (goto X) ... X: (return) --> (return) | |
127 ;; | |
128 ;; But this doesn't solve all of the problems: although by doing tail- | |
129 ;; recursion elimination in this way, the call-stack does not grow, the | |
130 ;; binding-stack would grow with each recursive step, and would eventually | |
131 ;; overflow. I don't believe there is any way around this without lexical | |
132 ;; scope. | |
133 ;; | |
134 ;; Wouldn't it be nice if Emacs Lisp had lexical scope. | |
135 ;; | |
440 | 136 ;; Idea: the form (lexical-scope) in a file means that the file may be |
137 ;; compiled lexically. This proclamation is file-local. Then, within | |
428 | 138 ;; that file, "let" would establish lexical bindings, and "let-dynamic" |
139 ;; would do things the old way. (Or we could use CL "declare" forms.) | |
140 ;; We'd have to notice defvars and defconsts, since those variables should | |
141 ;; always be dynamic, and attempting to do a lexical binding of them | |
142 ;; should simply do a dynamic binding instead. | |
143 ;; But! We need to know about variables that were not necessarily defvarred | |
144 ;; in the file being compiled (doing a boundp check isn't good enough.) | |
145 ;; Fdefvar() would have to be modified to add something to the plist. | |
146 ;; | |
440 | 147 ;; A major disadvantage of this scheme is that the interpreter and compiler |
148 ;; would have different semantics for files compiled with (dynamic-scope). | |
428 | 149 ;; Since this would be a file-local optimization, there would be no way to |
440 | 150 ;; modify the interpreter to obey this (unless the loader was hacked |
428 | 151 ;; in some grody way, but that's a really bad idea.) |
152 ;; | |
153 ;; HA! RMS removed the following paragraph from his version of | |
154 ;; byte-optimize.el. | |
155 ;; | |
156 ;; Really the Right Thing is to make lexical scope the default across | |
440 | 157 ;; the board, in the interpreter and compiler, and just FIX all of |
428 | 158 ;; the code that relies on dynamic scope of non-defvarred variables. |
159 | |
160 ;; Other things to consider: | |
161 | |
162 ;; Associative math should recognize subcalls to identical function: | |
163 ;;(disassemble #'(lambda (x) (+ (+ (foo) 1) (+ (bar) 2)))) | |
164 ;; This should generate the same as (1+ x) and (1- x) | |
165 | |
166 ;;(disassemble #'(lambda (x) (cons (+ x 1) (- x 1)))) | |
167 ;; An awful lot of functions always return a non-nil value. If they're | |
168 ;; error free also they may act as true-constants. | |
169 | |
170 ;;(disassemble #'(lambda (x) (and (point) (foo)))) | |
440 | 171 ;; When |
428 | 172 ;; - all but one arguments to a function are constant |
173 ;; - the non-constant argument is an if-expression (cond-expression?) | |
174 ;; then the outer function can be distributed. If the guarding | |
175 ;; condition is side-effect-free [assignment-free] then the other | |
176 ;; arguments may be any expressions. Since, however, the code size | |
177 ;; can increase this way they should be "simple". Compare: | |
178 | |
179 ;;(disassemble #'(lambda (x) (eq (if (point) 'a 'b) 'c))) | |
180 ;;(disassemble #'(lambda (x) (if (point) (eq 'a 'c) (eq 'b 'c)))) | |
181 | |
444 | 182 ;; (car (cons A B)) -> (prog1 A B) |
428 | 183 ;;(disassemble #'(lambda (x) (car (cons (foo) 42)))) |
184 | |
185 ;; (cdr (cons A B)) -> (progn A B) | |
186 ;;(disassemble #'(lambda (x) (cdr (cons 42 (foo))))) | |
187 | |
444 | 188 ;; (car (list A B ...)) -> (prog1 A ... B) |
428 | 189 ;;(disassemble #'(lambda (x) (car (list (foo) 42 (bar))))) |
190 | |
191 ;; (cdr (list A B ...)) -> (progn A (list B ...)) | |
192 ;;(disassemble #'(lambda (x) (cdr (list 42 (foo) (bar))))) | |
193 | |
194 | |
195 ;;; Code: | |
196 | |
197 (require 'byte-compile "bytecomp") | |
198 | |
199 (defun byte-compile-log-lap-1 (format &rest args) | |
200 (if (aref byte-code-vector 0) | |
201 (error "The old version of the disassembler is loaded. Reload new-bytecomp as well.")) | |
202 (byte-compile-log-1 | |
203 (apply 'format format | |
204 (let (c a) | |
205 (mapcar | |
206 #'(lambda (arg) | |
207 (if (not (consp arg)) | |
208 (if (and (symbolp arg) | |
209 (string-match "^byte-" (symbol-name arg))) | |
210 (intern (substring (symbol-name arg) 5)) | |
211 arg) | |
212 (if (integerp (setq c (car arg))) | |
213 (error "non-symbolic byte-op %s" c)) | |
214 (if (eq c 'TAG) | |
215 (setq c arg) | |
216 (setq a (cond ((memq c byte-goto-ops) | |
217 (car (cdr (cdr arg)))) | |
218 ((memq c byte-constref-ops) | |
219 (car (cdr arg))) | |
220 (t (cdr arg)))) | |
221 (setq c (symbol-name c)) | |
222 (if (string-match "^byte-." c) | |
223 (setq c (intern (substring c 5))))) | |
224 (if (eq c 'constant) (setq c 'const)) | |
225 (if (and (eq (cdr arg) 0) | |
226 (not (memq c '(unbind call const)))) | |
227 c | |
228 (format "(%s %s)" c a)))) | |
229 args))))) | |
230 | |
231 (defmacro byte-compile-log-lap (format-string &rest args) | |
232 (list 'and | |
233 '(memq byte-optimize-log '(t byte)) | |
234 (cons 'byte-compile-log-lap-1 | |
235 (cons format-string args)))) | |
236 | |
237 | |
238 ;;; byte-compile optimizers to support inlining | |
239 | |
240 (put 'inline 'byte-optimizer 'byte-optimize-inline-handler) | |
241 | |
242 (defun byte-optimize-inline-handler (form) | |
243 "byte-optimize-handler for the `inline' special-form." | |
244 (cons | |
245 'progn | |
246 (mapcar | |
247 #'(lambda (sexp) | |
248 (let ((fn (car-safe sexp))) | |
249 (if (and (symbolp fn) | |
250 (or (cdr (assq fn byte-compile-function-environment)) | |
251 (and (fboundp fn) | |
252 (not (or (cdr (assq fn byte-compile-macro-environment)) | |
253 (and (consp (setq fn (symbol-function fn))) | |
254 (eq (car fn) 'macro)) | |
255 (subrp fn)))))) | |
256 (byte-compile-inline-expand sexp) | |
257 sexp))) | |
258 (cdr form)))) | |
259 | |
260 | |
261 ;; Splice the given lap code into the current instruction stream. | |
262 ;; If it has any labels in it, you're responsible for making sure there | |
263 ;; are no collisions, and that byte-compile-tag-number is reasonable | |
264 ;; after this is spliced in. The provided list is destroyed. | |
265 (defun byte-inline-lapcode (lap) | |
266 (setq byte-compile-output (nconc (nreverse lap) byte-compile-output))) | |
267 | |
268 | |
269 (defun byte-compile-inline-expand (form) | |
270 (let* ((name (car form)) | |
271 (fn (or (cdr (assq name byte-compile-function-environment)) | |
272 (and (fboundp name) (symbol-function name))))) | |
273 (if (null fn) | |
274 (progn | |
275 (byte-compile-warn "attempt to inline %s before it was defined" name) | |
276 form) | |
277 ;; else | |
278 (if (and (consp fn) (eq (car fn) 'autoload)) | |
279 (progn | |
280 (load (nth 1 fn)) | |
281 (setq fn (or (cdr (assq name byte-compile-function-environment)) | |
282 (and (fboundp name) (symbol-function name)))))) | |
283 (if (and (consp fn) (eq (car fn) 'autoload)) | |
284 (error "file \"%s\" didn't define \"%s\"" (nth 1 fn) name)) | |
285 (if (symbolp fn) | |
286 (byte-compile-inline-expand (cons fn (cdr form))) | |
287 (if (compiled-function-p fn) | |
288 (progn | |
289 (fetch-bytecode fn) | |
290 (cons (list 'lambda (compiled-function-arglist fn) | |
291 (list 'byte-code | |
292 (compiled-function-instructions fn) | |
293 (compiled-function-constants fn) | |
294 (compiled-function-stack-depth fn))) | |
295 (cdr form))) | |
1297 | 296 (if (eq (car-safe fn) 'lambda) |
297 (cons fn (cdr form)) | |
298 ;; Give up on inlining. | |
299 form)))))) | |
428 | 300 |
301 ;;; ((lambda ...) ...) | |
440 | 302 ;;; |
428 | 303 (defun byte-compile-unfold-lambda (form &optional name) |
304 (or name (setq name "anonymous lambda")) | |
305 (let ((lambda (car form)) | |
306 (values (cdr form))) | |
307 (if (compiled-function-p lambda) | |
308 (setq lambda (list 'lambda (compiled-function-arglist lambda) | |
309 (list 'byte-code | |
310 (compiled-function-instructions lambda) | |
311 (compiled-function-constants lambda) | |
312 (compiled-function-stack-depth lambda))))) | |
313 (let ((arglist (nth 1 lambda)) | |
314 (body (cdr (cdr lambda))) | |
315 optionalp restp | |
316 bindings) | |
317 (if (and (stringp (car body)) (cdr body)) | |
318 (setq body (cdr body))) | |
319 (if (and (consp (car body)) (eq 'interactive (car (car body)))) | |
320 (setq body (cdr body))) | |
321 (while arglist | |
322 (cond ((eq (car arglist) '&optional) | |
323 ;; ok, I'll let this slide because funcall_lambda() does... | |
324 ;; (if optionalp (error "multiple &optional keywords in %s" name)) | |
325 (if restp (error "&optional found after &rest in %s" name)) | |
326 (if (null (cdr arglist)) | |
327 (error "nothing after &optional in %s" name)) | |
328 (setq optionalp t)) | |
329 ((eq (car arglist) '&rest) | |
330 ;; ...but it is by no stretch of the imagination a reasonable | |
331 ;; thing that funcall_lambda() allows (&rest x y) and | |
332 ;; (&rest x &optional y) in arglists. | |
333 (if (null (cdr arglist)) | |
334 (error "nothing after &rest in %s" name)) | |
335 (if (cdr (cdr arglist)) | |
336 (error "multiple vars after &rest in %s" name)) | |
337 (setq restp t)) | |
338 (restp | |
339 (setq bindings (cons (list (car arglist) | |
340 (and values (cons 'list values))) | |
341 bindings) | |
342 values nil)) | |
343 ((and (not optionalp) (null values)) | |
344 (byte-compile-warn "attempt to open-code %s with too few arguments" name) | |
345 (setq arglist nil values 'too-few)) | |
346 (t | |
347 (setq bindings (cons (list (car arglist) (car values)) | |
348 bindings) | |
349 values (cdr values)))) | |
350 (setq arglist (cdr arglist))) | |
351 (if values | |
352 (progn | |
353 (or (eq values 'too-few) | |
354 (byte-compile-warn | |
355 "attempt to open-code %s with too many arguments" name)) | |
356 form) | |
1297 | 357 (setq body (mapcar 'byte-optimize-form body)) |
440 | 358 (let ((newform |
428 | 359 (if bindings |
360 (cons 'let (cons (nreverse bindings) body)) | |
361 (cons 'progn body)))) | |
362 (byte-compile-log " %s\t==>\t%s" form newform) | |
363 newform))))) | |
364 | |
365 | |
366 ;;; implementing source-level optimizers | |
367 | |
368 (defun byte-optimize-form-code-walker (form for-effect) | |
369 ;; | |
370 ;; For normal function calls, We can just mapcar the optimizer the cdr. But | |
4905
755ae5b97edb
Change "special form" to "special operator" in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4885
diff
changeset
|
371 ;; we need to have special knowledge of the syntax of the special operators |
755ae5b97edb
Change "special form" to "special operator" in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4885
diff
changeset
|
372 ;; like let and defun (that's why they're special operators :-). (Actually, |
428 | 373 ;; the important aspect is that they are subrs that don't evaluate all of |
374 ;; their args.) | |
375 ;; | |
376 (let ((fn (car-safe form)) | |
377 tmp) | |
378 (cond ((not (consp form)) | |
379 (if (not (and for-effect | |
380 (or byte-compile-delete-errors | |
381 (not (symbolp form)) | |
382 (eq form t)))) | |
383 form)) | |
384 ((eq fn 'quote) | |
385 (if (cdr (cdr form)) | |
386 (byte-compile-warn "malformed quote form: %s" | |
387 (prin1-to-string form))) | |
388 ;; map (quote nil) to nil to simplify optimizer logic. | |
389 ;; map quoted constants to nil if for-effect (just because). | |
390 (and (nth 1 form) | |
391 (not for-effect) | |
392 form)) | |
393 ((or (compiled-function-p fn) | |
394 (eq 'lambda (car-safe fn))) | |
395 (byte-compile-unfold-lambda form)) | |
396 ((memq fn '(let let*)) | |
397 ;; recursively enter the optimizer for the bindings and body | |
398 ;; of a let or let*. This for depth-firstness: forms that | |
399 ;; are more deeply nested are optimized first. | |
400 (cons fn | |
401 (cons | |
402 (mapcar | |
403 #'(lambda (binding) | |
404 (if (symbolp binding) | |
405 binding | |
406 (if (cdr (cdr binding)) | |
407 (byte-compile-warn "malformed let binding: %s" | |
408 (prin1-to-string binding))) | |
409 (list (car binding) | |
410 (byte-optimize-form (nth 1 binding) nil)))) | |
411 (nth 1 form)) | |
412 (byte-optimize-body (cdr (cdr form)) for-effect)))) | |
413 ((eq fn 'cond) | |
414 (cons fn | |
415 (mapcar | |
416 #'(lambda (clause) | |
417 (if (consp clause) | |
418 (cons | |
419 (byte-optimize-form (car clause) nil) | |
420 (byte-optimize-body (cdr clause) for-effect)) | |
421 (byte-compile-warn "malformed cond form: %s" | |
422 (prin1-to-string clause)) | |
423 clause)) | |
424 (cdr form)))) | |
425 ((eq fn 'progn) | |
426 ;; as an extra added bonus, this simplifies (progn <x>) --> <x> | |
427 (if (cdr (cdr form)) | |
428 (progn | |
429 (setq tmp (byte-optimize-body (cdr form) for-effect)) | |
430 (if (cdr tmp) (cons 'progn tmp) (car tmp))) | |
431 (byte-optimize-form (nth 1 form) for-effect))) | |
432 ((eq fn 'prog1) | |
433 (if (cdr (cdr form)) | |
5651
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
434 (cons (if for-effect 'progn 'prog1) |
428 | 435 (cons (byte-optimize-form (nth 1 form) for-effect) |
436 (byte-optimize-body (cdr (cdr form)) t))) | |
4686
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
437 (byte-optimize-form `(or ,(nth 1 form) nil) for-effect))) |
428 | 438 ((eq fn 'prog2) |
439 (cons 'prog2 | |
440 (cons (byte-optimize-form (nth 1 form) t) | |
441 (cons (byte-optimize-form (nth 2 form) for-effect) | |
442 (byte-optimize-body (cdr (cdr (cdr form))) t))))) | |
440 | 443 |
428 | 444 ((memq fn '(save-excursion save-restriction save-current-buffer)) |
445 ;; those subrs which have an implicit progn; it's not quite good | |
446 ;; enough to treat these like normal function calls. | |
447 ;; This can turn (save-excursion ...) into (save-excursion) which | |
448 ;; will be optimized away in the lap-optimize pass. | |
449 (cons fn (byte-optimize-body (cdr form) for-effect))) | |
440 | 450 |
428 | 451 ((eq fn 'with-output-to-temp-buffer) |
452 ;; this is just like the above, except for the first argument. | |
453 (cons fn | |
454 (cons | |
455 (byte-optimize-form (nth 1 form) nil) | |
456 (byte-optimize-body (cdr (cdr form)) for-effect)))) | |
440 | 457 |
428 | 458 ((eq fn 'if) |
459 (cons fn | |
460 (cons (byte-optimize-form (nth 1 form) nil) | |
461 (cons | |
462 (byte-optimize-form (nth 2 form) for-effect) | |
463 (byte-optimize-body (nthcdr 3 form) for-effect))))) | |
440 | 464 |
428 | 465 ((memq fn '(and or)) ; remember, and/or are control structures. |
466 ;; take forms off the back until we can't any more. | |
467 ;; In the future it could conceivably be a problem that the | |
468 ;; subexpressions of these forms are optimized in the reverse | |
469 ;; order, but it's ok for now. | |
470 (if for-effect | |
471 (let ((backwards (reverse (cdr form)))) | |
472 (while (and backwards | |
473 (null (setcar backwards | |
474 (byte-optimize-form (car backwards) | |
475 for-effect)))) | |
476 (setq backwards (cdr backwards))) | |
477 (if (and (cdr form) (null backwards)) | |
478 (byte-compile-log | |
479 " all subforms of %s called for effect; deleted" form)) | |
452 | 480 (when backwards |
481 ;; Now optimize the rest of the forms. We need the return | |
482 ;; values. We already did the car. | |
483 (setcdr backwards | |
484 (mapcar 'byte-optimize-form (cdr backwards)))) | |
485 (cons fn (nreverse backwards))) | |
428 | 486 (cons fn (mapcar 'byte-optimize-form (cdr form))))) |
487 | |
488 ((eq fn 'interactive) | |
489 (byte-compile-warn "misplaced interactive spec: %s" | |
490 (prin1-to-string form)) | |
491 nil) | |
440 | 492 |
428 | 493 ((memq fn '(defun defmacro function |
494 condition-case save-window-excursion)) | |
495 ;; These forms are compiled as constants or by breaking out | |
496 ;; all the subexpressions and compiling them separately. | |
497 form) | |
498 | |
499 ((eq fn 'unwind-protect) | |
500 ;; the "protected" part of an unwind-protect is compiled (and thus | |
501 ;; optimized) as a top-level form, so don't do it here. But the | |
502 ;; non-protected part has the same for-effect status as the | |
503 ;; unwind-protect itself. (The protected part is always for effect, | |
504 ;; but that isn't handled properly yet.) | |
505 (cons fn | |
506 (cons (byte-optimize-form (nth 1 form) for-effect) | |
507 (cdr (cdr form))))) | |
440 | 508 |
428 | 509 ((eq fn 'catch) |
510 ;; the body of a catch is compiled (and thus optimized) as a | |
511 ;; top-level form, so don't do it here. The tag is never | |
512 ;; for-effect. The body should have the same for-effect status | |
513 ;; as the catch form itself, but that isn't handled properly yet. | |
514 (cons fn | |
515 (cons (byte-optimize-form (nth 1 form) nil) | |
516 (cdr (cdr form))))) | |
517 | |
518 ;; If optimization is on, this is the only place that macros are | |
519 ;; expanded. If optimization is off, then macroexpansion happens | |
520 ;; in byte-compile-form. Otherwise, the macros are already expanded | |
521 ;; by the time that is reached. | |
522 ((not (eq form | |
523 (setq form (macroexpand form | |
524 byte-compile-macro-environment)))) | |
525 (byte-optimize-form form for-effect)) | |
440 | 526 |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
527 ((not (symbolp fn)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
528 (byte-compile-warn "%s is a malformed function" (prin1-to-string fn)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
529 form) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
530 |
1297 | 531 ;; Support compiler macros as in cl.el. |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
532 ((and (get fn 'cl-compiler-macro) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
533 (not (eq form (setq form (compiler-macroexpand form))))) |
1297 | 534 (byte-optimize-form form for-effect)) |
535 | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
536 ((and for-effect |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
537 (setq tmp (byte-optimize-side-effect-free-p form)) |
428 | 538 (or byte-compile-delete-errors |
539 (eq tmp 'error-free) | |
5651
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
540 ;; XEmacs; GNU handles the expansion of (pop foo) specially |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
541 ;; here. We changed the macro to expand to (prog1 (car-safe |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
542 ;; PLACE) (setq PLACE (cdr PLACE))) , which has the same |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
543 ;; effect. (This only matters when |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
544 ;; byte-compile-delete-errors is nil, which is usually true |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
545 ;; for GNU and usually false for XEmacs.) |
428 | 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) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
713 (setq form (nconc (delete* nil form) |
428 | 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)) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
790 (remove* 0 form)))) |
428 | 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)) | |
5366
f00192e1cd49
Examining the result of #'length: `eql', not `=', it's better style & cheaper
Aidan Kehoe <kehoea@parhasard.net>
parents:
5311
diff
changeset
|
826 (if (eql 1 (length (cdr form))) "" "s")) |
428 | 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) | |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
4905
diff
changeset
|
880 (put 'equalp 'byte-optimizer 'byte-optimize-binary-predicate) |
428 | 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) | |
4686
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
954 ;; Throw away unneeded nils, and simplify if less than 2 args. |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
955 ;; XEmacs; change to be more careful about discarding multiple values. |
5651
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
956 (if (memq nil form) |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
957 (setq form (remove* nil form |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
958 ;; A trailing nil indicates to discard multiple |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
959 ;; values, and we need to respect that. No need if |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
960 ;; this is for-effect, though, multiple values |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
961 ;; will be discarded anyway. |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
962 :end (if (not for-effect) (1- (length form)))))) |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
963 ;; If there is a literal non-nil constant in the args to `or', throw |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
964 ;; away all following forms. We can do this because a literal non-nil |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
965 ;; constant cannot be multiple. |
ae2fdb1fd9e0
Improve for-effect handling in a few places, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5502
diff
changeset
|
966 (let ((rest form)) |
428 | 967 (while (cdr (setq rest (cdr rest))) |
968 (if (byte-compile-trueconstp (car rest)) | |
969 (setq form (copy-sequence form) | |
970 rest (setcdr (memq (car rest) form) nil)))) | |
971 (if (cdr (cdr form)) | |
972 (byte-optimize-predicate form) | |
973 (nth 1 form)))) | |
974 | |
1297 | 975 ;; END SYNC WITH 20.7. |
976 | |
448 | 977 ;;; For the byte optimizer, `cond' is just overly sweet syntactic sugar. |
978 ;;; So we rewrite (cond ...) in terms of `if' and `or', | |
979 ;;; which are easier to optimize. | |
428 | 980 (defun byte-optimize-cond (form) |
448 | 981 (byte-optimize-cond-1 (cdr form))) |
982 | |
983 (defun byte-optimize-cond-1 (clauses) | |
984 (cond | |
985 ((null clauses) nil) | |
986 ((consp (car clauses)) | |
987 (nconc | |
988 (case (length (car clauses)) | |
4686
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
989 (1 (if (cdr clauses) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
990 `(or ,(nth 0 (car clauses))) |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
991 ;; XEmacs: don't pass any multiple values back: |
cdabd56ce1b5
Fix various small issues with the multiple-value implementation.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
992 `(or ,(nth 0 (car clauses)) nil))) |
448 | 993 (2 `(if ,(nth 0 (car clauses)) ,(nth 1 (car clauses)))) |
994 (t `(if ,(nth 0 (car clauses)) (progn ,@(cdr (car clauses)))))) | |
995 (when (cdr clauses) (list (byte-optimize-cond-1 (cdr clauses)))))) | |
996 (t (error "malformed cond clause %s" (car clauses))))) | |
428 | 997 |
1297 | 998 ;; BEGIN SYNC WITH 20.7. |
999 | |
428 | 1000 (defun byte-optimize-if (form) |
1001 ;; (if <true-constant> <then> <else...>) ==> <then> | |
1002 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>) | |
1003 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>)) | |
1004 ;; (if <test> <then> nil) ==> (if <test> <then>) | |
1005 (let ((clause (nth 1 form))) | |
1006 (cond ((byte-compile-trueconstp clause) | |
1007 (nth 2 form)) | |
1008 ((null clause) | |
1009 (if (nthcdr 4 form) | |
1010 (cons 'progn (nthcdr 3 form)) | |
1011 (nth 3 form))) | |
1012 ((nth 2 form) | |
1013 (if (equal '(nil) (nthcdr 3 form)) | |
1014 (list 'if clause (nth 2 form)) | |
1015 form)) | |
1016 ((or (nth 3 form) (nthcdr 4 form)) | |
1017 (list 'if | |
1018 ;; Don't make a double negative; | |
1019 ;; instead, take away the one that is there. | |
1020 (if (and (consp clause) (memq (car clause) '(not null)) | |
5366
f00192e1cd49
Examining the result of #'length: `eql', not `=', it's better style & cheaper
Aidan Kehoe <kehoea@parhasard.net>
parents:
5311
diff
changeset
|
1021 (eql (length clause) 2)) ; (not xxxx) or (not (xxxx)) |
428 | 1022 (nth 1 clause) |
1023 (list 'not clause)) | |
1024 (if (nthcdr 4 form) | |
1025 (cons 'progn (nthcdr 3 form)) | |
1026 (nth 3 form)))) | |
1027 (t | |
1028 (list 'progn clause nil))))) | |
1029 | |
1030 (defun byte-optimize-while (form) | |
1031 (if (nth 1 form) | |
1032 form)) | |
1033 | |
1034 (put 'and 'byte-optimizer 'byte-optimize-and) | |
1035 (put 'or 'byte-optimizer 'byte-optimize-or) | |
1036 (put 'cond 'byte-optimizer 'byte-optimize-cond) | |
1037 (put 'if 'byte-optimizer 'byte-optimize-if) | |
1038 (put 'while 'byte-optimizer 'byte-optimize-while) | |
1039 | |
446 | 1040 ;; The supply of bytecodes is small and constrained by backward compatibility. |
1041 ;; Several functions have byte-coded versions and hence are very efficient. | |
1042 ;; Related functions which can be expressed in terms of the byte-coded | |
1043 ;; ones should be transformed into bytecoded calls for efficiency. | |
1044 ;; This is especially the case for functions with a backward- and | |
1045 ;; forward- version, but with a bytecode only for the forward one. | |
1046 | |
1047 ;; Some programmers have hand-optimized calls like (backward-char) | |
1048 ;; into the call (forward-char -1). | |
1049 ;; But it's so much nicer for the byte-compiler to do this automatically! | |
1050 | |
1051 ;; (char-before) ==> (char-after (1- (point))) | |
1052 (put 'char-before 'byte-optimizer 'byte-optimize-char-before) | |
434 | 1053 (defun byte-optimize-char-before (form) |
446 | 1054 `(char-after |
1055 ,(cond | |
1056 ((null (nth 1 form)) | |
1057 '(1- (point))) | |
1058 ((equal '(point) (nth 1 form)) | |
1059 '(1- (point))) | |
1060 (t `(1- (or ,(nth 1 form) (point))))) | |
1061 ,@(cdr (cdr form)))) | |
1062 | |
1063 ;; (backward-char n) ==> (forward-char (- n)) | |
1064 (put 'backward-char 'byte-optimizer 'byte-optimize-backward-char) | |
1065 (defun byte-optimize-backward-char (form) | |
1066 `(forward-char | |
1067 ,(typecase (nth 1 form) | |
1068 (null -1) | |
1069 (integer (- (nth 1 form))) | |
1070 (t `(- (or ,(nth 1 form) 1)))) | |
1071 ,@(cdr (cdr form)))) | |
440 | 1072 |
446 | 1073 ;; (backward-word n) ==> (forward-word (- n)) |
1074 (put 'backward-word 'byte-optimizer 'byte-optimize-backward-word) | |
1075 (defun byte-optimize-backward-word (form) | |
1076 `(forward-word | |
1077 ,(typecase (nth 1 form) | |
1078 (null -1) | |
1079 (integer (- (nth 1 form))) | |
1080 (t `(- (or ,(nth 1 form) 1)))) | |
1081 ,@(cdr (cdr form)))) | |
1082 | |
1083 ;; The following would be a valid optimization of the above kind, but | |
1084 ;; the gain in performance is very small, since the saved funcall is | |
1085 ;; counterbalanced by the necessity of adding a bytecode for (point). | |
1086 ;; | |
1087 ;; Also, users are more likely to have modified the behavior of | |
1088 ;; delete-char via advice or some similar mechanism. This is much | |
1089 ;; less of a problem for the previous functions because it wouldn't | |
1090 ;; make sense to modify the behaviour of `backward-char' without also | |
1091 ;; modifying `forward-char', for example. | |
1092 | |
1093 ;; (delete-char n) ==> (delete-region (point) (+ (point) n)) | |
1094 ;; (put 'delete-char 'byte-optimizer 'byte-optimize-delete-char) | |
1095 ;; (defun byte-optimize-delete-char (form) | |
1096 ;; (case (length (cdr form)) | |
1097 ;; (0 `(delete-region (point) (1+ (point)))) | |
1098 ;; (1 `(delete-region (point) (+ (point) ,(nth 1 form)))) | |
1099 ;; (t form))) | |
434 | 1100 |
428 | 1101 ;; byte-compile-negation-optimizer lives in bytecomp.el |
1102 ;(put '/= 'byte-optimizer 'byte-compile-negation-optimizer) | |
1103 (put 'atom 'byte-optimizer 'byte-compile-negation-optimizer) | |
1104 (put 'nlistp 'byte-optimizer 'byte-compile-negation-optimizer) | |
1105 | |
1106 (defun byte-optimize-funcall (form) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4601
diff
changeset
|
1107 ;; (funcall #'(lambda ...) ...) ==> ((lambda ...) ...) |
428 | 1108 ;; (funcall 'foo ...) ==> (foo ...) |
1109 (let ((fn (nth 1 form))) | |
1110 (if (memq (car-safe fn) '(quote function)) | |
1111 (cons (nth 1 fn) (cdr (cdr form))) | |
1112 form))) | |
1113 | |
1114 (defun byte-optimize-apply (form) | |
1115 ;; If the last arg is a literal constant, turn this into a funcall. | |
1116 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...). | |
1117 (let ((fn (nth 1 form)) | |
1118 (last (nth (1- (length form)) form))) ; I think this really is fastest | |
5264
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1119 (if (and (eq last (third form)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1120 (consp last) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1121 (eq 'mapcar (car last)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1122 (equal fn ''nconc)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1123 (progn |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1124 (byte-compile-warn |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1125 "(apply 'nconc (mapcar ..)), use #'mapcan instead: %s" last) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1126 (cons 'mapcan (cdr last))) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1127 (or (if (or (null last) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1128 (eq (car-safe last) 'quote)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1129 (if (listp (nth 1 last)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1130 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form))))))) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1131 (nconc (list 'funcall fn) butlast |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1132 (mapcar #'(lambda (x) (list 'quote x)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1133 (nth 1 last)))) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1134 (byte-compile-warn |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1135 "last arg to apply can't be a literal atom: %s" |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1136 (prin1-to-string last)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1137 nil)) |
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
1138 form)))) |
428 | 1139 |
1140 (put 'funcall 'byte-optimizer 'byte-optimize-funcall) | |
1141 (put 'apply 'byte-optimizer 'byte-optimize-apply) | |
1142 | |
1143 | |
1144 (put 'let 'byte-optimizer 'byte-optimize-letX) | |
1145 (put 'let* 'byte-optimizer 'byte-optimize-letX) | |
1146 (defun byte-optimize-letX (form) | |
1147 (cond ((null (nth 1 form)) | |
1148 ;; No bindings | |
1149 (cons 'progn (cdr (cdr form)))) | |
1150 ((or (nth 2 form) (nthcdr 3 form)) | |
1151 form) | |
1152 ;; The body is nil | |
1153 ((eq (car form) 'let) | |
1154 (append '(progn) (mapcar 'car-safe (mapcar 'cdr-safe (nth 1 form))) | |
1155 '(nil))) | |
1156 (t | |
1157 (let ((binds (reverse (nth 1 form)))) | |
1158 (list 'let* (reverse (cdr binds)) (nth 1 (car binds)) nil))))) | |
1159 | |
1160 | |
1161 (put 'nth 'byte-optimizer 'byte-optimize-nth) | |
1162 (defun byte-optimize-nth (form) | |
5366
f00192e1cd49
Examining the result of #'length: `eql', not `=', it's better style & cheaper
Aidan Kehoe <kehoea@parhasard.net>
parents:
5311
diff
changeset
|
1163 (if (and (eql (safe-length form) 3) (memq (nth 1 form) '(0 1))) |
428 | 1164 (list 'car (if (zerop (nth 1 form)) |
1165 (nth 2 form) | |
1166 (list 'cdr (nth 2 form)))) | |
1167 (byte-optimize-predicate form))) | |
1168 | |
1169 (put 'nthcdr 'byte-optimizer 'byte-optimize-nthcdr) | |
1170 (defun byte-optimize-nthcdr (form) | |
5366
f00192e1cd49
Examining the result of #'length: `eql', not `=', it's better style & cheaper
Aidan Kehoe <kehoea@parhasard.net>
parents:
5311
diff
changeset
|
1171 (if (and (eql (safe-length form) 3) (not (memq (nth 1 form) '(0 1 2)))) |
428 | 1172 (byte-optimize-predicate form) |
1173 (let ((count (nth 1 form))) | |
1174 (setq form (nth 2 form)) | |
1175 (while (>= (setq count (1- count)) 0) | |
1176 (setq form (list 'cdr form))) | |
1177 form))) | |
444 | 1178 |
1179 (put 'concat 'byte-optimizer 'byte-optimize-concat) | |
1180 (defun byte-optimize-concat (form) | |
1181 (let ((args (cdr form)) | |
1182 (constant t)) | |
1183 (while (and args constant) | |
1184 (or (byte-compile-constp (car args)) | |
1185 (setq constant nil)) | |
1186 (setq args (cdr args))) | |
1187 (if constant | |
1188 (eval form) | |
1189 form))) | |
4160 | 1190 |
4228 | 1191 (defvar byte-optimize-ever-present-features |
1192 '(xemacs cl cl-extra cl-19 backquote)) | |
1193 | |
1194 (put 'featurep 'byte-optimizer 'byte-optimize-featurep) | |
1195 (defun byte-optimize-featurep (form) | |
4288 | 1196 (if (memq (car-safe |
1197 (cdr-safe | |
1198 (car-safe | |
1199 (cdr-safe | |
1200 form)))) | |
1201 byte-optimize-ever-present-features) | |
1202 t | |
1203 form)) | |
4228 | 1204 |
428 | 1205 |
440 | 1206 ;;; enumerating those functions which need not be called if the returned |
428 | 1207 ;;; value is not used. That is, something like |
1208 ;;; (progn (list (something-with-side-effects) (yow)) | |
1209 ;;; (foo)) | |
1210 ;;; may safely be turned into | |
1211 ;;; (progn (progn (something-with-side-effects) (yow)) | |
1212 ;;; (foo)) | |
1213 ;;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo. | |
1214 | |
1215 ;;; I wonder if I missed any :-\) | |
1216 (let ((side-effect-free-fns | |
1217 '(% * + - / /= 1+ 1- < <= = > >= abs acos append aref ash asin atan | |
1218 assoc assq | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1219 bigfloat-get-precision boundp buffer-file-name buffer-local-variables |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1220 buffer-modified-p buffer-substring |
428 | 1221 capitalize car-less-than-car car cdr ceiling concat |
1222 ;; coordinates-in-window-p not in XEmacs | |
1223 copy-marker cos count-lines | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1224 default-boundp default-value denominator documentation downcase |
5285
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5264
diff
changeset
|
1225 elt endp exp expt fboundp featurep |
428 | 1226 file-directory-p file-exists-p file-locked-p file-name-absolute-p |
1227 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p | |
1228 float floor format | |
1229 get get-buffer get-buffer-window getenv get-file-buffer | |
1230 ;; hash-table functions | |
1231 make-hash-table copy-hash-table | |
1232 gethash | |
1233 hash-table-count | |
1234 hash-table-rehash-size | |
1235 hash-table-rehash-threshold | |
1236 hash-table-size | |
1237 hash-table-test | |
1238 hash-table-type | |
1239 ;; | |
1240 int-to-string | |
1241 length log log10 logand logb logior lognot logxor lsh | |
1242 marker-buffer max member memq min mod | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1243 next-window nth nthcdr number-to-string numerator |
440 | 1244 parse-colon-path plist-get previous-window |
5311
07d24b1f27a7
Mark #'remove, #'remq as free of side-effects.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5294
diff
changeset
|
1245 radians-to-degrees rassq rassoc remove remq regexp-quote reverse round |
428 | 1246 sin sqrt string< string= string-equal string-lessp string-to-char |
5294
bbff29a01820
Add compiler macros and compilation sanity-checks for functions with keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
1247 string-to-int string-to-number substring symbol-plist symbol-value |
bbff29a01820
Add compiler macros and compilation sanity-checks for functions with keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
1248 symbol-name symbol-function symbol |
428 | 1249 tan upcase user-variable-p vconcat |
1250 ;; XEmacs change: window-edges -> window-pixel-edges | |
1251 window-buffer window-dedicated-p window-pixel-edges window-height | |
1252 window-hscroll window-minibuffer-p window-width | |
1253 zerop | |
1254 ;; functions defined by cl | |
1255 oddp evenp plusp minusp | |
1256 abs expt signum last butlast ldiff | |
1257 pairlis gcd lcm | |
1258 isqrt floor* ceiling* truncate* round* mod* rem* subseq | |
440 | 1259 list-length getf |
428 | 1260 )) |
1261 (side-effect-and-error-free-fns | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1262 '(acons arrayp atom |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1263 bigfloatp bignump bobp bolp buffer-end buffer-list buffer-size |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1264 buffer-string bufferp |
428 | 1265 car-safe case-table-p cdr-safe char-or-string-p char-table-p |
1266 characterp commandp cons | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1267 consolep console-live-p consp copy-tree |
428 | 1268 current-buffer |
1269 ;; XEmacs: extent functions, frame-live-p, various other stuff | |
1270 devicep device-live-p | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1271 eobp eolp eq eql equal equalp eventp extentp |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1272 extent-live-p fixnump floatingp floatp framep frame-live-p |
428 | 1273 get-largest-window get-lru-window |
1274 hash-table-p | |
1275 identity ignore integerp integer-or-marker-p interactive-p | |
1276 invocation-directory invocation-name | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1277 keymapp list list* listp |
428 | 1278 make-marker mark mark-marker markerp memory-limit minibuffer-window |
1279 ;; mouse-movement-p not in XEmacs | |
1280 natnump nlistp not null number-or-marker-p numberp | |
1281 one-window-p ;; overlayp not in XEmacs | |
1282 point point-marker point-min point-max processp | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1283 random-state-p rationalp ratiop range-table-p realp |
428 | 1284 selected-window sequencep stringp subrp symbolp syntax-table-p |
1285 user-full-name user-login-name user-original-login-name | |
1286 user-real-login-name user-real-uid user-uid | |
1287 vector vectorp | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1288 window-configuration-p window-live-p windowp))) |
428 | 1289 (dolist (fn side-effect-free-fns) |
1290 (put fn 'side-effect-free t)) | |
1291 (dolist (fn side-effect-and-error-free-fns) | |
1292 (put fn 'side-effect-free 'error-free))) | |
1293 | |
5502
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1294 (dolist (function |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1295 '(adjoin assoc* count find intersection member* mismatch position |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1296 rassoc* remove* remove-duplicates search set-difference |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1297 set-exclusive-or stable-intersection stable-sort stable-union |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1298 sublis subsetp subst substitute tree-equal union)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1299 ;; These all throw errors, there's no point implementing an error-free |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1300 ;; version of the list. |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1301 (put function 'side-effect-free-if-keywords-are t)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1302 |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1303 (defun byte-optimize-side-effect-free-p (form) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1304 (or (get (car-safe form) 'side-effect-free) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1305 (and (get (car-safe form) 'side-effect-free-if-keywords-are) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1306 (loop |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1307 for (key value) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1308 on (nthcdr (get (car form) 'byte-compile-keyword-start) form) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1309 by #'cddr |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1310 never (or (and (member* key |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1311 '(:test :test-not :key :if :if-not)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1312 (or (not (byte-compile-constp value)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1313 (not (and (consp value) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1314 (symbolp (cadr value)) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1315 (get (cadr value) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1316 'side-effect-free))))) |
5b08be74bb53
Be better about recognising side-effect-free forms, byte-optimize.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5473
diff
changeset
|
1317 (not (keywordp key))))))) |
428 | 1318 |
1319 (defun byte-compile-splice-in-already-compiled-code (form) | |
1320 ;; form is (byte-code "..." [...] n) | |
446 | 1321 (if (not (memq byte-optimize '(t byte))) |
428 | 1322 (byte-compile-normal-call form) |
1323 (byte-inline-lapcode | |
1324 (byte-decompile-bytecode-1 (nth 1 form) (nth 2 form) t)) | |
1325 (setq byte-compile-maxdepth (max (+ byte-compile-depth (nth 3 form)) | |
1326 byte-compile-maxdepth)) | |
1327 (setq byte-compile-depth (1+ byte-compile-depth)))) | |
1328 | |
1329 (put 'byte-code 'byte-compile 'byte-compile-splice-in-already-compiled-code) | |
1330 | |
1331 | |
1332 (defconst byte-constref-ops | |
1333 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind)) | |
1334 | |
1335 ;;; This function extracts the bitfields from variable-length opcodes. | |
1336 ;;; Originally defined in disass.el (which no longer uses it.) | |
1337 | |
1338 (defun disassemble-offset () | |
1339 "Don't call this!" | |
1340 ;; fetch and return the offset for the current opcode. | |
1341 ;; return NIL if this opcode has no offset | |
1342 ;; OP, PTR and BYTES are used and set dynamically | |
442 | 1343 (declare (special op ptr bytes)) |
428 | 1344 (cond ((< op byte-nth) |
1345 (let ((tem (logand op 7))) | |
1346 (setq op (logand op 248)) | |
1347 (cond ((eq tem 6) | |
1348 (setq ptr (1+ ptr)) ;offset in next byte | |
1349 ;; char-to-int to avoid downstream problems | |
1350 ;; caused by chars appearing where ints are | |
1351 ;; expected. In bytecode the bytes in the | |
1352 ;; opcode string are always interpreted as ints. | |
1353 (char-to-int (aref bytes ptr))) | |
1354 ((eq tem 7) | |
1355 (setq ptr (1+ ptr)) ;offset in next 2 bytes | |
1356 (+ (aref bytes ptr) | |
1357 (progn (setq ptr (1+ ptr)) | |
1358 (lsh (aref bytes ptr) 8)))) | |
1359 (t tem)))) ;offset was in opcode | |
1360 ((>= op byte-constant) | |
1361 (prog1 (- op byte-constant) ;offset in opcode | |
1362 (setq op byte-constant))) | |
1363 ((and (>= op byte-constant2) | |
1364 (<= op byte-goto-if-not-nil-else-pop)) | |
1365 (setq ptr (1+ ptr)) ;offset in next 2 bytes | |
1366 (+ (aref bytes ptr) | |
1367 (progn (setq ptr (1+ ptr)) | |
1368 (lsh (aref bytes ptr) 8)))) | |
1369 ;; XEmacs: this code was here before. FSF's first comparison | |
1370 ;; is (>= op byte-listN). It appears that the rel-goto stuff | |
1371 ;; does not exist in FSF 19.30. It doesn't exist in 19.28 | |
1372 ;; either, so I'm going to assume that this is an improvement | |
1373 ;; on our part and leave it in. --ben | |
1374 ((and (>= op byte-rel-goto) | |
1375 (<= op byte-insertN)) | |
1376 (setq ptr (1+ ptr)) ;offset in next byte | |
1377 ;; Use char-to-int to avoid downstream problems caused by | |
1378 ;; chars appearing where ints are expected. In bytecode | |
1379 ;; the bytes in the opcode string are always interpreted as | |
1380 ;; ints. | |
1381 (char-to-int (aref bytes ptr))))) | |
1382 | |
1383 | |
1384 ;;; This de-compiler is used for inline expansion of compiled functions, | |
1385 ;;; and by the disassembler. | |
1386 ;;; | |
1387 ;;; This list contains numbers, which are pc values, | |
1388 ;;; before each instruction. | |
1389 (defun byte-decompile-bytecode (bytes constvec) | |
1390 "Turns BYTECODE into lapcode, referring to CONSTVEC." | |
1391 (let ((byte-compile-constants nil) | |
1392 (byte-compile-variables nil) | |
1393 (byte-compile-tag-number 0)) | |
1394 (byte-decompile-bytecode-1 bytes constvec))) | |
1395 | |
1396 ;; As byte-decompile-bytecode, but updates | |
1397 ;; byte-compile-{constants, variables, tag-number}. | |
1398 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced | |
1399 ;; with `goto's destined for the end of the code. | |
1400 ;; That is for use by the compiler. | |
1401 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler. | |
1402 ;; In that case, we put a pc value into the list | |
1403 ;; before each insn (or its label). | |
1404 (defun byte-decompile-bytecode-1 (bytes constvec &optional make-spliceable) | |
1405 (let ((length (length bytes)) | |
1406 (ptr 0) optr tags op offset | |
1407 ;; tag unused | |
1408 lap tmp | |
1409 endtag | |
1410 ;; (retcount 0) unused | |
1411 ) | |
1412 (while (not (= ptr length)) | |
1413 (or make-spliceable | |
1414 (setq lap (cons ptr lap))) | |
1415 (setq op (aref bytes ptr) | |
1416 optr ptr | |
1417 offset (disassemble-offset)) ; this does dynamic-scope magic | |
1418 (setq op (aref byte-code-vector op)) | |
1419 ;; XEmacs: the next line in FSF 19.30 reads | |
1420 ;; (cond ((memq op byte-goto-ops) | |
1421 ;; see the comment above about byte-rel-goto in XEmacs. | |
1422 (cond ((or (memq op byte-goto-ops) | |
1423 (cond ((memq op byte-rel-goto-ops) | |
1424 (setq op (aref byte-code-vector | |
1425 (- (symbol-value op) | |
1426 (- byte-rel-goto byte-goto)))) | |
1427 (setq offset (+ ptr (- offset 127))) | |
1428 t))) | |
1429 ;; it's a pc | |
1430 (setq offset | |
1431 (cdr (or (assq offset tags) | |
1432 (car (setq tags | |
1433 (cons (cons offset | |
1434 (byte-compile-make-tag)) | |
1435 tags))))))) | |
1436 ((cond ((eq op 'byte-constant2) (setq op 'byte-constant) t) | |
1437 ((memq op byte-constref-ops))) | |
1297 | 1438 (setq tmp (if (>= offset (length constvec)) |
1439 (list 'out-of-range offset) | |
1440 (aref constvec offset)) | |
428 | 1441 offset (if (eq op 'byte-constant) |
1442 (byte-compile-get-constant tmp) | |
1443 (or (assq tmp byte-compile-variables) | |
1444 (car (setq byte-compile-variables | |
1445 (cons (list tmp) | |
1446 byte-compile-variables))))))) | |
1447 ((and make-spliceable | |
1448 (eq op 'byte-return)) | |
1449 (if (= ptr (1- length)) | |
1450 (setq op nil) | |
1451 (setq offset (or endtag (setq endtag (byte-compile-make-tag))) | |
1452 op 'byte-goto)))) | |
1453 ;; lap = ( [ (pc . (op . arg)) ]* ) | |
1454 (setq lap (cons (cons optr (cons op (or offset 0))) | |
1455 lap)) | |
1456 (setq ptr (1+ ptr))) | |
1457 ;; take off the dummy nil op that we replaced a trailing "return" with. | |
1458 (let ((rest lap)) | |
1459 (while rest | |
1460 (cond ((numberp (car rest))) | |
1461 ((setq tmp (assq (car (car rest)) tags)) | |
1462 ;; this addr is jumped to | |
1463 (setcdr rest (cons (cons nil (cdr tmp)) | |
1464 (cdr rest))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1465 (setq tags (delete* tmp tags)) |
428 | 1466 (setq rest (cdr rest)))) |
1467 (setq rest (cdr rest)))) | |
1468 (if tags (error "optimizer error: missed tags %s" tags)) | |
1469 (if (null (car (cdr (car lap)))) | |
1470 (setq lap (cdr lap))) | |
1471 (if endtag | |
1472 (setq lap (cons (cons nil endtag) lap))) | |
1473 ;; remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* ) | |
1474 (mapcar #'(lambda (elt) (if (numberp elt) elt (cdr elt))) | |
1475 (nreverse lap)))) | |
1476 | |
1477 | |
1478 ;;; peephole optimizer | |
1479 | |
1480 (defconst byte-tagref-ops (cons 'TAG byte-goto-ops)) | |
1481 | |
1482 (defconst byte-conditional-ops | |
1483 '(byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop | |
1484 byte-goto-if-not-nil-else-pop)) | |
1485 | |
1486 (defconst byte-after-unbind-ops | |
1487 '(byte-constant byte-dup | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1488 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-fixnump |
444 | 1489 byte-eq byte-not |
428 | 1490 byte-cons byte-list1 byte-list2 ; byte-list3 byte-list4 |
1491 byte-interactive-p) | |
1492 ;; How about other side-effect-free-ops? Is it safe to move an | |
1493 ;; error invocation (such as from nth) out of an unwind-protect? | |
444 | 1494 ;; No, it is not, because the unwind-protect forms can alter |
1495 ;; the inside of the object to which nth would apply. | |
1496 ;; For the same reason, byte-equal was deleted from this list. | |
428 | 1497 "Byte-codes that can be moved past an unbind.") |
1498 | |
1499 (defconst byte-compile-side-effect-and-error-free-ops | |
1500 '(byte-constant byte-dup byte-symbolp byte-consp byte-stringp byte-listp | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4686
diff
changeset
|
1501 byte-fixnump byte-numberp byte-eq byte-equal byte-not byte-car-safe |
428 | 1502 byte-cdr-safe byte-cons byte-list1 byte-list2 byte-point byte-point-max |
1503 byte-point-min byte-following-char byte-preceding-char | |
1504 byte-current-column byte-eolp byte-eobp byte-bolp byte-bobp | |
1505 byte-current-buffer byte-interactive-p)) | |
1506 | |
1507 (defconst byte-compile-side-effect-free-ops | |
440 | 1508 (nconc |
428 | 1509 '(byte-varref byte-nth byte-memq byte-car byte-cdr byte-length byte-aref |
1510 byte-symbol-value byte-get byte-concat2 byte-concat3 byte-sub1 byte-add1 | |
1511 byte-eqlsign byte-gtr byte-lss byte-leq byte-geq byte-diff byte-negate | |
1512 byte-plus byte-max byte-min byte-mult byte-char-after byte-char-syntax | |
1513 byte-buffer-substring byte-string= byte-string< byte-nthcdr byte-elt | |
1514 byte-member byte-assq byte-quo byte-rem) | |
1515 byte-compile-side-effect-and-error-free-ops)) | |
1516 | |
1517 ;;; This piece of shit is because of the way DEFVAR_BOOL() variables work. | |
1518 ;;; Consider the code | |
1519 ;;; | |
1520 ;;; (defun foo (flag) | |
1521 ;;; (let ((old-pop-ups pop-up-windows) | |
1522 ;;; (pop-up-windows flag)) | |
1523 ;;; (cond ((not (eq pop-up-windows old-pop-ups)) | |
1524 ;;; (setq old-pop-ups pop-up-windows) | |
1525 ;;; ...)))) | |
1526 ;;; | |
1527 ;;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is | |
1528 ;;; something else. But if we optimize | |
1529 ;;; | |
1530 ;;; varref flag | |
1531 ;;; varbind pop-up-windows | |
1532 ;;; varref pop-up-windows | |
1533 ;;; not | |
1534 ;;; to | |
1535 ;;; varref flag | |
1536 ;;; dup | |
1537 ;;; varbind pop-up-windows | |
1538 ;;; not | |
1539 ;;; | |
440 | 1540 ;;; we break the program, because it will appear that pop-up-windows and |
428 | 1541 ;;; old-pop-ups are not EQ when really they are. So we have to know what |
1542 ;;; the BOOL variables are, and not perform this optimization on them. | |
1543 ;;; | |
1544 | |
1545 ;;; This used to hold a large list of boolean variables, which had to | |
1546 ;;; be updated every time a new DEFVAR_BOOL is added, making it very | |
1547 ;;; hard to maintain. Such a list is not necessary under XEmacs, | |
1548 ;;; where we can use `built-in-variable-type' to query for boolean | |
1549 ;;; variables. | |
1550 | |
1551 ;(defconst byte-boolean-vars | |
1297 | 1552 ; ...) |
428 | 1553 |
1554 (defun byte-optimize-lapcode (lap &optional for-effect) | |
1555 "Simple peephole optimizer. LAP is both modified and returned." | |
442 | 1556 (let (lap0 |
1557 lap1 | |
1558 lap2 | |
1559 variable-frequency | |
428 | 1560 (keep-going 'first-time) |
1561 (add-depth 0) | |
1562 rest tmp tmp2 tmp3 | |
1563 (side-effect-free (if byte-compile-delete-errors | |
1564 byte-compile-side-effect-free-ops | |
1565 byte-compile-side-effect-and-error-free-ops))) | |
1566 (while keep-going | |
1567 (or (eq keep-going 'first-time) | |
1568 (byte-compile-log-lap " ---- next pass")) | |
1569 (setq rest lap | |
1570 keep-going nil) | |
1571 (while rest | |
1572 (setq lap0 (car rest) | |
1573 lap1 (nth 1 rest) | |
1574 lap2 (nth 2 rest)) | |
1575 | |
1576 ;; You may notice that sequences like "dup varset discard" are | |
1577 ;; optimized but sequences like "dup varset TAG1: discard" are not. | |
1578 ;; You may be tempted to change this; resist that temptation. | |
1579 (cond ;; | |
1580 ;; <side-effect-free> pop --> <deleted> | |
1581 ;; ...including: | |
1582 ;; const-X pop --> <deleted> | |
1583 ;; varref-X pop --> <deleted> | |
1584 ;; dup pop --> <deleted> | |
1585 ;; | |
1586 ((and (eq 'byte-discard (car lap1)) | |
1587 (memq (car lap0) side-effect-free)) | |
1588 (setq keep-going t) | |
1589 (setq tmp (aref byte-stack+-info (symbol-value (car lap0)))) | |
1590 (setq rest (cdr rest)) | |
1591 (cond ((= tmp 1) | |
1592 (byte-compile-log-lap | |
1593 " %s discard\t-->\t<deleted>" lap0) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1594 (setq lap (delete* lap0 (delete* lap1 lap)))) |
428 | 1595 ((= tmp 0) |
1596 (byte-compile-log-lap | |
1597 " %s discard\t-->\t<deleted> discard" lap0) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1598 (setq lap (delete* lap0 lap))) |
428 | 1599 ((= tmp -1) |
1600 (byte-compile-log-lap | |
1601 " %s discard\t-->\tdiscard discard" lap0) | |
1602 (setcar lap0 'byte-discard) | |
1603 (setcdr lap0 0)) | |
1604 ((error "Optimizer error: too much on the stack")))) | |
1605 ;; | |
1606 ;; goto*-X X: --> X: | |
1607 ;; | |
1608 ((and (memq (car lap0) byte-goto-ops) | |
1609 (eq (cdr lap0) lap1)) | |
1610 (cond ((eq (car lap0) 'byte-goto) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1611 (setq lap (delete* lap0 lap)) |
428 | 1612 (setq tmp "<deleted>")) |
1613 ((memq (car lap0) byte-goto-always-pop-ops) | |
1614 (setcar lap0 (setq tmp 'byte-discard)) | |
1615 (setcdr lap0 0)) | |
1616 ((error "Depth conflict at tag %d" (nth 2 lap0)))) | |
1617 (and (memq byte-optimize-log '(t byte)) | |
1618 (byte-compile-log " (goto %s) %s:\t-->\t%s %s:" | |
1619 (nth 1 lap1) (nth 1 lap1) | |
1620 tmp (nth 1 lap1))) | |
1621 (setq keep-going t)) | |
1622 ;; | |
1623 ;; varset-X varref-X --> dup varset-X | |
1624 ;; varbind-X varref-X --> dup varbind-X | |
1625 ;; const/dup varset-X varref-X --> const/dup varset-X const/dup | |
1626 ;; const/dup varbind-X varref-X --> const/dup varbind-X const/dup | |
1627 ;; The latter two can enable other optimizations. | |
1628 ;; | |
1629 ((and (eq 'byte-varref (car lap2)) | |
1630 (eq (cdr lap1) (cdr lap2)) | |
1631 (memq (car lap1) '(byte-varset byte-varbind))) | |
1632 (if (and (setq tmp (eq (built-in-variable-type (car (cdr lap2))) | |
1633 'boolean)) | |
1634 (not (eq (car lap0) 'byte-constant))) | |
1635 nil | |
1636 (setq keep-going t) | |
1637 (if (memq (car lap0) '(byte-constant byte-dup)) | |
1638 (progn | |
1639 (setq tmp (if (or (not tmp) | |
1640 (memq (car (cdr lap0)) '(nil t))) | |
1641 (cdr lap0) | |
1642 (byte-compile-get-constant t))) | |
1643 (byte-compile-log-lap " %s %s %s\t-->\t%s %s %s" | |
1644 lap0 lap1 lap2 lap0 lap1 | |
1645 (cons (car lap0) tmp)) | |
1646 (setcar lap2 (car lap0)) | |
1647 (setcdr lap2 tmp)) | |
1648 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1) | |
1649 (setcar lap2 (car lap1)) | |
1650 (setcar lap1 'byte-dup) | |
1651 (setcdr lap1 0) | |
1652 ;; The stack depth gets locally increased, so we will | |
1653 ;; increase maxdepth in case depth = maxdepth here. | |
1654 ;; This can cause the third argument to byte-code to | |
1655 ;; be larger than necessary. | |
1656 (setq add-depth 1)))) | |
1657 ;; | |
1658 ;; dup varset-X discard --> varset-X | |
1659 ;; dup varbind-X discard --> varbind-X | |
1660 ;; (the varbind variant can emerge from other optimizations) | |
1661 ;; | |
1662 ((and (eq 'byte-dup (car lap0)) | |
1663 (eq 'byte-discard (car lap2)) | |
1664 (memq (car lap1) '(byte-varset byte-varbind))) | |
1665 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1) | |
1666 (setq keep-going t | |
1667 rest (cdr rest)) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1668 (setq lap (delete* lap0 (delete* lap2 lap)))) |
428 | 1669 ;; |
1670 ;; not goto-X-if-nil --> goto-X-if-non-nil | |
1671 ;; not goto-X-if-non-nil --> goto-X-if-nil | |
1672 ;; | |
1673 ;; it is wrong to do the same thing for the -else-pop variants. | |
1674 ;; | |
1675 ((and (eq 'byte-not (car lap0)) | |
1676 (or (eq 'byte-goto-if-nil (car lap1)) | |
1677 (eq 'byte-goto-if-not-nil (car lap1)))) | |
1678 (byte-compile-log-lap " not %s\t-->\t%s" | |
1679 lap1 | |
1680 (cons | |
1681 (if (eq (car lap1) 'byte-goto-if-nil) | |
1682 'byte-goto-if-not-nil | |
1683 'byte-goto-if-nil) | |
1684 (cdr lap1))) | |
1685 (setcar lap1 (if (eq (car lap1) 'byte-goto-if-nil) | |
1686 'byte-goto-if-not-nil | |
1687 'byte-goto-if-nil)) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1688 (setq lap (delete* lap0 lap)) |
428 | 1689 (setq keep-going t)) |
1690 ;; | |
1691 ;; goto-X-if-nil goto-Y X: --> goto-Y-if-non-nil X: | |
1692 ;; goto-X-if-non-nil goto-Y X: --> goto-Y-if-nil X: | |
1693 ;; | |
1694 ;; it is wrong to do the same thing for the -else-pop variants. | |
440 | 1695 ;; |
428 | 1696 ((and (or (eq 'byte-goto-if-nil (car lap0)) |
1697 (eq 'byte-goto-if-not-nil (car lap0))) ; gotoX | |
1698 (eq 'byte-goto (car lap1)) ; gotoY | |
1699 (eq (cdr lap0) lap2)) ; TAG X | |
1700 (let ((inverse (if (eq 'byte-goto-if-nil (car lap0)) | |
1701 'byte-goto-if-not-nil 'byte-goto-if-nil))) | |
1702 (byte-compile-log-lap " %s %s %s:\t-->\t%s %s:" | |
1703 lap0 lap1 lap2 | |
1704 (cons inverse (cdr lap1)) lap2) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1705 (setq lap (delete* lap0 lap)) |
428 | 1706 (setcar lap1 inverse) |
1707 (setq keep-going t))) | |
1708 ;; | |
1709 ;; const goto-if-* --> whatever | |
1710 ;; | |
1711 ((and (eq 'byte-constant (car lap0)) | |
1712 (memq (car lap1) byte-conditional-ops)) | |
1713 (cond ((if (or (eq (car lap1) 'byte-goto-if-nil) | |
1714 (eq (car lap1) 'byte-goto-if-nil-else-pop)) | |
1715 (car (cdr lap0)) | |
1716 (not (car (cdr lap0)))) | |
1717 (byte-compile-log-lap " %s %s\t-->\t<deleted>" | |
1718 lap0 lap1) | |
1719 (setq rest (cdr rest) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1720 lap (delete* lap0 (delete* lap1 lap)))) |
428 | 1721 (t |
1722 (if (memq (car lap1) byte-goto-always-pop-ops) | |
1723 (progn | |
1724 (byte-compile-log-lap " %s %s\t-->\t%s" | |
1725 lap0 lap1 (cons 'byte-goto (cdr lap1))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1726 (setq lap (delete* lap0 lap))) |
428 | 1727 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 |
1728 (cons 'byte-goto (cdr lap1)))) | |
1729 (setcar lap1 'byte-goto))) | |
1730 (setq keep-going t)) | |
1731 ;; | |
1732 ;; varref-X varref-X --> varref-X dup | |
1733 ;; varref-X [dup ...] varref-X --> varref-X [dup ...] dup | |
1734 ;; We don't optimize the const-X variations on this here, | |
1735 ;; because that would inhibit some goto optimizations; we | |
1736 ;; optimize the const-X case after all other optimizations. | |
1737 ;; | |
1738 ((and (eq 'byte-varref (car lap0)) | |
1739 (progn | |
1740 (setq tmp (cdr rest)) | |
1741 (while (eq (car (car tmp)) 'byte-dup) | |
1742 (setq tmp (cdr tmp))) | |
1743 t) | |
1744 (eq (cdr lap0) (cdr (car tmp))) | |
1745 (eq 'byte-varref (car (car tmp)))) | |
1746 (if (memq byte-optimize-log '(t byte)) | |
1747 (let ((str "")) | |
1748 (setq tmp2 (cdr rest)) | |
1749 (while (not (eq tmp tmp2)) | |
1750 (setq tmp2 (cdr tmp2) | |
1751 str (concat str " dup"))) | |
1752 (byte-compile-log-lap " %s%s %s\t-->\t%s%s dup" | |
1753 lap0 str lap0 lap0 str))) | |
1754 (setq keep-going t) | |
1755 (setcar (car tmp) 'byte-dup) | |
1756 (setcdr (car tmp) 0) | |
1757 (setq rest tmp)) | |
1758 ;; | |
1759 ;; TAG1: TAG2: --> TAG1: <deleted> | |
1760 ;; (and other references to TAG2 are replaced with TAG1) | |
1761 ;; | |
1762 ((and (eq (car lap0) 'TAG) | |
1763 (eq (car lap1) 'TAG)) | |
1764 (and (memq byte-optimize-log '(t byte)) | |
1765 (byte-compile-log " adjacent tags %d and %d merged" | |
1766 (nth 1 lap1) (nth 1 lap0))) | |
1767 (setq tmp3 lap) | |
1768 (while (setq tmp2 (rassq lap0 tmp3)) | |
1769 (setcdr tmp2 lap1) | |
1770 (setq tmp3 (cdr (memq tmp2 tmp3)))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1771 (setq lap (delete* lap0 lap) |
428 | 1772 keep-going t)) |
1773 ;; | |
1774 ;; unused-TAG: --> <deleted> | |
1775 ;; | |
1776 ((and (eq 'TAG (car lap0)) | |
1777 (not (rassq lap0 lap))) | |
1778 (and (memq byte-optimize-log '(t byte)) | |
1779 (byte-compile-log " unused tag %d removed" (nth 1 lap0))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1780 (setq lap (delete* lap0 lap) |
428 | 1781 keep-going t)) |
1782 ;; | |
1783 ;; goto ... --> goto <delete until TAG or end> | |
1784 ;; return ... --> return <delete until TAG or end> | |
1785 ;; | |
1786 ((and (memq (car lap0) '(byte-goto byte-return)) | |
1787 (not (memq (car lap1) '(TAG nil)))) | |
1788 (setq tmp rest) | |
1789 (let ((i 0) | |
1790 (opt-p (memq byte-optimize-log '(t lap))) | |
1791 str deleted) | |
1792 (while (and (setq tmp (cdr tmp)) | |
1793 (not (eq 'TAG (car (car tmp))))) | |
1794 (if opt-p (setq deleted (cons (car tmp) deleted) | |
1795 str (concat str " %s") | |
1796 i (1+ i)))) | |
1797 (if opt-p | |
440 | 1798 (let ((tagstr |
428 | 1799 (if (eq 'TAG (car (car tmp))) |
1800 (format "%d:" (car (cdr (car tmp)))) | |
1801 (or (car tmp) "")))) | |
1802 (if (< i 6) | |
1803 (apply 'byte-compile-log-lap-1 | |
1804 (concat " %s" str | |
1805 " %s\t-->\t%s <deleted> %s") | |
1806 lap0 | |
1807 (nconc (nreverse deleted) | |
1808 (list tagstr lap0 tagstr))) | |
1809 (byte-compile-log-lap | |
1810 " %s <%d unreachable op%s> %s\t-->\t%s <deleted> %s" | |
1811 lap0 i (if (= i 1) "" "s") | |
1812 tagstr lap0 tagstr)))) | |
1813 (rplacd rest tmp)) | |
1814 (setq keep-going t)) | |
1815 ;; | |
1816 ;; <safe-op> unbind --> unbind <safe-op> | |
1817 ;; (this may enable other optimizations.) | |
1818 ;; | |
1819 ((and (eq 'byte-unbind (car lap1)) | |
1820 (memq (car lap0) byte-after-unbind-ops)) | |
1821 (byte-compile-log-lap " %s %s\t-->\t%s %s" lap0 lap1 lap1 lap0) | |
1822 (setcar rest lap1) | |
1823 (setcar (cdr rest) lap0) | |
1824 (setq keep-going t)) | |
1825 ;; | |
1826 ;; varbind-X unbind-N --> discard unbind-(N-1) | |
1827 ;; save-excursion unbind-N --> unbind-(N-1) | |
1828 ;; save-restriction unbind-N --> unbind-(N-1) | |
1829 ;; | |
1830 ((and (eq 'byte-unbind (car lap1)) | |
1831 (memq (car lap0) '(byte-varbind byte-save-excursion | |
1832 byte-save-restriction)) | |
1833 (< 0 (cdr lap1))) | |
1834 (if (zerop (setcdr lap1 (1- (cdr lap1)))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1835 (delete* lap1 rest)) |
428 | 1836 (if (eq (car lap0) 'byte-varbind) |
1837 (setcar rest (cons 'byte-discard 0)) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1838 (setq lap (delete* lap0 lap))) |
428 | 1839 (byte-compile-log-lap " %s %s\t-->\t%s %s" |
1840 lap0 (cons (car lap1) (1+ (cdr lap1))) | |
1841 (if (eq (car lap0) 'byte-varbind) | |
1842 (car rest) | |
1843 (car (cdr rest))) | |
1844 (if (and (/= 0 (cdr lap1)) | |
1845 (eq (car lap0) 'byte-varbind)) | |
1846 (car (cdr rest)) | |
1847 "")) | |
1848 (setq keep-going t)) | |
1849 ;; | |
1850 ;; goto*-X ... X: goto-Y --> goto*-Y | |
1851 ;; goto-X ... X: return --> return | |
1852 ;; | |
1853 ((and (memq (car lap0) byte-goto-ops) | |
1854 (memq (car (setq tmp (nth 1 (memq (cdr lap0) lap)))) | |
1855 '(byte-goto byte-return))) | |
1856 (cond ((and (not (eq tmp lap0)) | |
1857 (or (eq (car lap0) 'byte-goto) | |
1858 (eq (car tmp) 'byte-goto))) | |
1859 (byte-compile-log-lap " %s [%s]\t-->\t%s" | |
1860 (car lap0) tmp tmp) | |
1861 (if (eq (car tmp) 'byte-return) | |
1862 (setcar lap0 'byte-return)) | |
1863 (setcdr lap0 (cdr tmp)) | |
1864 (setq keep-going t)))) | |
1865 ;; | |
1866 ;; goto-*-else-pop X ... X: goto-if-* --> whatever | |
1867 ;; goto-*-else-pop X ... X: discard --> whatever | |
1868 ;; | |
1869 ((and (memq (car lap0) '(byte-goto-if-nil-else-pop | |
1870 byte-goto-if-not-nil-else-pop)) | |
1871 (memq (car (car (setq tmp (cdr (memq (cdr lap0) lap))))) | |
1872 (eval-when-compile | |
1873 (cons 'byte-discard byte-conditional-ops))) | |
1874 (not (eq lap0 (car tmp)))) | |
1875 (setq tmp2 (car tmp)) | |
1876 (setq tmp3 (assq (car lap0) '((byte-goto-if-nil-else-pop | |
1877 byte-goto-if-nil) | |
1878 (byte-goto-if-not-nil-else-pop | |
1879 byte-goto-if-not-nil)))) | |
1880 (if (memq (car tmp2) tmp3) | |
1881 (progn (setcar lap0 (car tmp2)) | |
1882 (setcdr lap0 (cdr tmp2)) | |
1883 (byte-compile-log-lap " %s-else-pop [%s]\t-->\t%s" | |
1884 (car lap0) tmp2 lap0)) | |
1885 ;; Get rid of the -else-pop's and jump one step further. | |
1886 (or (eq 'TAG (car (nth 1 tmp))) | |
1887 (setcdr tmp (cons (byte-compile-make-tag) | |
1888 (cdr tmp)))) | |
1889 (byte-compile-log-lap " %s [%s]\t-->\t%s <skip>" | |
1890 (car lap0) tmp2 (nth 1 tmp3)) | |
1891 (setcar lap0 (nth 1 tmp3)) | |
1892 (setcdr lap0 (nth 1 tmp))) | |
1893 (setq keep-going t)) | |
1894 ;; | |
1895 ;; const goto-X ... X: goto-if-* --> whatever | |
1896 ;; const goto-X ... X: discard --> whatever | |
1897 ;; | |
1898 ((and (eq (car lap0) 'byte-constant) | |
1899 (eq (car lap1) 'byte-goto) | |
1900 (memq (car (car (setq tmp (cdr (memq (cdr lap1) lap))))) | |
1901 (eval-when-compile | |
1902 (cons 'byte-discard byte-conditional-ops))) | |
1903 (not (eq lap1 (car tmp)))) | |
1904 (setq tmp2 (car tmp)) | |
1905 (cond ((memq (car tmp2) | |
1906 (if (null (car (cdr lap0))) | |
1907 '(byte-goto-if-nil byte-goto-if-nil-else-pop) | |
1908 '(byte-goto-if-not-nil | |
1909 byte-goto-if-not-nil-else-pop))) | |
1910 (byte-compile-log-lap " %s goto [%s]\t-->\t%s %s" | |
1911 lap0 tmp2 lap0 tmp2) | |
1912 (setcar lap1 (car tmp2)) | |
1913 (setcdr lap1 (cdr tmp2)) | |
1914 ;; Let next step fix the (const,goto-if*) sequence. | |
1915 (setq rest (cons nil rest))) | |
1916 (t | |
1917 ;; Jump one step further | |
1918 (byte-compile-log-lap | |
1919 " %s goto [%s]\t-->\t<deleted> goto <skip>" | |
1920 lap0 tmp2) | |
1921 (or (eq 'TAG (car (nth 1 tmp))) | |
1922 (setcdr tmp (cons (byte-compile-make-tag) | |
1923 (cdr tmp)))) | |
1924 (setcdr lap1 (car (cdr tmp))) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
1925 (setq lap (delete* lap0 lap)))) |
428 | 1926 (setq keep-going t)) |
1927 ;; | |
1928 ;; X: varref-Y ... varset-Y goto-X --> | |
1929 ;; X: varref-Y Z: ... dup varset-Y goto-Z | |
1930 ;; (varset-X goto-BACK, BACK: varref-X --> copy the varref down.) | |
1931 ;; (This is so usual for while loops that it is worth handling). | |
1932 ;; | |
1933 ((and (eq (car lap1) 'byte-varset) | |
1934 (eq (car lap2) 'byte-goto) | |
1935 (not (memq (cdr lap2) rest)) ;Backwards jump | |
1936 (eq (car (car (setq tmp (cdr (memq (cdr lap2) lap))))) | |
1937 'byte-varref) | |
1938 (eq (cdr (car tmp)) (cdr lap1)) | |
1939 (not (eq (built-in-variable-type (car (cdr lap1))) | |
1940 'boolean))) | |
1941 ;;(byte-compile-log-lap " Pulled %s to end of loop" (car tmp)) | |
1942 (let ((newtag (byte-compile-make-tag))) | |
1943 (byte-compile-log-lap | |
1944 " %s: %s ... %s %s\t-->\t%s: %s %s: ... %s %s %s" | |
1945 (nth 1 (cdr lap2)) (car tmp) | |
1946 lap1 lap2 | |
1947 (nth 1 (cdr lap2)) (car tmp) | |
1948 (nth 1 newtag) 'byte-dup lap1 | |
1949 (cons 'byte-goto newtag) | |
1950 ) | |
1951 (setcdr rest (cons (cons 'byte-dup 0) (cdr rest))) | |
1952 (setcdr tmp (cons (setcdr lap2 newtag) (cdr tmp)))) | |
1953 (setq add-depth 1) | |
1954 (setq keep-going t)) | |
1955 ;; | |
1956 ;; goto-X Y: ... X: goto-if*-Y --> goto-if-not-*-X+1 Y: | |
1957 ;; (This can pull the loop test to the end of the loop) | |
1958 ;; | |
1959 ((and (eq (car lap0) 'byte-goto) | |
1960 (eq (car lap1) 'TAG) | |
1961 (eq lap1 | |
1962 (cdr (car (setq tmp (cdr (memq (cdr lap0) lap)))))) | |
1963 (memq (car (car tmp)) | |
1964 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil | |
1965 byte-goto-if-nil-else-pop))) | |
1966 ;; (byte-compile-log-lap " %s %s, %s %s --> moved conditional" | |
1967 ;; lap0 lap1 (cdr lap0) (car tmp)) | |
1968 (let ((newtag (byte-compile-make-tag))) | |
1969 (byte-compile-log-lap | |
1970 "%s %s: ... %s: %s\t-->\t%s ... %s:" | |
1971 lap0 (nth 1 lap1) (nth 1 (cdr lap0)) (car tmp) | |
1972 (cons (cdr (assq (car (car tmp)) | |
1973 '((byte-goto-if-nil . byte-goto-if-not-nil) | |
1974 (byte-goto-if-not-nil . byte-goto-if-nil) | |
1975 (byte-goto-if-nil-else-pop . | |
1976 byte-goto-if-not-nil-else-pop) | |
1977 (byte-goto-if-not-nil-else-pop . | |
1978 byte-goto-if-nil-else-pop)))) | |
1979 newtag) | |
440 | 1980 |
428 | 1981 (nth 1 newtag) |
1982 ) | |
1983 (setcdr tmp (cons (setcdr lap0 newtag) (cdr tmp))) | |
1984 (if (eq (car (car tmp)) 'byte-goto-if-nil-else-pop) | |
1985 ;; We can handle this case but not the -if-not-nil case, | |
1986 ;; because we won't know which non-nil constant to push. | |
1987 (setcdr rest (cons (cons 'byte-constant | |
1988 (byte-compile-get-constant nil)) | |
1989 (cdr rest)))) | |
1990 (setcar lap0 (nth 1 (memq (car (car tmp)) | |
1991 '(byte-goto-if-nil-else-pop | |
1992 byte-goto-if-not-nil | |
1993 byte-goto-if-nil | |
1994 byte-goto-if-not-nil | |
1995 byte-goto byte-goto)))) | |
1996 ) | |
1997 (setq keep-going t)) | |
1998 ) | |
1999 (setq rest (cdr rest))) | |
2000 ) | |
2001 ;; Cleanup stage: | |
2002 ;; Rebuild byte-compile-constants / byte-compile-variables. | |
2003 ;; Simple optimizations that would inhibit other optimizations if they | |
2004 ;; were done in the optimizing loop, and optimizations which there is no | |
442 | 2005 ;; need to do more than once. |
428 | 2006 (setq byte-compile-constants nil |
442 | 2007 byte-compile-variables nil |
2008 variable-frequency (make-hash-table :test 'eq)) | |
428 | 2009 (setq rest lap) |
2010 (while rest | |
2011 (setq lap0 (car rest) | |
2012 lap1 (nth 1 rest)) | |
1297 | 2013 (if (memq (car lap0) byte-constref-ops) |
2014 (if (not (eq (car lap0) 'byte-constant)) | |
2015 (progn | |
2016 (incf (gethash (cdr lap0) variable-frequency 0)) | |
2017 (or (memq (cdr lap0) byte-compile-variables) | |
2018 (setq byte-compile-variables | |
2019 (cons (cdr lap0) byte-compile-variables)))) | |
2020 (or (memq (cdr lap0) byte-compile-constants) | |
2021 (setq byte-compile-constants (cons (cdr lap0) | |
2022 byte-compile-constants))))) | |
428 | 2023 (cond (;; |
442 | 2024 ;; const-C varset-X const-C --> const-C dup varset-X |
428 | 2025 ;; const-C varbind-X const-C --> const-C dup varbind-X |
2026 ;; | |
2027 (and (eq (car lap0) 'byte-constant) | |
2028 (eq (car (nth 2 rest)) 'byte-constant) | |
442 | 2029 (eq (cdr lap0) (cdr (nth 2 rest))) |
428 | 2030 (memq (car lap1) '(byte-varbind byte-varset))) |
2031 (byte-compile-log-lap " %s %s %s\t-->\t%s dup %s" | |
2032 lap0 lap1 lap0 lap0 lap1) | |
2033 (setcar (cdr (cdr rest)) (cons (car lap1) (cdr lap1))) | |
2034 (setcar (cdr rest) (cons 'byte-dup 0)) | |
2035 (setq add-depth 1)) | |
2036 ;; | |
2037 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup | |
2038 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup | |
2039 ;; | |
2040 ((memq (car lap0) '(byte-constant byte-varref)) | |
2041 (setq tmp rest | |
2042 tmp2 nil) | |
2043 (while (progn | |
2044 (while (eq 'byte-dup (car (car (setq tmp (cdr tmp)))))) | |
2045 (and (eq (cdr lap0) (cdr (car tmp))) | |
2046 (eq (car lap0) (car (car tmp))))) | |
2047 (setcar tmp (cons 'byte-dup 0)) | |
2048 (setq tmp2 t)) | |
2049 (if tmp2 | |
2050 (byte-compile-log-lap | |
2051 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0))) | |
2052 ;; | |
2053 ;; unbind-N unbind-M --> unbind-(N+M) | |
2054 ;; | |
2055 ((and (eq 'byte-unbind (car lap0)) | |
2056 (eq 'byte-unbind (car lap1))) | |
2057 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 | |
2058 (cons 'byte-unbind | |
2059 (+ (cdr lap0) (cdr lap1)))) | |
2060 (setq keep-going t) | |
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5651
diff
changeset
|
2061 (setq lap (delete* lap0 lap)) |
428 | 2062 (setcdr lap1 (+ (cdr lap1) (cdr lap0)))) |
2063 ) | |
2064 (setq rest (cdr rest))) | |
442 | 2065 ;; Since the first 6 entries of the compiled-function constants |
2066 ;; vector are most efficient for varref/set/bind ops, we sort by | |
2067 ;; reference count. This generates maximally space efficient and | |
2068 ;; pretty time-efficient byte-code. See `byte-compile-constants-vector'. | |
2069 (setq byte-compile-variables | |
2070 (sort byte-compile-variables | |
2071 #'(lambda (v1 v2) | |
2072 (< (gethash v1 variable-frequency) | |
2073 (gethash v2 variable-frequency))))) | |
2074 ;; Another hack - put the most used variable in position 6, for | |
2075 ;; better locality of reference with adjoining constants. | |
2076 (let ((tail (last byte-compile-variables 6))) | |
2077 (setq byte-compile-variables | |
2078 (append (nbutlast byte-compile-variables 6) | |
2079 (nreverse tail)))) | |
428 | 2080 (setq byte-compile-maxdepth (+ byte-compile-maxdepth add-depth))) |
2081 lap) | |
2082 | |
2083 (provide 'byte-optimize) | |
2084 | |
2085 | |
2086 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when this file compiles | |
2087 ;; itself, compile some of its most used recursive functions (at load time). | |
2088 ;; | |
2089 (eval-when-compile | |
2090 (or (compiled-function-p (symbol-function 'byte-optimize-form)) | |
2091 (assq 'byte-code (symbol-function 'byte-optimize-form)) | |
2092 (let ((byte-optimize nil) | |
2093 (byte-compile-warnings nil)) | |
2094 (mapcar | |
2095 #'(lambda (x) | |
2096 (or noninteractive (message "compiling %s..." x)) | |
2097 (byte-compile x) | |
2098 (or noninteractive (message "compiling %s...done" x))) | |
2099 '(byte-optimize-form | |
2100 byte-optimize-body | |
2101 byte-optimize-predicate | |
2102 byte-optimize-binary-predicate | |
2103 ;; Inserted some more than necessary, to speed it up. | |
2104 byte-optimize-form-code-walker | |
2105 byte-optimize-lapcode)))) | |
2106 nil) | |
2107 | |
1297 | 2108 ;; END SYNC WITH 20.7. |
2109 | |
428 | 2110 ;;; byte-optimize.el ends here |