Commit | Line | Data |
---|---|---|
39605a34 | 1 | ;;; cconv.el --- Closure conversion for statically scoped Emacs lisp. -*- lexical-binding: t; coding: utf-8 -*- |
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 | ;; | |
876c194c SM |
50 | ;; (lambda (v0 ...) ... fv0 .. fv1 ...) => |
51 | ;; (internal-make-closure (v0 ...) (fv1 ...) | |
52 | ;; ... (internal-get-closed-var 0) ... (internal-get-closed-var 1) ...) | |
94d11cb5 IK |
53 | ;; |
54 | ;; If the function has no free variables, we don't do anything. | |
94d11cb5 | 55 | ;; |
d779e73c | 56 | ;; If a variable is mutated (updated by setq), and it is used in a closure |
876c194c | 57 | ;; we wrap its definition with list: (list val) and we also replace |
d779e73c SM |
58 | ;; var => (car var) wherever this variable is used, and also |
59 | ;; (setq var value) => (setcar var value) where it is updated. | |
94d11cb5 | 60 | ;; |
d779e73c SM |
61 | ;; If defun argument is closure mutable, we letbind it and wrap it's |
62 | ;; definition with list. | |
63 | ;; (defun foo (... mutable-arg ...) ...) => | |
64 | ;; (defun foo (... m-arg ...) (let ((m-arg (list m-arg))) ...)) | |
94d11cb5 IK |
65 | ;; |
66 | ;;; Code: | |
67 | ||
ca105506 | 68 | ;; TODO: (not just for cconv but also for the lexbind changes in general) |
ca105506 | 69 | ;; - let (e)debug find the value of lexical variables from the stack. |
7200d79c | 70 | ;; - make eval-region do the eval-sexp-add-defvars danse. |
e2abe5a1 | 71 | ;; - byte-optimize-form should be applied before cconv. |
ca105506 SM |
72 | ;; OTOH, the warnings emitted by cconv-analyze need to come before optimize |
73 | ;; since afterwards they can because obnoxious (warnings about an "unused | |
74 | ;; variable" should not be emitted when the variable use has simply been | |
75 | ;; optimized away). | |
7200d79c SM |
76 | ;; - turn defun and defmacro into macros (and remove special handling of |
77 | ;; `declare' afterwards). | |
78 | ;; - let macros specify that some let-bindings come from the same source, | |
79 | ;; so the unused warning takes all uses into account. | |
80 | ;; - let interactive specs return a function to build the args (to stash into | |
81 | ;; command-history). | |
cb9336bd SM |
82 | ;; - canonize code in macro-expand so we don't have to handle (let (var) body) |
83 | ;; and other oddities. | |
3e21b6a7 SM |
84 | ;; - new byte codes for unwind-protect, catch, and condition-case so that |
85 | ;; closures aren't needed at all. | |
7200d79c | 86 | ;; - inline source code of different binding mode by first compiling it. |
d032d5e7 SM |
87 | ;; - a reference to a var that is known statically to always hold a constant |
88 | ;; should be turned into a byte-constant rather than a byte-stack-ref. | |
e2abe5a1 SM |
89 | ;; Hmm... right, that's called constant propagation and could be done here, |
90 | ;; but when that constant is a function, we have to be careful to make sure | |
d032d5e7 SM |
91 | ;; the bytecomp only compiles it once. |
92 | ;; - Since we know here when a variable is not mutated, we could pass that | |
93 | ;; info to the byte-compiler, e.g. by using a new `immutable-let'. | |
e2abe5a1 | 94 | ;; - add tail-calls to bytecode.c and the byte compiler. |
29a4dcb0 | 95 | ;; - call known non-escaping functions with `goto' rather than `call'. |
6c075cd7 | 96 | ;; - optimize mapcar to a while loop. |
d032d5e7 SM |
97 | |
98 | ;; (defmacro dlet (binders &rest body) | |
99 | ;; ;; Works in both lexical and non-lexical mode. | |
100 | ;; `(progn | |
101 | ;; ,@(mapcar (lambda (binder) | |
102 | ;; `(defvar ,(if (consp binder) (car binder) binder))) | |
103 | ;; binders) | |
104 | ;; (let ,binders ,@body))) | |
105 | ||
106 | ;; (defmacro llet (binders &rest body) | |
107 | ;; ;; Only works in lexical-binding mode. | |
108 | ;; `(funcall | |
109 | ;; (lambda ,(mapcar (lambda (binder) (if (consp binder) (car binder) binder)) | |
110 | ;; binders) | |
111 | ;; ,@body) | |
112 | ;; ,@(mapcar (lambda (binder) (if (consp binder) (cadr binder))) | |
113 | ;; binders))) | |
114 | ||
115 | ;; (defmacro letrec (binders &rest body) | |
116 | ;; ;; Only useful in lexical-binding mode. | |
117 | ;; ;; As a special-form, we could implement it more efficiently (and cleanly, | |
118 | ;; ;; making the vars actually unbound during evaluation of the binders). | |
119 | ;; `(let ,(mapcar (lambda (binder) (if (consp binder) (car binder) binder)) | |
120 | ;; binders) | |
121 | ;; ,@(delq nil (mapcar (lambda (binder) (if (consp binder) `(setq ,@binder))) | |
122 | ;; binders)) | |
123 | ;; ,@body)) | |
b38b1ec0 | 124 | |
94d11cb5 IK |
125 | (eval-when-compile (require 'cl)) |
126 | ||
d032d5e7 | 127 | (defconst cconv-liftwhen 6 |
d779e73c | 128 | "Try to do lambda lifting if the number of arguments + free variables |
94d11cb5 | 129 | is less than this number.") |
a9de04fa SM |
130 | ;; List of all the variables that are both captured by a closure |
131 | ;; and mutated. Each entry in the list takes the form | |
132 | ;; (BINDER . PARENTFORM) where BINDER is the (VAR VAL) that introduces the | |
133 | ;; variable (or is just (VAR) for variables not introduced by let). | |
134 | (defvar cconv-captured+mutated) | |
d779e73c | 135 | |
a9de04fa SM |
136 | ;; List of candidates for lambda lifting. |
137 | ;; Each candidate has the form (BINDER . PARENTFORM). A candidate | |
138 | ;; is a variable that is only passed to `funcall' or `apply'. | |
139 | (defvar cconv-lambda-candidates) | |
d779e73c | 140 | |
a9de04fa SM |
141 | ;; Alist associating to each function body the list of its free variables. |
142 | (defvar cconv-freevars-alist) | |
94d11cb5 IK |
143 | |
144 | ;;;###autoload | |
295fb2ac SM |
145 | (defun cconv-closure-convert (form) |
146 | "Main entry point for closure conversion. | |
94d11cb5 IK |
147 | -- FORM is a piece of Elisp code after macroexpansion. |
148 | -- TOPLEVEL(optional) is a boolean variable, true if we are at the root of AST | |
149 | ||
150 | Returns a form where all lambdas don't have any free variables." | |
b38b1ec0 | 151 | ;; (message "Entering cconv-closure-convert...") |
a9de04fa | 152 | (let ((cconv-freevars-alist '()) |
94d11cb5 | 153 | (cconv-lambda-candidates '()) |
d779e73c | 154 | (cconv-captured+mutated '())) |
b38b1ec0 | 155 | ;; Analyse form - fill these variables with new information. |
a9de04fa SM |
156 | (cconv-analyse-form form '()) |
157 | (setq cconv-freevars-alist (nreverse cconv-freevars-alist)) | |
6c075cd7 | 158 | (cconv-convert form nil nil))) ; Env initially empty. |
94d11cb5 | 159 | |
295fb2ac | 160 | (defconst cconv--dummy-var (make-symbol "ignored")) |
b38b1ec0 SM |
161 | |
162 | (defun cconv--set-diff (s1 s2) | |
163 | "Return elements of set S1 that are not in set S2." | |
164 | (let ((res '())) | |
165 | (dolist (x s1) | |
166 | (unless (memq x s2) (push x res))) | |
167 | (nreverse res))) | |
168 | ||
169 | (defun cconv--set-diff-map (s m) | |
170 | "Return elements of set S that are not in Dom(M)." | |
171 | (let ((res '())) | |
172 | (dolist (x s) | |
173 | (unless (assq x m) (push x res))) | |
174 | (nreverse res))) | |
175 | ||
176 | (defun cconv--map-diff (m1 m2) | |
177 | "Return the submap of map M1 that has Dom(M2) removed." | |
178 | (let ((res '())) | |
179 | (dolist (x m1) | |
180 | (unless (assq (car x) m2) (push x res))) | |
181 | (nreverse res))) | |
182 | ||
183 | (defun cconv--map-diff-elem (m x) | |
184 | "Return the map M minus any mapping for X." | |
185 | ;; Here we assume that X appears at most once in M. | |
186 | (let* ((b (assq x m)) | |
187 | (res (if b (remq b m) m))) | |
188 | (assert (null (assq x res))) ;; Check the assumption was warranted. | |
189 | res)) | |
94d11cb5 | 190 | |
b38b1ec0 SM |
191 | (defun cconv--map-diff-set (m s) |
192 | "Return the map M minus any mapping for elements of S." | |
193 | ;; Here we assume that X appears at most once in M. | |
194 | (let ((res '())) | |
195 | (dolist (b m) | |
196 | (unless (memq (car b) s) (push b res))) | |
197 | (nreverse res))) | |
198 | ||
6c075cd7 SM |
199 | (defun cconv--convert-function (args body env parentform) |
200 | (assert (equal body (caar cconv-freevars-alist))) | |
201 | (let* ((fvs (cdr (pop cconv-freevars-alist))) | |
202 | (body-new '()) | |
a9de04fa | 203 | (letbind '()) |
6c075cd7 SM |
204 | (envector ()) |
205 | (i 0) | |
206 | (new-env ())) | |
207 | ;; Build the "formal and actual envs" for the closure-converted function. | |
208 | (dolist (fv fvs) | |
209 | (let ((exp (or (cdr (assq fv env)) fv))) | |
210 | (pcase exp | |
211 | ;; If `fv' is a variable that's wrapped in a cons-cell, | |
212 | ;; we want to put the cons-cell itself in the closure, | |
213 | ;; rather than just a copy of its current content. | |
214 | (`(car ,iexp . ,_) | |
215 | (push iexp envector) | |
216 | (push `(,fv . (car (internal-get-closed-var ,i))) new-env)) | |
217 | (_ | |
218 | (push exp envector) | |
219 | (push `(,fv . (internal-get-closed-var ,i)) new-env)))) | |
220 | (setq i (1+ i))) | |
221 | (setq envector (nreverse envector)) | |
222 | (setq new-env (nreverse new-env)) | |
223 | ||
224 | (dolist (arg args) | |
225 | (if (not (member (cons (list arg) parentform) cconv-captured+mutated)) | |
226 | (if (assq arg new-env) (push `(,arg) new-env)) | |
227 | (push `(,arg . (car ,arg)) new-env) | |
228 | (push `(,arg (list ,arg)) letbind))) | |
ca105506 | 229 | |
6c075cd7 SM |
230 | (setq body-new (mapcar (lambda (form) |
231 | (cconv-convert form new-env nil)) | |
232 | body)) | |
233 | ||
234 | (when letbind | |
235 | (let ((special-forms '())) | |
236 | ;; Keep special forms at the beginning of the body. | |
237 | (while (or (stringp (car body-new)) ;docstring. | |
238 | (memq (car-safe (car body-new)) '(interactive declare))) | |
239 | (push (pop body-new) special-forms)) | |
240 | (setq body-new | |
241 | `(,@(nreverse special-forms) (let ,letbind . ,body-new))))) | |
a9de04fa SM |
242 | |
243 | (cond | |
6c075cd7 SM |
244 | ((null envector) ;if no freevars - do nothing |
245 | `(function (lambda ,args . ,body-new))) | |
a9de04fa SM |
246 | (t |
247 | `(internal-make-closure | |
6c075cd7 | 248 | ,args ,envector . ,body-new))))) |
a9de04fa | 249 | |
6c075cd7 | 250 | (defun cconv-convert (form env extend) |
d779e73c | 251 | ;; This function actually rewrites the tree. |
6c075cd7 SM |
252 | "Return FORM with all its lambdas changed so they are closed. |
253 | ENV is a lexical environment mapping variables to the expression | |
254 | used to get its value. This is used for variables that are copied into | |
255 | closures, moved into cons cells, ... | |
256 | ENV is a list where each entry takes the shape either: | |
257 | (VAR . (car EXP)): VAR has been moved into the car of a cons-cell, and EXP | |
258 | is an expression that evaluates to this cons-cell. | |
259 | (VAR . (internal-get-closed-var N)): VAR has been copied into the closure | |
260 | environment's Nth slot. | |
261 | (VAR . (apply-partially F ARG1 ARG2 ..)): VAR has been λ-lifted and takes | |
262 | additional arguments ARGs. | |
263 | EXTEND is a list of variables which might need to be accessed even from places | |
264 | where they are shadowed, because some part of ENV causes them to be used at | |
265 | places where they originally did not directly appear." | |
266 | (assert (not (delq nil (mapcar (lambda (mapping) | |
267 | (if (eq (cadr mapping) 'apply-partially) | |
268 | (cconv--set-diff (cdr (cddr mapping)) | |
269 | extend))) | |
270 | env)))) | |
ca105506 | 271 | |
d779e73c | 272 | ;; What's the difference between fvrs and envs? |
94d11cb5 IK |
273 | ;; Suppose that we have the code |
274 | ;; (lambda (..) fvr (let ((fvr 1)) (+ fvr 1))) | |
d779e73c SM |
275 | ;; only the first occurrence of fvr should be replaced by |
276 | ;; (aref env ...). | |
94d11cb5 IK |
277 | ;; So initially envs and fvrs are the same thing, but when we descend to |
278 | ;; the 'let, we delete fvr from fvrs. Why we don't delete fvr from envs? | |
279 | ;; Because in envs the order of variables is important. We use this list | |
d779e73c SM |
280 | ;; to find the number of a specific variable in the environment vector, |
281 | ;; so we never touch it(unless we enter to the other closure). | |
282 | ;;(if (listp form) (print (car form)) form) | |
283 | (pcase form | |
6c075cd7 | 284 | (`(,(and letsym (or `let* `let)) ,binders . ,body) |
94d11cb5 IK |
285 | |
286 | ; let and let* special forms | |
6c075cd7 SM |
287 | (let ((binders-new '()) |
288 | (new-env env) | |
289 | (new-extend extend)) | |
d779e73c | 290 | |
295fb2ac SM |
291 | (dolist (binder binders) |
292 | (let* ((value nil) | |
293 | (var (if (not (consp binder)) | |
a9de04fa | 294 | (prog1 binder (setq binder (list binder))) |
295fb2ac SM |
295 | (setq value (cadr binder)) |
296 | (car binder))) | |
297 | (new-val | |
298 | (cond | |
299 | ;; Check if var is a candidate for lambda lifting. | |
6c075cd7 SM |
300 | ((and (member (cons binder form) cconv-lambda-candidates) |
301 | (progn | |
302 | (assert (and (eq (car value) 'function) | |
303 | (eq (car (cadr value)) 'lambda))) | |
304 | (assert (equal (cddr (cadr value)) | |
305 | (caar cconv-freevars-alist))) | |
306 | ;; Peek at the freevars to decide whether to λ-lift. | |
307 | (let* ((fvs (cdr (car cconv-freevars-alist))) | |
308 | (fun (cadr value)) | |
309 | (funargs (cadr fun)) | |
310 | (funcvars (append fvs funargs))) | |
94d11cb5 | 311 | ; lambda lifting condition |
6c075cd7 SM |
312 | (and fvs (>= cconv-liftwhen (length funcvars)))))) |
313 | ; Lift. | |
314 | (let* ((fvs (cdr (pop cconv-freevars-alist))) | |
315 | (fun (cadr value)) | |
316 | (funargs (cadr fun)) | |
317 | (funcvars (append fvs funargs)) | |
318 | (funcbody (cddr fun)) | |
319 | (funcbody-env ())) | |
320 | (push `(,var . (apply-partially ,var . ,fvs)) new-env) | |
321 | (dolist (fv fvs) | |
322 | (pushnew fv new-extend) | |
323 | (if (and (eq 'car (car-safe (cdr (assq fv env)))) | |
324 | (not (memq fv funargs))) | |
325 | (push `(,fv . (car ,fv)) funcbody-env))) | |
326 | `(function (lambda ,funcvars . | |
327 | ,(mapcar (lambda (form) | |
328 | (cconv-convert | |
329 | form funcbody-env nil)) | |
330 | funcbody))))) | |
295fb2ac SM |
331 | |
332 | ;; Check if it needs to be turned into a "ref-cell". | |
a9de04fa | 333 | ((member (cons binder form) cconv-captured+mutated) |
295fb2ac | 334 | ;; Declared variable is mutated and captured. |
6c075cd7 SM |
335 | (push `(,var . (car ,var)) new-env) |
336 | `(list ,(cconv-convert value env extend))) | |
295fb2ac SM |
337 | |
338 | ;; Normal default case. | |
339 | (t | |
6c075cd7 SM |
340 | (if (assq var new-env) (push `(,var) new-env)) |
341 | (cconv-convert value env extend))))) | |
342 | ||
343 | ;; The piece of code below letbinds free variables of a λ-lifted | |
344 | ;; function if they are redefined in this let, example: | |
345 | ;; (let* ((fun (lambda (x) (+ x y))) (y 1)) (funcall fun 1)) | |
346 | ;; Here we can not pass y as parameter because it is redefined. | |
347 | ;; So we add a (closed-y y) declaration. We do that even if the | |
348 | ;; function is not used inside this let(*). The reason why we | |
349 | ;; ignore this case is that we can't "look forward" to see if the | |
350 | ;; function is called there or not. To treat this case better we'd | |
351 | ;; need to traverse the tree one more time to collect this data, and | |
352 | ;; I think that it's not worth it. | |
353 | (when (memq var new-extend) | |
354 | (let ((closedsym | |
355 | (make-symbol (concat "closed-" (symbol-name var))))) | |
356 | (setq new-env | |
357 | (mapcar (lambda (mapping) | |
358 | (if (not (eq (cadr mapping) 'apply-partially)) | |
359 | mapping | |
360 | (assert (eq (car mapping) (nth 2 mapping))) | |
361 | (list* (car mapping) | |
362 | 'apply-partially | |
363 | (car mapping) | |
364 | (mapcar (lambda (arg) | |
365 | (if (eq var arg) | |
366 | closedsym arg)) | |
367 | (nthcdr 3 mapping))))) | |
368 | new-env)) | |
369 | (setq new-extend (remq var new-extend)) | |
370 | (push closedsym new-extend) | |
371 | (push `(,closedsym ,var) binders-new))) | |
d779e73c | 372 | |
b38b1ec0 SM |
373 | ;; We push the element after redefined free variables are |
374 | ;; processed. This is important to avoid the bug when free | |
375 | ;; variable and the function have the same name. | |
295fb2ac | 376 | (push (list var new-val) binders-new) |
d779e73c | 377 | |
6c075cd7 SM |
378 | (when (eq letsym 'let*) |
379 | (setq env new-env) | |
380 | (setq extend new-extend)) | |
381 | )) ; end of dolist over binders | |
382 | ||
383 | `(,letsym ,(nreverse binders-new) | |
384 | . ,(mapcar (lambda (form) | |
385 | (cconv-convert | |
386 | form new-env new-extend)) | |
387 | body)))) | |
94d11cb5 IK |
388 | ;end of let let* forms |
389 | ||
d779e73c | 390 | ; first element is lambda expression |
6c075cd7 SM |
391 | (`(,(and `(lambda . ,_) fun) . ,args) |
392 | ;; FIXME: it's silly to create a closure just to call it. | |
ca105506 | 393 | ;; Running byte-optimize-form earlier will resolve this. |
6c075cd7 SM |
394 | `(funcall |
395 | ,(cconv-convert `(function ,fun) env extend) | |
396 | ,@(mapcar (lambda (form) | |
397 | (cconv-convert form env extend)) | |
398 | args))) | |
d779e73c SM |
399 | |
400 | (`(cond . ,cond-forms) ; cond special form | |
6c075cd7 SM |
401 | `(cond . ,(mapcar (lambda (branch) |
402 | (mapcar (lambda (form) | |
403 | (cconv-convert form env extend)) | |
404 | branch)) | |
405 | cond-forms))) | |
d779e73c | 406 | |
6c075cd7 SM |
407 | (`(function (lambda ,args . ,body) . ,_) |
408 | (cconv--convert-function args body env form)) | |
d779e73c | 409 | |
876c194c | 410 | (`(internal-make-closure . ,_) |
6c075cd7 SM |
411 | (byte-compile-report-error |
412 | "Internal error in compiler: cconv called twice?")) | |
876c194c | 413 | |
6c075cd7 SM |
414 | (`(quote . ,_) form) |
415 | (`(function . ,_) form) | |
94d11cb5 IK |
416 | |
417 | ;defconst, defvar | |
6c075cd7 SM |
418 | (`(,(and sym (or `defconst `defvar)) ,definedsymbol . ,forms) |
419 | `(,sym ,definedsymbol | |
420 | . ,(mapcar (lambda (form) (cconv-convert form env extend)) | |
421 | forms))) | |
d779e73c SM |
422 | |
423 | ;defun, defmacro | |
424 | (`(,(and sym (or `defun `defmacro)) | |
6c075cd7 SM |
425 | ,func ,args . ,body) |
426 | (assert (equal body (caar cconv-freevars-alist))) | |
a9de04fa | 427 | (assert (null (cdar cconv-freevars-alist))) |
6c075cd7 SM |
428 | |
429 | (let ((new (cconv--convert-function args body env form))) | |
430 | (pcase new | |
431 | (`(function (lambda ,newargs . ,new-body)) | |
432 | (assert (equal args newargs)) | |
433 | `(,sym ,func ,args . ,new-body)) | |
434 | (t (byte-compile-report-error | |
435 | (format "Internal error in cconv of (%s %s ...)" sym func)))))) | |
94d11cb5 | 436 | |
94d11cb5 | 437 | ;condition-case |
295fb2ac | 438 | (`(condition-case ,var ,protected-form . ,handlers) |
6c075cd7 SM |
439 | (let ((newform (cconv--convert-function |
440 | () (list protected-form) env form))) | |
295fb2ac | 441 | `(condition-case :fun-body ,newform |
876c194c SM |
442 | ,@(mapcar (lambda (handler) |
443 | (list (car handler) | |
6c075cd7 SM |
444 | (cconv--convert-function |
445 | (list (or var cconv--dummy-var)) | |
446 | (cdr handler) env form))) | |
876c194c | 447 | handlers)))) |
295fb2ac SM |
448 | |
449 | (`(,(and head (or `catch `unwind-protect)) ,form . ,body) | |
6c075cd7 SM |
450 | `(,head ,(cconv-convert form env extend) |
451 | :fun-body ,(cconv--convert-function () body env form))) | |
295fb2ac | 452 | |
e0f57e65 SM |
453 | (`(track-mouse . ,body) |
454 | `(track-mouse | |
6c075cd7 | 455 | :fun-body ,(cconv--convert-function () body env form))) |
d779e73c SM |
456 | |
457 | (`(setq . ,forms) ; setq special form | |
6c075cd7 | 458 | (let ((prognlist ())) |
d779e73c | 459 | (while forms |
6c075cd7 SM |
460 | (let* ((sym (pop forms)) |
461 | (sym-new (or (cdr (assq sym env)) sym)) | |
462 | (value (cconv-convert (pop forms) env extend))) | |
463 | (push (pcase sym-new | |
464 | ((pred symbolp) `(setq ,sym-new ,value)) | |
465 | (`(car ,iexp) `(setcar ,iexp ,value)) | |
466 | ;; This "should never happen", but for variables which are | |
467 | ;; mutated+captured+unused, we may end up trying to `setq' | |
468 | ;; on a closed-over variable, so just drop the setq. | |
469 | (_ ;; (byte-compile-report-error | |
470 | ;; (format "Internal error in cconv of (setq %s ..)" | |
471 | ;; sym-new)) | |
472 | value)) | |
473 | prognlist))) | |
d779e73c | 474 | (if (cdr prognlist) |
6c075cd7 | 475 | `(progn . ,(nreverse prognlist)) |
d779e73c SM |
476 | (car prognlist)))) |
477 | ||
478 | (`(,(and (or `funcall `apply) callsym) ,fun . ,args) | |
6c075cd7 SM |
479 | ;; These are not special forms but we treat them separately for the needs |
480 | ;; of lambda lifting. | |
481 | (let ((mapping (cdr (assq fun env)))) | |
482 | (pcase mapping | |
483 | (`(apply-partially ,_ . ,(and fvs `(,_ . ,_))) | |
484 | (assert (eq (cadr mapping) fun)) | |
485 | `(,callsym ,fun | |
486 | ,@(mapcar (lambda (fv) | |
487 | (let ((exp (or (cdr (assq fv env)) fv))) | |
488 | (pcase exp | |
489 | (`(car ,iexp . ,_) iexp) | |
490 | (_ exp)))) | |
491 | fvs) | |
492 | ,@(mapcar (lambda (arg) | |
493 | (cconv-convert arg env extend)) | |
494 | args))) | |
495 | (_ `(,callsym ,@(mapcar (lambda (arg) | |
496 | (cconv-convert arg env extend)) | |
497 | (cons fun args))))))) | |
d779e73c | 498 | |
d032d5e7 | 499 | (`(interactive . ,forms) |
6c075cd7 SM |
500 | `(interactive . ,(mapcar (lambda (form) |
501 | (cconv-convert form nil nil)) | |
502 | forms))) | |
ca105506 | 503 | |
ba83908c | 504 | (`(declare . ,_) form) ;The args don't contain code. |
ca105506 | 505 | |
6c075cd7 SM |
506 | (`(,func . ,forms) |
507 | ;; First element is function or whatever function-like forms are: or, and, | |
508 | ;; if, progn, prog1, prog2, while, until | |
509 | `(,func . ,(mapcar (lambda (form) | |
510 | (cconv-convert form env extend)) | |
511 | forms))) | |
512 | ||
513 | (_ (or (cdr (assq form env)) form)))) | |
43e67019 | 514 | |
a9de04fa SM |
515 | (unless (fboundp 'byte-compile-not-lexical-var-p) |
516 | ;; Only used to test the code in non-lexbind Emacs. | |
517 | (defalias 'byte-compile-not-lexical-var-p 'boundp)) | |
518 | ||
6c075cd7 | 519 | (defun cconv--analyse-use (vardata form varkind) |
d032d5e7 SM |
520 | "Analyse the use of a variable. |
521 | VARDATA should be (BINDER READ MUTATED CAPTURED CALLED). | |
522 | VARKIND is the name of the kind of variable. | |
523 | FORM is the parent form that binds this var." | |
a9de04fa SM |
524 | ;; use = `(,binder ,read ,mutated ,captured ,called) |
525 | (pcase vardata | |
d032d5e7 SM |
526 | (`(,_ nil nil nil nil) nil) |
527 | (`((,(and (pred (lambda (var) (eq ?_ (aref (symbol-name var) 0)))) var) . ,_) | |
528 | ,_ ,_ ,_ ,_) | |
6c075cd7 SM |
529 | (byte-compile-log-warning |
530 | (format "%s `%S' not left unused" varkind var)))) | |
d032d5e7 SM |
531 | (pcase vardata |
532 | (`((,var . ,_) nil ,_ ,_ nil) | |
533 | ;; FIXME: This gives warnings in the wrong order, with imprecise line | |
534 | ;; numbers and without function name info. | |
535 | (unless (or ;; Uninterned symbols typically come from macro-expansion, so | |
536 | ;; it is often non-trivial for the programmer to avoid such | |
537 | ;; unused vars. | |
538 | (not (intern-soft var)) | |
e67a13ab CY |
539 | (eq ?_ (aref (symbol-name var) 0)) |
540 | ;; As a special exception, ignore "ignore". | |
541 | (eq var 'ignored)) | |
d032d5e7 SM |
542 | (byte-compile-log-warning (format "Unused lexical %s `%S'" |
543 | varkind var)))) | |
a9de04fa | 544 | ;; If it's unused, there's no point converting it into a cons-cell, even if |
d032d5e7 | 545 | ;; it's captured and mutated. |
a9de04fa SM |
546 | (`(,binder ,_ t t ,_) |
547 | (push (cons binder form) cconv-captured+mutated)) | |
548 | (`(,(and binder `(,_ (function (lambda . ,_)))) nil nil nil t) | |
6c075cd7 | 549 | (push (cons binder form) cconv-lambda-candidates)))) |
a9de04fa | 550 | |
6c075cd7 | 551 | (defun cconv--analyse-function (args body env parentform) |
a9de04fa SM |
552 | (let* ((newvars nil) |
553 | (freevars (list body)) | |
554 | ;; We analyze the body within a new environment where all uses are | |
555 | ;; nil, so we can distinguish uses within that function from uses | |
556 | ;; outside of it. | |
557 | (envcopy | |
558 | (mapcar (lambda (vdata) (list (car vdata) nil nil nil nil)) env)) | |
559 | (newenv envcopy)) | |
560 | ;; Push it before recursing, so cconv-freevars-alist contains entries in | |
561 | ;; the order they'll be used by closure-convert-rec. | |
562 | (push freevars cconv-freevars-alist) | |
563 | (dolist (arg args) | |
564 | (cond | |
565 | ((byte-compile-not-lexical-var-p arg) | |
06788a55 | 566 | (byte-compile-log-warning |
a9de04fa SM |
567 | (format "Argument %S is not a lexical variable" arg))) |
568 | ((eq ?& (aref (symbol-name arg) 0)) nil) ;Ignore &rest, &optional, ... | |
569 | (t (let ((varstruct (list arg nil nil nil nil))) | |
570 | (push (cons (list arg) (cdr varstruct)) newvars) | |
571 | (push varstruct newenv))))) | |
572 | (dolist (form body) ;Analyse body forms. | |
573 | (cconv-analyse-form form newenv)) | |
574 | ;; Summarize resulting data about arguments. | |
575 | (dolist (vardata newvars) | |
6c075cd7 | 576 | (cconv--analyse-use vardata parentform "argument")) |
a9de04fa SM |
577 | ;; Transfer uses collected in `envcopy' (via `newenv') back to `env'; |
578 | ;; and compute free variables. | |
579 | (while env | |
580 | (assert (and envcopy (eq (caar env) (caar envcopy)))) | |
581 | (let ((free nil) | |
582 | (x (cdr (car env))) | |
583 | (y (cdr (car envcopy)))) | |
584 | (while x | |
585 | (when (car y) (setcar x t) (setq free t)) | |
586 | (setq x (cdr x) y (cdr y))) | |
587 | (when free | |
588 | (push (caar env) (cdr freevars)) | |
589 | (setf (nth 3 (car env)) t)) | |
590 | (setq env (cdr env) envcopy (cdr envcopy)))))) | |
591 | ||
592 | (defun cconv-analyse-form (form env) | |
593 | "Find mutated variables and variables captured by closure. | |
594 | Analyse lambdas if they are suitable for lambda lifting. | |
d032d5e7 SM |
595 | - FORM is a piece of Elisp code after macroexpansion. |
596 | - ENV is an alist mapping each enclosing lexical variable to its info. | |
a9de04fa SM |
597 | I.e. each element has the form (VAR . (READ MUTATED CAPTURED CALLED)). |
598 | This function does not return anything but instead fills the | |
599 | `cconv-captured+mutated' and `cconv-lambda-candidates' variables | |
600 | and updates the data stored in ENV." | |
94d11cb5 IK |
601 | (pcase form |
602 | ; let special form | |
43e67019 | 603 | (`(,(and (or `let* `let) letsym) ,binders . ,body-forms) |
d779e73c | 604 | |
43e67019 | 605 | (let ((orig-env env) |
a9de04fa | 606 | (newvars nil) |
d779e73c | 607 | (var nil) |
43e67019 SM |
608 | (value nil)) |
609 | (dolist (binder binders) | |
610 | (if (not (consp binder)) | |
d779e73c | 611 | (progn |
43e67019 | 612 | (setq var binder) ; treat the form (let (x) ...) well |
a9de04fa | 613 | (setq binder (list binder)) |
43e67019 SM |
614 | (setq value nil)) |
615 | (setq var (car binder)) | |
616 | (setq value (cadr binder)) | |
617 | ||
a9de04fa | 618 | (cconv-analyse-form value (if (eq letsym 'let*) env orig-env))) |
43e67019 | 619 | |
ce5b520a | 620 | (unless (byte-compile-not-lexical-var-p var) |
a9de04fa SM |
621 | (let ((varstruct (list var nil nil nil nil))) |
622 | (push (cons binder (cdr varstruct)) newvars) | |
623 | (push varstruct env)))) | |
43e67019 | 624 | |
a9de04fa SM |
625 | (dolist (form body-forms) ; Analyse body forms. |
626 | (cconv-analyse-form form env)) | |
43e67019 | 627 | |
a9de04fa | 628 | (dolist (vardata newvars) |
6c075cd7 | 629 | (cconv--analyse-use vardata form "variable")))) |
43e67019 | 630 | |
94d11cb5 | 631 | ; defun special form |
d779e73c | 632 | (`(,(or `defun `defmacro) ,func ,vrs . ,body-forms) |
43e67019 SM |
633 | (when env |
634 | (byte-compile-log-warning | |
635 | (format "Function %S will ignore its context %S" | |
636 | func (mapcar #'car env)) | |
637 | t :warning)) | |
6c075cd7 | 638 | (cconv--analyse-function vrs body-forms nil form)) |
43e67019 SM |
639 | |
640 | (`(function (lambda ,vrs . ,body-forms)) | |
6c075cd7 | 641 | (cconv--analyse-function vrs body-forms env form)) |
ca105506 | 642 | |
43e67019 SM |
643 | (`(setq . ,forms) |
644 | ;; If a local variable (member of env) is modified by setq then | |
645 | ;; it is a mutated variable. | |
d779e73c | 646 | (while forms |
43e67019 | 647 | (let ((v (assq (car forms) env))) ; v = non nil if visible |
a9de04fa SM |
648 | (when v (setf (nth 2 v) t))) |
649 | (cconv-analyse-form (cadr forms) env) | |
43e67019 SM |
650 | (setq forms (cddr forms)))) |
651 | ||
652 | (`((lambda . ,_) . ,_) ; first element is lambda expression | |
d779e73c | 653 | (dolist (exp `((function ,(car form)) . ,(cdr form))) |
a9de04fa | 654 | (cconv-analyse-form exp env))) |
d779e73c SM |
655 | |
656 | (`(cond . ,cond-forms) ; cond special form | |
43e67019 | 657 | (dolist (forms cond-forms) |
d032d5e7 | 658 | (dolist (form forms) (cconv-analyse-form form env)))) |
d779e73c SM |
659 | |
660 | (`(quote . ,_) nil) ; quote form | |
d779e73c SM |
661 | (`(function . ,_) nil) ; same as quote |
662 | ||
43e67019 SM |
663 | (`(condition-case ,var ,protected-form . ,handlers) |
664 | ;; FIXME: The bytecode for condition-case forces us to wrap the | |
ca105506 SM |
665 | ;; form and handlers in closures (for handlers, it's understandable |
666 | ;; but not for the protected form). | |
6c075cd7 | 667 | (cconv--analyse-function () (list protected-form) env form) |
43e67019 | 668 | (dolist (handler handlers) |
6c075cd7 | 669 | (cconv--analyse-function (if var (list var)) (cdr handler) env form))) |
43e67019 SM |
670 | |
671 | ;; FIXME: The bytecode for catch forces us to wrap the body. | |
672 | (`(,(or `catch `unwind-protect) ,form . ,body) | |
a9de04fa | 673 | (cconv-analyse-form form env) |
6c075cd7 | 674 | (cconv--analyse-function () body env form)) |
43e67019 | 675 | |
ca105506 SM |
676 | ;; FIXME: The lack of bytecode for track-mouse forces us to wrap the body. |
677 | ;; `track-mouse' really should be made into a macro. | |
e0f57e65 | 678 | (`(track-mouse . ,body) |
6c075cd7 | 679 | (cconv--analyse-function () body env form)) |
43e67019 SM |
680 | |
681 | (`(,(or `defconst `defvar) ,var ,value . ,_) | |
682 | (push var byte-compile-bound-variables) | |
a9de04fa | 683 | (cconv-analyse-form value env)) |
d779e73c SM |
684 | |
685 | (`(,(or `funcall `apply) ,fun . ,args) | |
43e67019 SM |
686 | ;; Here we ignore fun because funcall and apply are the only two |
687 | ;; functions where we can pass a candidate for lambda lifting as | |
688 | ;; argument. So, if we see fun elsewhere, we'll delete it from | |
689 | ;; lambda candidate list. | |
a9de04fa SM |
690 | (let ((fdata (and (symbolp fun) (assq fun env)))) |
691 | (if fdata | |
692 | (setf (nth 4 fdata) t) | |
693 | (cconv-analyse-form fun env))) | |
d032d5e7 SM |
694 | (dolist (form args) (cconv-analyse-form form env))) |
695 | ||
696 | (`(interactive . ,forms) | |
697 | ;; These appear within the function body but they don't have access | |
698 | ;; to the function's arguments. | |
699 | ;; We could extend this to allow interactive specs to refer to | |
700 | ;; variables in the function's enclosing environment, but it doesn't | |
701 | ;; seem worth the trouble. | |
702 | (dolist (form forms) (cconv-analyse-form form nil))) | |
ba83908c SM |
703 | |
704 | (`(declare . ,_) nil) ;The args don't contain code. | |
ca105506 | 705 | |
43e67019 | 706 | (`(,_ . ,body-forms) ; First element is a function or whatever. |
d032d5e7 | 707 | (dolist (form body-forms) (cconv-analyse-form form env))) |
43e67019 SM |
708 | |
709 | ((pred symbolp) | |
710 | (let ((dv (assq form env))) ; dv = declared and visible | |
711 | (when dv | |
a9de04fa | 712 | (setf (nth 1 dv) t)))))) |
94d11cb5 IK |
713 | |
714 | (provide 'cconv) | |
715 | ;;; cconv.el ends here |