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

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

Use the fact that tags have lexical scope:
if they're not used, don't generate comparisons
for tags which are not used.

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