source: trunk/abcl/src/org/armedbear/lisp/compiler-pass1.lisp @ 11820

Last change on this file since 11820 was 11820, checked in by ehuelsmann, 14 years ago

Make local GO restore the environment of the TAGBODY,
in case it jumps out of blocks setting the environment.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 42.2 KB
Line 
1;;; compiler-pass1.lisp
2;;;
3;;; Copyright (C) 2003-2008 Peter Graves
4;;; $Id: compiler-pass1.lisp 11820 2009-05-03 10:10:21Z 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 "JVM")
33
34(eval-when (:compile-toplevel :load-toplevel :execute)
35  (require "LOOP")
36  (require "FORMAT")
37  (require "CLOS")
38  (require "PRINT-OBJECT")
39  (require "COMPILER-TYPES")
40  (require "KNOWN-FUNCTIONS")
41  (require "KNOWN-SYMBOLS")
42  (require "DUMP-FORM")
43  (require "OPCODES")
44  (require "JAVA"))
45
46
47(eval-when (:compile-toplevel :load-toplevel :execute)
48  (defun generate-inline-expansion (block-name lambda-list body)
49    (cond ((intersection lambda-list '(&optional &rest &key &allow-other-keys &aux) :test 'eq)
50           nil)
51          (t
52           (setf body (copy-tree body))
53           (list 'LAMBDA lambda-list (precompile-form (list* 'BLOCK block-name body) t)))))
54  ) ; EVAL-WHEN
55
56;;; Pass 1.
57
58
59;; Returns a list of declared free specials, if any are found.
60(declaim (ftype (function (list list) list) process-declarations-for-vars))
61(defun process-declarations-for-vars (body variables)
62  (let ((free-specials '()))
63    (dolist (subform body)
64      (unless (and (consp subform) (eq (%car subform) 'DECLARE))
65        (return))
66      (let ((decls (%cdr subform)))
67        (dolist (decl decls)
68          (case (car decl)
69            ((DYNAMIC-EXTENT FTYPE INLINE NOTINLINE OPTIMIZE)
70             ;; Nothing to do here.
71             )
72            ((IGNORE IGNORABLE)
73             (process-ignore/ignorable (%car decl) (%cdr decl) variables))
74            (SPECIAL
75             (dolist (name (%cdr decl))
76               (let ((variable (find-variable name variables)))
77                 (cond ((and variable
78                             ;; see comment below (and DO-ALL-SYMBOLS.11)
79                             (eq (variable-compiland variable) *current-compiland*))
80                        (setf (variable-special-p variable) t))
81                       (t
82                        (dformat t "adding free special ~S~%" name)
83                        (push (make-variable :name name :special-p t) free-specials))))))
84            (TYPE
85             (dolist (name (cddr decl))
86               (let ((variable (find-variable name variables)))
87                 (when (and variable
88                            ;; Don't apply a declaration in a local function to
89                            ;; a variable defined in its parent. For an example,
90                            ;; see CREATE-GREEDY-NO-ZERO-MATCHER in cl-ppcre.
91                            ;; FIXME suboptimal, since we ignore the declaration
92                            (eq (variable-compiland variable) *current-compiland*))
93                   (setf (variable-declared-type variable)
94                         (make-compiler-type (cadr decl)))))))
95            (t
96             (dolist (name (cdr decl))
97               (let ((variable (find-variable name variables)))
98                 (when variable
99                   (setf (variable-declared-type variable)
100                         (make-compiler-type (%car decl)))))))))))
101    free-specials))
102
103(defun check-name (name)
104  ;; FIXME Currently this error is signalled by the precompiler.
105  (unless (symbolp name)
106    (compiler-error "The variable ~S is not a symbol." name))
107  (when (constantp name)
108    (compiler-error "The name of the variable ~S is already in use to name a constant." name))
109  name)
110
111(declaim (ftype (function (t) t) p1-body))
112(defun p1-body (body)
113  (declare (optimize speed))
114  (let ((tail body))
115    (loop
116      (when (endp tail)
117        (return))
118      (setf (car tail) (p1 (%car tail)))
119      (setf tail (%cdr tail))))
120  body)
121
122(defknown p1-default (t) t)
123(declaim (inline p1-default))
124(defun p1-default (form)
125  (setf (cdr form) (p1-body (cdr form)))
126  form)
127
128(defknown p1-if (t) t)
129(defun p1-if (form)
130  (let ((test (cadr form)))
131    (cond ((unsafe-p test)
132           (cond ((and (consp test)
133                       (memq (%car test) '(GO RETURN-FROM THROW)))
134                  (p1 test))
135                 (t
136                  (let* ((var (gensym))
137                         (new-form
138                          `(let ((,var ,test))
139                             (if ,var ,(third form) ,(fourth form)))))
140                    (p1 new-form)))))
141          (t
142           (p1-default form)))))
143
144
145(defmacro p1-let/let*-vars 
146    (varlist variables-var var body1 body2)
147  (let ((varspec (gensym))
148  (initform (gensym))
149  (name (gensym)))
150    `(let ((,variables-var ()))
151       (dolist (,varspec ,varlist)
152   (cond ((consp ,varspec)
153                ;; Even though the precompiler already signals this
154                ;; error, double checking can't hurt; after all, we're
155                ;; also rewriting &AUX into LET* bindings.
156    (unless (<= 1 (length ,varspec) 2)
157      (compiler-error "The LET/LET* binding specification ~S is invalid."
158          ,varspec))
159    (let* ((,name (%car ,varspec))
160           (,initform (p1 (%cadr ,varspec)))
161           (,var (make-variable :name (check-name ,name) :initform ,initform)))
162      (push ,var ,variables-var)
163      ,@body1))
164         (t
165    (let ((,var (make-variable :name (check-name ,varspec))))
166      (push ,var ,variables-var)
167      ,@body1))))
168       ,@body2)))
169
170(defknown p1-let-vars (t) t)
171(defun p1-let-vars (varlist)
172  (p1-let/let*-vars 
173   varlist vars var
174   ()
175   ((setf vars (nreverse vars))
176    (dolist (variable vars)
177      (push variable *visible-variables*)
178      (push variable *all-variables*))
179    vars)))
180
181(defknown p1-let*-vars (t) t)
182(defun p1-let*-vars (varlist)
183  (p1-let/let*-vars 
184   varlist vars var
185   ((push var *visible-variables*)
186    (push var *all-variables*))
187   ((nreverse vars))))
188
189(defun p1-let/let* (form)
190  (declare (type cons form))
191  (let* ((*visible-variables* *visible-variables*)
192         (block (make-block-node '(LET)))
193         (*blocks* (cons block *blocks*))
194         (op (%car form))
195         (varlist (cadr form))
196         (body (cddr form)))
197    (aver (or (eq op 'LET) (eq op 'LET*)))
198    (when (eq op 'LET)
199      ;; Convert to LET* if possible.
200      (if (null (cdr varlist))
201          (setf op 'LET*)
202          (dolist (varspec varlist (setf op 'LET*))
203            (or (atom varspec)
204                (constantp (cadr varspec))
205                (eq (car varspec) (cadr varspec))
206                (return)))))
207    (let ((vars (if (eq op 'LET)
208                    (p1-let-vars varlist)
209                    (p1-let*-vars varlist))))
210      ;; Check for globally declared specials.
211      (dolist (variable vars)
212        (when (special-variable-p (variable-name variable))
213          (setf (variable-special-p variable) t)))
214      ;; For processing declarations, we want to walk the variable list from
215      ;; last to first, since declarations apply to the last-defined variable
216      ;; with the specified name.
217      (setf (block-free-specials block) (process-declarations-for-vars body (reverse vars)))
218      (setf (block-vars block) vars)
219      ;; Make free specials visible.
220      (dolist (variable (block-free-specials block))
221        (push variable *visible-variables*)))
222    (setf body (p1-body body))
223    (setf (block-form block) (list* op varlist body))
224    block))
225
226(defun p1-locally (form)
227  (let ((*visible-variables* *visible-variables*)
228        (specials (process-special-declarations (cdr form))))
229    (dolist (name specials)
230;;       (format t "p1-locally ~S is special~%" name)
231      (push (make-variable :name name :special-p t) *visible-variables*))
232    (setf (cdr form) (p1-body (cdr form)))
233    form))
234
235(defknown p1-m-v-b (t) t)
236(defun p1-m-v-b (form)
237  (when (= (length (cadr form)) 1)
238    (let ((new-form `(let* ((,(caadr form) ,(caddr form))) ,@(cdddr form))))
239      (return-from p1-m-v-b (p1-let/let* new-form))))
240  (let* ((*visible-variables* *visible-variables*)
241         (block (make-block-node '(MULTIPLE-VALUE-BIND)))
242         (*blocks* (cons block *blocks*))
243         (varlist (cadr form))
244         (values-form (caddr form))
245         (body (cdddr form)))
246    ;; Process the values-form first. ("The scopes of the name binding and
247    ;; declarations do not include the values-form.")
248    (setf values-form (p1 values-form))
249    (let ((vars ()))
250      (dolist (symbol varlist)
251        (let ((var (make-variable :name symbol)))
252          (push var vars)
253          (push var *visible-variables*)
254          (push var *all-variables*)))
255      ;; Check for globally declared specials.
256      (dolist (variable vars)
257        (when (special-variable-p (variable-name variable))
258          (setf (variable-special-p variable) t)))
259      (setf (block-free-specials block) (process-declarations-for-vars body vars))
260      (setf (block-vars block) (nreverse vars)))
261    (setf body (p1-body body))
262    (setf (block-form block) (list* 'MULTIPLE-VALUE-BIND varlist values-form body))
263    block))
264
265(defun p1-block (form)
266  (let* ((block (make-block-node (cadr form)))
267         (*blocks* (cons block *blocks*)))
268    (setf (cddr form) (p1-body (cddr form)))
269    (setf (block-form block) form)
270    block))
271
272(defun p1-catch (form)
273  (let* ((tag (p1 (cadr form)))
274         (body (cddr form))
275         (result '()))
276    (dolist (subform body)
277      (let ((op (and (consp subform) (%car subform))))
278        (push (p1 subform) result)
279        (when (memq op '(GO RETURN-FROM THROW))
280          (return))))
281    (setf result (nreverse result))
282    (when (and (null (cdr result))
283               (consp (car result))
284               (eq (caar result) 'GO))
285      (return-from p1-catch (car result)))
286    (push tag result)
287    (push 'CATCH result)
288    (let ((block (make-block-node '(CATCH))))
289      (setf (block-form block) result)
290      block)))
291
292(defun p1-unwind-protect (form)
293  (if (= (length form) 2)
294      (p1 (second form)) ; No cleanup forms: (unwind-protect (...)) => (...)
295      (let* ((block (make-block-node '(UNWIND-PROTECT)))
296             (*blocks* (cons block *blocks*)))
297        (setf (block-form block) (p1-default form))
298        block)))
299
300(defknown p1-return-from (t) t)
301(defun p1-return-from (form)
302  (let* ((name (second form))
303         (block (find-block name)))
304    (when (null block)
305      (compiler-error "RETURN-FROM ~S: no block named ~S is currently visible."
306                      name name))
307    (dformat t "p1-return-from block = ~S~%" (block-name block))
308    (setf (block-return-p block) t)
309    (cond ((eq (block-compiland block) *current-compiland*)
310           ;; Local case. If the RETURN is nested inside an UNWIND-PROTECT
311           ;; which is inside the block we're returning from, we'll do a non-
312           ;; local return anyway so that UNWIND-PROTECT can catch it and run
313           ;; its cleanup forms.
314           (dformat t "*blocks* = ~S~%" (mapcar #'block-name *blocks*))
315           (let ((protected (enclosed-by-protected-block-p block)))
316             (dformat t "p1-return-from protected = ~S~%" protected)
317             (when protected
318               (setf (block-non-local-return-p block) t))))
319          (t
320           (setf (block-non-local-return-p block) t)))
321    (when (block-non-local-return-p block)
322      (dformat t "non-local return from block ~S~%" (block-name block))))
323  (list* 'RETURN-FROM (cadr form) (mapcar #'p1 (cddr form))))
324
325(defun p1-tagbody (form)
326  (let* ((block (make-block-node '(TAGBODY)))
327         (*blocks* (cons block *blocks*))
328         (*visible-tags* *visible-tags*)
329         (body (cdr form)))
330    ;; Make all the tags visible before processing the body forms.
331    (dolist (subform body)
332      (when (or (symbolp subform) (integerp subform))
333        (let* ((tag (make-tag :name subform :label (gensym) :block block)))
334          (push tag *visible-tags*))))
335    (let ((new-body '())
336          (live t))
337      (dolist (subform body)
338        (cond ((or (symbolp subform) (integerp subform))
339               (push subform new-body)
340               (setf live t))
341              ((not live)
342               ;; Nothing to do.
343               )
344              (t
345               (when (and (consp subform)
346                          (memq (%car subform) '(GO RETURN-FROM THROW)))
347                 ;; Subsequent subforms are unreachable until we see another
348                 ;; tag.
349                 (setf live nil))
350               (push (p1 subform) new-body))))
351      (setf (block-form block) (list* 'TAGBODY (nreverse new-body))))
352    block))
353
354(defknown p1-go (t) t)
355(defun p1-go (form)
356  (let* ((name (cadr form))
357         (tag (find-tag name)))
358    (unless tag
359      (error "p1-go: tag not found: ~S" name))
360    (let ((tag-block (tag-block tag)))
361      (cond ((eq (tag-compiland tag) *current-compiland*)
362             ;; Does the GO leave an enclosing UNWIND-PROTECT?
363             (if (enclosed-by-protected-block-p tag-block)
364                 (setf (block-non-local-go-p tag-block) t)
365                 ;; non-local GO's ensure environment restoration
366                 ;; find out about this local GO
367                 (when (null (block-needs-environment-restoration tag-block))
368                   (setf (block-needs-environment-restoration tag-block)
369                         (enclosed-by-environment-setting-block-p tag-block)))))
370            (t
371             (setf (block-non-local-go-p tag-block) t)))))
372  form)
373
374(defun validate-function-name (name)
375  (unless (or (symbolp name) (setf-function-name-p name))
376    (compiler-error "~S is not a valid function name." name)))
377
378(defmacro with-local-functions-for-flet/labels
379    (form local-functions-var lambda-list-var name-var body-var body1 body2)
380  `(progn (incf (compiland-children *current-compiland*) (length (cadr ,form)))
381    (let ((*visible-variables* *visible-variables*)
382    (*local-functions* *local-functions*)
383    (*current-compiland* *current-compiland*)
384    (,local-functions-var '()))
385      (dolist (definition (cadr ,form))
386        (let ((,name-var (car definition))
387        (,lambda-list-var (cadr definition)))
388    (validate-function-name ,name-var)
389    (let* ((,body-var (cddr definition))
390           (compiland (make-compiland :name ,name-var
391              :parent *current-compiland*)))
392      ,@body1)))
393      (setf ,local-functions-var (nreverse ,local-functions-var))
394      ;; Make the local functions visible.
395      (dolist (local-function ,local-functions-var)
396        (push local-function *local-functions*)
397        (let ((variable (local-function-variable local-function)))
398    (when variable
399      (push variable *visible-variables*))))
400      ,@body2)))
401
402(defun split-decls (forms specific-vars)
403  (let ((other-decls nil)
404        (specific-decls nil))
405    (dolist (form forms)
406      (unless (and (consp form) (eq (car form) 'DECLARE)) ; shouldn't happen
407        (return))
408      (dolist (decl (cdr form))
409        (case (car decl)
410          ((OPTIMIZE DECLARATION DYNAMIC-EXTENT FTYPE INLINE NOTINLINE)
411           (push (list 'DECLARE decl) other-decls))
412          (SPECIAL
413           (dolist (name (cdr decl))
414             (if (memq name specific-vars)
415                 (push `(DECLARE (SPECIAL ,name)) specific-decls)
416                 (push `(DECLARE (SPECIAL ,name)) other-decls))))
417          (TYPE
418           (dolist (name (cddr decl))
419             (if (memq name specific-vars)
420                 (push `(DECLARE (TYPE ,(cadr decl) ,name)) specific-decls)
421                 (push `(DECLARE (TYPE ,(cadr decl) ,name)) other-decls))))
422          (t
423           (dolist (name (cdr decl))
424             (if (memq name specific-vars)
425                 (push `(DECLARE (,(car decl) ,name)) specific-decls)
426                 (push `(DECLARE (,(car decl) ,name)) other-decls)))))))
427    (values (nreverse other-decls)
428            (nreverse specific-decls))))
429
430(defun rewrite-aux-vars (form)
431  (let* ((lambda-list (cadr form))
432         (aux-p (memq '&AUX lambda-list))
433         (lets (cdr aux-p))
434         aux-vars)
435    (unless aux-p
436      ;; no rewriting required
437      (return-from rewrite-aux-vars form))
438    (multiple-value-bind (body decls)
439        (parse-body (cddr form))
440      (dolist (form lets)
441        (cond ((consp form)
442               (push (car form) aux-vars))
443              (t
444               (push form aux-vars))))
445      (setf lambda-list (subseq lambda-list 0 (position '&AUX lambda-list)))
446      (multiple-value-bind (let-decls lambda-decls)
447          (split-decls decls (lambda-list-names lambda-list))
448        `(lambda ,lambda-list
449           ,@lambda-decls
450           (let* ,lets
451             ,@let-decls
452             ,@body))))))
453
454(defun rewrite-lambda (form)
455  (setf form (rewrite-aux-vars form))
456  (let* ((lambda-list (cadr form)))
457    (if (not (or (memq '&optional lambda-list)
458                 (memq '&key lambda-list)))
459        ;; no need to rewrite: no arguments with possible initforms anyway
460        form
461      (multiple-value-bind (body decls doc)
462          (parse-body (cddr form))
463        (let (state let-bindings new-lambda-list
464                    (non-constants 0))
465          (do* ((vars lambda-list (cdr vars))
466                (var (car vars) (car vars)))
467               ((endp vars))
468            (push (car vars) new-lambda-list)
469            (let ((replacement (gensym)))
470              (flet ((parse-compound-argument (arg)
471                       "Returns the values NAME, KEYWORD, INITFORM, INITFORM-P,
472   SUPPLIED-P and SUPPLIED-P-P assuming ARG is a compound argument."
473                       (destructuring-bind
474                             (name &optional (initform nil initform-supplied-p)
475                                   (supplied-p nil supplied-p-supplied-p))
476                           (if (listp arg) arg (list arg))
477                         (if (listp name)
478                             (values (cadr name) (car name)
479                                     initform initform-supplied-p
480                                     supplied-p supplied-p-supplied-p)
481                             (values name (make-keyword name)
482                                     initform initform-supplied-p
483                                     supplied-p supplied-p-supplied-p)))))
484                (case var
485                  (&optional (setf state :optional))
486                  (&key (setf state :key))
487                  ((&whole &environment &rest &body &allow-other-keys)
488                   ;; do nothing special
489                   )
490                  (t
491                   (cond
492                     ((atom var)
493                      (setf (car new-lambda-list)
494                            (if (eq state :key)
495                                (list (list (make-keyword var) replacement))
496                                replacement))
497                      (push (list var replacement) let-bindings))
498                     ((constantp (second var))
499                      ;; so, we must have a consp-type var we're looking at
500                      ;; and it has a constantp initform
501                      (multiple-value-bind
502                            (name keyword initform initform-supplied-p
503                                  supplied-p supplied-p-supplied-p)
504                          (parse-compound-argument var)
505                        (let ((var-form (if (eq state :key)
506                                            (list keyword replacement)
507                                            replacement))
508                              (supplied-p-replacement (gensym)))
509                          (setf (car new-lambda-list)
510                                (cond
511                                  ((not initform-supplied-p)
512                                   (list var-form))
513                                  ((not supplied-p-supplied-p)
514                                   (list var-form initform))
515                                  (t
516                                   (list var-form initform
517                                         supplied-p-replacement))))
518                          (push (list name replacement) let-bindings)
519                          ;; if there was a 'supplied-p' variable, it might
520                          ;; be used in the declarations. Since those will be
521                          ;; moved below the LET* block, we need to move the
522                          ;; supplied-p parameter too.
523                          (when supplied-p-supplied-p
524                            (push (list supplied-p supplied-p-replacement)
525                                  let-bindings)))))
526                     (t
527                      (incf non-constants)
528                      ;; this is either a keyword or an optional argument
529                      ;; with a non-constantp initform
530                      (multiple-value-bind
531                            (name keyword initform initform-supplied-p
532                                  supplied-p supplied-p-supplied-p)
533                          (parse-compound-argument var)
534                        (declare (ignore initform-supplied-p))
535                        (let ((var-form (if (eq state :key)
536                                            (list keyword replacement)
537                                            replacement))
538                              (supplied-p-replacement (gensym)))
539                          (setf (car new-lambda-list)
540                                (list var-form nil supplied-p-replacement))
541                          (push (list name `(if ,supplied-p-replacement
542                                                ,replacement ,initform))
543                                let-bindings)
544                          (when supplied-p-supplied-p
545                            (push (list supplied-p supplied-p-replacement)
546                                  let-bindings)))))))))))
547          (if (zerop non-constants)
548              ;; there was no reason to rewrite...
549              form
550              (let ((rv
551                     `(lambda ,(nreverse new-lambda-list)
552                        ,@(when doc (list doc))
553                        (let* ,(nreverse let-bindings)
554                          ,@decls ,@body))))
555                rv)))))))
556
557(defun p1-flet (form)
558  (with-local-functions-for-flet/labels
559      form local-functions lambda-list name body
560      ((let ((local-function (make-local-function :name name
561             :compiland compiland)))
562   (multiple-value-bind (body decls) (parse-body body)
563     (let* ((block-name (fdefinition-block-name name))
564      (lambda-expression
565                   (rewrite-lambda
566       `(lambda ,lambda-list ,@decls (block ,block-name ,@body))))
567      (*visible-variables* *visible-variables*)
568      (*local-functions* *local-functions*)
569      (*current-compiland* compiland))
570       (setf (compiland-lambda-expression compiland) lambda-expression)
571       (setf (local-function-inline-expansion local-function)
572       (generate-inline-expansion block-name lambda-list body))
573       (p1-compiland compiland)))
574   (when *closure-variables*
575     (let ((variable (make-variable :name (gensym))))
576       (setf (local-function-variable local-function) variable)
577       (push variable *all-variables*)))
578   (push local-function local-functions)))
579      ((with-saved-compiler-policy
580     (process-optimization-declarations (cddr form))
581   (list* (car form) local-functions (p1-body (cddr form)))))))
582
583
584(defun p1-labels (form)
585  (with-local-functions-for-flet/labels
586      form local-functions lambda-list name body
587      ((let* ((variable (make-variable :name (gensym)))
588        (local-function (make-local-function :name name
589               :compiland compiland
590               :variable variable)))
591   (multiple-value-bind (body decls) (parse-body body)
592     (setf (compiland-lambda-expression compiland)
593                 (rewrite-lambda
594     `(lambda ,lambda-list ,@decls (block ,name ,@body)))))
595   (push variable *all-variables*)
596   (push local-function local-functions)))
597      ((dolist (local-function local-functions)
598   (let ((*visible-variables* *visible-variables*)
599         (*current-compiland* (local-function-compiland local-function)))
600     (p1-compiland (local-function-compiland local-function))))
601       (list* (car form) local-functions (p1-body (cddr form))))))
602
603(defknown p1-funcall (t) t)
604(defun p1-funcall (form)
605  (unless (> (length form) 1)
606    (compiler-warn "Wrong number of arguments for ~A." (car form))
607    (return-from p1-funcall form))
608  (let ((function-form (%cadr form)))
609    (when (and (consp function-form)
610               (eq (%car function-form) 'FUNCTION))
611      (let ((name (%cadr function-form)))
612;;         (format t "p1-funcall name = ~S~%" name)
613        (let ((source-transform (source-transform name)))
614          (when source-transform
615;;             (format t "found source transform for ~S~%" name)
616;;             (format t "old form = ~S~%" form)
617;;             (let ((new-form (expand-source-transform form)))
618;;               (when (neq new-form form)
619;;                 (format t "new form = ~S~%" new-form)
620;;                 (return-from p1-funcall (p1 new-form))))
621            (let ((new-form (expand-source-transform (list* name (cddr form)))))
622;;               (format t "new form = ~S~%" new-form)
623              (return-from p1-funcall (p1 new-form)))
624            )))))
625  ;; Otherwise...
626  (p1-function-call form))
627
628(defun p1-function (form)
629  (let ((form (copy-tree form))
630        local-function)
631    (cond ((and (consp (cadr form))
632                (or (eq (caadr form) 'LAMBDA)
633                    (eq (caadr form) 'NAMED-LAMBDA)))
634           (let* ((named-lambda-p (eq (caadr form) 'NAMED-LAMBDA))
635                  (named-lambda-form (when named-lambda-p
636                                       (cadr form)))
637                  (name (when named-lambda-p
638                          (cadr named-lambda-form)))
639                  (lambda-form (if named-lambda-p
640                                   (cons 'LAMBDA (cddr named-lambda-form))
641                                   (cadr form)))
642                  (lambda-list (cadr lambda-form))
643                  (body (cddr lambda-form))
644                  (compiland (make-compiland :name (if named-lambda-p
645                                                       name (gensym "ANONYMOUS-LAMBDA-"))
646                                             :lambda-expression lambda-form
647                                             :parent *current-compiland*)))
648             (when *current-compiland*
649               (incf (compiland-children *current-compiland*)))
650             (multiple-value-bind (body decls)
651                 (parse-body body)
652               (setf (compiland-lambda-expression compiland)
653                     ;; if there still was a doc-string present, remove it
654                     (rewrite-lambda
655                      `(lambda ,lambda-list ,@decls ,@body)))
656               (let ((*visible-variables* *visible-variables*)
657                     (*current-compiland* compiland))
658                 (p1-compiland compiland)))
659             (list 'FUNCTION compiland)))
660          ((setf local-function (find-local-function (cadr form)))
661           (dformat t "p1-function local function ~S~%" (cadr form))
662           (let ((variable (local-function-variable local-function)))
663             (when variable
664                 (dformat t "p1-function ~S used non-locally~%" (variable-name variable))
665                 (setf (variable-used-non-locally-p variable) t)))
666           form)
667          (t
668           form))))
669
670(defun p1-lambda (form)
671  (let* ((lambda-list (cadr form)))
672    (when (or (memq '&optional lambda-list)
673              (memq '&key lambda-list))
674      (let ((state nil))
675        (dolist (arg lambda-list)
676          (cond ((memq arg lambda-list-keywords)
677                 (setf state arg))
678                ((memq state '(&optional &key))
679                 (when (and (consp arg)
680                            (not (constantp (second arg))))
681                   (compiler-unsupported
682                    "P1-LAMBDA: can't handle optional argument with non-constant initform.")))))))
683    (p1-function (list 'FUNCTION
684                        (rewrite-lambda form)))))
685
686(defun p1-eval-when (form)
687  (list* (car form) (cadr form) (mapcar #'p1 (cddr form))))
688
689(defknown p1-progv (t) t)
690(defun p1-progv (form)
691  ;; We've already checked argument count in PRECOMPILE-PROGV.
692
693  ;; ### FIXME: we need to return a block here, so that
694  ;;  (local) GO in p2 can restore the lastSpecialBinding environment
695  (let ((new-form (rewrite-progv form)))
696    (when (neq new-form form)
697      (return-from p1-progv (p1 new-form))))
698  (let ((symbols-form (cadr form))
699        (values-form (caddr form))
700        (body (cdddr form)))
701    `(progv ,(p1 symbols-form) ,(p1 values-form) ,@(p1-body body))))
702
703(defknown rewrite-progv (t) t)
704(defun rewrite-progv (form)
705  (let ((symbols-form (cadr form))
706        (values-form (caddr form))
707        (body (cdddr form)))
708    (cond ((or (unsafe-p symbols-form) (unsafe-p values-form))
709           (let ((g1 (gensym))
710                 (g2 (gensym)))
711             `(let ((,g1 ,symbols-form)
712                    (,g2 ,values-form))
713                (progv ,g1 ,g2 ,@body))))
714          (t
715           form))))
716
717(defun p1-quote (form)
718  (unless (= (length form) 2)
719    (compiler-error "Wrong number of arguments for special operator ~A (expected 1, but received ~D)."
720                    'QUOTE
721                    (1- (length form))))
722  (let ((arg (%cadr form)))
723    (if (or (numberp arg) (characterp arg))
724        arg
725        form)))
726
727(defun p1-setq (form)
728  (unless (= (length form) 3)
729    (error "Too many arguments for SETQ."))
730  (let ((arg1 (%cadr form))
731        (arg2 (%caddr form)))
732    (let ((variable (find-visible-variable arg1)))
733      (if variable
734          (progn
735            (when (variable-ignore-p variable)
736              (compiler-style-warn
737               "Variable ~S is assigned even though it was declared to be ignored."
738               (variable-name variable)))
739            (incf (variable-writes variable))
740            (cond ((eq (variable-compiland variable) *current-compiland*)
741                   (dformat t "p1-setq: write ~S~%" arg1))
742                  (t
743                   (dformat t "p1-setq: non-local write ~S~%" arg1)
744                   (setf (variable-used-non-locally-p variable) t))))
745          (dformat t "p1-setq: unknown variable ~S~%" arg1)))
746    (list 'SETQ arg1 (p1 arg2))))
747
748(defun p1-the (form)
749  (unless (= (length form) 3)
750    (compiler-error "Wrong number of arguments for special operator ~A (expected 2, but received ~D)."
751                    'THE
752                    (1- (length form))))
753  (let ((type (%cadr form))
754        (expr (%caddr form)))
755    (cond ((and (listp type) (eq (car type) 'VALUES))
756           ;; FIXME
757           (p1 expr))
758          ((= *safety* 3)
759           (let* ((sym (gensym))
760                  (new-expr `(let ((,sym ,expr))
761                               (require-type ,sym ',type)
762                               ,sym)))
763             (p1 new-expr)))
764          (t
765           (list 'THE type (p1 expr))))))
766
767(defun p1-truly-the (form)
768  (unless (= (length form) 3)
769    (compiler-error "Wrong number of arguments for special operator ~A (expected 2, but received ~D)."
770                    'TRULY-THE
771                    (1- (length form))))
772  (list 'TRULY-THE (%cadr form) (p1 (%caddr form))))
773
774(defknown unsafe-p (t) t)
775(defun unsafe-p (args)
776  (cond ((node-p args)
777         (unsafe-p (node-form args)))
778        ((atom args)
779         nil)
780        (t
781         (case (%car args)
782           (QUOTE
783            nil)
784           (LAMBDA
785            nil)
786           ((RETURN-FROM GO CATCH THROW UNWIND-PROTECT BLOCK)
787            t)
788           (t
789            (dolist (arg args)
790              (when (unsafe-p arg)
791                (return t))))))))
792
793(defknown rewrite-throw (t) t)
794(defun rewrite-throw (form)
795  (let ((args (cdr form)))
796    (if (unsafe-p args)
797        (let ((syms ())
798              (lets ()))
799          ;; Tag.
800          (let ((arg (first args)))
801            (if (constantp arg)
802                (push arg syms)
803                (let ((sym (gensym)))
804                  (push sym syms)
805                  (push (list sym arg) lets))))
806          ;; Result. "If the result-form produces multiple values, then all the
807          ;; values are saved."
808          (let ((arg (second args)))
809            (if (constantp arg)
810                (push arg syms)
811                (let ((sym (gensym)))
812                  (cond ((single-valued-p arg)
813                         (push sym syms)
814                         (push (list sym arg) lets))
815                        (t
816                         (push (list 'VALUES-LIST sym) syms)
817                         (push (list sym (list 'MULTIPLE-VALUE-LIST arg)) lets))))))
818          (list 'LET* (nreverse lets) (list* 'THROW (nreverse syms))))
819        form)))
820
821(defknown p1-throw (t) t)
822(defun p1-throw (form)
823  (let ((new-form (rewrite-throw form)))
824    (when (neq new-form form)
825      (return-from p1-throw (p1 new-form))))
826  (list* 'THROW (mapcar #'p1 (cdr form))))
827
828(defknown rewrite-function-call (t) t)
829(defun rewrite-function-call (form)
830  (let ((args (cdr form)))
831    (if (unsafe-p args)
832        (let ((arg1 (car args)))
833          (cond ((and (consp arg1) (eq (car arg1) 'GO))
834                 arg1)
835                (t
836                 (let ((syms ())
837                       (lets ()))
838                   ;; Preserve the order of evaluation of the arguments!
839                   (dolist (arg args)
840                     (cond ((constantp arg)
841                            (push arg syms))
842                           ((and (consp arg) (eq (car arg) 'GO))
843                            (return-from rewrite-function-call
844                                         (list 'LET* (nreverse lets) arg)))
845                           (t
846                            (let ((sym (gensym)))
847                              (push sym syms)
848                              (push (list sym arg) lets)))))
849                   (list 'LET* (nreverse lets) (list* (car form) (nreverse syms)))))))
850        form)))
851
852(defknown p1-function-call (t) t)
853(defun p1-function-call (form)
854  (let ((new-form (rewrite-function-call form)))
855    (when (neq new-form form)
856;;       (let ((*print-structure* nil))
857;;         (format t "old form = ~S~%" form)
858;;         (format t "new form = ~S~%" new-form))
859      (return-from p1-function-call (p1 new-form))))
860  (let* ((op (car form))
861         (local-function (find-local-function op)))
862    (cond (local-function
863;;            (format t "p1 local call to ~S~%" op)
864;;            (format t "inline-p = ~S~%" (inline-p op))
865
866           (when (and *enable-inline-expansion* (inline-p op))
867             (let ((expansion (local-function-inline-expansion local-function)))
868               (when expansion
869                 (let ((explain *explain*))
870                   (when (and explain (memq :calls explain))
871                     (format t ";   inlining call to local function ~S~%" op)))
872                 (return-from p1-function-call (p1 (expand-inline form expansion))))))
873
874           ;; FIXME
875           (dformat t "local function assumed not single-valued~%")
876           (setf (compiland-%single-valued-p *current-compiland*) nil)
877
878           (let ((variable (local-function-variable local-function)))
879             (when variable
880               (dformat t "p1 ~S used non-locally~%" (variable-name variable))
881               (setf (variable-used-non-locally-p variable) t))))
882          (t
883           ;; Not a local function call.
884           (dformat t "p1 non-local call to ~S~%" op)
885           (unless (single-valued-p form)
886;;                (format t "not single-valued op = ~S~%" op)
887             (setf (compiland-%single-valued-p *current-compiland*) nil)))))
888  (p1-default form))
889
890(defknown p1 (t) t)
891(defun p1 (form)
892  (cond ((symbolp form)
893         (let (value)
894           (cond ((null form)
895                  form)
896                 ((eq form t)
897                  form)
898                 ((keywordp form)
899                  form)
900                 ((and (constantp form)
901                       (progn
902                         (setf value (symbol-value form))
903                         (or (numberp value)
904                             (stringp value)
905                             (pathnamep value))))
906                  (setf form value))
907                 (t
908                  (let ((variable (find-visible-variable form)))
909                    (when (null variable)
910          (unless (or (special-variable-p form)
911                                  (memq form *undefined-variables*))
912      (compiler-style-warn "Undefined variable: ~S" form)
913      (push form *undefined-variables*))
914                      (setf variable (make-variable :name form :special-p t))
915                      (push variable *visible-variables*))
916                    (let ((ref (make-var-ref variable)))
917                      (unless (variable-special-p variable)
918                        (when (variable-ignore-p variable)
919                          (compiler-style-warn
920                           "Variable ~S is read even though it was declared to be ignored."
921                           (variable-name variable)))
922                        (push ref (variable-references variable))
923                        (incf (variable-reads variable))
924                        (cond ((eq (variable-compiland variable) *current-compiland*)
925                               (dformat t "p1: read ~S~%" form))
926                              (t
927                               (dformat t "p1: non-local read ~S variable-compiland = ~S current compiland = ~S~%"
928                                        form
929                                        (compiland-name (variable-compiland variable))
930                                        (compiland-name *current-compiland*))
931                               (setf (variable-used-non-locally-p variable) t))))
932                      (setf form ref)))
933                  form))))
934        ((atom form)
935         form)
936        (t
937         (let ((op (%car form))
938               handler)
939           (cond ((symbolp op)
940                  (when (compiler-macro-function op)
941                    (unless (notinline-p op)
942                      (multiple-value-bind (expansion expanded-p)
943                          (compiler-macroexpand form)
944                        ;; Fall through if no change...
945                        (when expanded-p
946                          (return-from p1 (p1 expansion))))))
947                  (cond ((setf handler (get op 'p1-handler))
948                         (funcall handler form))
949                        ((macro-function op *compile-file-environment*)
950                         (p1 (macroexpand form *compile-file-environment*)))
951                        ((special-operator-p op)
952                         (compiler-unsupported "P1: unsupported special operator ~S" op))
953                        (t
954                         (p1-function-call form))))
955                 ((and (consp op) (eq (%car op) 'LAMBDA))
956                  (p1 (list* 'FUNCALL form)))
957                 (t
958                  form))))))
959
960(defun install-p1-handler (symbol handler)
961  (setf (get symbol 'p1-handler) handler))
962
963(defun initialize-p1-handlers ()
964  (dolist (pair '((AND                  p1-default)
965                  (BLOCK                p1-block)
966                  (CATCH                p1-catch)
967                  (DECLARE              identity)
968                  (EVAL-WHEN            p1-eval-when)
969                  (FLET                 p1-flet)
970                  (FUNCALL              p1-funcall)
971                  (FUNCTION             p1-function)
972                  (GO                   p1-go)
973                  (IF                   p1-if)
974                  (LABELS               p1-labels)
975                  (LAMBDA               p1-lambda)
976                  (LET                  p1-let/let*)
977                  (LET*                 p1-let/let*)
978                  (LOAD-TIME-VALUE      identity)
979                  (LOCALLY              p1-locally)
980                  (MULTIPLE-VALUE-BIND  p1-m-v-b)
981                  (MULTIPLE-VALUE-CALL  p1-default)
982                  (MULTIPLE-VALUE-LIST  p1-default)
983                  (MULTIPLE-VALUE-PROG1 p1-default)
984                  (OR                   p1-default)
985                  (PROGN                p1-default)
986                  (PROGV                p1-progv)
987                  (QUOTE                p1-quote)
988                  (RETURN-FROM          p1-return-from)
989                  (SETQ                 p1-setq)
990                  (SYMBOL-MACROLET      identity)
991                  (TAGBODY              p1-tagbody)
992                  (THE                  p1-the)
993                  (THROW                p1-throw)
994                  (TRULY-THE            p1-truly-the)
995                  (UNWIND-PROTECT       p1-unwind-protect)))
996    (install-p1-handler (%car pair) (%cadr pair))))
997
998(initialize-p1-handlers)
999
1000(defun p1-compiland (compiland)
1001;;   (format t "p1-compiland name = ~S~%" (compiland-name compiland))
1002  (let ((form (compiland-lambda-expression compiland)))
1003    (aver (eq (car form) 'LAMBDA))
1004    (setf form (rewrite-lambda form))
1005    (process-optimization-declarations (cddr form))
1006
1007    (let* ((lambda-list (cadr form))
1008           (body (cddr form)))
1009
1010      (let* ((closure (make-closure `(lambda ,lambda-list nil) nil))
1011             (syms (sys::varlist closure))
1012             (vars nil))
1013        (dolist (sym syms)
1014          (let ((var (make-variable :name sym
1015                                    :special-p (special-variable-p sym))))
1016            (push var vars)
1017            (push var *all-variables*)))
1018        (setf (compiland-arg-vars compiland) (nreverse vars))
1019        (let ((*visible-variables* *visible-variables*))
1020          (dolist (var (compiland-arg-vars compiland))
1021            (push var *visible-variables*))
1022          (let ((free-specials (process-declarations-for-vars body *visible-variables*)))
1023            (dolist (var free-specials)
1024              (push var *visible-variables*)))
1025          (setf (compiland-p1-result compiland)
1026                (list* 'LAMBDA lambda-list (p1-body body))))))))
1027
1028(provide "COMPILER-PASS1")
Note: See TracBrowser for help on using the repository browser.