source: branches/0.20.x/abcl/src/org/armedbear/lisp/precompiler.lisp

Last change on this file was 12340, checked in by ehuelsmann, 15 years ago

Fix symbol-macrolet expanding variables declared in
a lambda-list for LAMBDA and NAMED-LAMBDA forms.

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