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

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

Add synchronization like in Java through the special operator SYNCHRONIZED-ON.

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