1 ;;; cconv.el --- Closure conversion for statically scoped Emacs lisp. -*- lexical-binding: nil -*-
3 ;; Copyright (C) 2011 Free Software Foundation, Inc.
5 ;; Author: Igor Kuzmin <kzuminig@iro.umontreal.ca>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This takes a piece of Elisp code, and eliminates all free variables from
28 ;; lambda expressions. The user entry points are cconv-closure-convert and
29 ;; cconv-closure-convert-toplevel(for toplevel forms).
30 ;; All macros should be expanded beforehand.
32 ;; Here is a brief explanation how this code works.
33 ;; Firstly, we analyse the tree by calling cconv-analyse-form.
34 ;; This function finds all mutated variables, all functions that are suitable
35 ;; for lambda lifting and all variables captured by closure. It passes the tree
36 ;; once, returning a list of three lists.
38 ;; Then we calculate the intersection of first and third lists returned by
39 ;; cconv-analyse form to find all mutated variables that are captured by
42 ;; Armed with this data, we call cconv-closure-convert-rec, that rewrites the
43 ;; tree recursivly, lifting lambdas where possible, building closures where it
44 ;; is needed and eliminating mutable variables used in closure.
46 ;; We do following replacements :
47 ;; (lambda (v1 ...) ... fv1 fv2 ...) => (lambda (v1 ... fv1 fv2 ) ... fv1 fv2 .)
48 ;; if the function is suitable for lambda lifting (if all calls are known)
50 ;; (lambda (v1 ...) ... fv ...) =>
51 ;; (curry (lambda (env v1 ...) ... env ...) env)
52 ;; if the function has only 1 free variable
55 ;; (lambda (v1 ...) ... fv1 fv2 ...) =>
56 ;; (curry (lambda (env v1 ..) .. (aref env 0) (aref env 1) ..) (vector fv1 fv2))
57 ;; if the function has 2 or more free variables.
59 ;; If the function has no free variables, we don't do anything.
61 ;; If a variable is mutated (updated by setq), and it is used in a closure
62 ;; we wrap it's definition with list: (list val) and we also replace
63 ;; var => (car var) wherever this variable is used, and also
64 ;; (setq var value) => (setcar var value) where it is updated.
66 ;; If defun argument is closure mutable, we letbind it and wrap it's
67 ;; definition with list.
68 ;; (defun foo (... mutable-arg ...) ...) =>
69 ;; (defun foo (... m-arg ...) (let ((m-arg (list m-arg))) ...))
74 ;; - Use abstract `make-closure' and `closure-ref' expressions, which bytecomp
75 ;; should turn into building corresponding byte-code function.
76 ;; - don't use `curry', instead build a new compiled-byte-code object
77 ;; (merge the closure env into the static constants pool).
78 ;; - use relative addresses for byte-code-stack-ref.
79 ;; - warn about unused lexical vars.
80 ;; - clean up cconv-closure-convert-rec, especially the `let' binding part.
82 (eval-when-compile (require 'cl
))
84 (defconst cconv-liftwhen
3
85 "Try to do lambda lifting if the number of arguments + free variables
86 is less than this number.")
87 (defvar cconv-mutated nil
88 "List of mutated variables in current form")
89 (defvar cconv-captured nil
90 "List of closure captured variables in current form")
91 (defvar cconv-captured
+mutated nil
92 "An intersection between cconv-mutated and cconv-captured lists.")
93 (defvar cconv-lambda-candidates nil
94 "List of candidates for lambda lifting.
95 Each candidate has the form (VAR INCLOSURE BINDER PARENTFORM).")
97 (defun cconv-freevars (form &optional fvrs
)
98 "Find all free variables of given form.
100 -- FORM is a piece of Elisp code after macroexpansion.
101 -- FVRS(optional) is a list of variables already found. Used for recursive tree
104 Returns a list of free variables."
105 ;; If a leaf in the tree is a symbol, but it is not a global variable, not a
106 ;; keyword, not 'nil or 't we consider this leaf as a variable.
107 ;; Free variables are the variables that are not declared above in this tree.
108 ;; For example free variables of (lambda (a1 a2 ..) body-forms) are
109 ;; free variables of body-forms excluding a1, a2 ..
110 ;; Free variables of (let ((v1 ..) (v2) ..)) body-forms) are
111 ;; free variables of body-forms excluding v1, v2 ...
114 ;; A list of free variables already found(FVRS) is passed in parameter
115 ;; to try to use cons or push where possible, and to minimize the usage
118 ;; This function can return duplicates (because we use 'append instead
119 ;; of union of two sets - for performance reasons).
121 (`(let ,varsvalues .
,body-forms
) ; let special form
123 (dolist (exp body-forms
)
124 (setq fvrs-1
(cconv-freevars exp fvrs-1
)))
125 (dolist (elm varsvalues
)
126 (setq fvrs-1
(delq (if (consp elm
) (car elm
) elm
) fvrs-1
)))
127 (setq fvrs
(nconc fvrs-1 fvrs
))
128 (dolist (exp varsvalues
)
129 (when (consp exp
) (setq fvrs
(cconv-freevars (cadr exp
) fvrs
))))
132 (`(let* ,varsvalues .
,body-forms
) ; let* special form
135 (dolist (exp varsvalues
)
138 (setq fvrs-1
(cconv-freevars (cadr exp
) fvrs-1
))
139 (dolist (elm vrs
) (setq fvrs-1
(delq elm fvrs-1
)))
140 (push (car exp
) vrs
))
142 (dolist (elm vrs
) (setq fvrs-1
(delq elm fvrs-1
)))
144 (dolist (exp body-forms
)
145 (setq fvrs-1
(cconv-freevars exp fvrs-1
)))
146 (dolist (elm vrs
) (setq fvrs-1
(delq elm fvrs-1
)))
147 (append fvrs fvrs-1
)))
149 (`((lambda .
,_
) .
,_
) ; first element is lambda expression
150 (dolist (exp `((function ,(car form
)) .
,(cdr form
)))
151 (setq fvrs
(cconv-freevars exp fvrs
))) fvrs
)
153 (`(cond .
,cond-forms
) ; cond special form
154 (dolist (exp1 cond-forms
)
156 (setq fvrs
(cconv-freevars exp2 fvrs
)))) fvrs
)
158 (`(quote .
,_
) fvrs
) ; quote form
160 (`(function .
((lambda ,vars .
,body-forms
)))
161 (let ((functionform (cadr form
)) (fvrs-1 '()))
162 (dolist (exp body-forms
)
163 (setq fvrs-1
(cconv-freevars exp fvrs-1
)))
164 (dolist (elm vars
) (setq fvrs-1
(delq elm fvrs-1
)))
165 (append fvrs fvrs-1
))) ; function form
167 (`(function .
,_
) fvrs
) ; same as quote
169 (`(condition-case ,var
,protected-form .
,conditions-bodies
)
171 (dolist (exp conditions-bodies
)
172 (setq fvrs-1
(cconv-freevars (cadr exp
) fvrs-1
)))
173 (setq fvrs-1
(delq var fvrs-1
))
174 (setq fvrs-1
(cconv-freevars protected-form fvrs-1
))
175 (append fvrs fvrs-1
)))
177 (`(,(and sym
(or `defun
`defconst
`defvar
)) .
,_
)
178 ;; We call cconv-freevars only for functions(lambdas)
179 ;; defun, defconst, defvar are not allowed to be inside
180 ;; a function (lambda).
181 ;; FIXME: should be a byte-compile-report-error!
182 (error "Invalid form: %s inside a function" sym
))
184 (`(,_ .
,body-forms
) ; First element is (like) a function.
185 (dolist (exp body-forms
)
186 (setq fvrs
(cconv-freevars exp fvrs
))) fvrs
)
188 (_ (if (byte-compile-not-lexical-var-p form
)
193 (defun cconv-closure-convert (form)
194 "Main entry point for closure conversion.
195 -- FORM is a piece of Elisp code after macroexpansion.
196 -- TOPLEVEL(optional) is a boolean variable, true if we are at the root of AST
198 Returns a form where all lambdas don't have any free variables."
199 ;; (message "Entering cconv-closure-convert...")
200 (let ((cconv-mutated '())
201 (cconv-lambda-candidates '())
203 (cconv-captured+mutated
'()))
204 ;; Analyse form - fill these variables with new information.
205 (cconv-analyse-form form
'() 0)
206 ;; Calculate an intersection of cconv-mutated and cconv-captured.
207 (dolist (mvr cconv-mutated
)
208 (when (memq mvr cconv-captured
) ;
209 (push mvr cconv-captured
+mutated
)))
210 (cconv-closure-convert-rec
213 '() ; fvrs initially empty
214 '() ; envs initially empty
218 (defun cconv-lookup-let (table var binder form
)
221 (when (and (eq (nth 2 elem
) binder
)
222 (eq (nth 3 elem
) form
))
223 (assert (eq (car elem
) var
))
227 (defconst cconv--dummy-var
(make-symbol "ignored"))
228 (defconst cconv--env-var
(make-symbol "env"))
230 (defun cconv--set-diff (s1 s2
)
231 "Return elements of set S1 that are not in set S2."
234 (unless (memq x s2
) (push x res
)))
237 (defun cconv--set-diff-map (s m
)
238 "Return elements of set S that are not in Dom(M)."
241 (unless (assq x m
) (push x res
)))
244 (defun cconv--map-diff (m1 m2
)
245 "Return the submap of map M1 that has Dom(M2) removed."
248 (unless (assq (car x
) m2
) (push x res
)))
251 (defun cconv--map-diff-elem (m x
)
252 "Return the map M minus any mapping for X."
253 ;; Here we assume that X appears at most once in M.
254 (let* ((b (assq x m
))
255 (res (if b
(remq b m
) m
)))
256 (assert (null (assq x res
))) ;; Check the assumption was warranted.
259 (defun cconv--map-diff-set (m s
)
260 "Return the map M minus any mapping for elements of S."
261 ;; Here we assume that X appears at most once in M.
264 (unless (memq (car b
) s
) (push b res
)))
267 (defun cconv-closure-convert-rec (form emvrs fvrs envs lmenvs
)
268 ;; This function actually rewrites the tree.
269 "Eliminates all free variables of all lambdas in given forms.
271 -- FORM is a piece of Elisp code after macroexpansion.
272 -- LMENVS is a list of environments used for lambda-lifting. Initially empty.
273 -- EMVRS is a list that contains mutated variables that are visible
274 within current environment.
275 -- ENVS is an environment(list of free variables) of current closure.
277 -- FVRS is a list of variables to substitute in each context.
280 Returns a form where all lambdas don't have any free variables."
281 ;; What's the difference between fvrs and envs?
282 ;; Suppose that we have the code
283 ;; (lambda (..) fvr (let ((fvr 1)) (+ fvr 1)))
284 ;; only the first occurrence of fvr should be replaced by
286 ;; So initially envs and fvrs are the same thing, but when we descend to
287 ;; the 'let, we delete fvr from fvrs. Why we don't delete fvr from envs?
288 ;; Because in envs the order of variables is important. We use this list
289 ;; to find the number of a specific variable in the environment vector,
290 ;; so we never touch it(unless we enter to the other closure).
291 ;;(if (listp form) (print (car form)) form)
293 (`(,(and letsym
(or `let
* `let
)) ,binders .
,body-forms
)
295 ; let and let* special forms
296 (let ((body-forms-new '())
298 ;; next for variables needed for delayed push
299 ;; because we should process <value(s)>
300 ;; before we change any arguments
301 (lmenvs-new '()) ;needed only in case of let
302 (emvrs-new '()) ;needed only in case of let
303 (emvr-push) ;needed only in case of let*
304 (lmenv-push)) ;needed only in case of let*
306 (dolist (binder binders
)
308 (var (if (not (consp binder
))
310 (setq value
(cadr binder
))
314 ;; Check if var is a candidate for lambda lifting.
315 ((cconv-lookup-let cconv-lambda-candidates var binder form
)
317 (let* ((fv (delete-dups (cconv-freevars value
'())))
318 (funargs (cadr (cadr value
)))
319 (funcvars (append fv funargs
))
320 (funcbodies (cddadr value
)) ; function bodies
321 (funcbodies-new '()))
322 ; lambda lifting condition
323 (if (or (not fv
) (< cconv-liftwhen
(length funcvars
)))
325 (cconv-closure-convert-rec
326 value emvrs fvrs envs lmenvs
)
329 (dolist (elm2 funcbodies
)
330 (push ; convert function bodies
331 (cconv-closure-convert-rec
332 elm2 emvrs nil envs lmenvs
)
334 (if (eq letsym
'let
*)
335 (setq lmenv-push
(cons var fv
))
336 (push (cons var fv
) lmenvs-new
))
337 ; push lifted function
341 ,(reverse funcbodies-new
))))))))
343 ;; Check if it needs to be turned into a "ref-cell".
344 ((cconv-lookup-let cconv-captured
+mutated var binder form
)
345 ;; Declared variable is mutated and captured.
347 `(list ,(cconv-closure-convert-rec
350 (if (eq letsym
'let
*)
352 (push var emvrs-new
))))
354 ;; Normal default case.
356 (cconv-closure-convert-rec
357 value emvrs fvrs envs lmenvs
)))))
359 ;; this piece of code below letbinds free
360 ;; variables of a lambda lifted function
361 ;; if they are redefined in this let
363 ;; (let* ((fun (lambda (x) (+ x y))) (y 1)) (funcall fun 1))
364 ;; Here we can not pass y as parameter because it is
365 ;; redefined. We add a (closed-y y) declaration.
366 ;; We do that even if the function is not used inside
367 ;; this let(*). The reason why we ignore this case is
368 ;; that we can't "look forward" to see if the function
369 ;; is called there or not. To treat well this case we
370 ;; need to traverse the tree one more time to collect this
371 ;; data, and I think that it's not worth it.
373 (when (eq letsym
'let
*)
374 (let ((closedsym '())
377 (dolist (lmenv lmenvs
)
378 (when (memq var
(cdr lmenv
))
381 (concat "closed-" (symbol-name var
))))
382 (setq new-lmenv
(list (car lmenv
)))
383 (dolist (frv (cdr lmenv
)) (if (eq frv var
)
384 (push closedsym new-lmenv
)
385 (push frv new-lmenv
)))
386 (setq new-lmenv
(reverse new-lmenv
))
387 (setq old-lmenv lmenv
)))
389 (setq lmenvs
(remq old-lmenv lmenvs
))
390 (push new-lmenv lmenvs
)
391 (push `(,closedsym
,var
) binders-new
))))
392 ;; We push the element after redefined free variables are
393 ;; processed. This is important to avoid the bug when free
394 ;; variable and the function have the same name.
395 (push (list var new-val
) binders-new
)
397 (when (eq letsym
'let
*) ; update fvrs
398 (setq fvrs
(remq var fvrs
))
399 (setq emvrs
(remq var emvrs
)) ; remove if redefined
401 (push emvr-push emvrs
)
402 (setq emvr-push nil
))
403 (setq lmenvs
(cconv--map-diff-elem lmenvs var
))
405 (push lmenv-push lmenvs
)
406 (setq lmenv-push nil
)))
407 )) ; end of dolist over binders
408 (when (eq letsym
'let
)
410 (let (var fvrs-1 emvrs-1 lmenvs-1
)
411 ;; Here we update emvrs, fvrs and lmenvs lists
412 (setq fvrs
(cconv--set-diff-map fvrs binders-new
))
413 (setq emvrs
(cconv--set-diff-map emvrs binders-new
))
414 (setq emvrs
(append emvrs emvrs-new
))
415 (setq lmenvs
(cconv--set-diff-map lmenvs binders-new
))
416 (setq lmenvs
(append lmenvs lmenvs-new
)))
418 ;; Here we do the same letbinding as for let* above
419 ;; to avoid situation when a free variable of a lambda lifted
420 ;; function got redefined.
426 (dolist (binder binders
)
427 (setq var
(if (consp binder
) (car binder
) binder
))
429 (let ((lmenvs-1 lmenvs
)) ; just to avoid manipulating
430 (dolist (lmenv lmenvs-1
) ; the counter inside the loop
431 (when (memq var
(cdr lmenv
))
432 (setq closedsym
(make-symbol
436 (setq new-lmenv
(list (car lmenv
)))
437 (dolist (frv (cdr lmenv
))
438 (push (if (eq frv var
) closedsym frv
)
440 (setq new-lmenv
(reverse new-lmenv
))
441 (setq lmenvs
(remq lmenv lmenvs
))
442 (push new-lmenv lmenvs
)
443 (push `(,closedsym
,var
) letbinds
)
445 (setq binders-new
(append binders-new letbinds
))))
447 (dolist (elm body-forms
) ; convert body forms
448 (push (cconv-closure-convert-rec
449 elm emvrs fvrs envs lmenvs
)
451 `(,letsym
,(reverse binders-new
) .
,(reverse body-forms-new
))))
452 ;end of let let* forms
454 ; first element is lambda expression
455 (`(,(and `(lambda .
,_
) fun
) .
,other-body-forms
)
457 (let ((other-body-forms-new '()))
458 (dolist (elm other-body-forms
)
459 (push (cconv-closure-convert-rec
460 elm emvrs fvrs envs lmenvs
)
461 other-body-forms-new
))
463 ,(cconv-closure-convert-rec
464 (list 'function fun
) emvrs fvrs envs lmenvs
)
465 ,@(nreverse other-body-forms-new
))))
467 (`(cond .
,cond-forms
) ; cond special form
468 (let ((cond-forms-new '()))
469 (dolist (elm cond-forms
)
470 (push (let ((elm-new '()))
473 (cconv-closure-convert-rec
474 elm-2 emvrs fvrs envs lmenvs
)
479 (reverse cond-forms-new
))))
481 (`(quote .
,_
) form
) ; quote form
483 (`(function .
((lambda ,vars .
,body-forms
))) ; function form
484 (let* ((fvrs-new (cconv--set-diff fvrs vars
)) ; Remove vars from fvrs.
485 (fv (delete-dups (cconv-freevars form
'())))
486 (leave fvrs-new
) ; leave=non-nil if we should leave env unchanged.
492 ;; Here we form our environment vector.
493 ;; If outer closure contains all
494 ;; free variables of this function(and nothing else)
495 ;; then we use the same environment vector as for outer closure,
496 ;; i.e. we leave the environment vector unchanged
497 ;; otherwise we build a new environmet vector
498 (if (eq (length envs
) (length fv
))
500 (while (and fv-temp leave
)
501 (when (not (memq (car fv-temp
) fvrs-new
)) (setq leave nil
))
502 (setq fv-temp
(cdr fv-temp
))))
509 (cconv-closure-convert-rec
510 ;; Remove `elm' from `emvrs' for this call because in case
511 ;; `elm' is a variable that's wrapped in a cons-cell, we
512 ;; want to put the cons-cell itself in the closure, rather
513 ;; than just a copy of its current content.
514 elm
(remq elm emvrs
) fvrs envs lmenvs
)
515 envector
)) ; Process vars for closure vector.
516 (setq envector
(reverse envector
))
518 (setq envector
`(,cconv--env-var
))) ; Leave unchanged.
519 (setq fvrs-new fv
)) ; Update substitution list.
521 (setq emvrs
(cconv--set-diff emvrs vars
))
522 (setq lmenvs
(cconv--map-diff-set lmenvs vars
))
524 ;; The difference between envs and fvrs is explained
525 ;; in comment in the beginning of the function.
526 (dolist (elm cconv-captured
+mutated
) ; Find mutated arguments
527 (setq mv
(car elm
)) ; used in inner closures.
528 (when (and (memq mv vars
) (eq form
(caddr elm
)))
529 (progn (push mv emvrs
)
530 (push `(,mv
(list ,mv
)) letbind
))))
531 (dolist (elm body-forms
) ; convert function body
532 (push (cconv-closure-convert-rec
533 elm emvrs fvrs-new envs lmenvs
)
537 (if letbind
`((let ,letbind .
,(reverse body-forms-new
)))
538 (reverse body-forms-new
)))
541 ;if no freevars - do nothing
543 `(function (lambda ,vars .
,body-forms-new
)))
544 ; 1 free variable - do not build vector
545 ((null (cdr envector
))
547 (function (lambda (,cconv--env-var .
,vars
) .
,body-forms-new
))
549 ; >=2 free variables - build vector
552 (function (lambda (,cconv--env-var .
,vars
) .
,body-forms-new
))
553 (vector .
,envector
))))))
555 (`(function .
,_
) form
) ; same as quote
558 (`(,(and sym
(or `defconst
`defvar
)) ,definedsymbol .
,body-forms
)
560 (let ((body-forms-new '()))
561 (dolist (elm body-forms
)
562 (push (cconv-closure-convert-rec
563 elm emvrs fvrs envs lmenvs
)
565 (setq body-forms-new
(reverse body-forms-new
))
566 `(,sym
,definedsymbol .
,body-forms-new
)))
569 (`(,(and sym
(or `defun
`defmacro
))
570 ,func
,vars .
,body-forms
)
571 (let ((body-new '()) ; the whole body
572 (body-forms-new '()) ; body w\o docstring and interactive
574 ; find mutable arguments
575 (let ((lmutated cconv-captured
+mutated
) ismutated
)
578 (while (and lmutated
(not ismutated
))
579 (when (and (eq (caar lmutated
) elm
)
580 (eq (cadar lmutated
) form
))
582 (setq lmutated
(cdr lmutated
)))
586 ;transform body-forms
587 (when (stringp (car body-forms
)) ; treat docstring well
588 (push (car body-forms
) body-new
)
589 (setq body-forms
(cdr body-forms
)))
590 (when (eq (car-safe (car body-forms
)) 'interactive
)
591 (push (cconv-closure-convert-rec
593 emvrs fvrs envs lmenvs
)
595 (setq body-forms
(cdr body-forms
)))
597 (dolist (elm body-forms
)
598 (push (cconv-closure-convert-rec
599 elm emvrs fvrs envs lmenvs
)
601 (setq body-forms-new
(reverse body-forms-new
))
604 ; letbind mutable arguments
605 (let ((binders-new '()))
606 (dolist (elm letbind
) (push `(,elm
(list ,elm
))
608 (push `(let ,(reverse binders-new
) .
609 ,body-forms-new
) body-new
)
610 (setq body-new
(reverse body-new
)))
611 (setq body-new
(append (reverse body-new
) body-forms-new
)))
613 `(,sym
,func
,vars .
,body-new
)))
616 (`(condition-case ,var
,protected-form .
,handlers
)
617 (let ((handlers-new '())
618 (newform (cconv-closure-convert-rec
619 `(function (lambda () ,protected-form
))
620 emvrs fvrs envs lmenvs
)))
621 (setq fvrs
(remq var fvrs
))
622 (dolist (handler handlers
)
623 (push (list (car handler
)
624 (cconv-closure-convert-rec
625 `(function (lambda (,(or var cconv--dummy-var
))
627 emvrs fvrs envs lmenvs
))
629 `(condition-case :fun-body
,newform
630 ,@(nreverse handlers-new
))))
632 (`(,(and head
(or `catch
`unwind-protect
)) ,form .
,body
)
633 `(,head
,(cconv-closure-convert-rec form emvrs fvrs envs lmenvs
)
635 ,(cconv-closure-convert-rec `(function (lambda () ,@body
))
636 emvrs fvrs envs lmenvs
)))
638 (`(track-mouse .
,body
)
641 ,(cconv-closure-convert-rec `(function (lambda () ,@body
))
642 emvrs fvrs envs lmenvs
)))
644 (`(setq .
,forms
) ; setq special form
645 (let (prognlist sym sym-new value
)
647 (setq sym
(car forms
))
648 (setq sym-new
(cconv-closure-convert-rec
650 (remq sym emvrs
) fvrs envs lmenvs
))
652 (cconv-closure-convert-rec
653 (cadr forms
) emvrs fvrs envs lmenvs
))
655 (push `(setcar ,sym-new
,value
) prognlist
)
656 (if (symbolp sym-new
)
657 (push `(setq ,sym-new
,value
) prognlist
)
658 (push `(set ,sym-new
,value
) prognlist
)))
659 (setq forms
(cddr forms
)))
661 `(progn .
,(reverse prognlist
))
664 (`(,(and (or `funcall
`apply
) callsym
) ,fun .
,args
)
665 ; funcall is not a special form
666 ; but we treat it separately
667 ; for the needs of lambda lifting
668 (let ((fv (cdr (assq fun lmenvs
))))
672 ;; All args (free variables and actual arguments)
673 ;; should be processed, because they can be fvrs
674 ;; (free variables of another closure)
676 (push (cconv-closure-convert-rec
680 (setq processed-fv
(reverse processed-fv
))
682 (push (cconv-closure-convert-rec
683 elm emvrs fvrs envs lmenvs
)
685 (setq args-new
(append processed-fv
(reverse args-new
)))
686 (setq fun
(cconv-closure-convert-rec
687 fun emvrs fvrs envs lmenvs
))
688 `(,callsym
,fun .
,args-new
))
690 (dolist (elm (cdr form
))
691 (push (cconv-closure-convert-rec
692 elm emvrs fvrs envs lmenvs
)
694 `(,callsym .
,(reverse cdr-new
))))))
696 (`(,func .
,body-forms
) ; first element is function or whatever
697 ; function-like forms are:
698 ; or, and, if, progn, prog1, prog2,
700 (let ((body-forms-new '()))
701 (dolist (elm body-forms
)
702 (push (cconv-closure-convert-rec
703 elm emvrs fvrs envs lmenvs
)
705 (setq body-forms-new
(reverse body-forms-new
))
706 `(,func .
,body-forms-new
)))
709 (let ((free (memq form fvrs
)))
710 (if free
;form is a free variable
711 (let* ((numero (- (length fvrs
) (length free
)))
712 (var (if (null (cdr envs
))
714 ;; Replace form => (aref env #)
715 `(aref ,cconv--env-var
,numero
))))
716 (if (memq form emvrs
) ; form => (car (aref env #)) if mutable
719 (if (memq form emvrs
) ; if form is a mutable variable
720 `(car ,form
) ; replace form => (car form)
723 (defun cconv-analyse-function (args body env parentform inclosure
)
726 ((byte-compile-not-lexical-var-p arg
)
727 (byte-compile-report-error
728 (format "Argument %S is not a lexical variable" arg
)))
729 ((eq ?
& (aref (symbol-name arg
) 0)) nil
) ;Ignore &rest, &optional, ...
730 (t (push (list arg inclosure parentform
) env
)))) ;Push vrs to vars.
731 (dolist (form body
) ;Analyse body forms.
732 (cconv-analyse-form form env inclosure
)))
734 (defun cconv-analyse-form (form env inclosure
)
735 "Find mutated variables and variables captured by closure. Analyse
736 lambdas if they are suitable for lambda lifting.
737 -- FORM is a piece of Elisp code after macroexpansion.
738 -- ENV is a list of variables visible in current lexical environment.
739 Each entry has the form (VAR INCLOSURE BINDER PARENTFORM)
740 for let-bound vars and (VAR INCLOSURE PARENTFORM) for function arguments.
741 -- INCLOSURE is the nesting level within lambdas."
744 (`(,(and (or `let
* `let
) letsym
) ,binders .
,body-forms
)
749 (dolist (binder binders
)
750 (if (not (consp binder
))
752 (setq var binder
) ; treat the form (let (x) ...) well
754 (setq var
(car binder
))
755 (setq value
(cadr binder
))
757 (cconv-analyse-form value
(if (eq letsym
'let
*) env orig-env
)
760 (unless (byte-compile-not-lexical-var-p var
)
761 (let ((varstruct (list var inclosure binder form
)))
762 (push varstruct env
) ; Push a new one.
765 (`(function (lambda .
,_
))
766 ;; If var is a function push it to lambda list.
767 (push varstruct cconv-lambda-candidates
)))))))
769 (dolist (form body-forms
) ; Analyse body forms.
770 (cconv-analyse-form form env inclosure
)))
773 (`(,(or `defun
`defmacro
) ,func
,vrs .
,body-forms
)
775 (byte-compile-log-warning
776 (format "Function %S will ignore its context %S"
777 func
(mapcar #'car env
))
779 (cconv-analyse-function vrs body-forms nil form
0))
781 (`(function (lambda ,vrs .
,body-forms
))
782 (cconv-analyse-function vrs body-forms env form
(1+ inclosure
)))
785 ;; If a local variable (member of env) is modified by setq then
786 ;; it is a mutated variable.
788 (let ((v (assq (car forms
) env
))) ; v = non nil if visible
790 (push v cconv-mutated
)
791 ;; Delete from candidate list for lambda lifting.
792 (setq cconv-lambda-candidates
(delq v cconv-lambda-candidates
))
793 (unless (eq inclosure
(cadr v
)) ;Bound in a different closure level.
794 (push v cconv-captured
))))
795 (cconv-analyse-form (cadr forms
) env inclosure
)
796 (setq forms
(cddr forms
))))
798 (`((lambda .
,_
) .
,_
) ; first element is lambda expression
799 (dolist (exp `((function ,(car form
)) .
,(cdr form
)))
800 (cconv-analyse-form exp env inclosure
)))
802 (`(cond .
,cond-forms
) ; cond special form
803 (dolist (forms cond-forms
)
805 (cconv-analyse-form form env inclosure
))))
807 (`(quote .
,_
) nil
) ; quote form
808 (`(function .
,_
) nil
) ; same as quote
810 (`(condition-case ,var
,protected-form .
,handlers
)
811 ;; FIXME: The bytecode for condition-case forces us to wrap the
812 ;; form and handlers in closures (for handlers, it's probably
813 ;; unavoidable, but not for the protected form).
814 (setq inclosure
(1+ inclosure
))
815 (cconv-analyse-form protected-form env inclosure
)
816 (push (list var inclosure form
) env
)
817 (dolist (handler handlers
)
818 (dolist (form (cdr handler
))
819 (cconv-analyse-form form env inclosure
))))
821 ;; FIXME: The bytecode for catch forces us to wrap the body.
822 (`(,(or `catch
`unwind-protect
) ,form .
,body
)
823 (cconv-analyse-form form env inclosure
)
824 (setq inclosure
(1+ inclosure
))
826 (cconv-analyse-form form env inclosure
)))
828 ;; FIXME: The bytecode for save-window-excursion and the lack of
829 ;; bytecode for track-mouse forces us to wrap the body.
830 (`(track-mouse .
,body
)
831 (setq inclosure
(1+ inclosure
))
833 (cconv-analyse-form form env inclosure
)))
835 (`(,(or `defconst
`defvar
) ,var
,value .
,_
)
836 (push var byte-compile-bound-variables
)
837 (cconv-analyse-form value env inclosure
))
839 (`(,(or `funcall
`apply
) ,fun .
,args
)
840 ;; Here we ignore fun because funcall and apply are the only two
841 ;; functions where we can pass a candidate for lambda lifting as
842 ;; argument. So, if we see fun elsewhere, we'll delete it from
843 ;; lambda candidate list.
845 (let ((lv (assq fun cconv-lambda-candidates
)))
847 (unless (eq (cadr lv
) inclosure
)
848 (push lv cconv-captured
)
849 ;; If this funcall and the definition of fun are in
850 ;; different closures - we delete fun from candidate
851 ;; list, because it is too complicated to manage free
852 ;; variables in this case.
853 (setq cconv-lambda-candidates
854 (delq lv cconv-lambda-candidates
)))))
855 (cconv-analyse-form fun env inclosure
))
857 (cconv-analyse-form form env inclosure
)))
859 (`(,_ .
,body-forms
) ; First element is a function or whatever.
860 (dolist (form body-forms
)
861 (cconv-analyse-form form env inclosure
)))
864 (let ((dv (assq form env
))) ; dv = declared and visible
866 (unless (eq inclosure
(cadr dv
)) ; capturing condition
867 (push dv cconv-captured
))
868 ;; Delete lambda if it is found here, since it escapes.
869 (setq cconv-lambda-candidates
870 (delq dv cconv-lambda-candidates
)))))))
873 ;;; cconv.el ends here