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

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

LET/LET* bindings can be (in case of a CONS)
of length 1 or 2 (not only 2).

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