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

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

Compilation P1:

  • Set up a CATCH block before processing the subforms.
  • Exclude the unwinding forms from the UNWIND-PROTECT block: they are themselves not protected by their own block.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 42.6 KB
Line 
1;;; compiler-pass1.lisp
2;;;
3;;; Copyright (C) 2003-2008 Peter Graves
4;;; $Id: compiler-pass1.lisp 11825 2009-05-03 19:50:20Z 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         (block (make-block-node '(CATCH)))
276         ;; our subform processors need to know
277         ;; they're enclosed in a CATCH block
278         (*blocks* (cons block *blocks*))
279         (result '()))
280    (dolist (subform body)
281      (let ((op (and (consp subform) (%car subform))))
282        (push (p1 subform) result)
283        (when (memq op '(GO RETURN-FROM THROW))
284          (return))))
285    (setf result (nreverse result))
286    (when (and (null (cdr result))
287               (consp (car result))
288               (eq (caar result) 'GO))
289      (return-from p1-catch (car result)))
290    (push tag result)
291    (push 'CATCH result)
292    (setf (block-form block) result)
293    block))
294
295(defun p1-unwind-protect (form)
296  (if (= (length form) 2)
297      (p1 (second form)) ; No cleanup forms: (unwind-protect (...)) => (...)
298      (let* ((block (make-block-node '(UNWIND-PROTECT)))
299             ;; a bit of jumping through hoops...
300             (unprotected-forms (p1-body (cddr form)))
301             ;; ... because only the protected form is
302             ;; protected by the UNWIND-PROTECT block
303             (*blocks* (cons block *blocks*))
304             (protected-form (p1 (cadr form))))
305        (setf (block-form block)
306              `(unwind-protect ,protected-form
307                 ,@unprotected-forms))
308        block)))
309
310(defknown p1-return-from (t) t)
311(defun p1-return-from (form)
312  (let* ((name (second form))
313         (block (find-block name)))
314    (when (null block)
315      (compiler-error "RETURN-FROM ~S: no block named ~S is currently visible."
316                      name name))
317    (dformat t "p1-return-from block = ~S~%" (block-name block))
318    (setf (block-return-p block) t)
319    (cond ((eq (block-compiland block) *current-compiland*)
320           ;; Local case. If the RETURN is nested inside an UNWIND-PROTECT
321           ;; which is inside the block we're returning from, we'll do a non-
322           ;; local return anyway so that UNWIND-PROTECT can catch it and run
323           ;; its cleanup forms.
324           (dformat t "*blocks* = ~S~%" (mapcar #'block-name *blocks*))
325           (let ((protected (enclosed-by-protected-block-p block)))
326             (dformat t "p1-return-from protected = ~S~%" protected)
327             (when protected
328               (setf (block-non-local-return-p block) t))))
329          (t
330           (setf (block-non-local-return-p block) t)))
331    (when (block-non-local-return-p block)
332      (dformat t "non-local return from block ~S~%" (block-name block))))
333  (list* 'RETURN-FROM (cadr form) (mapcar #'p1 (cddr form))))
334
335(defun p1-tagbody (form)
336  (let* ((block (make-block-node '(TAGBODY)))
337         (*blocks* (cons block *blocks*))
338         (*visible-tags* *visible-tags*)
339         (body (cdr form)))
340    ;; Make all the tags visible before processing the body forms.
341    (dolist (subform body)
342      (when (or (symbolp subform) (integerp subform))
343        (let* ((tag (make-tag :name subform :label (gensym) :block block)))
344          (push tag *visible-tags*))))
345    (let ((new-body '())
346          (live t))
347      (dolist (subform body)
348        (cond ((or (symbolp subform) (integerp subform))
349               (push subform new-body)
350               (setf live t))
351              ((not live)
352               ;; Nothing to do.
353               )
354              (t
355               (when (and (consp subform)
356                          (memq (%car subform) '(GO RETURN-FROM THROW)))
357                 ;; Subsequent subforms are unreachable until we see another
358                 ;; tag.
359                 (setf live nil))
360               (push (p1 subform) new-body))))
361      (setf (block-form block) (list* 'TAGBODY (nreverse new-body))))
362    block))
363
364(defknown p1-go (t) t)
365(defun p1-go (form)
366  (let* ((name (cadr form))
367         (tag (find-tag name)))
368    (unless tag
369      (error "p1-go: tag not found: ~S" name))
370    (let ((tag-block (tag-block tag)))
371      (cond ((eq (tag-compiland tag) *current-compiland*)
372             ;; Does the GO leave an enclosing UNWIND-PROTECT?
373             (if (enclosed-by-protected-block-p tag-block)
374                 (setf (block-non-local-go-p tag-block) t)
375                 ;; non-local GO's ensure environment restoration
376                 ;; find out about this local GO
377                 (when (null (block-needs-environment-restoration tag-block))
378                   (setf (block-needs-environment-restoration tag-block)
379                         (enclosed-by-environment-setting-block-p tag-block)))))
380            (t
381             (setf (block-non-local-go-p tag-block) t)))))
382  form)
383
384(defun validate-function-name (name)
385  (unless (or (symbolp name) (setf-function-name-p name))
386    (compiler-error "~S is not a valid function name." name)))
387
388(defmacro with-local-functions-for-flet/labels
389    (form local-functions-var lambda-list-var name-var body-var body1 body2)
390  `(progn (incf (compiland-children *current-compiland*) (length (cadr ,form)))
391    (let ((*visible-variables* *visible-variables*)
392    (*local-functions* *local-functions*)
393    (*current-compiland* *current-compiland*)
394    (,local-functions-var '()))
395      (dolist (definition (cadr ,form))
396        (let ((,name-var (car definition))
397        (,lambda-list-var (cadr definition)))
398    (validate-function-name ,name-var)
399    (let* ((,body-var (cddr definition))
400           (compiland (make-compiland :name ,name-var
401              :parent *current-compiland*)))
402      ,@body1)))
403      (setf ,local-functions-var (nreverse ,local-functions-var))
404      ;; Make the local functions visible.
405      (dolist (local-function ,local-functions-var)
406        (push local-function *local-functions*)
407        (let ((variable (local-function-variable local-function)))
408    (when variable
409      (push variable *visible-variables*))))
410      ,@body2)))
411
412(defun split-decls (forms specific-vars)
413  (let ((other-decls nil)
414        (specific-decls nil))
415    (dolist (form forms)
416      (unless (and (consp form) (eq (car form) 'DECLARE)) ; shouldn't happen
417        (return))
418      (dolist (decl (cdr form))
419        (case (car decl)
420          ((OPTIMIZE DECLARATION DYNAMIC-EXTENT FTYPE INLINE NOTINLINE)
421           (push (list 'DECLARE decl) other-decls))
422          (SPECIAL
423           (dolist (name (cdr decl))
424             (if (memq name specific-vars)
425                 (push `(DECLARE (SPECIAL ,name)) specific-decls)
426                 (push `(DECLARE (SPECIAL ,name)) other-decls))))
427          (TYPE
428           (dolist (name (cddr decl))
429             (if (memq name specific-vars)
430                 (push `(DECLARE (TYPE ,(cadr decl) ,name)) specific-decls)
431                 (push `(DECLARE (TYPE ,(cadr decl) ,name)) other-decls))))
432          (t
433           (dolist (name (cdr decl))
434             (if (memq name specific-vars)
435                 (push `(DECLARE (,(car decl) ,name)) specific-decls)
436                 (push `(DECLARE (,(car decl) ,name)) other-decls)))))))
437    (values (nreverse other-decls)
438            (nreverse specific-decls))))
439
440(defun rewrite-aux-vars (form)
441  (let* ((lambda-list (cadr form))
442         (aux-p (memq '&AUX lambda-list))
443         (lets (cdr aux-p))
444         aux-vars)
445    (unless aux-p
446      ;; no rewriting required
447      (return-from rewrite-aux-vars form))
448    (multiple-value-bind (body decls)
449        (parse-body (cddr form))
450      (dolist (form lets)
451        (cond ((consp form)
452               (push (car form) aux-vars))
453              (t
454               (push form aux-vars))))
455      (setf lambda-list (subseq lambda-list 0 (position '&AUX lambda-list)))
456      (multiple-value-bind (let-decls lambda-decls)
457          (split-decls decls (lambda-list-names lambda-list))
458        `(lambda ,lambda-list
459           ,@lambda-decls
460           (let* ,lets
461             ,@let-decls
462             ,@body))))))
463
464(defun rewrite-lambda (form)
465  (setf form (rewrite-aux-vars form))
466  (let* ((lambda-list (cadr form)))
467    (if (not (or (memq '&optional lambda-list)
468                 (memq '&key lambda-list)))
469        ;; no need to rewrite: no arguments with possible initforms anyway
470        form
471      (multiple-value-bind (body decls doc)
472          (parse-body (cddr form))
473        (let (state let-bindings new-lambda-list
474                    (non-constants 0))
475          (do* ((vars lambda-list (cdr vars))
476                (var (car vars) (car vars)))
477               ((endp vars))
478            (push (car vars) new-lambda-list)
479            (let ((replacement (gensym)))
480              (flet ((parse-compound-argument (arg)
481                       "Returns the values NAME, KEYWORD, INITFORM, INITFORM-P,
482   SUPPLIED-P and SUPPLIED-P-P assuming ARG is a compound argument."
483                       (destructuring-bind
484                             (name &optional (initform nil initform-supplied-p)
485                                   (supplied-p nil supplied-p-supplied-p))
486                           (if (listp arg) arg (list arg))
487                         (if (listp name)
488                             (values (cadr name) (car name)
489                                     initform initform-supplied-p
490                                     supplied-p supplied-p-supplied-p)
491                             (values name (make-keyword name)
492                                     initform initform-supplied-p
493                                     supplied-p supplied-p-supplied-p)))))
494                (case var
495                  (&optional (setf state :optional))
496                  (&key (setf state :key))
497                  ((&whole &environment &rest &body &allow-other-keys)
498                   ;; do nothing special
499                   )
500                  (t
501                   (cond
502                     ((atom var)
503                      (setf (car new-lambda-list)
504                            (if (eq state :key)
505                                (list (list (make-keyword var) replacement))
506                                replacement))
507                      (push (list var replacement) let-bindings))
508                     ((constantp (second var))
509                      ;; so, we must have a consp-type var we're looking at
510                      ;; and it has a constantp initform
511                      (multiple-value-bind
512                            (name keyword initform initform-supplied-p
513                                  supplied-p supplied-p-supplied-p)
514                          (parse-compound-argument var)
515                        (let ((var-form (if (eq state :key)
516                                            (list keyword replacement)
517                                            replacement))
518                              (supplied-p-replacement (gensym)))
519                          (setf (car new-lambda-list)
520                                (cond
521                                  ((not initform-supplied-p)
522                                   (list var-form))
523                                  ((not supplied-p-supplied-p)
524                                   (list var-form initform))
525                                  (t
526                                   (list var-form initform
527                                         supplied-p-replacement))))
528                          (push (list name replacement) let-bindings)
529                          ;; if there was a 'supplied-p' variable, it might
530                          ;; be used in the declarations. Since those will be
531                          ;; moved below the LET* block, we need to move the
532                          ;; supplied-p parameter too.
533                          (when supplied-p-supplied-p
534                            (push (list supplied-p supplied-p-replacement)
535                                  let-bindings)))))
536                     (t
537                      (incf non-constants)
538                      ;; this is either a keyword or an optional argument
539                      ;; with a non-constantp initform
540                      (multiple-value-bind
541                            (name keyword initform initform-supplied-p
542                                  supplied-p supplied-p-supplied-p)
543                          (parse-compound-argument var)
544                        (declare (ignore initform-supplied-p))
545                        (let ((var-form (if (eq state :key)
546                                            (list keyword replacement)
547                                            replacement))
548                              (supplied-p-replacement (gensym)))
549                          (setf (car new-lambda-list)
550                                (list var-form nil supplied-p-replacement))
551                          (push (list name `(if ,supplied-p-replacement
552                                                ,replacement ,initform))
553                                let-bindings)
554                          (when supplied-p-supplied-p
555                            (push (list supplied-p supplied-p-replacement)
556                                  let-bindings)))))))))))
557          (if (zerop non-constants)
558              ;; there was no reason to rewrite...
559              form
560              (let ((rv
561                     `(lambda ,(nreverse new-lambda-list)
562                        ,@(when doc (list doc))
563                        (let* ,(nreverse let-bindings)
564                          ,@decls ,@body))))
565                rv)))))))
566
567(defun p1-flet (form)
568  (with-local-functions-for-flet/labels
569      form local-functions lambda-list name body
570      ((let ((local-function (make-local-function :name name
571             :compiland compiland)))
572   (multiple-value-bind (body decls) (parse-body body)
573     (let* ((block-name (fdefinition-block-name name))
574      (lambda-expression
575                   (rewrite-lambda
576       `(lambda ,lambda-list ,@decls (block ,block-name ,@body))))
577      (*visible-variables* *visible-variables*)
578      (*local-functions* *local-functions*)
579      (*current-compiland* compiland))
580       (setf (compiland-lambda-expression compiland) lambda-expression)
581       (setf (local-function-inline-expansion local-function)
582       (generate-inline-expansion block-name lambda-list body))
583       (p1-compiland compiland)))
584   (when *closure-variables*
585     (let ((variable (make-variable :name (gensym))))
586       (setf (local-function-variable local-function) variable)
587       (push variable *all-variables*)))
588   (push local-function local-functions)))
589      ((with-saved-compiler-policy
590     (process-optimization-declarations (cddr form))
591   (list* (car form) local-functions (p1-body (cddr form)))))))
592
593
594(defun p1-labels (form)
595  (with-local-functions-for-flet/labels
596      form local-functions lambda-list name body
597      ((let* ((variable (make-variable :name (gensym)))
598        (local-function (make-local-function :name name
599               :compiland compiland
600               :variable variable)))
601   (multiple-value-bind (body decls) (parse-body body)
602     (setf (compiland-lambda-expression compiland)
603                 (rewrite-lambda
604     `(lambda ,lambda-list ,@decls (block ,name ,@body)))))
605   (push variable *all-variables*)
606   (push local-function local-functions)))
607      ((dolist (local-function local-functions)
608   (let ((*visible-variables* *visible-variables*)
609         (*current-compiland* (local-function-compiland local-function)))
610     (p1-compiland (local-function-compiland local-function))))
611       (list* (car form) local-functions (p1-body (cddr form))))))
612
613(defknown p1-funcall (t) t)
614(defun p1-funcall (form)
615  (unless (> (length form) 1)
616    (compiler-warn "Wrong number of arguments for ~A." (car form))
617    (return-from p1-funcall form))
618  (let ((function-form (%cadr form)))
619    (when (and (consp function-form)
620               (eq (%car function-form) 'FUNCTION))
621      (let ((name (%cadr function-form)))
622;;         (format t "p1-funcall name = ~S~%" name)
623        (let ((source-transform (source-transform name)))
624          (when source-transform
625;;             (format t "found source transform for ~S~%" name)
626;;             (format t "old form = ~S~%" form)
627;;             (let ((new-form (expand-source-transform form)))
628;;               (when (neq new-form form)
629;;                 (format t "new form = ~S~%" new-form)
630;;                 (return-from p1-funcall (p1 new-form))))
631            (let ((new-form (expand-source-transform (list* name (cddr form)))))
632;;               (format t "new form = ~S~%" new-form)
633              (return-from p1-funcall (p1 new-form)))
634            )))))
635  ;; Otherwise...
636  (p1-function-call form))
637
638(defun p1-function (form)
639  (let ((form (copy-tree form))
640        local-function)
641    (cond ((and (consp (cadr form))
642                (or (eq (caadr form) 'LAMBDA)
643                    (eq (caadr form) 'NAMED-LAMBDA)))
644           (let* ((named-lambda-p (eq (caadr form) 'NAMED-LAMBDA))
645                  (named-lambda-form (when named-lambda-p
646                                       (cadr form)))
647                  (name (when named-lambda-p
648                          (cadr named-lambda-form)))
649                  (lambda-form (if named-lambda-p
650                                   (cons 'LAMBDA (cddr named-lambda-form))
651                                   (cadr form)))
652                  (lambda-list (cadr lambda-form))
653                  (body (cddr lambda-form))
654                  (compiland (make-compiland :name (if named-lambda-p
655                                                       name (gensym "ANONYMOUS-LAMBDA-"))
656                                             :lambda-expression lambda-form
657                                             :parent *current-compiland*)))
658             (when *current-compiland*
659               (incf (compiland-children *current-compiland*)))
660             (multiple-value-bind (body decls)
661                 (parse-body body)
662               (setf (compiland-lambda-expression compiland)
663                     ;; if there still was a doc-string present, remove it
664                     (rewrite-lambda
665                      `(lambda ,lambda-list ,@decls ,@body)))
666               (let ((*visible-variables* *visible-variables*)
667                     (*current-compiland* compiland))
668                 (p1-compiland compiland)))
669             (list 'FUNCTION compiland)))
670          ((setf local-function (find-local-function (cadr form)))
671           (dformat t "p1-function local function ~S~%" (cadr form))
672           (let ((variable (local-function-variable local-function)))
673             (when variable
674                 (dformat t "p1-function ~S used non-locally~%" (variable-name variable))
675                 (setf (variable-used-non-locally-p variable) t)))
676           form)
677          (t
678           form))))
679
680(defun p1-lambda (form)
681  (let* ((lambda-list (cadr form)))
682    (when (or (memq '&optional lambda-list)
683              (memq '&key lambda-list))
684      (let ((state nil))
685        (dolist (arg lambda-list)
686          (cond ((memq arg lambda-list-keywords)
687                 (setf state arg))
688                ((memq state '(&optional &key))
689                 (when (and (consp arg)
690                            (not (constantp (second arg))))
691                   (compiler-unsupported
692                    "P1-LAMBDA: can't handle optional argument with non-constant initform.")))))))
693    (p1-function (list 'FUNCTION
694                        (rewrite-lambda form)))))
695
696(defun p1-eval-when (form)
697  (list* (car form) (cadr form) (mapcar #'p1 (cddr form))))
698
699(defknown p1-progv (t) t)
700(defun p1-progv (form)
701  ;; We've already checked argument count in PRECOMPILE-PROGV.
702
703  ;; ### FIXME: we need to return a block here, so that
704  ;;  (local) GO in p2 can restore the lastSpecialBinding environment
705  (let ((new-form (rewrite-progv form)))
706    (when (neq new-form form)
707      (return-from p1-progv (p1 new-form))))
708  (let ((symbols-form (cadr form))
709        (values-form (caddr form))
710        (body (cdddr form)))
711    `(progv ,(p1 symbols-form) ,(p1 values-form) ,@(p1-body body))))
712
713(defknown rewrite-progv (t) t)
714(defun rewrite-progv (form)
715  (let ((symbols-form (cadr form))
716        (values-form (caddr form))
717        (body (cdddr form)))
718    (cond ((or (unsafe-p symbols-form) (unsafe-p values-form))
719           (let ((g1 (gensym))
720                 (g2 (gensym)))
721             `(let ((,g1 ,symbols-form)
722                    (,g2 ,values-form))
723                (progv ,g1 ,g2 ,@body))))
724          (t
725           form))))
726
727(defun p1-quote (form)
728  (unless (= (length form) 2)
729    (compiler-error "Wrong number of arguments for special operator ~A (expected 1, but received ~D)."
730                    'QUOTE
731                    (1- (length form))))
732  (let ((arg (%cadr form)))
733    (if (or (numberp arg) (characterp arg))
734        arg
735        form)))
736
737(defun p1-setq (form)
738  (unless (= (length form) 3)
739    (error "Too many arguments for SETQ."))
740  (let ((arg1 (%cadr form))
741        (arg2 (%caddr form)))
742    (let ((variable (find-visible-variable arg1)))
743      (if variable
744          (progn
745            (when (variable-ignore-p variable)
746              (compiler-style-warn
747               "Variable ~S is assigned even though it was declared to be ignored."
748               (variable-name variable)))
749            (incf (variable-writes variable))
750            (cond ((eq (variable-compiland variable) *current-compiland*)
751                   (dformat t "p1-setq: write ~S~%" arg1))
752                  (t
753                   (dformat t "p1-setq: non-local write ~S~%" arg1)
754                   (setf (variable-used-non-locally-p variable) t))))
755          (dformat t "p1-setq: unknown variable ~S~%" arg1)))
756    (list 'SETQ arg1 (p1 arg2))))
757
758(defun p1-the (form)
759  (unless (= (length form) 3)
760    (compiler-error "Wrong number of arguments for special operator ~A (expected 2, but received ~D)."
761                    'THE
762                    (1- (length form))))
763  (let ((type (%cadr form))
764        (expr (%caddr form)))
765    (cond ((and (listp type) (eq (car type) 'VALUES))
766           ;; FIXME
767           (p1 expr))
768          ((= *safety* 3)
769           (let* ((sym (gensym))
770                  (new-expr `(let ((,sym ,expr))
771                               (require-type ,sym ',type)
772                               ,sym)))
773             (p1 new-expr)))
774          (t
775           (list 'THE type (p1 expr))))))
776
777(defun p1-truly-the (form)
778  (unless (= (length form) 3)
779    (compiler-error "Wrong number of arguments for special operator ~A (expected 2, but received ~D)."
780                    'TRULY-THE
781                    (1- (length form))))
782  (list 'TRULY-THE (%cadr form) (p1 (%caddr form))))
783
784(defknown unsafe-p (t) t)
785(defun unsafe-p (args)
786  (cond ((node-p args)
787         (unsafe-p (node-form args)))
788        ((atom args)
789         nil)
790        (t
791         (case (%car args)
792           (QUOTE
793            nil)
794           (LAMBDA
795            nil)
796           ((RETURN-FROM GO CATCH THROW UNWIND-PROTECT BLOCK)
797            t)
798           (t
799            (dolist (arg args)
800              (when (unsafe-p arg)
801                (return t))))))))
802
803(defknown rewrite-throw (t) t)
804(defun rewrite-throw (form)
805  (let ((args (cdr form)))
806    (if (unsafe-p args)
807        (let ((syms ())
808              (lets ()))
809          ;; Tag.
810          (let ((arg (first args)))
811            (if (constantp arg)
812                (push arg syms)
813                (let ((sym (gensym)))
814                  (push sym syms)
815                  (push (list sym arg) lets))))
816          ;; Result. "If the result-form produces multiple values, then all the
817          ;; values are saved."
818          (let ((arg (second args)))
819            (if (constantp arg)
820                (push arg syms)
821                (let ((sym (gensym)))
822                  (cond ((single-valued-p arg)
823                         (push sym syms)
824                         (push (list sym arg) lets))
825                        (t
826                         (push (list 'VALUES-LIST sym) syms)
827                         (push (list sym (list 'MULTIPLE-VALUE-LIST arg)) lets))))))
828          (list 'LET* (nreverse lets) (list* 'THROW (nreverse syms))))
829        form)))
830
831(defknown p1-throw (t) t)
832(defun p1-throw (form)
833  (let ((new-form (rewrite-throw form)))
834    (when (neq new-form form)
835      (return-from p1-throw (p1 new-form))))
836  (list* 'THROW (mapcar #'p1 (cdr form))))
837
838(defknown rewrite-function-call (t) t)
839(defun rewrite-function-call (form)
840  (let ((args (cdr form)))
841    (if (unsafe-p args)
842        (let ((arg1 (car args)))
843          (cond ((and (consp arg1) (eq (car arg1) 'GO))
844                 arg1)
845                (t
846                 (let ((syms ())
847                       (lets ()))
848                   ;; Preserve the order of evaluation of the arguments!
849                   (dolist (arg args)
850                     (cond ((constantp arg)
851                            (push arg syms))
852                           ((and (consp arg) (eq (car arg) 'GO))
853                            (return-from rewrite-function-call
854                                         (list 'LET* (nreverse lets) arg)))
855                           (t
856                            (let ((sym (gensym)))
857                              (push sym syms)
858                              (push (list sym arg) lets)))))
859                   (list 'LET* (nreverse lets) (list* (car form) (nreverse syms)))))))
860        form)))
861
862(defknown p1-function-call (t) t)
863(defun p1-function-call (form)
864  (let ((new-form (rewrite-function-call form)))
865    (when (neq new-form form)
866;;       (let ((*print-structure* nil))
867;;         (format t "old form = ~S~%" form)
868;;         (format t "new form = ~S~%" new-form))
869      (return-from p1-function-call (p1 new-form))))
870  (let* ((op (car form))
871         (local-function (find-local-function op)))
872    (cond (local-function
873;;            (format t "p1 local call to ~S~%" op)
874;;            (format t "inline-p = ~S~%" (inline-p op))
875
876           (when (and *enable-inline-expansion* (inline-p op))
877             (let ((expansion (local-function-inline-expansion local-function)))
878               (when expansion
879                 (let ((explain *explain*))
880                   (when (and explain (memq :calls explain))
881                     (format t ";   inlining call to local function ~S~%" op)))
882                 (return-from p1-function-call (p1 (expand-inline form expansion))))))
883
884           ;; FIXME
885           (dformat t "local function assumed not single-valued~%")
886           (setf (compiland-%single-valued-p *current-compiland*) nil)
887
888           (let ((variable (local-function-variable local-function)))
889             (when variable
890               (dformat t "p1 ~S used non-locally~%" (variable-name variable))
891               (setf (variable-used-non-locally-p variable) t))))
892          (t
893           ;; Not a local function call.
894           (dformat t "p1 non-local call to ~S~%" op)
895           (unless (single-valued-p form)
896;;                (format t "not single-valued op = ~S~%" op)
897             (setf (compiland-%single-valued-p *current-compiland*) nil)))))
898  (p1-default form))
899
900(defknown p1 (t) t)
901(defun p1 (form)
902  (cond ((symbolp form)
903         (let (value)
904           (cond ((null form)
905                  form)
906                 ((eq form t)
907                  form)
908                 ((keywordp form)
909                  form)
910                 ((and (constantp form)
911                       (progn
912                         (setf value (symbol-value form))
913                         (or (numberp value)
914                             (stringp value)
915                             (pathnamep value))))
916                  (setf form value))
917                 (t
918                  (let ((variable (find-visible-variable form)))
919                    (when (null variable)
920          (unless (or (special-variable-p form)
921                                  (memq form *undefined-variables*))
922      (compiler-style-warn "Undefined variable: ~S" form)
923      (push form *undefined-variables*))
924                      (setf variable (make-variable :name form :special-p t))
925                      (push variable *visible-variables*))
926                    (let ((ref (make-var-ref variable)))
927                      (unless (variable-special-p variable)
928                        (when (variable-ignore-p variable)
929                          (compiler-style-warn
930                           "Variable ~S is read even though it was declared to be ignored."
931                           (variable-name variable)))
932                        (push ref (variable-references variable))
933                        (incf (variable-reads variable))
934                        (cond ((eq (variable-compiland variable) *current-compiland*)
935                               (dformat t "p1: read ~S~%" form))
936                              (t
937                               (dformat t "p1: non-local read ~S variable-compiland = ~S current compiland = ~S~%"
938                                        form
939                                        (compiland-name (variable-compiland variable))
940                                        (compiland-name *current-compiland*))
941                               (setf (variable-used-non-locally-p variable) t))))
942                      (setf form ref)))
943                  form))))
944        ((atom form)
945         form)
946        (t
947         (let ((op (%car form))
948               handler)
949           (cond ((symbolp op)
950                  (when (compiler-macro-function op)
951                    (unless (notinline-p op)
952                      (multiple-value-bind (expansion expanded-p)
953                          (compiler-macroexpand form)
954                        ;; Fall through if no change...
955                        (when expanded-p
956                          (return-from p1 (p1 expansion))))))
957                  (cond ((setf handler (get op 'p1-handler))
958                         (funcall handler form))
959                        ((macro-function op *compile-file-environment*)
960                         (p1 (macroexpand form *compile-file-environment*)))
961                        ((special-operator-p op)
962                         (compiler-unsupported "P1: unsupported special operator ~S" op))
963                        (t
964                         (p1-function-call form))))
965                 ((and (consp op) (eq (%car op) 'LAMBDA))
966                  (p1 (list* 'FUNCALL form)))
967                 (t
968                  form))))))
969
970(defun install-p1-handler (symbol handler)
971  (setf (get symbol 'p1-handler) handler))
972
973(defun initialize-p1-handlers ()
974  (dolist (pair '((AND                  p1-default)
975                  (BLOCK                p1-block)
976                  (CATCH                p1-catch)
977                  (DECLARE              identity)
978                  (EVAL-WHEN            p1-eval-when)
979                  (FLET                 p1-flet)
980                  (FUNCALL              p1-funcall)
981                  (FUNCTION             p1-function)
982                  (GO                   p1-go)
983                  (IF                   p1-if)
984                  (LABELS               p1-labels)
985                  (LAMBDA               p1-lambda)
986                  (LET                  p1-let/let*)
987                  (LET*                 p1-let/let*)
988                  (LOAD-TIME-VALUE      identity)
989                  (LOCALLY              p1-locally)
990                  (MULTIPLE-VALUE-BIND  p1-m-v-b)
991                  (MULTIPLE-VALUE-CALL  p1-default)
992                  (MULTIPLE-VALUE-LIST  p1-default)
993                  (MULTIPLE-VALUE-PROG1 p1-default)
994                  (OR                   p1-default)
995                  (PROGN                p1-default)
996                  (PROGV                p1-progv)
997                  (QUOTE                p1-quote)
998                  (RETURN-FROM          p1-return-from)
999                  (SETQ                 p1-setq)
1000                  (SYMBOL-MACROLET      identity)
1001                  (TAGBODY              p1-tagbody)
1002                  (THE                  p1-the)
1003                  (THROW                p1-throw)
1004                  (TRULY-THE            p1-truly-the)
1005                  (UNWIND-PROTECT       p1-unwind-protect)))
1006    (install-p1-handler (%car pair) (%cadr pair))))
1007
1008(initialize-p1-handlers)
1009
1010(defun p1-compiland (compiland)
1011;;   (format t "p1-compiland name = ~S~%" (compiland-name compiland))
1012  (let ((form (compiland-lambda-expression compiland)))
1013    (aver (eq (car form) 'LAMBDA))
1014    (setf form (rewrite-lambda form))
1015    (process-optimization-declarations (cddr form))
1016
1017    (let* ((lambda-list (cadr form))
1018           (body (cddr form)))
1019
1020      (let* ((closure (make-closure `(lambda ,lambda-list nil) nil))
1021             (syms (sys::varlist closure))
1022             (vars nil))
1023        (dolist (sym syms)
1024          (let ((var (make-variable :name sym
1025                                    :special-p (special-variable-p sym))))
1026            (push var vars)
1027            (push var *all-variables*)))
1028        (setf (compiland-arg-vars compiland) (nreverse vars))
1029        (let ((*visible-variables* *visible-variables*))
1030          (dolist (var (compiland-arg-vars compiland))
1031            (push var *visible-variables*))
1032          (let ((free-specials (process-declarations-for-vars body *visible-variables*)))
1033            (dolist (var free-specials)
1034              (push var *visible-variables*)))
1035          (setf (compiland-p1-result compiland)
1036                (list* 'LAMBDA lambda-list (p1-body body))))))))
1037
1038(provide "COMPILER-PASS1")
Note: See TracBrowser for help on using the repository browser.