source: branches/unsafe-p-removal/abcl/src/org/armedbear/lisp/compiler-pass1.lisp @ 13160

Last change on this file since 13160 was 13160, checked in by ehuelsmann, 12 years ago

Further transition to unsafety detection in pass2.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 57.0 KB
Line 
1;;; compiler-pass1.lisp
2;;;
3;;; Copyright (C) 2003-2008 Peter Graves
4;;; $Id: compiler-pass1.lisp 13160 2011-01-20 12:20:29Z 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 (name lambda-list body
49            &optional (args nil args-p))
50    "Generates code that can be used to expand a named local function inline. It can work either per-function (no args provided) or per-call."
51    (if args-p
52  (expand-function-call-inline
53   nil lambda-list
54   (copy-tree `((block ,name ,@body)))
55   args)
56  (cond ((intersection lambda-list
57           '(&optional &rest &key &allow-other-keys &aux)
58           :test #'eq)
59         nil)
60        (t
61         (setf body (copy-tree body))
62         (list 'LAMBDA lambda-list
63         (list* 'BLOCK name body))))))
64    ) ; EVAL-WHEN
65
66;;; Pass 1.
67
68(defun parse-lambda-list (lambda-list)
69  "Breaks the lambda list into the different elements, returning the values
70
71 required-vars
72 optional-vars
73 key-vars
74 key-p
75 rest-var
76 allow-other-keys-p
77 aux-vars
78 whole-var
79 env-var
80
81where each of the vars returned is a list with these elements:
82
83 var      - the actual variable name
84 initform - the init form if applicable; optional, keyword and aux vars
85 p-var    - variable indicating presence
86 keyword  - the keyword argument to match against
87
88"
89  (let ((state :req)
90        req opt key rest whole env aux key-p allow-others-p)
91    (dolist (arg lambda-list)
92      (case arg
93        (&optional (setf state :opt))
94        (&key (setf state :key
95                    key-p t))
96        (&rest (setf state :rest))
97        (&aux (setf state :aux))
98        (&allow-other-keys (setf state :none
99                                 allow-others-p t))
100        (&whole (setf state :whole))
101        (&environment (setf state :env))
102        (t
103         (case state
104           (:req (push arg req))
105           (:rest (setf rest (list arg)
106                        state :none))
107           (:env (setf env (list arg)
108                       state :req))
109           (:whole (setf whole (list arg)
110                         state :req))
111           (:none
112            (error "Invalid lambda list: argument found in :none state."))
113           (:opt
114            (cond
115              ((symbolp arg)
116               (push (list arg nil nil nil) opt))
117              ((consp arg)
118               (push (list (car arg) (cadr arg) (caddr arg)) opt))
119              (t
120               (error "Invalid state."))))
121           (:aux
122            (cond
123              ((symbolp arg)
124               (push (list arg nil nil nil) aux))
125              ((consp arg)
126               (push (list (car arg) (cadr arg) nil nil) aux))
127              (t
128               (error "Invalid :aux state."))))
129           (:key
130            (cond
131              ((symbolp arg)
132               (push (list arg nil nil (sys::keywordify arg)) key))
133              ((and (consp arg)
134                    (consp (car arg)))
135               (push (list (cadar arg) (cadr arg) (caddr arg) (caar arg)) key))
136              ((consp arg)
137               (push (list (car arg) (cadr arg) (caddr arg)
138                           (sys::keywordify (car arg))) key))
139              (t
140               (error "Invalid :key state."))))
141           (t (error "Invalid state found."))))))
142    (values
143     (nreverse req)
144     (nreverse opt)
145     (nreverse key)
146     key-p
147     rest allow-others-p
148     (nreverse aux) whole env)))
149
150(define-condition lambda-list-mismatch (error)
151  ((mismatch-type :reader lambda-list-mismatch-type :initarg :mismatch-type)))
152
153(defmacro push-argument-binding (var form temp-bindings bindings)
154  (let ((g (gensym)))
155    `(let ((,g (gensym (symbol-name '#:temp))))
156       (push (list ,g ,form) ,temp-bindings)
157       (push (list ,var ,g) ,bindings))))
158
159(defun match-lambda-list (parsed-lambda-list arguments)
160  (flet ((pop-required-argument ()
161     (if (null arguments)
162         (error 'lambda-list-mismatch :mismatch-type :too-few-arguments)
163         (pop arguments)))
164   (var (var-info) (car var-info))
165   (initform (var-info) (cadr var-info))
166   (p-var (var-info) (caddr var-info)))
167    (destructuring-bind (req opt key key-p rest allow-others-p aux whole env)
168  parsed-lambda-list
169      (declare (ignore whole env))
170      (let (req-bindings temp-bindings bindings ignorables)
171  ;;Required arguments.
172  (setf req-bindings
173        (loop :for var :in req :collect `(,var ,(pop-required-argument))))
174
175  ;;Optional arguments.
176  (when opt
177    (dolist (var-info opt)
178      (if arguments
179    (progn
180      (push-argument-binding (var var-info) (pop arguments)
181           temp-bindings bindings)
182      (when (p-var var-info)
183        (push `(,(p-var var-info) t) bindings)))
184    (progn
185      (push `(,(var var-info) ,(initform var-info)) bindings)
186      (when (p-var var-info)
187        (push `(,(p-var var-info) nil) bindings)))))
188    (setf bindings (nreverse bindings)))
189 
190  (unless (or key-p rest (null arguments))
191    (error 'lambda-list-mismatch :mismatch-type :too-many-arguments))
192
193  ;;Keyword and rest arguments.
194  (if key-p
195      (multiple-value-bind (kbindings ktemps kignor)
196    (match-keyword-and-rest-args 
197     key allow-others-p rest arguments)
198        (setf bindings (append bindings kbindings)
199        temp-bindings (append temp-bindings ktemps)
200        ignorables (append kignor ignorables)))
201      (when rest
202        (let (rest-binding)
203    (push-argument-binding (var rest) `(list ,@arguments)
204               temp-bindings rest-binding)
205    (setf bindings (append bindings rest-binding)))))
206  ;;Aux parameters.
207  (when aux
208    (setf bindings
209    `(,@bindings
210      ,@(loop
211           :for var-info :in aux
212           :collect `(,(var var-info) ,(initform var-info))))))
213  (values (append req-bindings temp-bindings bindings)
214    ignorables)))))
215
216(defun match-keyword-and-rest-args (key allow-others-p rest arguments)
217  (flet ((var (var-info) (car var-info))
218   (initform (var-info) (cadr var-info))
219   (p-var (var-info) (caddr var-info))
220   (keyword (var-info) (cadddr var-info)))
221    (when (oddp (list-length arguments))
222      (error 'lambda-list-mismatch
223       :mismatch-type :odd-number-of-keyword-arguments))
224   
225    (let (temp-bindings bindings other-keys-found-p ignorables already-seen
226    args)
227      ;;If necessary, make up a fake argument to hold :allow-other-keys,
228      ;;needed later. This also handles nicely:
229      ;;  3.4.1.4.1 Suppressing Keyword Argument Checking
230      ;;third statement.
231      (unless (find :allow-other-keys key :key #'keyword)
232  (let ((allow-other-keys-temp (gensym (symbol-name :allow-other-keys))))
233    (push allow-other-keys-temp ignorables)
234    (push (list allow-other-keys-temp nil nil :allow-other-keys) key)))
235     
236      ;;First, let's bind the keyword arguments that have been passed by
237      ;;the caller. If we encounter an unknown keyword, remember it.
238      ;;As per the above, :allow-other-keys will never be considered
239      ;;an unknown keyword.
240      (loop
241   :for var :in arguments :by #'cddr
242   :for value :in (cdr arguments) :by #'cddr
243   :do (let ((var-info (find var key :key #'keyword)))
244         (if (and var-info (not (member var already-seen)))
245       ;;var is one of the declared keyword arguments
246       (progn
247         (push-argument-binding (var var-info) value
248              temp-bindings bindings)
249         (when (p-var var-info)
250           (push `(,(p-var var-info) t) bindings))
251         (push var args)
252         (push (var var-info) args)
253         (push var already-seen))
254       (let ((g (gensym)))
255         (push `(,g ,value) temp-bindings)
256         (push var args)
257         (push g args)
258         (push g ignorables)
259         (unless var-info
260           (setf other-keys-found-p t))))))
261     
262      ;;Then, let's bind those arguments that haven't been passed in
263      ;;to their default value, in declaration order.
264      (let (defaults)
265  (loop
266     :for var-info :in key
267     :do (unless (find (var var-info) bindings :key #'car)
268     (push `(,(var var-info) ,(initform var-info)) defaults)
269     (when (p-var var-info)
270       (push `(,(p-var var-info) nil) defaults))))
271  (setf bindings (append (nreverse defaults) bindings)))
272     
273      ;;If necessary, check for unrecognized keyword arguments.
274      (when (and other-keys-found-p (not allow-others-p))
275  (if (loop
276         :for var :in arguments :by #'cddr
277         :if (eq var :allow-other-keys)
278         :do (return t))
279      ;;We know that :allow-other-keys has been passed, so we
280      ;;can access the binding for it and be sure to get the
281      ;;value passed by the user and not an initform.
282      (let* ((arg (var (find :allow-other-keys key :key #'keyword)))
283       (binding (find arg bindings :key #'car))
284       (form (cadr binding)))
285        (if (constantp form)
286      (unless (eval form)
287        (error 'lambda-list-mismatch
288         :mismatch-type :unknown-keyword))
289      (setf (cadr binding)
290      `(or ,(cadr binding)
291           (error 'program-error
292            "Unrecognized keyword argument")))))
293      ;;TODO: it would be nice to report *which* keyword
294      ;;is unknown
295      (error 'lambda-list-mismatch :mismatch-type :unknown-keyword)))
296      (when rest
297  (setf bindings (append bindings `((,(var rest) (list ,@(nreverse args)))))))
298      (values bindings temp-bindings ignorables))))
299
300#||test for the above
301(handler-case
302    (let ((lambda-list
303     (multiple-value-list
304      (jvm::parse-lambda-list
305       '(a b &optional (c 42) &rest foo &key (bar c) baz ((kaz kuz) bar))))))
306      (jvm::match-lambda-list
307       lambda-list
308       '((print 1) 3 (print 32) :bar 2)))
309  (jvm::lambda-list-mismatch (x) (jvm::lambda-list-mismatch-type x)))
310||#
311
312(defun expand-function-call-inline (form lambda-list body args)
313  (handler-case
314      (multiple-value-bind (bindings ignorables)
315    (match-lambda-list (multiple-value-list
316            (parse-lambda-list lambda-list))
317           args)
318  `(let* ,bindings
319     ,@(when ignorables
320       `((declare (ignorable ,@ignorables))))
321     ,@body))
322    (lambda-list-mismatch (x)
323      (compiler-warn "Invalid function call: ~S (mismatch type: ~A)"
324         form (lambda-list-mismatch-type x))
325      form)))
326
327;; Returns a list of declared free specials, if any are found.
328(declaim (ftype (function (list list block-node) list)
329                process-declarations-for-vars))
330(defun process-declarations-for-vars (body variables block)
331  (let ((free-specials '()))
332    (dolist (subform body)
333      (unless (and (consp subform) (eq (%car subform) 'DECLARE))
334        (return))
335      (let ((decls (%cdr subform)))
336        (dolist (decl decls)
337          (case (car decl)
338            ((DYNAMIC-EXTENT FTYPE INLINE NOTINLINE OPTIMIZE)
339             ;; Nothing to do here.
340             )
341            ((IGNORE IGNORABLE)
342             (process-ignore/ignorable (%car decl) (%cdr decl) variables))
343            (SPECIAL
344             (dolist (name (%cdr decl))
345               (let ((variable (find-variable name variables)))
346                 (cond ((and variable
347                             ;; see comment below (and DO-ALL-SYMBOLS.11)
348                             (eq (variable-compiland variable)
349                                 *current-compiland*))
350                        (setf (variable-special-p variable) t))
351                       (t
352                        (dformat t "adding free special ~S~%" name)
353                        (push (make-variable :name name :special-p t
354                                             :block block)
355                              free-specials))))))
356            (TYPE
357             (dolist (name (cddr decl))
358               (let ((variable (find-variable name variables)))
359                 (when (and variable
360                            ;; Don't apply a declaration in a local function to
361                            ;; a variable defined in its parent. For an example,
362                            ;; see CREATE-GREEDY-NO-ZERO-MATCHER in cl-ppcre.
363                            ;; FIXME suboptimal, since we ignore the declaration
364                            (eq (variable-compiland variable)
365                                *current-compiland*))
366                   (setf (variable-declared-type variable)
367                         (make-compiler-type (cadr decl)))))))
368            (t
369             (dolist (name (cdr decl))
370               (let ((variable (find-variable name variables)))
371                 (when variable
372                   (setf (variable-declared-type variable)
373                         (make-compiler-type (%car decl)))))))))))
374    free-specials))
375
376(defun check-name (name)
377  ;; FIXME Currently this error is signalled by the precompiler.
378  (unless (symbolp name)
379    (compiler-error "The variable ~S is not a symbol." name))
380  (when (constantp name)
381    (compiler-error "The name of the variable ~S is already in use to name a constant." name))
382  name)
383
384(declaim (ftype (function (t) t) p1-body))
385(defun p1-body (body)
386  (declare (optimize speed))
387  (let ((tail body))
388    (loop
389      (when (endp tail)
390        (return))
391      (setf (car tail) (p1 (%car tail)))
392      (setf tail (%cdr tail))))
393  body)
394
395(defknown p1-default (t) t)
396(declaim (inline p1-default))
397(defun p1-default (form)
398  (setf (cdr form) (p1-body (cdr form)))
399  form)
400
401(defmacro p1-let/let*-vars
402    (block varlist variables-var var body1 body2)
403  (let ((varspec (gensym))
404  (initform (gensym))
405  (name (gensym)))
406    `(let ((,variables-var ()))
407       (dolist (,varspec ,varlist)
408   (cond ((consp ,varspec)
409                ;; Even though the precompiler already signals this
410                ;; error, double checking can't hurt; after all, we're
411                ;; also rewriting &AUX into LET* bindings.
412    (unless (<= 1 (length ,varspec) 2)
413      (compiler-error "The LET/LET* binding specification ~S is invalid."
414          ,varspec))
415    (let* ((,name (%car ,varspec))
416           (,initform (p1 (%cadr ,varspec)))
417           (,var (make-variable :name (check-name ,name)
418                                            :initform ,initform
419                                            :block ,block)))
420      (when (neq ,initform (cadr ,varspec))
421        (setf (cadr ,varspec) ,initform))
422      (push ,var ,variables-var)
423      ,@body1))
424         (t
425    (let ((,var (make-variable :name (check-name ,varspec)
426                                           :block ,block)))
427      (push ,var ,variables-var)
428      ,@body1))))
429       ,@body2)))
430
431(defknown p1-let-vars (t) t)
432(defun p1-let-vars (block varlist)
433  (p1-let/let*-vars block
434   varlist vars var
435   ()
436   ((setf vars (nreverse vars))
437    (dolist (variable vars)
438      (push variable *visible-variables*)
439      (push variable *all-variables*))
440    vars)))
441
442(defknown p1-let*-vars (t) t)
443(defun p1-let*-vars (block varlist)
444  (p1-let/let*-vars block
445   varlist vars var
446   ((push var *visible-variables*)
447    (push var *all-variables*))
448   ((nreverse vars))))
449
450(defun p1-let/let* (form)
451  (declare (type cons form))
452  (let* ((*visible-variables* *visible-variables*)
453         (block (make-let/let*-node))
454   (*block* block)
455         (op (%car form))
456         (varlist (cadr form))
457         (body (cddr form)))
458    (aver (or (eq op 'LET) (eq op 'LET*)))
459    (when (eq op 'LET)
460      ;; Convert to LET* if possible.
461      (if (null (cdr varlist))
462          (setf op 'LET*)
463          (dolist (varspec varlist (setf op 'LET*))
464            (or (atom varspec)
465                (constantp (cadr varspec))
466                (eq (car varspec) (cadr varspec))
467                (return)))))
468    (let ((vars (if (eq op 'LET)
469                    (p1-let-vars block varlist)
470                    (p1-let*-vars block varlist))))
471      ;; Check for globally declared specials.
472      (dolist (variable vars)
473        (when (special-variable-p (variable-name variable))
474          (setf (variable-special-p variable) t
475                (let-environment-register block) t)))
476      ;; For processing declarations, we want to walk the variable list from
477      ;; last to first, since declarations apply to the last-defined variable
478      ;; with the specified name.
479      (setf (let-free-specials block)
480            (process-declarations-for-vars body (reverse vars) block))
481      (setf (let-vars block) vars)
482      ;; Make free specials visible.
483      (dolist (variable (let-free-specials block))
484        (push variable *visible-variables*)))
485    (let ((*blocks* (cons block *blocks*)))
486      (setf body (p1-body body)))
487    (setf (let-form block) (list* op varlist body))
488    block))
489
490(defun p1-locally (form)
491  (let* ((*visible-variables* *visible-variables*)
492         (block (make-locally-node))
493   (*block* block)
494         (free-specials (process-declarations-for-vars (cdr form) nil block)))
495    (setf (locally-free-specials block) free-specials)
496    (dolist (special free-specials)
497;;       (format t "p1-locally ~S is special~%" name)
498      (push special *visible-variables*))
499    (let ((*blocks* (cons block *blocks*)))
500      (setf (locally-form block)
501            (list* 'LOCALLY (p1-body (cdr form))))
502      block)))
503
504(defknown p1-m-v-b (t) t)
505(defun p1-m-v-b (form)
506  (when (= (length (cadr form)) 1)
507    (let ((new-form `(let* ((,(caadr form) ,(caddr form))) ,@(cdddr form))))
508      (return-from p1-m-v-b (p1-let/let* new-form))))
509  (let* ((*visible-variables* *visible-variables*)
510         (block (make-m-v-b-node))
511   (*block* block)
512         (varlist (cadr form))
513         ;; Process the values-form first. ("The scopes of the name binding and
514         ;; declarations do not include the values-form.")
515         (values-form (p1 (caddr form)))
516         (*blocks* (cons block *blocks*))
517         (body (cdddr form)))
518    (let ((vars ()))
519      (dolist (symbol varlist)
520        (let ((var (make-variable :name symbol :block block)))
521          (push var vars)
522          (push var *visible-variables*)
523          (push var *all-variables*)))
524      ;; Check for globally declared specials.
525      (dolist (variable vars)
526        (when (special-variable-p (variable-name variable))
527          (setf (variable-special-p variable) t
528                (m-v-b-environment-register block) t)))
529      (setf (m-v-b-free-specials block)
530            (process-declarations-for-vars body vars block))
531      (dolist (special (m-v-b-free-specials block))
532        (push special *visible-variables*))
533      (setf (m-v-b-vars block) (nreverse vars)))
534    (setf body (p1-body body))
535    (setf (m-v-b-form block)
536          (list* 'MULTIPLE-VALUE-BIND varlist values-form body))
537    block))
538
539(defun p1-block (form)
540  (let* ((block (make-block-node (cadr form)))
541   (*block* block)
542         (*blocks* (cons block *blocks*)))
543    (setf (cddr form) (p1-body (cddr form)))
544    (setf (block-form block) form)
545    (when (block-non-local-return-p block)
546      ;; Add a closure variable for RETURN-FROM to use
547      (push (setf (block-id-variable block)
548                  (make-variable :name (gensym)
549                                 :block block
550                                 :used-non-locally-p t))
551            *all-variables*))
552    block))
553
554(defun p1-catch (form)
555  (let* ((tag (p1 (cadr form)))
556         (body (cddr form))
557         (block (make-catch-node))
558   (*block* block)
559         ;; our subform processors need to know
560         ;; they're enclosed in a CATCH block
561         (*blocks* (cons block *blocks*))
562         (result '()))
563    (dolist (subform body)
564      (let ((op (and (consp subform) (%car subform))))
565        (push (p1 subform) result)
566        (when (memq op '(GO RETURN-FROM THROW))
567          (return))))
568    (setf result (nreverse result))
569    (when (and (null (cdr result))
570               (consp (car result))
571               (eq (caar result) 'GO))
572      (return-from p1-catch (car result)))
573    (push tag result)
574    (push 'CATCH result)
575    (setf (catch-form block) result)
576    block))
577
578(defun p1-threads-synchronized-on (form)
579  (let* ((synchronized-object (p1 (cadr form)))
580         (body (cddr form))
581         (block (make-synchronized-node))
582   (*block* block)
583         (*blocks* (cons block *blocks*))
584         result)
585    (dolist (subform body)
586      (let ((op (and (consp subform) (%car subform))))
587        (push (p1 subform) result)
588        (when (memq op '(GO RETURN-FROM THROW))
589          (return))))
590    (setf (synchronized-form block)
591          (list* 'threads:synchronized-on synchronized-object
592                 (nreverse result)))
593    block))
594
595(defun p1-unwind-protect (form)
596  (if (= (length form) 2)
597      (p1 (second form)) ; No cleanup forms: (unwind-protect (...)) => (...)
598
599      ;; in order to compile the cleanup forms twice (see
600      ;; p2-unwind-protect-node), we need to p1 them twice; p1 outcomes
601      ;; can be compiled (in the same compiland?) only once.
602      ;;
603      ;; However, p1 transforms the forms being processed, so, we
604      ;; need to copy the forms to create a second copy.
605      (let* ((block (make-unwind-protect-node))
606       (*block* block)
607             ;; a bit of jumping through hoops...
608             (unwinding-forms (p1-body (copy-tree (cddr form))))
609             (unprotected-forms (p1-body (cddr form)))
610             ;; ... because only the protected form is
611             ;; protected by the UNWIND-PROTECT block
612             (*blocks* (cons block *blocks*))
613             (protected-form (p1 (cadr form))))
614        (setf (unwind-protect-form block)
615              `(unwind-protect ,protected-form
616                 (progn ,@unwinding-forms)
617                 ,@unprotected-forms))
618        block)))
619
620(defknown p1-return-from (t) t)
621(defun p1-return-from (form)
622  (let* ((name (second form))
623         (block (find-block name))
624         non-local-p)
625    (when (null block)
626      (compiler-error "RETURN-FROM ~S: no block named ~S is currently visible."
627                      name name))
628    (dformat t "p1-return-from block = ~S~%" (block-name block))
629    (cond ((eq (block-compiland block) *current-compiland*)
630           ;; Local case. If the RETURN is nested inside an UNWIND-PROTECT
631           ;; which is inside the block we're returning from, we'll do a non-
632           ;; local return anyway so that UNWIND-PROTECT can catch it and run
633           ;; its cleanup forms.
634           ;;(dformat t "*blocks* = ~S~%" (mapcar #'node-name *blocks*))
635           (let ((protected (enclosed-by-protected-block-p block)))
636             (dformat t "p1-return-from protected = ~S~%" protected)
637             (if protected
638                 (setf (block-non-local-return-p block) t
639                       non-local-p t)
640                 ;; non-local GO's ensure environment restoration
641                 ;; find out about this local GO
642                 (when (null (block-needs-environment-restoration block))
643                   (setf (block-needs-environment-restoration block)
644                         (enclosed-by-environment-setting-block-p block))))))
645          (t
646           (setf (block-non-local-return-p block) t
647                 non-local-p t)))
648    (when (block-non-local-return-p block)
649      (dformat t "non-local return from block ~S~%" (block-name block)))
650    (let ((value-form (p1 (caddr form))))
651      (push value-form (block-return-value-forms block))
652      (make-jump-node (list 'RETURN-FROM name value-form)
653                      non-local-p block))))
654
655(defun p1-tagbody (form)
656  (let* ((block (make-tagbody-node))
657   (*block* block)
658         (*blocks* (cons block *blocks*))
659         (*visible-tags* *visible-tags*)
660         (local-tags '())
661         (body (cdr form)))
662    ;; Make all the tags visible before processing the body forms.
663    (dolist (subform body)
664      (when (or (symbolp subform) (integerp subform))
665        (let* ((tag (make-tag :name subform :label (gensym) :block block)))
666          (push tag local-tags)
667          (push tag *visible-tags*))))
668    (let ((new-body '())
669          (live t))
670      (dolist (subform body)
671        (cond ((or (symbolp subform) (integerp subform))
672               (push subform new-body)
673               (push (find subform local-tags :key #'tag-name :test #'eql)
674                     (tagbody-tags block))
675               (setf live t))
676              ((not live)
677               ;; Nothing to do.
678               )
679              (t
680               (when (and (consp subform)
681                          (memq (%car subform) '(GO RETURN-FROM THROW)))
682                 ;; Subsequent subforms are unreachable until we see another
683                 ;; tag.
684                 (setf live nil))
685               (push (p1 subform) new-body))))
686      (setf (tagbody-form block) (list* 'TAGBODY (nreverse new-body))))
687    (when (some #'tag-used-non-locally (tagbody-tags block))
688      (push (setf (tagbody-id-variable block)
689                  (make-variable :name (gensym)
690                                 :block block
691                                 :used-non-locally-p t))
692            *all-variables*))
693    block))
694
695(defknown p1-go (t) t)
696(defun p1-go (form)
697  (let* ((name (cadr form))
698         (tag (find-tag name)))
699    (unless tag
700      (error "p1-go: tag not found: ~S" name))
701    (setf (tag-used tag) t)
702    (let ((tag-block (tag-block tag))
703          non-local-p)
704      (cond ((eq (tag-compiland tag) *current-compiland*)
705             ;; Does the GO leave an enclosing UNWIND-PROTECT or CATCH?
706             (if (enclosed-by-protected-block-p tag-block)
707                 (setf (tagbody-non-local-go-p tag-block) t
708                       (tag-used-non-locally tag) t
709                       non-local-p t)
710                 ;; non-local GO's ensure environment restoration
711                 ;; find out about this local GO
712                 (when (null (tagbody-needs-environment-restoration tag-block))
713                   (setf (tagbody-needs-environment-restoration tag-block)
714                         (enclosed-by-environment-setting-block-p tag-block)))))
715            (t
716             (setf (tagbody-non-local-go-p tag-block) t
717                   (tag-used-non-locally tag) t
718                   non-local-p t)))
719      (make-jump-node form non-local-p tag-block tag))))
720
721(defun validate-function-name (name)
722  (unless (or (symbolp name) (setf-function-name-p name))
723    (compiler-error "~S is not a valid function name." name)))
724
725(defmacro with-local-functions-for-flet/labels
726    (form local-functions-var lambda-list-var name-var body-var body1 body2)
727  `(progn (incf (compiland-children *current-compiland*) (length (cadr ,form)))
728    (let ((*visible-variables* *visible-variables*)
729    (*local-functions* *local-functions*)
730    (*current-compiland* *current-compiland*)
731    (,local-functions-var '()))
732      (dolist (definition (cadr ,form))
733        (let ((,name-var (car definition))
734        (,lambda-list-var (cadr definition)))
735    (validate-function-name ,name-var)
736    (let* ((,body-var (cddr definition))
737           (compiland (make-compiland :name ,name-var
738              :parent *current-compiland*)))
739      ,@body1)))
740      (setf ,local-functions-var (nreverse ,local-functions-var))
741      ;; Make the local functions visible.
742      (dolist (local-function ,local-functions-var)
743        (push local-function *local-functions*)
744        (let ((variable (local-function-variable local-function)))
745    (when variable
746      (push variable *visible-variables*))))
747      ,@body2)))
748
749(defun split-decls (forms specific-vars)
750  (let ((other-decls nil)
751        (specific-decls nil))
752    (dolist (form forms)
753      (unless (and (consp form) (eq (car form) 'DECLARE)) ; shouldn't happen
754        (return))
755      (dolist (decl (cdr form))
756        (case (car decl)
757          ((OPTIMIZE DECLARATION DYNAMIC-EXTENT FTYPE INLINE NOTINLINE)
758           (push (list 'DECLARE decl) other-decls))
759          (SPECIAL
760           (dolist (name (cdr decl))
761             (if (memq name specific-vars)
762                 (push `(DECLARE (SPECIAL ,name)) specific-decls)
763                 (push `(DECLARE (SPECIAL ,name)) other-decls))))
764          (TYPE
765           (dolist (name (cddr decl))
766             (if (memq name specific-vars)
767                 (push `(DECLARE (TYPE ,(cadr decl) ,name)) specific-decls)
768                 (push `(DECLARE (TYPE ,(cadr decl) ,name)) other-decls))))
769          (t
770           (dolist (name (cdr decl))
771             (if (memq name specific-vars)
772                 (push `(DECLARE (,(car decl) ,name)) specific-decls)
773                 (push `(DECLARE (,(car decl) ,name)) other-decls)))))))
774    (values (nreverse other-decls)
775            (nreverse specific-decls))))
776
777(defun rewrite-aux-vars (form)
778  (let* ((lambda-list (cadr form))
779         (aux-p (memq '&AUX lambda-list))
780         (lets (cdr aux-p))
781         aux-vars)
782    (unless aux-p
783      ;; no rewriting required
784      (return-from rewrite-aux-vars form))
785    (multiple-value-bind (body decls)
786        (parse-body (cddr form))
787      (dolist (form lets)
788        (cond ((consp form)
789               (push (car form) aux-vars))
790              (t
791               (push form aux-vars))))
792      (setf lambda-list (subseq lambda-list 0 (position '&AUX lambda-list)))
793      (multiple-value-bind (let-decls lambda-decls)
794          (split-decls decls (lambda-list-names lambda-list))
795        `(lambda ,lambda-list
796           ,@lambda-decls
797           (let* ,lets
798             ,@let-decls
799             ,@body))))))
800
801(defun rewrite-lambda (form)
802  (setf form (rewrite-aux-vars form))
803  (let* ((lambda-list (cadr form)))
804    (if (not (or (memq '&optional lambda-list)
805                 (memq '&key lambda-list)))
806        ;; no need to rewrite: no arguments with possible initforms anyway
807        form
808      (multiple-value-bind (body decls doc)
809          (parse-body (cddr form))
810        (let (state let-bindings new-lambda-list
811                    (non-constants 0))
812          (do* ((vars lambda-list (cdr vars))
813                (var (car vars) (car vars)))
814               ((endp vars))
815            (push (car vars) new-lambda-list)
816            (let ((replacement (gensym)))
817              (flet ((parse-compound-argument (arg)
818                       "Returns the values NAME, KEYWORD, INITFORM, INITFORM-P,
819   SUPPLIED-P and SUPPLIED-P-P assuming ARG is a compound argument."
820                       (destructuring-bind
821                             (name &optional (initform nil initform-supplied-p)
822                                   (supplied-p nil supplied-p-supplied-p))
823                           (if (listp arg) arg (list arg))
824                         (if (listp name)
825                             (values (cadr name) (car name)
826                                     initform initform-supplied-p
827                                     supplied-p supplied-p-supplied-p)
828                             (values name (make-keyword name)
829                                     initform initform-supplied-p
830                                     supplied-p supplied-p-supplied-p)))))
831                (case var
832                  (&optional (setf state :optional))
833                  (&key (setf state :key))
834                  ((&whole &environment &rest &body &allow-other-keys)
835                   ;; do nothing special
836                   )
837                  (t
838                   (cond
839                     ((atom var)
840                      (setf (car new-lambda-list)
841                            (if (eq state :key)
842                                (list (list (make-keyword var) replacement))
843                                replacement))
844                      (push (list var replacement) let-bindings))
845                     ((constantp (second var))
846                      ;; so, we must have a consp-type var we're looking at
847                      ;; and it has a constantp initform
848                      (multiple-value-bind
849                            (name keyword initform initform-supplied-p
850                                  supplied-p supplied-p-supplied-p)
851                          (parse-compound-argument var)
852                        (let ((var-form (if (eq state :key)
853                                            (list keyword replacement)
854                                            replacement))
855                              (supplied-p-replacement (gensym)))
856                          (setf (car new-lambda-list)
857                                (cond
858                                  ((not initform-supplied-p)
859                                   (list var-form))
860                                  ((not supplied-p-supplied-p)
861                                   (list var-form initform))
862                                  (t
863                                   (list var-form initform
864                                         supplied-p-replacement))))
865                          (push (list name replacement) let-bindings)
866                          ;; if there was a 'supplied-p' variable, it might
867                          ;; be used in the declarations. Since those will be
868                          ;; moved below the LET* block, we need to move the
869                          ;; supplied-p parameter too.
870                          (when supplied-p-supplied-p
871                            (push (list supplied-p supplied-p-replacement)
872                                  let-bindings)))))
873                     (t
874                      (incf non-constants)
875                      ;; this is either a keyword or an optional argument
876                      ;; with a non-constantp initform
877                      (multiple-value-bind
878                            (name keyword initform initform-supplied-p
879                                  supplied-p supplied-p-supplied-p)
880                          (parse-compound-argument var)
881                        (declare (ignore initform-supplied-p))
882                        (let ((var-form (if (eq state :key)
883                                            (list keyword replacement)
884                                            replacement))
885                              (supplied-p-replacement (gensym)))
886                          (setf (car new-lambda-list)
887                                (list var-form nil supplied-p-replacement))
888                          (push (list name `(if ,supplied-p-replacement
889                                                ,replacement ,initform))
890                                let-bindings)
891                          (when supplied-p-supplied-p
892                            (push (list supplied-p supplied-p-replacement)
893                                  let-bindings)))))))))))
894          (if (zerop non-constants)
895              ;; there was no reason to rewrite...
896              form
897              (let ((rv
898                     `(lambda ,(nreverse new-lambda-list)
899                        ,@(when doc (list doc))
900                        (let* ,(nreverse let-bindings)
901                          ,@decls ,@body))))
902                rv)))))))
903
904(defun p1-flet (form)
905  (with-local-functions-for-flet/labels
906      form local-functions lambda-list name body
907      ((let ((local-function (make-local-function :name name
908                                                  :compiland compiland))
909       (definition (cons lambda-list body)))
910   (multiple-value-bind (body decls) (parse-body body)
911     (let* ((block-name (fdefinition-block-name name))
912      (lambda-expression
913       (rewrite-lambda `(lambda ,lambda-list ,@decls (block ,block-name ,@body))))
914      (*visible-variables* *visible-variables*)
915      (*local-functions* *local-functions*)
916      (*current-compiland* compiland))
917       (setf (compiland-lambda-expression compiland) lambda-expression)
918       (setf (local-function-definition local-function)
919       (copy-tree definition))
920       ;(setf (local-function-inline-expansion local-function)
921       ;(generate-inline-expansion block-name lambda-list body))
922       (p1-compiland compiland)))
923   (push local-function local-functions)))
924      ((with-saved-compiler-policy
925   (process-optimization-declarations (cddr form))
926   (let* ((block (make-flet-node))
927    (*block* block)
928    (*blocks* (cons block *blocks*))
929    (body (cddr form))
930    (*visible-variables* *visible-variables*))
931     (setf (flet-free-specials block)
932     (process-declarations-for-vars body nil block))
933     (dolist (special (flet-free-specials block))
934       (push special *visible-variables*))
935     (setf (flet-form block)
936     (list* (car form)
937      (remove-if (lambda (fn)
938             (and (inline-p (local-function-name fn))
939            (not (local-function-references-needed-p fn))))
940           local-functions)
941      (p1-body (cddr form))))
942     block)))))
943
944
945(defun p1-labels (form)
946  (with-local-functions-for-flet/labels
947      form local-functions lambda-list name body
948      ((let* ((variable (make-variable :name (gensym)))
949        (local-function (make-local-function :name name
950               :compiland compiland
951               :variable variable))
952              (block-name (fdefinition-block-name name)))
953   (setf (local-function-definition local-function)
954         (copy-tree (cons lambda-list body)))
955   (multiple-value-bind (body decls) (parse-body body)
956     (setf (compiland-lambda-expression compiland)
957                 (rewrite-lambda
958     `(lambda ,lambda-list ,@decls (block ,block-name ,@body)))))
959   (push variable *all-variables*)
960   (push local-function local-functions)))
961      ((dolist (local-function local-functions)
962   (let ((*visible-variables* *visible-variables*)
963         (*current-compiland* (local-function-compiland local-function)))
964     (p1-compiland (local-function-compiland local-function))))
965       (let* ((block (make-labels-node))
966        (*block* block)
967              (*blocks* (cons block *blocks*))
968              (body (cddr form))
969              (*visible-variables* *visible-variables*))
970         (setf (labels-free-specials block)
971               (process-declarations-for-vars body nil block))
972         (dolist (special (labels-free-specials block))
973           (push special *visible-variables*))
974         (setf (labels-form block)
975               (list* (car form) local-functions (p1-body (cddr form))))
976         block))))
977
978(defknown p1-funcall (t) t)
979(defun p1-funcall (form)
980  (unless (> (length form) 1)
981    (compiler-warn "Wrong number of arguments for ~A." (car form))
982    (return-from p1-funcall form))
983  (let ((function-form (%cadr form)))
984    (when (and (consp function-form)
985               (eq (%car function-form) 'FUNCTION))
986      (let ((name (%cadr function-form)))
987;;         (format t "p1-funcall name = ~S~%" name)
988        (let ((source-transform (source-transform name)))
989          (when source-transform
990;;             (format t "found source transform for ~S~%" name)
991;;             (format t "old form = ~S~%" form)
992;;             (let ((new-form (expand-source-transform form)))
993;;               (when (neq new-form form)
994;;                 (format t "new form = ~S~%" new-form)
995;;                 (return-from p1-funcall (p1 new-form))))
996            (let ((new-form (expand-source-transform (list* name (cddr form)))))
997;;               (format t "new form = ~S~%" new-form)
998              (return-from p1-funcall (p1 new-form)))
999            )))))
1000  ;; Otherwise...
1001  (p1-function-call form))
1002
1003(defun p1-function (form)
1004  (let ((form (copy-tree form))
1005        local-function)
1006    (cond ((and (consp (cadr form))
1007                (or (eq (caadr form) 'LAMBDA)
1008                    (eq (caadr form) 'NAMED-LAMBDA)))
1009           (let* ((named-lambda-p (eq (caadr form) 'NAMED-LAMBDA))
1010                  (named-lambda-form (when named-lambda-p
1011                                       (cadr form)))
1012                  (name (when named-lambda-p
1013                          (cadr named-lambda-form)))
1014                  (lambda-form (if named-lambda-p
1015                                   (cons 'LAMBDA (cddr named-lambda-form))
1016                                   (cadr form)))
1017                  (lambda-list (cadr lambda-form))
1018                  (body (cddr lambda-form))
1019                  (compiland (make-compiland :name (if named-lambda-p
1020                                                       name (gensym "ANONYMOUS-LAMBDA-"))
1021                                             :lambda-expression lambda-form
1022                                             :parent *current-compiland*)))
1023             (when *current-compiland*
1024               (incf (compiland-children *current-compiland*)))
1025             (multiple-value-bind (body decls)
1026                 (parse-body body)
1027               (setf (compiland-lambda-expression compiland)
1028                     ;; if there still was a doc-string present, remove it
1029                     (rewrite-lambda
1030                      `(lambda ,lambda-list ,@decls ,@body)))
1031               (let ((*visible-variables* *visible-variables*)
1032                     (*current-compiland* compiland))
1033                 (p1-compiland compiland)))
1034             (list 'FUNCTION compiland)))
1035          ((setf local-function (find-local-function (cadr form)))
1036           (dformat t "p1-function local function ~S~%" (cadr form))
1037     ;;we found out that the function needs a reference
1038     (setf (local-function-references-needed-p local-function) t)
1039           (let ((variable (local-function-variable local-function)))
1040             (when variable
1041                 (dformat t "p1-function ~S used non-locally~%"
1042                          (variable-name variable))
1043                 (setf (variable-used-non-locally-p variable) t)))
1044           form)
1045          (t
1046           form))))
1047
1048(defun p1-lambda (form)
1049  (setf form (rewrite-lambda form))
1050  (let* ((lambda-list (cadr form)))
1051    (when (or (memq '&optional lambda-list)
1052              (memq '&key lambda-list))
1053      (let ((state nil))
1054        (dolist (arg lambda-list)
1055          (cond ((memq arg lambda-list-keywords)
1056                 (setf state arg))
1057                ((memq state '(&optional &key))
1058                 (when (and (consp arg)
1059                            (not (constantp (second arg))))
1060                   (compiler-unsupported
1061                    "P1-LAMBDA: can't handle optional argument with non-constant initform.")))))))
1062    (p1-function (list 'FUNCTION form))))
1063
1064(defun p1-eval-when (form)
1065  (list* (car form) (cadr form) (mapcar #'p1 (cddr form))))
1066
1067(defknown p1-progv (t) t)
1068(defun p1-progv (form)
1069  ;; We've already checked argument count in PRECOMPILE-PROGV.
1070  (let* ((symbols-form (p1 (cadr form)))
1071         (values-form (p1 (caddr form)))
1072         (block (make-progv-node))
1073   (*block* block)
1074         (*blocks* (cons block *blocks*))
1075         (body (cdddr form)))
1076;;  The (commented out) block below means to detect compile-time
1077;;  enumeration of bindings to be created (a quoted form in the symbols
1078;;  position).
1079;;    (when (and (quoted-form-p symbols-form)
1080;;               (listp (second symbols-form)))
1081;;      (dolist (name (second symbols-form))
1082;;        (let ((variable (make-variable :name name :special-p t)))
1083;;          (push
1084    (setf (progv-environment-register block) t
1085          (progv-form block)
1086          `(progv ,symbols-form ,values-form ,@(p1-body body)))
1087    block))
1088
1089(defun p1-quote (form)
1090  (unless (= (length form) 2)
1091    (compiler-error "Wrong number of arguments for special operator ~A (expected 1, but received ~D)."
1092                    'QUOTE
1093                    (1- (length form))))
1094  (let ((arg (%cadr form)))
1095    (if (or (numberp arg) (characterp arg))
1096        arg
1097        form)))
1098
1099(defun p1-setq (form)
1100  (unless (= (length form) 3)
1101    (error "Too many arguments for SETQ."))
1102  (let ((arg1 (%cadr form))
1103        (arg2 (%caddr form)))
1104    (let ((variable (find-visible-variable arg1)))
1105      (if variable
1106          (progn
1107            (when (variable-ignore-p variable)
1108              (compiler-style-warn
1109               "Variable ~S is assigned even though it was declared to be ignored."
1110               (variable-name variable)))
1111            (incf (variable-writes variable))
1112            (cond ((eq (variable-compiland variable) *current-compiland*)
1113                   (dformat t "p1-setq: write ~S~%" arg1))
1114                  (t
1115                   (dformat t "p1-setq: non-local write ~S~%" arg1)
1116                   (setf (variable-used-non-locally-p variable) t))))
1117          (dformat t "p1-setq: unknown variable ~S~%" arg1)))
1118    (list 'SETQ arg1 (p1 arg2))))
1119
1120(defun p1-the (form)
1121  (unless (= (length form) 3)
1122    (compiler-error "Wrong number of arguments for special operator ~A (expected 2, but received ~D)."
1123                    'THE
1124                    (1- (length form))))
1125  (let ((type (%cadr form))
1126        (expr (%caddr form)))
1127    (cond ((and (listp type) (eq (car type) 'VALUES))
1128           ;; FIXME
1129           (p1 expr))
1130          ((= *safety* 3)
1131           (let* ((sym (gensym))
1132                  (new-expr `(let ((,sym ,expr))
1133                               (require-type ,sym ',type)
1134                               ,sym)))
1135             (p1 new-expr)))
1136          ((and (<= 1 *safety* 2) ;; at safety 1 or 2 check relatively
1137                (symbolp type))   ;; simple types (those specified by a single symbol)
1138           (let* ((sym (gensym))
1139                  (new-expr `(let ((,sym ,expr))
1140                               (require-type ,sym ',type)
1141                               ,sym)))
1142             (p1 new-expr)))
1143          (t
1144           (list 'THE type (p1 expr))))))
1145
1146(defun p1-truly-the (form)
1147  (unless (= (length form) 3)
1148    (compiler-error "Wrong number of arguments for special operator ~A (expected 2, but received ~D)."
1149                    'TRULY-THE
1150                    (1- (length form))))
1151  (list 'TRULY-THE (%cadr form) (p1 (%caddr form))))
1152
1153(defvar *pass2-unsafe-p-special-treatment-functions*
1154  '(
1155     logand
1156     logior
1157     lognot
1158     logxor
1159)
1160"The functions named in the list bound to this variable
1161need to be rewritten if UNSAFE-P returns non-NIL for their
1162argument list.
1163
1164All other function calls are handled by generic function calling
1165in pass2, which accounts for OPSTACK unsafety itself.")
1166
1167
1168
1169
1170(defknown unsafe-p (t) t)
1171(defun unsafe-p (args)
1172  "Determines whether the args can cause 'stack unsafe situations'.
1173Returns T if this is the case.
1174
1175When a 'stack unsafe situation' is encountered, the stack cannot
1176be used for temporary storage of intermediary results. This happens
1177because one of the forms in ARGS causes a local transfer of control
1178- local GO instruction - which assumes an empty stack, or if one of
1179the args causes a Java exception handler to be installed, which
1180- when triggered - clears out the stack.
1181"
1182  (cond ((node-p args)
1183         (unsafe-p (node-form args)))
1184        ((atom args)
1185         nil)
1186        (t
1187         (case (%car args)
1188           (QUOTE
1189            nil)
1190;;           (LAMBDA
1191;;            nil)
1192           ((RETURN-FROM GO CATCH THROW UNWIND-PROTECT BLOCK)
1193            t)
1194           (t
1195            (dolist (arg args)
1196              (when (unsafe-p arg)
1197                (return t))))))))
1198
1199(defknown p1-throw (t) t)
1200(defun p1-throw (form)
1201  (list* 'THROW (mapcar #'p1 (cdr form))))
1202
1203(defknown rewrite-function-call (t) t)
1204(defun rewrite-function-call (form)
1205  (let ((op (car form)) (args (cdr form)))
1206    (cond
1207      ((and (eq op 'funcall) (listp (car args)) (eq (caar args) 'lambda))
1208       ;;(funcall (lambda (...) ...) ...)
1209       (let ((op (car args)) (args (cdr args)))
1210   (expand-function-call-inline form (cadr op) (copy-tree (cddr op))
1211              args)))
1212      ((and (listp op) (eq (car op) 'lambda))
1213       ;;((lambda (...) ...) ...)
1214       (expand-function-call-inline form (cadr op) (copy-tree (cddr op)) args))
1215      (t (if (and (member op *pass2-unsafe-p-special-treatment-functions*)
1216                  (unsafe-p args))
1217       (let ((arg1 (car args)))
1218         (cond ((and (consp arg1) (eq (car arg1) 'GO))
1219          arg1)
1220         (t
1221          (let ((syms ())
1222          (lets ()))
1223      ;; Preserve the order of evaluation of the arguments!
1224      (dolist (arg args)
1225        (cond ((and (constantp arg)
1226                                      (not (node-p arg)))
1227         (push arg syms))
1228        ((and (consp arg) (eq (car arg) 'GO))
1229         (return-from rewrite-function-call
1230           (list 'LET* (nreverse lets) arg)))
1231        (t
1232         (let ((sym (gensym)))
1233           (push sym syms)
1234           (push (list sym arg) lets)))))
1235      (list 'LET* (nreverse lets)
1236            (list* (car form) (nreverse syms)))))))
1237       form)))))
1238
1239(defknown p1-function-call (t) t)
1240(defun p1-function-call (form)
1241  (let ((new-form (rewrite-function-call form)))
1242    (when (neq new-form form)
1243      (return-from p1-function-call (p1 new-form))))
1244  (let* ((op (car form))
1245         (local-function (find-local-function op)))
1246    (cond (local-function
1247;;            (format t "p1 local call to ~S~%" op)
1248;;            (format t "inline-p = ~S~%" (inline-p op))
1249           (when (and *enable-inline-expansion* (inline-p op)
1250          (local-function-definition local-function))
1251             (let* ((definition (local-function-definition local-function))
1252        (lambda-list (car definition))
1253        (body (cdr definition))
1254        (expansion (generate-inline-expansion op lambda-list body
1255                (cdr form))))
1256               (when expansion
1257                 (let ((explain *explain*))
1258                   (when (and explain (memq :calls explain))
1259                     (format t ";   inlining call to local function ~S~%" op)))
1260                 (return-from p1-function-call
1261       (let ((*inline-declarations*
1262        (remove op *inline-declarations* :key #'car :test #'equal)))
1263         (p1 expansion))))))
1264
1265           ;; FIXME
1266           (dformat t "local function assumed not single-valued~%")
1267           (setf (compiland-%single-valued-p *current-compiland*) nil)
1268
1269           (let ((variable (local-function-variable local-function)))
1270             (when variable
1271               (dformat t "p1 ~S used non-locally~%" (variable-name variable))
1272               (setf (variable-used-non-locally-p variable) t))))
1273          (t
1274           ;; Not a local function call.
1275           (dformat t "p1 non-local call to ~S~%" op)
1276           (unless (single-valued-p form)
1277;;                (format t "not single-valued op = ~S~%" op)
1278             (setf (compiland-%single-valued-p *current-compiland*) nil)))))
1279  (p1-default form))
1280
1281(defun %funcall (fn &rest args)
1282  "Dummy FUNCALL wrapper to force p1 not to optimize the call."
1283  (apply fn args))
1284
1285(defknown p1 (t) t)
1286(defun p1 (form)
1287  (cond ((symbolp form)
1288         (let (value)
1289           (cond ((null form)
1290                  form)
1291                 ((eq form t)
1292                  form)
1293                 ((keywordp form)
1294                  form)
1295                 ((and (constantp form)
1296                       (progn
1297                         (setf value (symbol-value form))
1298                         (or (numberp value)
1299                             (stringp value)
1300                             (pathnamep value))))
1301                  (setf form value))
1302                 (t
1303                  (let ((variable (find-visible-variable form)))
1304                    (when (null variable)
1305          (unless (or (special-variable-p form)
1306                                  (memq form *undefined-variables*))
1307      (compiler-style-warn
1308                         "Undefined variable ~S assumed special" form)
1309      (push form *undefined-variables*))
1310                      (setf variable (make-variable :name form :special-p t))
1311                      (push variable *visible-variables*))
1312                    (let ((ref (make-var-ref variable)))
1313                      (unless (variable-special-p variable)
1314                        (when (variable-ignore-p variable)
1315                          (compiler-style-warn
1316                           "Variable ~S is read even though it was declared to be ignored."
1317                           (variable-name variable)))
1318                        (push ref (variable-references variable))
1319                        (incf (variable-reads variable))
1320                        (cond ((eq (variable-compiland variable) *current-compiland*)
1321                               (dformat t "p1: read ~S~%" form))
1322                              (t
1323                               (dformat t "p1: non-local read ~S variable-compiland = ~S current compiland = ~S~%"
1324                                        form
1325                                        (compiland-name (variable-compiland variable))
1326                                        (compiland-name *current-compiland*))
1327                               (setf (variable-used-non-locally-p variable) t))))
1328                      (setf form ref)))
1329                  form))))
1330        ((atom form)
1331         form)
1332        (t
1333         (let ((op (%car form))
1334               handler)
1335           (cond ((symbolp op)
1336                  (when (compiler-macro-function op)
1337                    (unless (notinline-p op)
1338                      (multiple-value-bind (expansion expanded-p)
1339                          (compiler-macroexpand form)
1340                        ;; Fall through if no change...
1341                        (when expanded-p
1342                          (return-from p1 (p1 expansion))))))
1343                  (cond ((setf handler (get op 'p1-handler))
1344                         (funcall handler form))
1345                        ((macro-function op *compile-file-environment*)
1346                         (p1 (macroexpand form *compile-file-environment*)))
1347                        ((special-operator-p op)
1348                         (compiler-unsupported "P1: unsupported special operator ~S" op))
1349                        (t
1350                         (p1-function-call form))))
1351                 ((and (consp op) (eq (%car op) 'LAMBDA))
1352      (let ((maybe-optimized-call (rewrite-function-call form)))
1353        (if (eq maybe-optimized-call form)
1354      (p1 `(%funcall (function ,op) ,@(cdr form)))
1355      (p1 maybe-optimized-call))))
1356                 (t
1357                  form))))))
1358
1359(defun install-p1-handler (symbol handler)
1360  (setf (get symbol 'p1-handler) handler))
1361
1362(defun initialize-p1-handlers ()
1363  (dolist (pair '((AND                  p1-default)
1364                  (BLOCK                p1-block)
1365                  (CATCH                p1-catch)
1366                  (DECLARE              identity)
1367                  (EVAL-WHEN            p1-eval-when)
1368                  (FLET                 p1-flet)
1369                  (FUNCALL              p1-funcall)
1370                  (FUNCTION             p1-function)
1371                  (GO                   p1-go)
1372                  (IF                   p1-default)
1373                  ;; used to be p1-if, which was used to rewrite the test
1374                  ;; form to a LET-binding; that's not necessary, because
1375                  ;; the test form doesn't lead to multiple operands on the
1376                  ;; operand stack
1377                  (LABELS               p1-labels)
1378                  (LAMBDA               p1-lambda)
1379                  (LET                  p1-let/let*)
1380                  (LET*                 p1-let/let*)
1381                  (LOAD-TIME-VALUE      identity)
1382                  (LOCALLY              p1-locally)
1383                  (MULTIPLE-VALUE-BIND  p1-m-v-b)
1384                  (MULTIPLE-VALUE-CALL  p1-default)
1385                  (MULTIPLE-VALUE-LIST  p1-default)
1386                  (MULTIPLE-VALUE-PROG1 p1-default)
1387                  (OR                   p1-default)
1388                  (PROGN                p1-default)
1389                  (PROGV                p1-progv)
1390                  (QUOTE                p1-quote)
1391                  (RETURN-FROM          p1-return-from)
1392                  (SETQ                 p1-setq)
1393                  (SYMBOL-MACROLET      identity)
1394                  (TAGBODY              p1-tagbody)
1395                  (THE                  p1-the)
1396                  (THROW                p1-throw)
1397                  (TRULY-THE            p1-truly-the)
1398                  (UNWIND-PROTECT       p1-unwind-protect)
1399                  (THREADS:SYNCHRONIZED-ON
1400                                        p1-threads-synchronized-on)
1401      (JVM::WITH-INLINE-CODE identity)))
1402    (install-p1-handler (%car pair) (%cadr pair))))
1403
1404(initialize-p1-handlers)
1405
1406(defun p1-compiland (compiland)
1407;;   (format t "p1-compiland name = ~S~%" (compiland-name compiland))
1408  (let ((form (compiland-lambda-expression compiland)))
1409    (aver (eq (car form) 'LAMBDA))
1410    (setf form (rewrite-lambda form))
1411    (process-optimization-declarations (cddr form))
1412
1413    (let* ((lambda-list (cadr form))
1414           (body (cddr form))
1415           (*visible-variables* *visible-variables*)
1416           (closure (make-closure `(lambda ,lambda-list nil) nil))
1417           (syms (sys::varlist closure))
1418           (vars nil))
1419      (dolist (sym syms)
1420        (let ((var (make-variable :name sym
1421                                  :special-p (special-variable-p sym))))
1422          (push var vars)
1423          (push var *all-variables*)
1424          (push var *visible-variables*)))
1425      (setf (compiland-arg-vars compiland) (nreverse vars))
1426      (let ((free-specials (process-declarations-for-vars body vars nil)))
1427        (setf (compiland-free-specials compiland) free-specials)
1428        (dolist (var free-specials)
1429          (push var *visible-variables*)))
1430      (setf (compiland-p1-result compiland)
1431            (list* 'LAMBDA lambda-list (p1-body body))))))
1432
1433(provide "COMPILER-PASS1")
Note: See TracBrowser for help on using the repository browser.