1 | ;;; precompiler.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2008 Peter Graves <peter@armedbear.org> |
---|
4 | ;;; $Id: precompiler.lisp 12804 2010-07-12 21:05:28Z astalla $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | |
---|
32 | (in-package "SYSTEM") |
---|
33 | |
---|
34 | |
---|
35 | (export '(process-optimization-declarations |
---|
36 | inline-p notinline-p inline-expansion expand-inline |
---|
37 | *defined-functions* *undefined-functions* note-name-defined)) |
---|
38 | |
---|
39 | (declaim (ftype (function (t) t) process-optimization-declarations)) |
---|
40 | (defun process-optimization-declarations (forms) |
---|
41 | (dolist (form forms) |
---|
42 | (unless (and (consp form) (eq (%car form) 'DECLARE)) |
---|
43 | (return)) |
---|
44 | (dolist (decl (%cdr form)) |
---|
45 | (case (car decl) |
---|
46 | (OPTIMIZE |
---|
47 | (dolist (spec (%cdr decl)) |
---|
48 | (let ((val 3) |
---|
49 | (quality spec)) |
---|
50 | (when (consp spec) |
---|
51 | (setf quality (%car spec) |
---|
52 | val (cadr spec))) |
---|
53 | (when (and (fixnump val) |
---|
54 | (<= 0 val 3)) |
---|
55 | (case quality |
---|
56 | (speed |
---|
57 | (setf *speed* val)) |
---|
58 | (safety |
---|
59 | (setf *safety* val)) |
---|
60 | (debug |
---|
61 | (setf *debug* val)) |
---|
62 | (space |
---|
63 | (setf *space* val)) |
---|
64 | (compilation-speed) ;; Ignored. |
---|
65 | (t |
---|
66 | (compiler-warn "Ignoring unknown optimization quality ~S in ~S." quality decl))))))) |
---|
67 | ((INLINE NOTINLINE) |
---|
68 | (dolist (symbol (%cdr decl)) |
---|
69 | (push (cons symbol (%car decl)) *inline-declarations*))) |
---|
70 | (:explain |
---|
71 | (dolist (spec (%cdr decl)) |
---|
72 | (let ((val t) |
---|
73 | (quality spec)) |
---|
74 | (when (consp spec) |
---|
75 | (setf quality (%car spec)) |
---|
76 | (when (= (length spec) 2) |
---|
77 | (setf val (%cadr spec)))) |
---|
78 | (if val |
---|
79 | (pushnew quality *explain*) |
---|
80 | (setf *explain* (remove quality *explain*))))))))) |
---|
81 | t) |
---|
82 | |
---|
83 | (declaim (ftype (function (t) t) inline-p)) |
---|
84 | (defun inline-p (name) |
---|
85 | (declare (optimize speed)) |
---|
86 | (let ((entry (assoc name *inline-declarations* :test #'equal))) |
---|
87 | (if entry |
---|
88 | (eq (cdr entry) 'INLINE) |
---|
89 | (and (symbolp name) (eq (get name '%inline) 'INLINE))))) |
---|
90 | |
---|
91 | (declaim (ftype (function (t) t) notinline-p)) |
---|
92 | (defun notinline-p (name) |
---|
93 | (declare (optimize speed)) |
---|
94 | (let ((entry (assoc name *inline-declarations* :test #'equal))) |
---|
95 | (if entry |
---|
96 | (eq (cdr entry) 'NOTINLINE) |
---|
97 | (and (symbolp name) (eq (get name '%inline) 'NOTINLINE))))) |
---|
98 | |
---|
99 | (defun expand-inline (form expansion) |
---|
100 | ;; (format t "expand-inline form = ~S~%" form) |
---|
101 | ;; (format t "expand-inline expansion = ~S~%" expansion) |
---|
102 | (let* ((op (car form)) |
---|
103 | (proclaimed-ftype (proclaimed-ftype op)) |
---|
104 | (args (cdr form)) |
---|
105 | (vars (cadr expansion)) |
---|
106 | (varlist ()) |
---|
107 | new-form) |
---|
108 | ;; (format t "op = ~S proclaimed-ftype = ~S~%" op (proclaimed-ftype op)) |
---|
109 | (do ((vars vars (cdr vars)) |
---|
110 | (args args (cdr args))) |
---|
111 | ((null vars)) |
---|
112 | (push (list (car vars) (car args)) varlist)) |
---|
113 | (setf new-form (list* 'LET (nreverse varlist) |
---|
114 | (copy-tree (cddr expansion)))) |
---|
115 | (when proclaimed-ftype |
---|
116 | (let ((result-type (ftype-result-type proclaimed-ftype))) |
---|
117 | (when (and result-type |
---|
118 | (neq result-type t) |
---|
119 | (neq result-type '*)) |
---|
120 | (setf new-form (list 'TRULY-THE result-type new-form))))) |
---|
121 | ;; (format t "expand-inline new form = ~S~%" new-form) |
---|
122 | new-form)) |
---|
123 | |
---|
124 | (define-compiler-macro assoc (&whole form &rest args) |
---|
125 | (cond ((and (= (length args) 4) |
---|
126 | (eq (third args) :test) |
---|
127 | (or (equal (fourth args) '(quote eq)) |
---|
128 | (equal (fourth args) '(function eq)))) |
---|
129 | `(assq ,(first args) ,(second args))) |
---|
130 | ((= (length args) 2) |
---|
131 | `(assql ,(first args) ,(second args))) |
---|
132 | (t form))) |
---|
133 | |
---|
134 | (define-compiler-macro member (&whole form &rest args) |
---|
135 | (let ((arg1 (first args)) |
---|
136 | (arg2 (second args))) |
---|
137 | (case (length args) |
---|
138 | (2 |
---|
139 | `(memql ,arg1 ,arg2)) |
---|
140 | (4 |
---|
141 | (let ((arg3 (third args)) |
---|
142 | (arg4 (fourth args))) |
---|
143 | (cond ((and (eq arg3 :test) |
---|
144 | (or (equal arg4 '(quote eq)) |
---|
145 | (equal arg4 '(function eq)))) |
---|
146 | `(memq ,arg1 ,arg2)) |
---|
147 | ((and (eq arg3 :test) |
---|
148 | (or (equal arg4 '(quote eql)) |
---|
149 | (equal arg4 '(function eql)) |
---|
150 | (equal arg4 '(quote char=)) |
---|
151 | (equal arg4 '(function char=)))) |
---|
152 | `(memql ,arg1 ,arg2)) |
---|
153 | (t |
---|
154 | form)))) |
---|
155 | (t |
---|
156 | form)))) |
---|
157 | |
---|
158 | (define-compiler-macro search (&whole form &rest args) |
---|
159 | (if (= (length args) 2) |
---|
160 | `(simple-search ,@args) |
---|
161 | form)) |
---|
162 | |
---|
163 | (define-compiler-macro identity (&whole form &rest args) |
---|
164 | (if (= (length args) 1) |
---|
165 | `(progn ,(car args)) |
---|
166 | form)) |
---|
167 | |
---|
168 | (defun quoted-form-p (form) |
---|
169 | (and (consp form) (eq (%car form) 'QUOTE) (= (length form) 2))) |
---|
170 | |
---|
171 | (define-compiler-macro eql (&whole form &rest args) |
---|
172 | (let ((first (car args)) |
---|
173 | (second (cadr args))) |
---|
174 | (if (or (and (quoted-form-p first) (symbolp (cadr first))) |
---|
175 | (and (quoted-form-p second) (symbolp (cadr second)))) |
---|
176 | `(eq ,first ,second) |
---|
177 | form))) |
---|
178 | |
---|
179 | (define-compiler-macro not (&whole form arg) |
---|
180 | (if (atom arg) |
---|
181 | form |
---|
182 | (let ((op (case (car arg) |
---|
183 | (>= '<) |
---|
184 | (< '>=) |
---|
185 | (<= '>) |
---|
186 | (> '<=) |
---|
187 | (t nil)))) |
---|
188 | (if (and op (= (length arg) 3)) |
---|
189 | (cons op (cdr arg)) |
---|
190 | form)))) |
---|
191 | |
---|
192 | (defun predicate-for-type (type) |
---|
193 | (cdr (assq type '((ARRAY . arrayp) |
---|
194 | (ATOM . atom) |
---|
195 | (BIT-VECTOR . bit-vector-p) |
---|
196 | (CHARACTER . characterp) |
---|
197 | (COMPLEX . complexp) |
---|
198 | (CONS . consp) |
---|
199 | (FIXNUM . fixnump) |
---|
200 | (FLOAT . floatp) |
---|
201 | (FUNCTION . functionp) |
---|
202 | (HASH-TABLE . hash-table-p) |
---|
203 | (INTEGER . integerp) |
---|
204 | (LIST . listp) |
---|
205 | (NULL . null) |
---|
206 | (NUMBER . numberp) |
---|
207 | (NUMBER . numberp) |
---|
208 | (PACKAGE . packagep) |
---|
209 | (RATIONAL . rationalp) |
---|
210 | (REAL . realp) |
---|
211 | (SIMPLE-BIT-VECTOR . simple-bit-vector-p) |
---|
212 | (SIMPLE-STRING . simple-string-p) |
---|
213 | (SIMPLE-VECTOR . simple-vector-p) |
---|
214 | (STREAM . streamp) |
---|
215 | (STRING . stringp) |
---|
216 | (SYMBOL . symbolp))))) |
---|
217 | |
---|
218 | (define-compiler-macro typep (&whole form &rest args) |
---|
219 | (if (= (length args) 2) ; no environment arg |
---|
220 | (let* ((object (%car args)) |
---|
221 | (type-specifier (%cadr args)) |
---|
222 | (type (and (consp type-specifier) |
---|
223 | (eq (%car type-specifier) 'QUOTE) |
---|
224 | (%cadr type-specifier))) |
---|
225 | (predicate (and type (predicate-for-type type)))) |
---|
226 | (if predicate |
---|
227 | `(,predicate ,object) |
---|
228 | `(%typep ,@args))) |
---|
229 | form)) |
---|
230 | |
---|
231 | (define-compiler-macro subtypep (&whole form &rest args) |
---|
232 | (if (= (length args) 2) |
---|
233 | `(%subtypep ,@args) |
---|
234 | form)) |
---|
235 | |
---|
236 | (define-compiler-macro funcall (&whole form |
---|
237 | &environment env &rest args) |
---|
238 | (let ((callee (car args))) |
---|
239 | (if (and (>= *speed* *debug*) |
---|
240 | (consp callee) |
---|
241 | (eq (%car callee) 'function) |
---|
242 | (symbolp (cadr callee)) |
---|
243 | (not (special-operator-p (cadr callee))) |
---|
244 | (not (macro-function (cadr callee) env)) |
---|
245 | (memq (symbol-package (cadr callee)) |
---|
246 | (list (find-package "CL") (find-package "SYS")))) |
---|
247 | `(,(cadr callee) ,@(cdr args)) |
---|
248 | form))) |
---|
249 | |
---|
250 | (define-compiler-macro byte (size position) |
---|
251 | `(cons ,size ,position)) |
---|
252 | |
---|
253 | (define-compiler-macro byte-size (bytespec) |
---|
254 | `(car ,bytespec)) |
---|
255 | |
---|
256 | (define-compiler-macro byte-position (bytespec) |
---|
257 | `(cdr ,bytespec)) |
---|
258 | |
---|
259 | (define-source-transform concatenate (&whole form result-type &rest sequences) |
---|
260 | (if (equal result-type '(quote STRING)) |
---|
261 | `(sys::concatenate-to-string (list ,@sequences)) |
---|
262 | form)) |
---|
263 | |
---|
264 | (define-source-transform ldb (&whole form bytespec integer) |
---|
265 | (if (and (consp bytespec) |
---|
266 | (eq (%car bytespec) 'byte) |
---|
267 | (= (length bytespec) 3)) |
---|
268 | (let ((size (%cadr bytespec)) |
---|
269 | (position (%caddr bytespec))) |
---|
270 | `(%ldb ,size ,position ,integer)) |
---|
271 | form)) |
---|
272 | |
---|
273 | (define-source-transform find (&whole form item sequence &key from-end test test-not start end key) |
---|
274 | (cond ((and (>= (length form) 3) (null start) (null end)) |
---|
275 | (cond ((and (stringp sequence) |
---|
276 | (null from-end) |
---|
277 | (member test '(#'eql #'char=) :test #'equal) |
---|
278 | (null test-not) |
---|
279 | (null key)) |
---|
280 | `(string-find ,item ,sequence)) |
---|
281 | (t |
---|
282 | (let ((item-var (gensym)) |
---|
283 | (seq-var (gensym))) |
---|
284 | `(let ((,item-var ,item) |
---|
285 | (,seq-var ,sequence)) |
---|
286 | (if (listp ,seq-var) |
---|
287 | (list-find* ,item-var ,seq-var ,from-end ,test ,test-not 0 (length ,seq-var) ,key) |
---|
288 | (vector-find* ,item-var ,seq-var ,from-end ,test ,test-not 0 (length ,seq-var) ,key))))))) |
---|
289 | (t |
---|
290 | form))) |
---|
291 | |
---|
292 | (define-source-transform adjoin (&whole form &rest args) |
---|
293 | (if (= (length args) 2) |
---|
294 | `(adjoin-eql ,(first args) ,(second args)) |
---|
295 | form)) |
---|
296 | |
---|
297 | (define-source-transform format (&whole form &rest args) |
---|
298 | (if (stringp (second args)) |
---|
299 | `(format ,(pop args) (formatter ,(pop args)) ,@args) |
---|
300 | form)) |
---|
301 | |
---|
302 | (define-compiler-macro catch (&whole form tag &rest args) |
---|
303 | (declare (ignore tag)) |
---|
304 | (if (and (null (cdr args)) |
---|
305 | (constantp (car args))) |
---|
306 | (car args) |
---|
307 | form)) |
---|
308 | |
---|
309 | (define-compiler-macro string= (&whole form &rest args) |
---|
310 | (if (= (length args) 2) |
---|
311 | `(sys::%%string= ,@args) |
---|
312 | form)) |
---|
313 | |
---|
314 | (define-compiler-macro <= (&whole form &rest args) |
---|
315 | (cond ((and (= (length args) 3) |
---|
316 | (numberp (first args)) |
---|
317 | (numberp (third args)) |
---|
318 | (= (first args) (third args))) |
---|
319 | `(= ,(second args) ,(first args))) |
---|
320 | (t |
---|
321 | form))) |
---|
322 | |
---|
323 | |
---|
324 | (in-package "PRECOMPILER") |
---|
325 | |
---|
326 | ;; No source-transforms and inlining in precompile-function-call |
---|
327 | ;; No macro expansion in precompile-dolist and precompile-dotimes |
---|
328 | ;; No macro expansion in precompile-do/do* |
---|
329 | ;; No macro expansion in precompile-defun |
---|
330 | ;; Special precompilation in precompile-case and precompile-cond |
---|
331 | ;; Special precompilation in precompile-when and precompile-unless |
---|
332 | ;; No precompilation in precompile-nth-value |
---|
333 | ;; Special precompilation in precompile-return |
---|
334 | ;; Special precompilation in expand-macro |
---|
335 | ;; |
---|
336 | ;; if *in-jvm-compile* is false |
---|
337 | |
---|
338 | (defvar *in-jvm-compile* nil) |
---|
339 | (defvar *precompile-env* nil) |
---|
340 | |
---|
341 | |
---|
342 | (declaim (ftype (function (t) t) precompile1)) |
---|
343 | (defun precompile1 (form) |
---|
344 | (cond ((symbolp form) |
---|
345 | (multiple-value-bind |
---|
346 | (expansion expanded) |
---|
347 | (expand-macro form) |
---|
348 | (if expanded |
---|
349 | (precompile1 expansion) |
---|
350 | form))) |
---|
351 | ((atom form) |
---|
352 | form) |
---|
353 | (t |
---|
354 | (let ((op (%car form)) |
---|
355 | handler) |
---|
356 | (when (symbolp op) |
---|
357 | (cond ((setf handler (get op 'precompile-handler)) |
---|
358 | (return-from precompile1 (funcall handler form))) |
---|
359 | ((macro-function op *precompile-env*) |
---|
360 | (return-from precompile1 (precompile1 (expand-macro form)))) |
---|
361 | ((special-operator-p op) |
---|
362 | (error "PRECOMPILE1: unsupported special operator ~S." op)))) |
---|
363 | (precompile-function-call form))))) |
---|
364 | |
---|
365 | (defun precompile-identity (form) |
---|
366 | (declare (optimize speed)) |
---|
367 | form) |
---|
368 | |
---|
369 | (declaim (ftype (function (t) cons) precompile-cons)) |
---|
370 | (defun precompile-cons (form) |
---|
371 | (cons (car form) (mapcar #'precompile1 (cdr form)))) |
---|
372 | |
---|
373 | (declaim (ftype (function (t t) t) precompile-function-call)) |
---|
374 | (defun precompile-function-call (form) |
---|
375 | (let ((op (car form))) |
---|
376 | (when (and (consp op) (eq (%car op) 'LAMBDA)) |
---|
377 | (return-from precompile-function-call |
---|
378 | (cons (precompile-lambda op) |
---|
379 | (mapcar #'precompile1 (cdr form))))) |
---|
380 | (when (or (not *in-jvm-compile*) (notinline-p op)) |
---|
381 | (return-from precompile-function-call (precompile-cons form))) |
---|
382 | (when (source-transform op) |
---|
383 | (let ((new-form (expand-source-transform form))) |
---|
384 | (when (neq new-form form) |
---|
385 | (return-from precompile-function-call (precompile1 new-form))))) |
---|
386 | (when *enable-inline-expansion* |
---|
387 | (let ((expansion (inline-expansion op))) |
---|
388 | (when expansion |
---|
389 | (let ((explain *explain*)) |
---|
390 | (when (and explain (memq :calls explain)) |
---|
391 | (format t "; inlining call to ~S~%" op))) |
---|
392 | (return-from precompile-function-call (precompile1 (expand-inline form expansion)))))) |
---|
393 | (cons op (mapcar #'precompile1 (cdr form))))) |
---|
394 | |
---|
395 | (defun precompile-locally (form) |
---|
396 | (let ((*inline-declarations* *inline-declarations*)) |
---|
397 | (process-optimization-declarations (cdr form)) |
---|
398 | (cons 'LOCALLY (mapcar #'precompile1 (cdr form))))) |
---|
399 | |
---|
400 | (defun precompile-block (form) |
---|
401 | (let ((args (cdr form))) |
---|
402 | (if (null (cdr args)) |
---|
403 | nil |
---|
404 | (list* 'BLOCK (car args) (mapcar #'precompile1 (cdr args)))))) |
---|
405 | |
---|
406 | (defun precompile-dolist (form) |
---|
407 | (if *in-jvm-compile* |
---|
408 | (precompile1 (macroexpand form *precompile-env*)) |
---|
409 | (cons 'DOLIST (cons (mapcar #'precompile1 (cadr form)) |
---|
410 | (mapcar #'precompile1 (cddr form)))))) |
---|
411 | |
---|
412 | (defun precompile-dotimes (form) |
---|
413 | (if *in-jvm-compile* |
---|
414 | (precompile1 (macroexpand form *precompile-env*)) |
---|
415 | (cons 'DOTIMES (cons (mapcar #'precompile1 (cadr form)) |
---|
416 | (mapcar #'precompile1 (cddr form)))))) |
---|
417 | |
---|
418 | (defun precompile-do/do*-vars (varlist) |
---|
419 | (let ((result nil)) |
---|
420 | (dolist (varspec varlist) |
---|
421 | (if (atom varspec) |
---|
422 | (push varspec result) |
---|
423 | (case (length varspec) |
---|
424 | (1 |
---|
425 | (push (%car varspec) result)) |
---|
426 | (2 |
---|
427 | (let* ((var (%car varspec)) |
---|
428 | (init-form (%cadr varspec))) |
---|
429 | (unless (symbolp var) |
---|
430 | (error 'type-error)) |
---|
431 | (push (list var (precompile1 init-form)) |
---|
432 | result))) |
---|
433 | (3 |
---|
434 | (let* ((var (%car varspec)) |
---|
435 | (init-form (%cadr varspec)) |
---|
436 | (step-form (%caddr varspec))) |
---|
437 | (unless (symbolp var) |
---|
438 | (error 'type-error)) |
---|
439 | (push (list var (precompile1 init-form) (precompile1 step-form)) |
---|
440 | result)))))) |
---|
441 | (nreverse result))) |
---|
442 | |
---|
443 | (defun precompile-do/do*-end-form (end-form) |
---|
444 | (let ((end-test-form (car end-form)) |
---|
445 | (result-forms (cdr end-form))) |
---|
446 | (list* end-test-form (mapcar #'precompile1 result-forms)))) |
---|
447 | |
---|
448 | (defun precompile-do/do* (form) |
---|
449 | (if *in-jvm-compile* |
---|
450 | (precompile1 (macroexpand form *precompile-env*)) |
---|
451 | (list* (car form) |
---|
452 | (precompile-do/do*-vars (cadr form)) |
---|
453 | (precompile-do/do*-end-form (caddr form)) |
---|
454 | (mapcar #'precompile1 (cdddr form))))) |
---|
455 | |
---|
456 | (defun precompile-do-symbols (form) |
---|
457 | (list* (car form) (cadr form) (mapcar #'precompile1 (cddr form)))) |
---|
458 | |
---|
459 | (defun precompile-load-time-value (form) |
---|
460 | form) |
---|
461 | |
---|
462 | (defun precompile-progn (form) |
---|
463 | (let ((body (cdr form))) |
---|
464 | (if (eql (length body) 1) |
---|
465 | (let ((res (precompile1 (%car body)))) |
---|
466 | ;; If the result turns out to be a bare symbol, leave it wrapped |
---|
467 | ;; with PROGN so it won't be mistaken for a tag in an enclosing |
---|
468 | ;; TAGBODY. |
---|
469 | (if (symbolp res) |
---|
470 | (list 'progn res) |
---|
471 | res)) |
---|
472 | (cons 'PROGN (mapcar #'precompile1 body))))) |
---|
473 | |
---|
474 | (defun precompile-threads-synchronized-on (form) |
---|
475 | (cons 'threads:synchronized-on (mapcar #'precompile1 (cdr form)))) |
---|
476 | |
---|
477 | (defun precompile-progv (form) |
---|
478 | (if (< (length form) 3) |
---|
479 | (error "Not enough arguments for ~S." 'progv) |
---|
480 | (list* 'PROGV (mapcar #'precompile1 (%cdr form))))) |
---|
481 | |
---|
482 | (defun precompile-setf (form) |
---|
483 | (let ((place (second form))) |
---|
484 | (cond ((and (consp place) |
---|
485 | (eq (%car place) 'VALUES)) |
---|
486 | (setf form |
---|
487 | (list* 'SETF |
---|
488 | (list* 'VALUES |
---|
489 | (mapcar #'precompile1 (%cdr place))) |
---|
490 | (cddr form))) |
---|
491 | (precompile1 (expand-macro form))) |
---|
492 | ((symbolp place) |
---|
493 | (multiple-value-bind |
---|
494 | (expansion expanded) |
---|
495 | (expand-macro place) |
---|
496 | (if expanded |
---|
497 | (precompile1 (list* 'SETF expansion |
---|
498 | (cddr form))) |
---|
499 | (precompile1 (expand-macro form))))) |
---|
500 | (t |
---|
501 | (precompile1 (expand-macro form)))))) |
---|
502 | |
---|
503 | (defun precompile-setq (form) |
---|
504 | (let* ((args (cdr form)) |
---|
505 | (len (length args))) |
---|
506 | (when (oddp len) |
---|
507 | (error 'simple-program-error |
---|
508 | :format-control "Odd number of arguments to SETQ.")) |
---|
509 | (if (= len 2) |
---|
510 | (let* ((sym (%car args)) |
---|
511 | (val (%cadr args))) |
---|
512 | (multiple-value-bind |
---|
513 | (expansion expanded) |
---|
514 | (expand-macro sym) |
---|
515 | (if expanded |
---|
516 | (precompile1 (list 'SETF expansion val)) |
---|
517 | (list 'SETQ sym (precompile1 val))))) |
---|
518 | (let ((result ())) |
---|
519 | (loop |
---|
520 | (when (null args) |
---|
521 | (return)) |
---|
522 | (push (precompile-setq (list 'SETQ (car args) (cadr args))) result) |
---|
523 | (setq args (cddr args))) |
---|
524 | (setq result (nreverse result)) |
---|
525 | (push 'PROGN result) |
---|
526 | result)))) |
---|
527 | |
---|
528 | (defun precompile-psetf (form) |
---|
529 | (setf form |
---|
530 | (list* 'PSETF |
---|
531 | (mapcar #'precompile1 (cdr form)))) |
---|
532 | (precompile1 (expand-macro form))) |
---|
533 | |
---|
534 | (defun precompile-psetq (form) |
---|
535 | ;; Make sure all the vars are symbols. |
---|
536 | (do* ((rest (cdr form) (cddr rest)) |
---|
537 | (var (car rest))) |
---|
538 | ((null rest)) |
---|
539 | (unless (symbolp var) |
---|
540 | (error 'simple-error |
---|
541 | :format-control "~S is not a symbol." |
---|
542 | :format-arguments (list var)))) |
---|
543 | ;; Delegate to PRECOMPILE-PSETF so symbol macros are handled correctly. |
---|
544 | (precompile-psetf form)) |
---|
545 | |
---|
546 | |
---|
547 | (defun precompile-lambda-list (form) |
---|
548 | (let (new aux-tail) |
---|
549 | (dolist (arg form (nreverse new)) |
---|
550 | (if (or (atom arg) (> 2 (length arg))) |
---|
551 | (progn |
---|
552 | (when (eq arg '&aux) |
---|
553 | (setf aux-tail t)) |
---|
554 | (push arg new)) |
---|
555 | ;; must be a cons of more than 1 cell |
---|
556 | (let ((new-arg (copy-list arg))) |
---|
557 | (unless (<= 1 (length arg) (if aux-tail 2 3)) |
---|
558 | ;; the aux-vars have a maximum length of 2 conses |
---|
559 | ;; optional and key vars may have 3 |
---|
560 | (error 'program-error |
---|
561 | :format-control |
---|
562 | "The ~A binding specification ~S is invalid." |
---|
563 | :format-arguments (list (if aux-tail "&AUX" |
---|
564 | "&OPTIONAL/&KEY") arg))) |
---|
565 | (setf (second new-arg) |
---|
566 | (precompile1 (second arg))) |
---|
567 | (push new-arg new)))))) |
---|
568 | |
---|
569 | (defun extract-lambda-vars (lambda-list) |
---|
570 | (let ((state :required) |
---|
571 | vars) |
---|
572 | (dolist (var/key lambda-list vars) |
---|
573 | (cond |
---|
574 | ((eq '&aux var/key) (setf state :aux)) |
---|
575 | ((eq '&key var/key) (setf state :key)) |
---|
576 | ((eq '&optional var/key) (setf state :optional)) |
---|
577 | ((eq '&rest var/key) (setf state :rest)) |
---|
578 | ((symbolp var/key) (unless (eq var/key '&allow-other-keys) |
---|
579 | (push var/key vars))) |
---|
580 | ((and (consp var/key) |
---|
581 | (member state '(:optional :key))) |
---|
582 | (setf var/key (car var/key)) |
---|
583 | (when (and (consp var/key) (eq state :key)) |
---|
584 | (setf var/key (second var/key))) |
---|
585 | (if (symbolp var/key) |
---|
586 | (push var/key vars) |
---|
587 | (error 'program-error |
---|
588 | :format-control |
---|
589 | "Unexpected ~A variable specifier ~A." |
---|
590 | :format-arguments (list state var/key)))) |
---|
591 | ((and (consp var/key) (eq state :aux)) |
---|
592 | (if (symbolp (car var/key)) |
---|
593 | (push (car var/key) vars) |
---|
594 | (error 'program-error |
---|
595 | :format-control "Unexpected &AUX format for ~A." |
---|
596 | :format-arguments (list var/key)))) |
---|
597 | (t |
---|
598 | (error 'program-error |
---|
599 | :format-control "Unexpected lambda-list format: ~A." |
---|
600 | :format-arguments (list lambda-list))))))) |
---|
601 | |
---|
602 | (defun precompile-lambda (form) |
---|
603 | (let ((body (cddr form)) |
---|
604 | (precompiled-lambda-list |
---|
605 | (precompile-lambda-list (cadr form))) |
---|
606 | (*inline-declarations* *inline-declarations*) |
---|
607 | (*precompile-env* (make-environment *precompile-env*))) |
---|
608 | (process-optimization-declarations body) |
---|
609 | (dolist (var (extract-lambda-vars precompiled-lambda-list)) |
---|
610 | (environment-add-symbol-binding *precompile-env* var nil)) |
---|
611 | (list* 'LAMBDA precompiled-lambda-list |
---|
612 | (mapcar #'precompile1 body)))) |
---|
613 | |
---|
614 | (defun precompile-named-lambda (form) |
---|
615 | (let ((lambda-form (list* 'LAMBDA (caddr form) (cdddr form)))) |
---|
616 | (let ((body (cddr lambda-form)) |
---|
617 | (precompiled-lambda-list |
---|
618 | (precompile-lambda-list (cadr lambda-form))) |
---|
619 | (*inline-declarations* *inline-declarations*) |
---|
620 | (*precompile-env* (make-environment *precompile-env*))) |
---|
621 | (process-optimization-declarations body) |
---|
622 | (dolist (var (extract-lambda-vars precompiled-lambda-list)) |
---|
623 | (environment-add-symbol-binding *precompile-env* var nil)) |
---|
624 | (list* 'NAMED-LAMBDA (cadr form) precompiled-lambda-list |
---|
625 | (mapcar #'precompile1 body))))) |
---|
626 | |
---|
627 | (defun precompile-defun (form) |
---|
628 | (if *in-jvm-compile* |
---|
629 | (precompile1 (expand-macro form)) |
---|
630 | form)) |
---|
631 | |
---|
632 | (defun precompile-macrolet (form) |
---|
633 | (let ((*precompile-env* (make-environment *precompile-env*))) |
---|
634 | (dolist (definition (cadr form)) |
---|
635 | (environment-add-macro-definition |
---|
636 | *precompile-env* |
---|
637 | (car definition) |
---|
638 | (make-macro (car definition) |
---|
639 | (make-closure |
---|
640 | (make-expander-for-macrolet definition) |
---|
641 | NIL)))) |
---|
642 | (multiple-value-bind (body decls) |
---|
643 | (parse-body (cddr form) nil) |
---|
644 | `(locally ,@decls ,@(mapcar #'precompile1 body))))) |
---|
645 | |
---|
646 | (defun precompile-symbol-macrolet (form) |
---|
647 | (let ((*precompile-env* (make-environment *precompile-env*)) |
---|
648 | (defs (cadr form))) |
---|
649 | (dolist (def defs) |
---|
650 | (let ((sym (car def)) |
---|
651 | (expansion (cadr def))) |
---|
652 | (when (special-variable-p sym) |
---|
653 | (error 'program-error |
---|
654 | :format-control |
---|
655 | "Attempt to bind the special variable ~S with SYMBOL-MACROLET." |
---|
656 | :format-arguments (list sym))) |
---|
657 | (environment-add-symbol-binding *precompile-env* |
---|
658 | sym |
---|
659 | (sys::make-symbol-macro expansion)))) |
---|
660 | (multiple-value-bind (body decls) |
---|
661 | (parse-body (cddr form) nil) |
---|
662 | (when decls |
---|
663 | (let ((specials ())) |
---|
664 | (dolist (decl decls) |
---|
665 | (when (eq (car decl) 'DECLARE) |
---|
666 | (dolist (declspec (cdr decl)) |
---|
667 | (when (eq (car declspec) 'SPECIAL) |
---|
668 | (setf specials (append specials (cdr declspec))))))) |
---|
669 | (when specials |
---|
670 | (let ((syms (mapcar #'car (cadr form)))) |
---|
671 | (dolist (special specials) |
---|
672 | (when (memq special syms) |
---|
673 | (error 'program-error |
---|
674 | :format-control |
---|
675 | "~S is a symbol-macro and may not be declared special." |
---|
676 | :format-arguments (list special)))))))) |
---|
677 | `(locally ,@decls ,@(mapcar #'precompile1 body))))) |
---|
678 | |
---|
679 | (defun precompile-the (form) |
---|
680 | (list 'THE |
---|
681 | (second form) |
---|
682 | (precompile1 (third form)))) |
---|
683 | |
---|
684 | (defun precompile-truly-the (form) |
---|
685 | (list 'TRULY-THE |
---|
686 | (second form) |
---|
687 | (precompile1 (third form)))) |
---|
688 | |
---|
689 | (defun precompile-let/let*-vars (vars) |
---|
690 | (let ((result nil)) |
---|
691 | (dolist (var vars) |
---|
692 | (cond ((consp var) |
---|
693 | (unless (<= 1 (length var) 2) |
---|
694 | (error 'program-error |
---|
695 | :format-control |
---|
696 | "The LET/LET* binding specification ~S is invalid." |
---|
697 | :format-arguments (list var))) |
---|
698 | (let ((v (%car var)) |
---|
699 | (expr (cadr var))) |
---|
700 | (unless (symbolp v) |
---|
701 | (error 'simple-type-error |
---|
702 | :format-control "The variable ~S is not a symbol." |
---|
703 | :format-arguments (list v))) |
---|
704 | (push (list v (precompile1 expr)) result) |
---|
705 | (environment-add-symbol-binding *precompile-env* v nil))) |
---|
706 | ;; any value will do: we just need to shadow any symbol macros |
---|
707 | (t |
---|
708 | (push var result) |
---|
709 | (environment-add-symbol-binding *precompile-env* var nil)))) |
---|
710 | (nreverse result))) |
---|
711 | |
---|
712 | (defun precompile-let (form) |
---|
713 | (let ((*precompile-env* (make-environment *precompile-env*))) |
---|
714 | (list* 'LET |
---|
715 | (precompile-let/let*-vars (cadr form)) |
---|
716 | (mapcar #'precompile1 (cddr form))))) |
---|
717 | |
---|
718 | ;; (LET* ((X 1)) (LET* ((Y 2)) (LET* ((Z 3)) (+ X Y Z)))) => |
---|
719 | ;; (LET* ((X 1) (Y 2) (Z 3)) (+ X Y Z)) |
---|
720 | (defun maybe-fold-let* (form) |
---|
721 | (if (and (= (length form) 3) |
---|
722 | (consp (%caddr form)) |
---|
723 | (eq (%car (%caddr form)) 'LET*)) |
---|
724 | (let ((third (maybe-fold-let* (%caddr form)))) |
---|
725 | (list* 'LET* (append (%cadr form) (cadr third)) (cddr third))) |
---|
726 | form)) |
---|
727 | |
---|
728 | (defun precompile-let* (form) |
---|
729 | (setf form (maybe-fold-let* form)) |
---|
730 | (let ((*precompile-env* (make-environment *precompile-env*))) |
---|
731 | (list* 'LET* |
---|
732 | (precompile-let/let*-vars (cadr form)) |
---|
733 | (mapcar #'precompile1 (cddr form))))) |
---|
734 | |
---|
735 | (defun precompile-case (form) |
---|
736 | (if *in-jvm-compile* |
---|
737 | (precompile1 (macroexpand form *precompile-env*)) |
---|
738 | (let* ((keyform (cadr form)) |
---|
739 | (clauses (cddr form)) |
---|
740 | (result (list (precompile1 keyform)))) |
---|
741 | (dolist (clause clauses) |
---|
742 | (push (precompile-case-clause clause) result)) |
---|
743 | (cons (car form) (nreverse result))))) |
---|
744 | |
---|
745 | (defun precompile-case-clause (clause) |
---|
746 | (let ((keys (car clause)) |
---|
747 | (forms (cdr clause))) |
---|
748 | (cons keys (mapcar #'precompile1 forms)))) |
---|
749 | |
---|
750 | (defun precompile-cond (form) |
---|
751 | (if *in-jvm-compile* |
---|
752 | (precompile1 (macroexpand form *precompile-env*)) |
---|
753 | (let ((clauses (cdr form)) |
---|
754 | (result nil)) |
---|
755 | (dolist (clause clauses) |
---|
756 | (push (precompile-cond-clause clause) result)) |
---|
757 | (cons 'COND (nreverse result))))) |
---|
758 | |
---|
759 | (defun precompile-cond-clause (clause) |
---|
760 | (let ((test (car clause)) |
---|
761 | (forms (cdr clause))) |
---|
762 | (cons (precompile1 test) (mapcar #'precompile1 forms)))) |
---|
763 | |
---|
764 | (defun precompile-local-function-def (def) |
---|
765 | (let ((name (car def)) |
---|
766 | (body (cddr def))) |
---|
767 | ;; Macro names are shadowed by local functions. |
---|
768 | (environment-add-function-definition *precompile-env* name body) |
---|
769 | (cdr (precompile-named-lambda (list* 'NAMED-LAMBDA def))))) |
---|
770 | |
---|
771 | (defun precompile-local-functions (defs) |
---|
772 | (let ((result nil)) |
---|
773 | (dolist (def defs (nreverse result)) |
---|
774 | (push (precompile-local-function-def def) result)))) |
---|
775 | |
---|
776 | (defun find-use (name expression) |
---|
777 | (cond ((atom expression) |
---|
778 | nil) |
---|
779 | ((eq (%car expression) name) |
---|
780 | t) |
---|
781 | ((consp name) |
---|
782 | t) ;; FIXME Recognize use of SETF functions! |
---|
783 | (t |
---|
784 | (or (find-use name (%car expression)) |
---|
785 | (find-use name (%cdr expression)))))) |
---|
786 | |
---|
787 | (defun precompile-flet/labels (form) |
---|
788 | (let ((*precompile-env* (make-environment *precompile-env*)) |
---|
789 | (operator (car form)) |
---|
790 | (locals (cadr form)) |
---|
791 | body) |
---|
792 | ;; first augment the environment with the newly-defined local functions |
---|
793 | ;; to shadow preexisting macro definitions with the same names |
---|
794 | (dolist (local locals) |
---|
795 | (environment-add-function-definition *precompile-env* |
---|
796 | (car local) (cddr local))) |
---|
797 | ;; then precompile (thus macro-expand) the body before inspecting it |
---|
798 | ;; for the use of our locals and optimizing them away |
---|
799 | (setq body (mapcar #'precompile1 (cddr form))) |
---|
800 | (dolist (local locals) |
---|
801 | (let* ((name (car local)) |
---|
802 | (used-p (find-use name body))) |
---|
803 | (unless used-p |
---|
804 | (when (eq operator 'LABELS) |
---|
805 | (dolist (local locals) |
---|
806 | (when (neq name (car local)) |
---|
807 | (when (find-use name (cddr local)) |
---|
808 | (setf used-p t) |
---|
809 | (return)) |
---|
810 | ;; Scope of defined function names includes |
---|
811 | ;; &OPTIONAL, &KEY and &AUX parameters |
---|
812 | ;; (LABELS.7B, LABELS.7C and LABELS.7D). |
---|
813 | (let ((vars (or |
---|
814 | (cdr (memq '&optional (cadr local))) |
---|
815 | (cdr (memq '&key (cadr local))) |
---|
816 | (cdr (memq '&aux (cadr local)))))) |
---|
817 | (when (and vars (find-use name vars) |
---|
818 | (setf used-p t) |
---|
819 | (return)))))))) |
---|
820 | (unless used-p |
---|
821 | (format t "; Note: deleting unused local function ~A ~S~%" |
---|
822 | operator name) |
---|
823 | (let* ((new-locals (remove local locals :test 'eq)) |
---|
824 | (new-form |
---|
825 | (if new-locals |
---|
826 | (list* operator new-locals body) |
---|
827 | (list* 'LOCALLY body)))) |
---|
828 | (return-from precompile-flet/labels (precompile1 new-form)))))) |
---|
829 | (list* (car form) |
---|
830 | (precompile-local-functions locals) |
---|
831 | body))) |
---|
832 | |
---|
833 | (defun precompile-function (form) |
---|
834 | (if (and (consp (cadr form)) (eq (caadr form) 'LAMBDA)) |
---|
835 | (list 'FUNCTION (precompile-lambda (%cadr form))) |
---|
836 | form)) |
---|
837 | |
---|
838 | (defun precompile-if (form) |
---|
839 | (let ((args (cdr form))) |
---|
840 | (case (length args) |
---|
841 | (2 |
---|
842 | (let ((test (precompile1 (%car args)))) |
---|
843 | (cond ((null test) |
---|
844 | nil) |
---|
845 | (;;(constantp test) |
---|
846 | (eq test t) |
---|
847 | (precompile1 (%cadr args))) |
---|
848 | (t |
---|
849 | (list 'IF |
---|
850 | test |
---|
851 | (precompile1 (%cadr args))))))) |
---|
852 | (3 |
---|
853 | (let ((test (precompile1 (%car args)))) |
---|
854 | (cond ((null test) |
---|
855 | (precompile1 (%caddr args))) |
---|
856 | (;;(constantp test) |
---|
857 | (eq test t) |
---|
858 | (precompile1 (%cadr args))) |
---|
859 | (t |
---|
860 | (list 'IF |
---|
861 | test |
---|
862 | (precompile1 (%cadr args)) |
---|
863 | (precompile1 (%caddr args))))))) |
---|
864 | (t |
---|
865 | (error "wrong number of arguments for IF"))))) |
---|
866 | |
---|
867 | (defun precompile-when (form) |
---|
868 | (if *in-jvm-compile* |
---|
869 | (precompile1 (macroexpand form *precompile-env*)) |
---|
870 | (precompile-cons form))) |
---|
871 | |
---|
872 | (defun precompile-unless (form) |
---|
873 | (if *in-jvm-compile* |
---|
874 | (precompile1 (macroexpand form *precompile-env*)) |
---|
875 | (precompile-cons form))) |
---|
876 | |
---|
877 | ;; MULTIPLE-VALUE-BIND is handled explicitly by the JVM compiler. |
---|
878 | (defun precompile-multiple-value-bind (form) |
---|
879 | (let ((vars (cadr form)) |
---|
880 | (values-form (caddr form)) |
---|
881 | (body (cdddr form)) |
---|
882 | (*precompile-env* (make-environment *precompile-env*))) |
---|
883 | (dolist (var vars) |
---|
884 | (environment-add-symbol-binding *precompile-env* var nil)) |
---|
885 | (list* 'MULTIPLE-VALUE-BIND |
---|
886 | vars |
---|
887 | (precompile1 values-form) |
---|
888 | (mapcar #'precompile1 body)))) |
---|
889 | |
---|
890 | ;; MULTIPLE-VALUE-LIST is handled explicitly by the JVM compiler. |
---|
891 | (defun precompile-multiple-value-list (form) |
---|
892 | (list 'MULTIPLE-VALUE-LIST (precompile1 (cadr form)))) |
---|
893 | |
---|
894 | (defun precompile-nth-value (form) |
---|
895 | (if *in-jvm-compile* |
---|
896 | (precompile1 (macroexpand form *precompile-env*)) |
---|
897 | form)) |
---|
898 | |
---|
899 | (defun precompile-return (form) |
---|
900 | (if *in-jvm-compile* |
---|
901 | (precompile1 (macroexpand form *precompile-env*)) |
---|
902 | (list 'RETURN (precompile1 (cadr form))))) |
---|
903 | |
---|
904 | (defun precompile-return-from (form) |
---|
905 | (list 'RETURN-FROM (cadr form) (precompile1 (caddr form)))) |
---|
906 | |
---|
907 | (defun precompile-tagbody (form) |
---|
908 | (do ((body (cdr form) (cdr body)) |
---|
909 | (result ())) |
---|
910 | ((null body) (cons 'TAGBODY (nreverse result))) |
---|
911 | (if (atom (car body)) |
---|
912 | (push (car body) result) |
---|
913 | (push (let* ((first-form (car body)) |
---|
914 | (expanded (precompile1 first-form))) |
---|
915 | (if (and (symbolp expanded) |
---|
916 | (neq expanded first-form)) |
---|
917 | ;; Workaround: |
---|
918 | ;; Since our expansion/compilation order |
---|
919 | ;; is out of sync with the definition of |
---|
920 | ;; TAGBODY (which requires the compiler |
---|
921 | ;; to look for tags before expanding), |
---|
922 | ;; we need to disguise anything which might |
---|
923 | ;; look like a tag. We do this by wrapping |
---|
924 | ;; it in a PROGN form. |
---|
925 | (list 'PROGN expanded) |
---|
926 | expanded)) result)))) |
---|
927 | |
---|
928 | (defun precompile-eval-when (form) |
---|
929 | (list* 'EVAL-WHEN (cadr form) (mapcar #'precompile1 (cddr form)))) |
---|
930 | |
---|
931 | (defun precompile-unwind-protect (form) |
---|
932 | (list* 'UNWIND-PROTECT |
---|
933 | (precompile1 (cadr form)) |
---|
934 | (mapcar #'precompile1 (cddr form)))) |
---|
935 | |
---|
936 | ;; EXPAND-MACRO is like MACROEXPAND, but EXPAND-MACRO quits if *IN-JVM-COMPILE* |
---|
937 | ;; is false and a macro is encountered that is also implemented as a special |
---|
938 | ;; operator, so interpreted code can use the special operator implementation. |
---|
939 | (defun expand-macro (form) |
---|
940 | (let (exp) |
---|
941 | (loop |
---|
942 | (unless *in-jvm-compile* |
---|
943 | (when (and (consp form) |
---|
944 | (symbolp (%car form)) |
---|
945 | (special-operator-p (%car form))) |
---|
946 | (return-from expand-macro (values form exp)))) |
---|
947 | (multiple-value-bind (result expanded) |
---|
948 | (macroexpand-1 form *precompile-env*) |
---|
949 | (unless expanded |
---|
950 | (return-from expand-macro (values result exp))) |
---|
951 | (setf form result |
---|
952 | exp t))))) |
---|
953 | |
---|
954 | (declaim (ftype (function (t t) t) precompile-form)) |
---|
955 | (defun precompile-form (form in-jvm-compile |
---|
956 | &optional precompile-env) |
---|
957 | (let ((*in-jvm-compile* in-jvm-compile) |
---|
958 | (*inline-declarations* *inline-declarations*) |
---|
959 | (pre::*precompile-env* precompile-env)) |
---|
960 | (precompile1 form))) |
---|
961 | |
---|
962 | (defun install-handler (symbol &optional handler) |
---|
963 | (declare (type symbol symbol)) |
---|
964 | (let ((handler (or handler |
---|
965 | (find-symbol (sys::%format nil "PRECOMPILE-~A" |
---|
966 | (symbol-name symbol)) |
---|
967 | 'precompiler)))) |
---|
968 | (unless (and handler (fboundp handler)) |
---|
969 | (error "No handler for ~S." (let ((*package* (find-package :keyword))) |
---|
970 | (format nil "~S" symbol)))) |
---|
971 | (setf (get symbol 'precompile-handler) handler))) |
---|
972 | |
---|
973 | (defun install-handlers () |
---|
974 | (mapcar #'install-handler '(BLOCK |
---|
975 | CASE |
---|
976 | COND |
---|
977 | DOLIST |
---|
978 | DOTIMES |
---|
979 | EVAL-WHEN |
---|
980 | FUNCTION |
---|
981 | IF |
---|
982 | LAMBDA |
---|
983 | MACROLET |
---|
984 | MULTIPLE-VALUE-BIND |
---|
985 | MULTIPLE-VALUE-LIST |
---|
986 | NAMED-LAMBDA |
---|
987 | NTH-VALUE |
---|
988 | PROGN |
---|
989 | PROGV |
---|
990 | PSETF |
---|
991 | PSETQ |
---|
992 | RETURN |
---|
993 | RETURN-FROM |
---|
994 | SETF |
---|
995 | SETQ |
---|
996 | SYMBOL-MACROLET |
---|
997 | TAGBODY |
---|
998 | UNWIND-PROTECT |
---|
999 | UNLESS |
---|
1000 | WHEN)) |
---|
1001 | |
---|
1002 | (dolist (pair '((ECASE precompile-case) |
---|
1003 | |
---|
1004 | (AND precompile-cons) |
---|
1005 | (OR precompile-cons) |
---|
1006 | |
---|
1007 | (CATCH precompile-cons) |
---|
1008 | (MULTIPLE-VALUE-CALL precompile-cons) |
---|
1009 | (MULTIPLE-VALUE-PROG1 precompile-cons) |
---|
1010 | |
---|
1011 | (DO precompile-do/do*) |
---|
1012 | (DO* precompile-do/do*) |
---|
1013 | |
---|
1014 | (LET precompile-let) |
---|
1015 | (LET* precompile-let*) |
---|
1016 | |
---|
1017 | (LOCALLY precompile-locally) |
---|
1018 | |
---|
1019 | (FLET precompile-flet/labels) |
---|
1020 | (LABELS precompile-flet/labels) |
---|
1021 | |
---|
1022 | (LOAD-TIME-VALUE precompile-load-time-value) |
---|
1023 | |
---|
1024 | (DECLARE precompile-identity) |
---|
1025 | (DEFUN precompile-defun) |
---|
1026 | (GO precompile-identity) |
---|
1027 | (QUOTE precompile-identity) |
---|
1028 | (THE precompile-the) |
---|
1029 | (THROW precompile-cons) |
---|
1030 | (TRULY-THE precompile-truly-the) |
---|
1031 | |
---|
1032 | (THREADS:SYNCHRONIZED-ON |
---|
1033 | precompile-threads-synchronized-on) |
---|
1034 | |
---|
1035 | (JVM::WITH-INLINE-CODE precompile-identity))) |
---|
1036 | (install-handler (first pair) (second pair)))) |
---|
1037 | |
---|
1038 | (install-handlers) |
---|
1039 | |
---|
1040 | (export '(precompile-form)) |
---|
1041 | |
---|
1042 | (in-package #:ext) |
---|
1043 | (defun macroexpand-all (form &optional env) |
---|
1044 | (precompiler:precompile-form form t env)) |
---|
1045 | |
---|
1046 | (in-package #:lisp) |
---|
1047 | |
---|
1048 | (defmacro compiler-let (bindings &body forms &environment env) |
---|
1049 | (let ((bindings (mapcar #'(lambda (binding) |
---|
1050 | (if (atom binding) (list binding) binding)) |
---|
1051 | bindings))) |
---|
1052 | (progv (mapcar #'car bindings) |
---|
1053 | (mapcar #'(lambda (binding) |
---|
1054 | (eval (cadr binding))) bindings) |
---|
1055 | (macroexpand-all `(progn ,@forms) env)))) |
---|
1056 | |
---|
1057 | (in-package #:system) |
---|
1058 | |
---|
1059 | (defun set-function-definition (name new old) |
---|
1060 | (let ((*warn-on-redefinition* nil)) |
---|
1061 | (sys::%set-lambda-name new name) |
---|
1062 | (sys:set-call-count new (sys:call-count old)) |
---|
1063 | (sys::%set-arglist new (sys::arglist old)) |
---|
1064 | (when (macro-function name) |
---|
1065 | (setf new (make-macro name new))) |
---|
1066 | (if (typep old 'standard-generic-function) |
---|
1067 | (mop:set-funcallable-instance-function old new) |
---|
1068 | (setf (fdefinition name) new)))) |
---|
1069 | |
---|
1070 | (defun precompile (name &optional definition) |
---|
1071 | (unless definition |
---|
1072 | (setq definition (or (and (symbolp name) (macro-function name)) |
---|
1073 | (fdefinition name)))) |
---|
1074 | (let ((expr definition) |
---|
1075 | env result |
---|
1076 | (pre::*precompile-env* nil)) |
---|
1077 | (when (functionp definition) |
---|
1078 | (multiple-value-bind (form closure-p) |
---|
1079 | (function-lambda-expression definition) |
---|
1080 | (unless form |
---|
1081 | (return-from precompile (values nil t t))) |
---|
1082 | (setq env closure-p) |
---|
1083 | (setq expr form))) |
---|
1084 | (unless (and (consp expr) (eq (car expr) 'lambda)) |
---|
1085 | (format t "Unable to precompile ~S.~%" name) |
---|
1086 | (return-from precompile (values nil t t))) |
---|
1087 | (setf result |
---|
1088 | (sys:make-closure (precompiler:precompile-form expr nil env) env)) |
---|
1089 | (when (and name (functionp result)) |
---|
1090 | (sys::set-function-definition name result definition)) |
---|
1091 | (values (or name result) nil nil))) |
---|
1092 | |
---|
1093 | (defun precompile-package (pkg &key verbose) |
---|
1094 | (dolist (sym (package-symbols pkg)) |
---|
1095 | (when (fboundp sym) |
---|
1096 | (unless (special-operator-p sym) |
---|
1097 | (let ((f (fdefinition sym))) |
---|
1098 | (unless (compiled-function-p f) |
---|
1099 | (when verbose |
---|
1100 | (format t "Precompiling ~S~%" sym) |
---|
1101 | (finish-output)) |
---|
1102 | (precompile sym)))))) |
---|
1103 | t) |
---|
1104 | |
---|
1105 | (defun %compile (name definition) |
---|
1106 | (if (and name (fboundp name) (%typep (symbol-function name) 'generic-function)) |
---|
1107 | (values name nil nil) |
---|
1108 | (precompile name definition))) |
---|
1109 | |
---|
1110 | ;; ;; Redefine EVAL to precompile its argument. |
---|
1111 | ;; (defun eval (form) |
---|
1112 | ;; (%eval (precompile-form form nil))) |
---|
1113 | |
---|
1114 | ;; ;; Redefine DEFMACRO to precompile the expansion function on the fly. |
---|
1115 | ;; (defmacro defmacro (name lambda-list &rest body) |
---|
1116 | ;; (let* ((form (gensym "WHOLE-")) |
---|
1117 | ;; (env (gensym "ENVIRONMENT-"))) |
---|
1118 | ;; (multiple-value-bind (body decls) |
---|
1119 | ;; (parse-defmacro lambda-list form body name 'defmacro :environment env) |
---|
1120 | ;; (let ((expander `(lambda (,form ,env) ,@decls (block ,name ,body)))) |
---|
1121 | ;; `(progn |
---|
1122 | ;; (let ((macro (make-macro ',name |
---|
1123 | ;; (or (precompile nil ,expander) ,expander)))) |
---|
1124 | ;; ,@(if (special-operator-p name) |
---|
1125 | ;; `((put ',name 'macroexpand-macro macro)) |
---|
1126 | ;; `((fset ',name macro))) |
---|
1127 | ;; (%set-arglist macro ',lambda-list) |
---|
1128 | ;; ',name)))))) |
---|
1129 | |
---|
1130 | ;; Make an exception just this one time... |
---|
1131 | (when (get 'defmacro 'macroexpand-macro) |
---|
1132 | (fset 'defmacro (get 'defmacro 'macroexpand-macro)) |
---|
1133 | (remprop 'defmacro 'macroexpand-macro)) |
---|
1134 | |
---|
1135 | (defvar *defined-functions*) |
---|
1136 | |
---|
1137 | (defvar *undefined-functions*) |
---|
1138 | |
---|
1139 | (defun note-name-defined (name) |
---|
1140 | (when (boundp '*defined-functions*) |
---|
1141 | (push name *defined-functions*)) |
---|
1142 | (when (and (boundp '*undefined-functions*) (not (null *undefined-functions*))) |
---|
1143 | (setf *undefined-functions* (remove name *undefined-functions*)))) |
---|
1144 | |
---|
1145 | ;; Redefine DEFUN to precompile the definition on the fly. |
---|
1146 | (defmacro defun (name lambda-list &body body &environment env) |
---|
1147 | (note-name-defined name) |
---|
1148 | (multiple-value-bind (body decls doc) |
---|
1149 | (parse-body body) |
---|
1150 | (let* ((block-name (fdefinition-block-name name)) |
---|
1151 | (lambda-expression |
---|
1152 | `(named-lambda ,name ,lambda-list |
---|
1153 | ,@decls |
---|
1154 | ,@(when doc `(,doc)) |
---|
1155 | (block ,block-name ,@body)))) |
---|
1156 | (cond ((and (boundp 'jvm::*file-compilation*) |
---|
1157 | ;; when JVM.lisp isn't loaded yet, this variable isn't bound |
---|
1158 | ;; meaning that we're not trying to compile to a file: |
---|
1159 | ;; Both COMPILE and COMPILE-FILE bind this variable. |
---|
1160 | ;; This function is also triggered by MACROEXPAND, though |
---|
1161 | jvm::*file-compilation*) |
---|
1162 | `(fset ',name ,lambda-expression)) |
---|
1163 | (t |
---|
1164 | (when (and env (empty-environment-p env)) |
---|
1165 | (setf env nil)) |
---|
1166 | (when (null env) |
---|
1167 | (setf lambda-expression (precompiler:precompile-form lambda-expression nil))) |
---|
1168 | `(progn |
---|
1169 | (%defun ',name ,lambda-expression) |
---|
1170 | ,@(when doc |
---|
1171 | `((%set-documentation ',name 'function ,doc))))))))) |
---|
1172 | |
---|
1173 | (export '(precompile)) |
---|
1174 | |
---|
1175 | ;;(provide "PRECOMPILER") |
---|