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