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

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

Small refactoring: introduce a centralized definition of
"enclosed by a block which associates extensive cleanup
with a transfer of control exception".

Also some reordering of functions in jvm.lisp.

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