source: branches/0.15.x/abcl/src/org/armedbear/lisp/compiler-pass1.lisp

Last change on this file was 11923, checked in by ehuelsmann, 16 years ago

Our input is already preprocessed. Don't do it again.
(Eliminated in GENERATE-INLINE-EXPANSION).

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