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