byte-opt.el 80 KB

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