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

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

Convert LABELS BLOCK-NODEs to LABELS-NODEs.

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