* lisp/emacs-lisp/cconv.el (cconv-closure-convert-rec): Let the byte
[bpt/emacs.git] / lisp / emacs-lisp / cconv.el
CommitLineData
3e21b6a7 1;;; cconv.el --- Closure conversion for statically scoped Emacs lisp. -*- lexical-binding: t -*-
94d11cb5 2
d779e73c
SM
3;; Copyright (C) 2011 Free Software Foundation, Inc.
4
5;; Author: Igor Kuzmin <kzuminig@iro.umontreal.ca>
6;; Maintainer: FSF
7;; Keywords: lisp
8;; Package: emacs
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
94d11cb5
IK
24
25;;; Commentary:
26
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).
d779e73c
SM
30;; All macros should be expanded beforehand.
31;;
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
94d11cb5
IK
35;; for lambda lifting and all variables captured by closure. It passes the tree
36;; once, returning a list of three lists.
d779e73c
SM
37;;
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
40;; closure.
94d11cb5 41
d779e73c
SM
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
94d11cb5
IK
44;; is needed and eliminating mutable variables used in closure.
45;;
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)
49;;
d779e73c 50;; (lambda (v1 ...) ... fv ...) =>
94d11cb5
IK
51;; (curry (lambda (env v1 ...) ... env ...) env)
52;; if the function has only 1 free variable
53;;
d779e73c
SM
54;; and finally
55;; (lambda (v1 ...) ... fv1 fv2 ...) =>
94d11cb5 56;; (curry (lambda (env v1 ..) .. (aref env 0) (aref env 1) ..) (vector fv1 fv2))
d779e73c 57;; if the function has 2 or more free variables.
94d11cb5
IK
58;;
59;; If the function has no free variables, we don't do anything.
94d11cb5 60;;
d779e73c
SM
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.
94d11cb5 65;;
d779e73c
SM
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))) ...))
94d11cb5
IK
70;;
71;;; Code:
72
b38b1ec0 73;;; TODO:
cb9336bd
SM
74;; - canonize code in macro-expand so we don't have to handle (let (var) body)
75;; and other oddities.
3e21b6a7
SM
76;; - Change new byte-code representation, so it directly gives the
77;; number of mandatory and optional arguments as well as whether or
78;; not there's a &rest arg.
b38b1ec0
SM
79;; - Use abstract `make-closure' and `closure-ref' expressions, which bytecomp
80;; should turn into building corresponding byte-code function.
81;; - don't use `curry', instead build a new compiled-byte-code object
82;; (merge the closure env into the static constants pool).
b38b1ec0
SM
83;; - warn about unused lexical vars.
84;; - clean up cconv-closure-convert-rec, especially the `let' binding part.
3e21b6a7
SM
85;; - new byte codes for unwind-protect, catch, and condition-case so that
86;; closures aren't needed at all.
b38b1ec0 87
94d11cb5
IK
88(eval-when-compile (require 'cl))
89
90(defconst cconv-liftwhen 3
d779e73c 91 "Try to do lambda lifting if the number of arguments + free variables
94d11cb5 92is less than this number.")
d779e73c 93(defvar cconv-mutated nil
94d11cb5 94 "List of mutated variables in current form")
d779e73c 95(defvar cconv-captured nil
94d11cb5 96 "List of closure captured variables in current form")
d779e73c 97(defvar cconv-captured+mutated nil
94d11cb5 98 "An intersection between cconv-mutated and cconv-captured lists.")
d779e73c 99(defvar cconv-lambda-candidates nil
43e67019
SM
100 "List of candidates for lambda lifting.
101Each candidate has the form (VAR INCLOSURE BINDER PARENTFORM).")
102
94d11cb5
IK
103(defun cconv-freevars (form &optional fvrs)
104 "Find all free variables of given form.
105Arguments:
106-- FORM is a piece of Elisp code after macroexpansion.
107-- FVRS(optional) is a list of variables already found. Used for recursive tree
108traversal
109
110Returns a list of free variables."
111 ;; If a leaf in the tree is a symbol, but it is not a global variable, not a
112 ;; keyword, not 'nil or 't we consider this leaf as a variable.
113 ;; Free variables are the variables that are not declared above in this tree.
d779e73c 114 ;; For example free variables of (lambda (a1 a2 ..) body-forms) are
94d11cb5 115 ;; free variables of body-forms excluding a1, a2 ..
d779e73c 116 ;; Free variables of (let ((v1 ..) (v2) ..)) body-forms) are
94d11cb5 117 ;; free variables of body-forms excluding v1, v2 ...
d779e73c 118 ;; and so on.
94d11cb5 119
d779e73c 120 ;; A list of free variables already found(FVRS) is passed in parameter
94d11cb5 121 ;; to try to use cons or push where possible, and to minimize the usage
d779e73c 122 ;; of append.
94d11cb5 123
d779e73c 124 ;; This function can return duplicates (because we use 'append instead
94d11cb5
IK
125 ;; of union of two sets - for performance reasons).
126 (pcase form
d779e73c
SM
127 (`(let ,varsvalues . ,body-forms) ; let special form
128 (let ((fvrs-1 '()))
129 (dolist (exp body-forms)
130 (setq fvrs-1 (cconv-freevars exp fvrs-1)))
131 (dolist (elm varsvalues)
132 (setq fvrs-1 (delq (if (consp elm) (car elm) elm) fvrs-1)))
133 (setq fvrs (nconc fvrs-1 fvrs))
134 (dolist (exp varsvalues)
135 (when (consp exp) (setq fvrs (cconv-freevars (cadr exp) fvrs))))
136 fvrs))
137
138 (`(let* ,varsvalues . ,body-forms) ; let* special form
139 (let ((vrs '())
140 (fvrs-1 '()))
141 (dolist (exp varsvalues)
142 (if (consp exp)
143 (progn
144 (setq fvrs-1 (cconv-freevars (cadr exp) fvrs-1))
145 (dolist (elm vrs) (setq fvrs-1 (delq elm fvrs-1)))
146 (push (car exp) vrs))
147 (progn
148 (dolist (elm vrs) (setq fvrs-1 (delq elm fvrs-1)))
149 (push exp vrs))))
150 (dolist (exp body-forms)
151 (setq fvrs-1 (cconv-freevars exp fvrs-1)))
152 (dolist (elm vrs) (setq fvrs-1 (delq elm fvrs-1)))
153 (append fvrs fvrs-1)))
154
155 (`((lambda . ,_) . ,_) ; first element is lambda expression
156 (dolist (exp `((function ,(car form)) . ,(cdr form)))
157 (setq fvrs (cconv-freevars exp fvrs))) fvrs)
158
159 (`(cond . ,cond-forms) ; cond special form
160 (dolist (exp1 cond-forms)
161 (dolist (exp2 exp1)
162 (setq fvrs (cconv-freevars exp2 fvrs)))) fvrs)
163
164 (`(quote . ,_) fvrs) ; quote form
165
166 (`(function . ((lambda ,vars . ,body-forms)))
167 (let ((functionform (cadr form)) (fvrs-1 '()))
168 (dolist (exp body-forms)
169 (setq fvrs-1 (cconv-freevars exp fvrs-1)))
170 (dolist (elm vars) (setq fvrs-1 (delq elm fvrs-1)))
171 (append fvrs fvrs-1))) ; function form
172
173 (`(function . ,_) fvrs) ; same as quote
94d11cb5 174 ;condition-case
d779e73c
SM
175 (`(condition-case ,var ,protected-form . ,conditions-bodies)
176 (let ((fvrs-1 '()))
177 (dolist (exp conditions-bodies)
178 (setq fvrs-1 (cconv-freevars (cadr exp) fvrs-1)))
179 (setq fvrs-1 (delq var fvrs-1))
180 (setq fvrs-1 (cconv-freevars protected-form fvrs-1))
181 (append fvrs fvrs-1)))
182
183 (`(,(and sym (or `defun `defconst `defvar)) . ,_)
43e67019 184 ;; We call cconv-freevars only for functions(lambdas)
d779e73c 185 ;; defun, defconst, defvar are not allowed to be inside
43e67019
SM
186 ;; a function (lambda).
187 ;; FIXME: should be a byte-compile-report-error!
d779e73c
SM
188 (error "Invalid form: %s inside a function" sym))
189
43e67019 190 (`(,_ . ,body-forms) ; First element is (like) a function.
d779e73c
SM
191 (dolist (exp body-forms)
192 (setq fvrs (cconv-freevars exp fvrs))) fvrs)
193
ce5b520a 194 (_ (if (byte-compile-not-lexical-var-p form)
d779e73c
SM
195 fvrs
196 (cons form fvrs)))))
94d11cb5
IK
197
198;;;###autoload
295fb2ac
SM
199(defun cconv-closure-convert (form)
200 "Main entry point for closure conversion.
94d11cb5
IK
201-- FORM is a piece of Elisp code after macroexpansion.
202-- TOPLEVEL(optional) is a boolean variable, true if we are at the root of AST
203
204Returns a form where all lambdas don't have any free variables."
b38b1ec0 205 ;; (message "Entering cconv-closure-convert...")
94d11cb5
IK
206 (let ((cconv-mutated '())
207 (cconv-lambda-candidates '())
208 (cconv-captured '())
d779e73c 209 (cconv-captured+mutated '()))
b38b1ec0 210 ;; Analyse form - fill these variables with new information.
43e67019 211 (cconv-analyse-form form '() 0)
b38b1ec0 212 ;; Calculate an intersection of cconv-mutated and cconv-captured.
d779e73c
SM
213 (dolist (mvr cconv-mutated)
214 (when (memq mvr cconv-captured) ;
215 (push mvr cconv-captured+mutated)))
216 (cconv-closure-convert-rec
217 form ; the tree
218 '() ;
219 '() ; fvrs initially empty
220 '() ; envs initially empty
94d11cb5 221 '()
295fb2ac 222 )))
94d11cb5 223
3e21b6a7 224(defun cconv--lookup-let (table var binder form)
295fb2ac
SM
225 (let ((res nil))
226 (dolist (elem table)
227 (when (and (eq (nth 2 elem) binder)
228 (eq (nth 3 elem) form))
229 (assert (eq (car elem) var))
230 (setq res elem)))
231 res))
94d11cb5 232
295fb2ac 233(defconst cconv--dummy-var (make-symbol "ignored"))
b38b1ec0
SM
234
235(defun cconv--set-diff (s1 s2)
236 "Return elements of set S1 that are not in set S2."
237 (let ((res '()))
238 (dolist (x s1)
239 (unless (memq x s2) (push x res)))
240 (nreverse res)))
241
242(defun cconv--set-diff-map (s m)
243 "Return elements of set S that are not in Dom(M)."
244 (let ((res '()))
245 (dolist (x s)
246 (unless (assq x m) (push x res)))
247 (nreverse res)))
248
249(defun cconv--map-diff (m1 m2)
250 "Return the submap of map M1 that has Dom(M2) removed."
251 (let ((res '()))
252 (dolist (x m1)
253 (unless (assq (car x) m2) (push x res)))
254 (nreverse res)))
255
256(defun cconv--map-diff-elem (m x)
257 "Return the map M minus any mapping for X."
258 ;; Here we assume that X appears at most once in M.
259 (let* ((b (assq x m))
260 (res (if b (remq b m) m)))
261 (assert (null (assq x res))) ;; Check the assumption was warranted.
262 res))
94d11cb5 263
b38b1ec0
SM
264(defun cconv--map-diff-set (m s)
265 "Return the map M minus any mapping for elements of S."
266 ;; Here we assume that X appears at most once in M.
267 (let ((res '()))
268 (dolist (b m)
269 (unless (memq (car b) s) (push b res)))
270 (nreverse res)))
271
272(defun cconv-closure-convert-rec (form emvrs fvrs envs lmenvs)
d779e73c 273 ;; This function actually rewrites the tree.
94d11cb5
IK
274 "Eliminates all free variables of all lambdas in given forms.
275Arguments:
276-- FORM is a piece of Elisp code after macroexpansion.
b38b1ec0 277-- LMENVS is a list of environments used for lambda-lifting. Initially empty.
94d11cb5
IK
278-- EMVRS is a list that contains mutated variables that are visible
279within current environment.
d779e73c
SM
280-- ENVS is an environment(list of free variables) of current closure.
281Initially empty.
282-- FVRS is a list of variables to substitute in each context.
283Initially empty.
94d11cb5
IK
284
285Returns a form where all lambdas don't have any free variables."
d779e73c 286 ;; What's the difference between fvrs and envs?
94d11cb5
IK
287 ;; Suppose that we have the code
288 ;; (lambda (..) fvr (let ((fvr 1)) (+ fvr 1)))
d779e73c
SM
289 ;; only the first occurrence of fvr should be replaced by
290 ;; (aref env ...).
94d11cb5
IK
291 ;; So initially envs and fvrs are the same thing, but when we descend to
292 ;; the 'let, we delete fvr from fvrs. Why we don't delete fvr from envs?
293 ;; Because in envs the order of variables is important. We use this list
d779e73c
SM
294 ;; to find the number of a specific variable in the environment vector,
295 ;; so we never touch it(unless we enter to the other closure).
296 ;;(if (listp form) (print (car form)) form)
297 (pcase form
295fb2ac 298 (`(,(and letsym (or `let* `let)) ,binders . ,body-forms)
94d11cb5
IK
299
300 ; let and let* special forms
d779e73c 301 (let ((body-forms-new '())
295fb2ac 302 (binders-new '())
d779e73c
SM
303 ;; next for variables needed for delayed push
304 ;; because we should process <value(s)>
305 ;; before we change any arguments
306 (lmenvs-new '()) ;needed only in case of let
307 (emvrs-new '()) ;needed only in case of let
308 (emvr-push) ;needed only in case of let*
309 (lmenv-push)) ;needed only in case of let*
310
295fb2ac
SM
311 (dolist (binder binders)
312 (let* ((value nil)
313 (var (if (not (consp binder))
314 binder
315 (setq value (cadr binder))
316 (car binder)))
317 (new-val
318 (cond
319 ;; Check if var is a candidate for lambda lifting.
3e21b6a7 320 ((cconv--lookup-let cconv-lambda-candidates var binder form)
295fb2ac
SM
321
322 (let* ((fv (delete-dups (cconv-freevars value '())))
323 (funargs (cadr (cadr value)))
324 (funcvars (append fv funargs))
325 (funcbodies (cddadr value)) ; function bodies
326 (funcbodies-new '()))
94d11cb5 327 ; lambda lifting condition
295fb2ac 328 (if (or (not fv) (< cconv-liftwhen (length funcvars)))
94d11cb5 329 ; do not lift
295fb2ac
SM
330 (cconv-closure-convert-rec
331 value emvrs fvrs envs lmenvs)
94d11cb5 332 ; lift
295fb2ac
SM
333 (progn
334 (dolist (elm2 funcbodies)
335 (push ; convert function bodies
336 (cconv-closure-convert-rec
337 elm2 emvrs nil envs lmenvs)
338 funcbodies-new))
339 (if (eq letsym 'let*)
340 (setq lmenv-push (cons var fv))
341 (push (cons var fv) lmenvs-new))
94d11cb5
IK
342 ; push lifted function
343
295fb2ac
SM
344 `(function .
345 ((lambda ,funcvars .
346 ,(reverse funcbodies-new))))))))
347
348 ;; Check if it needs to be turned into a "ref-cell".
3e21b6a7 349 ((cconv--lookup-let cconv-captured+mutated var binder form)
295fb2ac
SM
350 ;; Declared variable is mutated and captured.
351 (prog1
352 `(list ,(cconv-closure-convert-rec
353 value emvrs
354 fvrs envs lmenvs))
d779e73c
SM
355 (if (eq letsym 'let*)
356 (setq emvr-push var)
295fb2ac
SM
357 (push var emvrs-new))))
358
359 ;; Normal default case.
360 (t
361 (cconv-closure-convert-rec
362 value emvrs fvrs envs lmenvs)))))
d779e73c
SM
363
364 ;; this piece of code below letbinds free
365 ;; variables of a lambda lifted function
366 ;; if they are redefined in this let
367 ;; example:
368 ;; (let* ((fun (lambda (x) (+ x y))) (y 1)) (funcall fun 1))
369 ;; Here we can not pass y as parameter because it is
370 ;; redefined. We add a (closed-y y) declaration.
371 ;; We do that even if the function is not used inside
372 ;; this let(*). The reason why we ignore this case is
373 ;; that we can't "look forward" to see if the function
374 ;; is called there or not. To treat well this case we
375 ;; need to traverse the tree one more time to collect this
376 ;; data, and I think that it's not worth it.
377
378 (when (eq letsym 'let*)
379 (let ((closedsym '())
380 (new-lmenv '())
381 (old-lmenv '()))
382 (dolist (lmenv lmenvs)
383 (when (memq var (cdr lmenv))
384 (setq closedsym
385 (make-symbol
386 (concat "closed-" (symbol-name var))))
387 (setq new-lmenv (list (car lmenv)))
388 (dolist (frv (cdr lmenv)) (if (eq frv var)
389 (push closedsym new-lmenv)
390 (push frv new-lmenv)))
391 (setq new-lmenv (reverse new-lmenv))
392 (setq old-lmenv lmenv)))
393 (when new-lmenv
394 (setq lmenvs (remq old-lmenv lmenvs))
395 (push new-lmenv lmenvs)
295fb2ac 396 (push `(,closedsym ,var) binders-new))))
b38b1ec0
SM
397 ;; We push the element after redefined free variables are
398 ;; processed. This is important to avoid the bug when free
399 ;; variable and the function have the same name.
295fb2ac 400 (push (list var new-val) binders-new)
d779e73c
SM
401
402 (when (eq letsym 'let*) ; update fvrs
403 (setq fvrs (remq var fvrs))
404 (setq emvrs (remq var emvrs)) ; remove if redefined
405 (when emvr-push
406 (push emvr-push emvrs)
407 (setq emvr-push nil))
b38b1ec0 408 (setq lmenvs (cconv--map-diff-elem lmenvs var))
d779e73c
SM
409 (when lmenv-push
410 (push lmenv-push lmenvs)
411 (setq lmenv-push nil)))
295fb2ac 412 )) ; end of dolist over binders
d779e73c
SM
413 (when (eq letsym 'let)
414
415 (let (var fvrs-1 emvrs-1 lmenvs-1)
416 ;; Here we update emvrs, fvrs and lmenvs lists
b38b1ec0
SM
417 (setq fvrs (cconv--set-diff-map fvrs binders-new))
418 (setq emvrs (cconv--set-diff-map emvrs binders-new))
d779e73c 419 (setq emvrs (append emvrs emvrs-new))
b38b1ec0 420 (setq lmenvs (cconv--set-diff-map lmenvs binders-new))
d779e73c
SM
421 (setq lmenvs (append lmenvs lmenvs-new)))
422
423 ;; Here we do the same letbinding as for let* above
424 ;; to avoid situation when a free variable of a lambda lifted
425 ;; function got redefined.
426
427 (let ((new-lmenv)
428 (var nil)
429 (closedsym nil)
295fb2ac
SM
430 (letbinds '()))
431 (dolist (binder binders)
432 (setq var (if (consp binder) (car binder) binder))
d779e73c
SM
433
434 (let ((lmenvs-1 lmenvs)) ; just to avoid manipulating
435 (dolist (lmenv lmenvs-1) ; the counter inside the loop
436 (when (memq var (cdr lmenv))
437 (setq closedsym (make-symbol
438 (concat "closed-"
439 (symbol-name var))))
440
441 (setq new-lmenv (list (car lmenv)))
b38b1ec0
SM
442 (dolist (frv (cdr lmenv))
443 (push (if (eq frv var) closedsym frv)
444 new-lmenv))
d779e73c
SM
445 (setq new-lmenv (reverse new-lmenv))
446 (setq lmenvs (remq lmenv lmenvs))
447 (push new-lmenv lmenvs)
448 (push `(,closedsym ,var) letbinds)
449 ))))
295fb2ac 450 (setq binders-new (append binders-new letbinds))))
d779e73c
SM
451
452 (dolist (elm body-forms) ; convert body forms
453 (push (cconv-closure-convert-rec
295fb2ac 454 elm emvrs fvrs envs lmenvs)
d779e73c 455 body-forms-new))
295fb2ac 456 `(,letsym ,(reverse binders-new) . ,(reverse body-forms-new))))
94d11cb5
IK
457 ;end of let let* forms
458
d779e73c
SM
459 ; first element is lambda expression
460 (`(,(and `(lambda . ,_) fun) . ,other-body-forms)
461
462 (let ((other-body-forms-new '()))
463 (dolist (elm other-body-forms)
464 (push (cconv-closure-convert-rec
295fb2ac 465 elm emvrs fvrs envs lmenvs)
d779e73c 466 other-body-forms-new))
295fb2ac
SM
467 `(funcall
468 ,(cconv-closure-convert-rec
469 (list 'function fun) emvrs fvrs envs lmenvs)
470 ,@(nreverse other-body-forms-new))))
d779e73c
SM
471
472 (`(cond . ,cond-forms) ; cond special form
473 (let ((cond-forms-new '()))
474 (dolist (elm cond-forms)
475 (push (let ((elm-new '()))
476 (dolist (elm-2 elm)
477 (push
478 (cconv-closure-convert-rec
295fb2ac 479 elm-2 emvrs fvrs envs lmenvs)
d779e73c
SM
480 elm-new))
481 (reverse elm-new))
482 cond-forms-new))
483 (cons 'cond
484 (reverse cond-forms-new))))
485
3e21b6a7 486 (`(quote . ,_) form)
d779e73c 487
3e21b6a7 488 (`(function (lambda ,vars . ,body-forms)) ; function form
b38b1ec0
SM
489 (let* ((fvrs-new (cconv--set-diff fvrs vars)) ; Remove vars from fvrs.
490 (fv (delete-dups (cconv-freevars form '())))
491 (leave fvrs-new) ; leave=non-nil if we should leave env unchanged.
d779e73c
SM
492 (body-forms-new '())
493 (letbind '())
494 (mv nil)
495 (envector nil))
496 (when fv
497 ;; Here we form our environment vector.
cb9336bd
SM
498
499 (dolist (elm fv)
500 (push
501 (cconv-closure-convert-rec
502 ;; Remove `elm' from `emvrs' for this call because in case
503 ;; `elm' is a variable that's wrapped in a cons-cell, we
504 ;; want to put the cons-cell itself in the closure, rather
505 ;; than just a copy of its current content.
506 elm (remq elm emvrs) fvrs envs lmenvs)
507 envector)) ; Process vars for closure vector.
508 (setq envector (reverse envector))
509 (setq envs fv)
b38b1ec0
SM
510 (setq fvrs-new fv)) ; Update substitution list.
511
512 (setq emvrs (cconv--set-diff emvrs vars))
513 (setq lmenvs (cconv--map-diff-set lmenvs vars))
514
515 ;; The difference between envs and fvrs is explained
516 ;; in comment in the beginning of the function.
517 (dolist (elm cconv-captured+mutated) ; Find mutated arguments
518 (setq mv (car elm)) ; used in inner closures.
d779e73c
SM
519 (when (and (memq mv vars) (eq form (caddr elm)))
520 (progn (push mv emvrs)
521 (push `(,mv (list ,mv)) letbind))))
522 (dolist (elm body-forms) ; convert function body
523 (push (cconv-closure-convert-rec
b38b1ec0 524 elm emvrs fvrs-new envs lmenvs)
d779e73c
SM
525 body-forms-new))
526
527 (setq body-forms-new
528 (if letbind `((let ,letbind . ,(reverse body-forms-new)))
529 (reverse body-forms-new)))
530
531 (cond
94d11cb5 532 ;if no freevars - do nothing
d779e73c
SM
533 ((null envector)
534 `(function (lambda ,vars . ,body-forms-new)))
535 ; 1 free variable - do not build vector
d779e73c 536 (t
cb9336bd
SM
537 `(internal-make-closure
538 ,vars ,envector . ,body-forms-new)))))
d779e73c 539
3e21b6a7 540 (`(function . ,_) form) ; Same as quote.
94d11cb5
IK
541
542 ;defconst, defvar
d779e73c
SM
543 (`(,(and sym (or `defconst `defvar)) ,definedsymbol . ,body-forms)
544
295fb2ac
SM
545 (let ((body-forms-new '()))
546 (dolist (elm body-forms)
547 (push (cconv-closure-convert-rec
548 elm emvrs fvrs envs lmenvs)
549 body-forms-new))
550 (setq body-forms-new (reverse body-forms-new))
551 `(,sym ,definedsymbol . ,body-forms-new)))
d779e73c
SM
552
553 ;defun, defmacro
554 (`(,(and sym (or `defun `defmacro))
555 ,func ,vars . ,body-forms)
3e21b6a7
SM
556 (let ((body-new '()) ; The whole body.
557 (body-forms-new '()) ; Body w\o docstring and interactive.
295fb2ac 558 (letbind '()))
3e21b6a7
SM
559 ; Find mutable arguments.
560 (dolist (elm vars)
561 (let ((lmutated cconv-captured+mutated)
562 (ismutated nil))
295fb2ac
SM
563 (while (and lmutated (not ismutated))
564 (when (and (eq (caar lmutated) elm)
3e21b6a7 565 (eq (caddar lmutated) form))
295fb2ac
SM
566 (setq ismutated t))
567 (setq lmutated (cdr lmutated)))
568 (when ismutated
569 (push elm letbind)
570 (push elm emvrs))))
3e21b6a7
SM
571 ;Transform body-forms.
572 (when (stringp (car body-forms)) ; Treat docstring well.
295fb2ac
SM
573 (push (car body-forms) body-new)
574 (setq body-forms (cdr body-forms)))
575 (when (eq (car-safe (car body-forms)) 'interactive)
576 (push (cconv-closure-convert-rec
577 (car body-forms)
578 emvrs fvrs envs lmenvs)
579 body-new)
580 (setq body-forms (cdr body-forms)))
581
582 (dolist (elm body-forms)
583 (push (cconv-closure-convert-rec
584 elm emvrs fvrs envs lmenvs)
585 body-forms-new))
586 (setq body-forms-new (reverse body-forms-new))
d779e73c 587
295fb2ac 588 (if letbind
3e21b6a7 589 ; Letbind mutable arguments.
295fb2ac
SM
590 (let ((binders-new '()))
591 (dolist (elm letbind) (push `(,elm (list ,elm))
592 binders-new))
593 (push `(let ,(reverse binders-new) .
594 ,body-forms-new) body-new)
595 (setq body-new (reverse body-new)))
596 (setq body-new (append (reverse body-new) body-forms-new)))
94d11cb5 597
295fb2ac 598 `(,sym ,func ,vars . ,body-new)))
94d11cb5 599
94d11cb5 600 ;condition-case
295fb2ac
SM
601 (`(condition-case ,var ,protected-form . ,handlers)
602 (let ((handlers-new '())
603 (newform (cconv-closure-convert-rec
604 `(function (lambda () ,protected-form))
605 emvrs fvrs envs lmenvs)))
d779e73c 606 (setq fvrs (remq var fvrs))
295fb2ac
SM
607 (dolist (handler handlers)
608 (push (list (car handler)
609 (cconv-closure-convert-rec
610 `(function (lambda (,(or var cconv--dummy-var))
611 ,@(cdr handler)))
612 emvrs fvrs envs lmenvs))
613 handlers-new))
614 `(condition-case :fun-body ,newform
615 ,@(nreverse handlers-new))))
616
617 (`(,(and head (or `catch `unwind-protect)) ,form . ,body)
618 `(,head ,(cconv-closure-convert-rec form emvrs fvrs envs lmenvs)
619 :fun-body
620 ,(cconv-closure-convert-rec `(function (lambda () ,@body))
621 emvrs fvrs envs lmenvs)))
622
e0f57e65
SM
623 (`(track-mouse . ,body)
624 `(track-mouse
295fb2ac
SM
625 :fun-body
626 ,(cconv-closure-convert-rec `(function (lambda () ,@body))
627 emvrs fvrs envs lmenvs)))
d779e73c
SM
628
629 (`(setq . ,forms) ; setq special form
630 (let (prognlist sym sym-new value)
631 (while forms
632 (setq sym (car forms))
633 (setq sym-new (cconv-closure-convert-rec
634 sym
295fb2ac 635 (remq sym emvrs) fvrs envs lmenvs))
d779e73c
SM
636 (setq value
637 (cconv-closure-convert-rec
295fb2ac 638 (cadr forms) emvrs fvrs envs lmenvs))
d779e73c
SM
639 (if (memq sym emvrs)
640 (push `(setcar ,sym-new ,value) prognlist)
641 (if (symbolp sym-new)
642 (push `(setq ,sym-new ,value) prognlist)
3e21b6a7 643 (debug) ;FIXME: When can this be right?
d779e73c
SM
644 (push `(set ,sym-new ,value) prognlist)))
645 (setq forms (cddr forms)))
646 (if (cdr prognlist)
647 `(progn . ,(reverse prognlist))
648 (car prognlist))))
649
650 (`(,(and (or `funcall `apply) callsym) ,fun . ,args)
651 ; funcall is not a special form
652 ; but we treat it separately
653 ; for the needs of lambda lifting
654 (let ((fv (cdr (assq fun lmenvs))))
655 (if fv
656 (let ((args-new '())
657 (processed-fv '()))
658 ;; All args (free variables and actual arguments)
659 ;; should be processed, because they can be fvrs
660 ;; (free variables of another closure)
661 (dolist (fvr fv)
662 (push (cconv-closure-convert-rec
663 fvr (remq fvr emvrs)
295fb2ac 664 fvrs envs lmenvs)
d779e73c
SM
665 processed-fv))
666 (setq processed-fv (reverse processed-fv))
667 (dolist (elm args)
668 (push (cconv-closure-convert-rec
295fb2ac 669 elm emvrs fvrs envs lmenvs)
d779e73c
SM
670 args-new))
671 (setq args-new (append processed-fv (reverse args-new)))
672 (setq fun (cconv-closure-convert-rec
295fb2ac 673 fun emvrs fvrs envs lmenvs))
d779e73c
SM
674 `(,callsym ,fun . ,args-new))
675 (let ((cdr-new '()))
676 (dolist (elm (cdr form))
677 (push (cconv-closure-convert-rec
295fb2ac 678 elm emvrs fvrs envs lmenvs)
d779e73c
SM
679 cdr-new))
680 `(,callsym . ,(reverse cdr-new))))))
681
682 (`(,func . ,body-forms) ; first element is function or whatever
683 ; function-like forms are:
684 ; or, and, if, progn, prog1, prog2,
685 ; while, until
686 (let ((body-forms-new '()))
687 (dolist (elm body-forms)
688 (push (cconv-closure-convert-rec
295fb2ac 689 elm emvrs fvrs envs lmenvs)
d779e73c
SM
690 body-forms-new))
691 (setq body-forms-new (reverse body-forms-new))
692 `(,func . ,body-forms-new)))
693
694 (_
43e67019
SM
695 (let ((free (memq form fvrs)))
696 (if free ;form is a free variable
697 (let* ((numero (- (length fvrs) (length free)))
cb9336bd
SM
698 ;; Replace form => (aref env #)
699 (var `(internal-get-closed-var ,numero)))
43e67019
SM
700 (if (memq form emvrs) ; form => (car (aref env #)) if mutable
701 `(car ,var)
702 var))
703 (if (memq form emvrs) ; if form is a mutable variable
704 `(car ,form) ; replace form => (car form)
705 form))))))
706
707(defun cconv-analyse-function (args body env parentform inclosure)
708 (dolist (arg args)
709 (cond
ce5b520a 710 ((byte-compile-not-lexical-var-p arg)
43e67019
SM
711 (byte-compile-report-error
712 (format "Argument %S is not a lexical variable" arg)))
713 ((eq ?& (aref (symbol-name arg) 0)) nil) ;Ignore &rest, &optional, ...
714 (t (push (list arg inclosure parentform) env)))) ;Push vrs to vars.
715 (dolist (form body) ;Analyse body forms.
716 (cconv-analyse-form form env inclosure)))
717
718(defun cconv-analyse-form (form env inclosure)
d779e73c
SM
719 "Find mutated variables and variables captured by closure. Analyse
720lambdas if they are suitable for lambda lifting.
94d11cb5 721-- FORM is a piece of Elisp code after macroexpansion.
43e67019
SM
722-- ENV is a list of variables visible in current lexical environment.
723 Each entry has the form (VAR INCLOSURE BINDER PARENTFORM)
724 for let-bound vars and (VAR INCLOSURE PARENTFORM) for function arguments.
725-- INCLOSURE is the nesting level within lambdas."
94d11cb5
IK
726 (pcase form
727 ; let special form
43e67019 728 (`(,(and (or `let* `let) letsym) ,binders . ,body-forms)
d779e73c 729
43e67019 730 (let ((orig-env env)
d779e73c 731 (var nil)
43e67019
SM
732 (value nil))
733 (dolist (binder binders)
734 (if (not (consp binder))
d779e73c 735 (progn
43e67019
SM
736 (setq var binder) ; treat the form (let (x) ...) well
737 (setq value nil))
738 (setq var (car binder))
739 (setq value (cadr binder))
740
741 (cconv-analyse-form value (if (eq letsym 'let*) env orig-env)
742 inclosure))
743
ce5b520a 744 (unless (byte-compile-not-lexical-var-p var)
43e67019
SM
745 (let ((varstruct (list var inclosure binder form)))
746 (push varstruct env) ; Push a new one.
747
748 (pcase value
749 (`(function (lambda . ,_))
750 ;; If var is a function push it to lambda list.
751 (push varstruct cconv-lambda-candidates)))))))
752
753 (dolist (form body-forms) ; Analyse body forms.
754 (cconv-analyse-form form env inclosure)))
755
94d11cb5 756 ; defun special form
d779e73c 757 (`(,(or `defun `defmacro) ,func ,vrs . ,body-forms)
43e67019
SM
758 (when env
759 (byte-compile-log-warning
760 (format "Function %S will ignore its context %S"
761 func (mapcar #'car env))
762 t :warning))
763 (cconv-analyse-function vrs body-forms nil form 0))
764
765 (`(function (lambda ,vrs . ,body-forms))
766 (cconv-analyse-function vrs body-forms env form (1+ inclosure)))
767
768 (`(setq . ,forms)
769 ;; If a local variable (member of env) is modified by setq then
770 ;; it is a mutated variable.
d779e73c 771 (while forms
43e67019 772 (let ((v (assq (car forms) env))) ; v = non nil if visible
d779e73c
SM
773 (when v
774 (push v cconv-mutated)
43e67019 775 ;; Delete from candidate list for lambda lifting.
d779e73c 776 (setq cconv-lambda-candidates (delq v cconv-lambda-candidates))
43e67019
SM
777 (unless (eq inclosure (cadr v)) ;Bound in a different closure level.
778 (push v cconv-captured))))
779 (cconv-analyse-form (cadr forms) env inclosure)
780 (setq forms (cddr forms))))
781
782 (`((lambda . ,_) . ,_) ; first element is lambda expression
d779e73c 783 (dolist (exp `((function ,(car form)) . ,(cdr form)))
43e67019 784 (cconv-analyse-form exp env inclosure)))
d779e73c
SM
785
786 (`(cond . ,cond-forms) ; cond special form
43e67019
SM
787 (dolist (forms cond-forms)
788 (dolist (form forms)
789 (cconv-analyse-form form env inclosure))))
d779e73c
SM
790
791 (`(quote . ,_) nil) ; quote form
d779e73c
SM
792 (`(function . ,_) nil) ; same as quote
793
43e67019
SM
794 (`(condition-case ,var ,protected-form . ,handlers)
795 ;; FIXME: The bytecode for condition-case forces us to wrap the
796 ;; form and handlers in closures (for handlers, it's probably
797 ;; unavoidable, but not for the protected form).
798 (setq inclosure (1+ inclosure))
799 (cconv-analyse-form protected-form env inclosure)
800 (push (list var inclosure form) env)
801 (dolist (handler handlers)
802 (dolist (form (cdr handler))
803 (cconv-analyse-form form env inclosure))))
804
805 ;; FIXME: The bytecode for catch forces us to wrap the body.
806 (`(,(or `catch `unwind-protect) ,form . ,body)
807 (cconv-analyse-form form env inclosure)
808 (setq inclosure (1+ inclosure))
809 (dolist (form body)
810 (cconv-analyse-form form env inclosure)))
811
812 ;; FIXME: The bytecode for save-window-excursion and the lack of
813 ;; bytecode for track-mouse forces us to wrap the body.
e0f57e65 814 (`(track-mouse . ,body)
43e67019
SM
815 (setq inclosure (1+ inclosure))
816 (dolist (form body)
817 (cconv-analyse-form form env inclosure)))
818
819 (`(,(or `defconst `defvar) ,var ,value . ,_)
820 (push var byte-compile-bound-variables)
821 (cconv-analyse-form value env inclosure))
d779e73c
SM
822
823 (`(,(or `funcall `apply) ,fun . ,args)
43e67019
SM
824 ;; Here we ignore fun because funcall and apply are the only two
825 ;; functions where we can pass a candidate for lambda lifting as
826 ;; argument. So, if we see fun elsewhere, we'll delete it from
827 ;; lambda candidate list.
828 (if (symbolp fun)
829 (let ((lv (assq fun cconv-lambda-candidates)))
830 (when lv
831 (unless (eq (cadr lv) inclosure)
832 (push lv cconv-captured)
833 ;; If this funcall and the definition of fun are in
834 ;; different closures - we delete fun from candidate
835 ;; list, because it is too complicated to manage free
836 ;; variables in this case.
837 (setq cconv-lambda-candidates
838 (delq lv cconv-lambda-candidates)))))
839 (cconv-analyse-form fun env inclosure))
840 (dolist (form args)
841 (cconv-analyse-form form env inclosure)))
842
843 (`(,_ . ,body-forms) ; First element is a function or whatever.
844 (dolist (form body-forms)
845 (cconv-analyse-form form env inclosure)))
846
847 ((pred symbolp)
848 (let ((dv (assq form env))) ; dv = declared and visible
849 (when dv
850 (unless (eq inclosure (cadr dv)) ; capturing condition
851 (push dv cconv-captured))
852 ;; Delete lambda if it is found here, since it escapes.
853 (setq cconv-lambda-candidates
854 (delq dv cconv-lambda-candidates)))))))
94d11cb5
IK
855
856(provide 'cconv)
857;;; cconv.el ends here