source: trunk/abcl/src/org/armedbear/lisp/compile-file.lisp @ 11843

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

Reflow PROCESS-TOPLEVEL-FORM in order to make
more lines meet our 80-character length limit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 23.7 KB
Line 
1;;; compile-file.lisp
2;;;
3;;; Copyright (C) 2004-2006 Peter Graves
4;;; $Id: compile-file.lisp 11843 2009-05-08 21:09:09Z 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #:system)
33
34(require '#:jvm)
35
36(defvar *fbound-names*)
37
38(defvar *class-number*)
39
40(defvar *output-file-pathname*)
41
42(declaim (ftype (function (t) t) compute-classfile-name))
43(defun compute-classfile-name (n &optional (output-file-pathname
44                                            *output-file-pathname*))
45  "Computes the name of the class file associated with number `n'."
46  (let ((name
47         (%format nil "~A-~D"
48                  (substitute #\_ #\.
49                              (pathname-name output-file-pathname)) n)))
50    (namestring (merge-pathnames (make-pathname :name name :type "cls")
51                                 output-file-pathname))))
52
53(declaim (ftype (function () t) next-classfile-name))
54(defun next-classfile-name ()
55  (compute-classfile-name (incf *class-number*)))
56
57(defmacro report-error (&rest forms)
58  `(handler-case (progn ,@forms)
59     (compiler-unsupported-feature-error (condition)
60       (fresh-line)
61       (%format t "; UNSUPPORTED-FEATURE: ~A~%" condition)
62       (values nil condition))))
63
64;; Dummy function. Should never be called.
65(defun dummy (&rest ignored)
66  (declare (ignore ignored))
67  (assert nil))
68
69(declaim (ftype (function (t) t) verify-load))
70(defun verify-load (classfile)
71  (and classfile
72       (let ((*load-truename* *output-file-pathname*))
73         (report-error
74          (load-compiled-function classfile)))))
75
76(declaim (ftype (function (t stream) t) process-defconstant))
77(defun process-defconstant (form stream)
78  ;; "If a DEFCONSTANT form appears as a top level form, the compiler
79  ;; must recognize that [the] name names a constant variable. An
80  ;; implementation may choose to evaluate the value-form at compile
81  ;; time, load time, or both. Therefore, users must ensure that the
82  ;; initial-value can be evaluated at compile time (regardless of
83  ;; whether or not references to name appear in the file) and that
84  ;; it always evaluates to the same value."
85  (eval form)
86  (dump-form form stream)
87  (%stream-terpri stream))
88
89(declaim (ftype (function (t) t) note-toplevel-form))
90(defun note-toplevel-form (form)
91  (when *compile-print*
92    (fresh-line)
93    (princ "; ")
94    (let ((*print-length* 2)
95          (*print-level* 2)
96          (*print-pretty* nil))
97      (prin1 form))
98    (terpri)))
99
100(declaim (ftype (function (t stream t) t) process-toplevel-form))
101(defun process-toplevel-form (form stream compile-time-too)
102  (if (atom form)
103      (when compile-time-too
104        (eval form))
105    (progn
106      (let ((operator (%car form)))
107        (case operator
108          (MACROLET
109           (process-toplevel-macrolet form stream compile-time-too)
110           (return-from process-toplevel-form))
111          ((IN-PACKAGE DEFPACKAGE)
112           (note-toplevel-form form)
113           (setf form (precompile-form form nil))
114           (eval form)
115           ;; Force package prefix to be used when dumping form.
116           (let ((*package* +keyword-package+))
117             (dump-form form stream))
118           (%stream-terpri stream)
119           (return-from process-toplevel-form))
120          ((DEFVAR DEFPARAMETER)
121           (note-toplevel-form form)
122           (if compile-time-too
123               (eval form)
124               ;; "If a DEFVAR or DEFPARAMETER form appears as a top level form,
125               ;; the compiler must recognize that the name has been proclaimed
126               ;; special. However, it must neither evaluate the initial-value
127               ;; form nor assign the dynamic variable named NAME at compile
128               ;; time."
129               (let ((name (second form)))
130                 (%defvar name))))
131          (DEFCONSTANT
132           (note-toplevel-form form)
133           (process-defconstant form stream)
134           (return-from process-toplevel-form))
135          (DEFUN
136           (note-toplevel-form form)
137           (let* ((name (second form))
138                  (block-name (fdefinition-block-name name))
139                  (lambda-list (third form))
140                  (body (nthcdr 3 form))
141                  (*speed* *speed*)
142                  (*space* *space*)
143                  (*safety* *safety*)
144                  (*debug* *debug*))
145             (multiple-value-bind (body decls doc)
146                 (parse-body body)
147               (let* ((expr `(lambda ,lambda-list
148                               ,@decls (block ,block-name ,@body)))
149                      (classfile-name (next-classfile-name))
150                      (classfile (report-error
151                                  (jvm:compile-defun name expr nil
152                                                     classfile-name)))
153                      (compiled-function (verify-load classfile)))
154                 (cond
155                   (compiled-function
156                    (setf form
157                          `(fset ',name
158                                 (load-compiled-function ,(file-namestring classfile))
159                                 ,*source-position*
160                                 ',lambda-list
161                                 ,doc))
162                    (when compile-time-too
163                      (fset name compiled-function)))
164                   (t
165                    ;; FIXME Should be a warning or error of some sort...
166                    (format *error-output*
167                            "; Unable to compile function ~A~%" name)
168                    (let ((precompiled-function (precompile-form expr nil)))
169                      (setf form
170                            `(fset ',name
171                                   ,precompiled-function
172                                   ,*source-position*
173                                   ',lambda-list
174                                   ,doc)))
175                    (when compile-time-too
176                      (eval form)))))
177               (when (and (symbolp name) (eq (get name '%inline) 'INLINE))
178                 ;; FIXME Need to support SETF functions too!
179                 (setf (inline-expansion name)
180                       (jvm::generate-inline-expansion block-name
181                                                       lambda-list body))
182                 (dump-form `(setf (inline-expansion ',name)
183                                   ',(inline-expansion name))
184                            stream)
185                 (%stream-terpri stream)))
186             (push name jvm::*functions-defined-in-current-file*)
187             (note-name-defined name)
188             ;; If NAME is not fbound, provide a dummy definition so that
189             ;; getSymbolFunctionOrDie() will succeed when we try to verify that
190             ;; functions defined later in the same file can be loaded correctly.
191             (unless (fboundp name)
192               (setf (fdefinition name) #'dummy)
193               (push name *fbound-names*))))
194          ((DEFGENERIC DEFMETHOD)
195           (note-toplevel-form form)
196           (note-name-defined (second form))
197           (let ((*compile-print* nil))
198             (process-toplevel-form (macroexpand-1 form *compile-file-environment*)
199                                    stream compile-time-too))
200             (return-from process-toplevel-form))
201          (DEFMACRO
202           (note-toplevel-form form)
203           (let ((name (second form)))
204             (eval form)
205             (let* ((expr (function-lambda-expression (macro-function name)))
206                    (classfile-name (next-classfile-name))
207                    (classfile
208                     (ignore-errors
209                       (jvm:compile-defun nil expr nil classfile-name))))
210               (if (null (verify-load classfile))
211                   ;; FIXME error or warning
212                   (format *error-output* "; Unable to compile macro ~A~%" name)
213                 (progn
214                   (setf form
215                         (if (special-operator-p name)
216                             `(put ',name 'macroexpand-macro
217                                   (make-macro ',name
218                                               (load-compiled-function
219                                                ,(file-namestring classfile))))
220                             `(fset ',name
221                                    (make-macro ',name
222                                                (load-compiled-function
223                                                 ,(file-namestring classfile)))
224                                    ,*source-position*
225                                    ',(third form)))))))))
226          (DEFTYPE
227           (note-toplevel-form form)
228           (eval form))
229          (EVAL-WHEN
230           (multiple-value-bind (ct lt e)
231               (parse-eval-when-situations (cadr form))
232             (let ((new-compile-time-too (or ct (and compile-time-too e)))
233                   (body (cddr form)))
234               (if lt
235                   (process-toplevel-progn body stream new-compile-time-too)
236                 (when new-compile-time-too
237                   (eval `(progn ,@body)))))
238           (return-from process-toplevel-form)))
239          (LOCALLY
240           ;; FIXME Need to handle special declarations too!
241           (let ((*speed* *speed*)
242                 (*safety* *safety*)
243                 (*debug* *debug*)
244                 (*space* *space*)
245                 (*inline-declarations* *inline-declarations*))
246             (multiple-value-bind (forms decls)
247                 (parse-body (cdr form) nil)
248               (process-optimization-declarations decls)
249               (process-toplevel-progn forms stream compile-time-too)
250               (return-from process-toplevel-form))))
251          (PROGN
252           (process-toplevel-progn (cdr form) stream compile-time-too)
253           (return-from process-toplevel-form))
254          (DECLARE
255           (compiler-style-warn "Misplaced declaration: ~S" form))
256          (t
257           (when (and (symbolp operator)
258                        (macro-function operator *compile-file-environment*))
259             (note-toplevel-form form)
260             ;; Note that we want MACROEXPAND-1 and not MACROEXPAND here, in
261             ;; case the form being expanded expands into something that needs
262             ;; special handling by PROCESS-TOPLEVEL-FORM (e.g. DEFMACRO).
263             (let ((*compile-print* nil))
264               (process-toplevel-form (macroexpand-1 form *compile-file-environment*)
265                                      stream compile-time-too))
266             (return-from process-toplevel-form))
267
268           (cond ((eq operator 'QUOTE)
269;;;                      (setf form (precompile-form form nil))
270                  (when compile-time-too
271                    (eval form))
272                  (return-from process-toplevel-form))
273                 ((eq operator 'PUT)
274                  (setf form (precompile-form form nil)))
275                 ((eq operator 'COMPILER-DEFSTRUCT)
276                  (setf form (precompile-form form nil)))
277                 ((eq operator 'PROCLAIM)
278                  (setf form (precompile-form form nil)))
279                 ((and (memq operator '(EXPORT REQUIRE PROVIDE SHADOW))
280                       (or (keywordp (second form))
281                           (and (listp (second form))
282                                (eq (first (second form)) 'QUOTE))))
283                  (setf form (precompile-form form nil)))
284                 ((eq operator 'IMPORT)
285                  (setf form (precompile-form form nil))
286                  ;; Make sure package prefix is printed when symbols are imported.
287                  (let ((*package* +keyword-package+))
288                    (dump-form form stream))
289                  (%stream-terpri stream)
290                  (when compile-time-too
291                    (eval form))
292                  (return-from process-toplevel-form))
293                 ((and (eq operator '%SET-FDEFINITION)
294                       (eq (car (second form)) 'QUOTE)
295                       (consp (third form))
296                       (eq (%car (third form)) 'FUNCTION)
297                       (symbolp (cadr (third form))))
298                  (setf form (precompile-form form nil)))
299;;;                     ((memq operator '(LET LET*))
300;;;                      (let ((body (cddr form)))
301;;;                        (if (dolist (subform body nil)
302;;;                              (when (and (consp subform) (eq (%car subform) 'DEFUN))
303;;;                                (return t)))
304;;;                            (setf form (convert-toplevel-form form))
305;;;                            (setf form (precompile-form form nil)))))
306                 ((eq operator 'mop::ensure-method)
307                  (setf form (convert-ensure-method form)))
308                 ((and (symbolp operator)
309                       (not (special-operator-p operator))
310                       (null (cdr form)))
311                  (setf form (precompile-form form nil)))
312                 (t
313;;;                      (setf form (precompile-form form nil))
314                  (note-toplevel-form form)
315                  (setf form (convert-toplevel-form form)))))))))
316  (when (consp form)
317    (dump-form form stream)
318    (%stream-terpri stream))
319  ;; Make sure the compiled-function loader knows where
320  ;; to load the compiled functions. Note that this trickery
321  ;; was already used in verify-load before I used it,
322  ;; however, binding *load-truename* isn't fully compliant, I think.
323  (let ((*load-truename* *output-file-pathname*))
324    (when compile-time-too
325      (eval form))))
326
327(declaim (ftype (function (t) t) convert-ensure-method))
328(defun convert-ensure-method (form)
329  (c-e-m-1 form :function)
330  (c-e-m-1 form :fast-function)
331  (precompile-form form nil))
332
333(declaim (ftype (function (t t) t) c-e-m-1))
334(defun c-e-m-1 (form key)
335  (let* ((tail (cddr form))
336         (function-form (getf tail key)))
337    (when (and function-form (consp function-form)
338               (eq (%car function-form) 'FUNCTION))
339      (let ((lambda-expression (cadr function-form)))
340        (let* ((*speed* *speed*)
341               (*space* *space*)
342               (*safety* *safety*)
343               (*debug* *debug*))
344          (let* ((classfile-name (next-classfile-name))
345                 (classfile (report-error
346                             (jvm:compile-defun nil lambda-expression nil classfile-name)))
347                 (compiled-function (verify-load classfile)))
348            (cond (compiled-function
349                   (setf (getf tail key)
350                         `(load-compiled-function ,(file-namestring classfile))))
351                  (t
352                   ;; FIXME This should be a warning or error of some sort...
353                   (format *error-output* "; Unable to compile method~%")))))))))
354
355(declaim (ftype (function (t) t) convert-toplevel-form))
356(defun convert-toplevel-form (form)
357  (let* ((expr `(lambda () ,form))
358         (classfile-name (next-classfile-name))
359         (classfile (report-error (jvm:compile-defun nil expr nil classfile-name)))
360         (compiled-function (verify-load classfile)))
361    (setf form
362          (if compiled-function
363              `(funcall (load-compiled-function ,(file-namestring classfile)))
364              (precompile-form form nil)))))
365
366
367(defun process-toplevel-macrolet (form stream compile-time-too)
368  (let ((*compile-file-environment* (make-environment *compile-file-environment*)))
369    (dolist (definition (cadr form))
370      (environment-add-macro-definition *compile-file-environment*
371                                        (car definition)
372                                        (make-macro (car definition)
373                                                    (make-expander-for-macrolet definition))))
374    (dolist (body-form (cddr form))
375      (process-toplevel-form body-form stream compile-time-too))))
376
377(declaim (ftype (function (t stream t) t) process-toplevel-progn))
378(defun process-toplevel-progn (forms stream compile-time-too)
379  (dolist (form forms)
380    (process-toplevel-form form stream compile-time-too)))
381
382;;; Adapted from SBCL.
383;;; Parse an EVAL-WHEN situations list, returning three flags,
384;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
385;;; the types of situations present in the list.
386(defun parse-eval-when-situations (situations)
387  (when (or (not (listp situations))
388      (set-difference situations
389          '(:compile-toplevel
390            compile
391            :load-toplevel
392            load
393            :execute
394            eval)))
395    (error "Bad EVAL-WHEN situation list: ~S." situations))
396  (values (intersection '(:compile-toplevel compile) situations)
397    (intersection '(:load-toplevel load) situations)
398    (intersection '(:execute eval) situations)))
399
400(defun compile-file (input-file
401                     &key
402                     output-file
403                     ((:verbose *compile-verbose*) *compile-verbose*)
404                     ((:print *compile-print*) *compile-print*)
405                     external-format)
406  (declare (ignore external-format)) ; FIXME
407  (unless (or (and (probe-file input-file) (not (file-directory-p input-file)))
408              (pathname-type input-file))
409    (let ((pathname (merge-pathnames (make-pathname :type "lisp") input-file)))
410      (when (probe-file pathname)
411        (setf input-file pathname))))
412  (setf output-file (if output-file
413                        (merge-pathnames output-file *default-pathname-defaults*)
414                        (compile-file-pathname input-file)))
415  (let* ((*output-file-pathname* output-file)
416         (type (pathname-type output-file))
417         (temp-file (merge-pathnames (make-pathname :type (concatenate 'string type "-tmp"))
418                                     output-file))
419         (warnings-p nil)
420         (failure-p nil))
421    (with-open-file (in input-file :direction :input)
422      (let* ((*compile-file-pathname* (pathname in))
423             (*compile-file-truename* (truename in))
424             (*source* *compile-file-truename*)
425             (*class-number* 0)
426             (namestring (namestring *compile-file-truename*))
427             (start (get-internal-real-time))
428             elapsed)
429        (when *compile-verbose*
430          (format t "; Compiling ~A ...~%" namestring))
431        (with-compilation-unit ()
432          (with-open-file (out temp-file :direction :output :if-exists :supersede)
433            (let ((*readtable* *readtable*)
434                  (*read-default-float-format* *read-default-float-format*)
435                  (*read-base* *read-base*)
436                  (*package* *package*)
437                  (*speed* *speed*)
438                  (*space* *space*)
439                  (*safety* *safety*)
440                  (*debug* *debug*)
441                  (*explain* *explain*)
442                  (jvm::*functions-defined-in-current-file* '())
443                  (*fbound-names* '())
444                  (*fasl-anonymous-package* (%make-package)))
445              (jvm::with-file-compilation
446                (write "; -*- Mode: Lisp -*-" :escape nil :stream out)
447                (%stream-terpri out)
448                (let ((*package* (find-package '#:cl)))
449                  (write (list 'init-fasl :version *fasl-version*) :stream out)
450                  (%stream-terpri out)
451                  (write (list 'setq '*source* *compile-file-truename*) :stream out)
452                  (%stream-terpri out))
453                (handler-bind ((style-warning #'(lambda (c)
454                                                  (declare (ignore c))
455                                                  (setf warnings-p t)
456                                                  nil))
457                               ((or warning
458                                    compiler-error) #'(lambda (c)
459                                                        (declare (ignore c))
460                                                        (setf warnings-p t
461                                                              failure-p t)
462                                                        nil)))
463                  (loop
464                     (let* ((*source-position* (file-position in))
465                            (jvm::*source-line-number* (stream-line-number in))
466                            (form (read in nil in))
467                            (*compiler-error-context* form))
468                       (when (eq form in)
469                         (return))
470                       (process-toplevel-form form out nil))))
471                (dolist (name *fbound-names*)
472                  (fmakunbound name))))))
473        (rename-file temp-file output-file)
474
475        (when *compile-file-zip*
476          (let* ((type ;; Don't use ".zip", it'll result in an extension
477                       ;;  with a dot, which is rejected by NAMESTRING
478                  (%format nil "~A~A" (pathname-type output-file) "-zip"))
479                 (zipfile (namestring
480                           (merge-pathnames (make-pathname :type type)
481                                            output-file)))
482                 (pathnames ()))
483            (dotimes (i *class-number*)
484              (let* ((pathname (compute-classfile-name (1+ i))))
485                (when (probe-file pathname)
486                  (push pathname pathnames))))
487            (setf pathnames (nreverse pathnames))
488            (let ((load-file (merge-pathnames (make-pathname :type "_")
489                                              output-file)))
490              (rename-file output-file load-file)
491              (push load-file pathnames))
492            (zip zipfile pathnames)
493            (dolist (pathname pathnames)
494              (let ((truename (probe-file pathname)))
495                (when truename
496                  (delete-file truename))))
497            (rename-file zipfile output-file)))
498
499        (setf elapsed (/ (- (get-internal-real-time) start) 1000.0))
500        (when *compile-verbose*
501          (format t "~&; Wrote ~A (~A seconds)~%" (namestring output-file) elapsed))))
502    (values (truename output-file) warnings-p failure-p)))
503
504(defun compile-file-if-needed (input-file &rest allargs &key force-compile
505                               &allow-other-keys)
506  (setf input-file (truename input-file))
507  (cond (force-compile
508         (remf allargs :force-compile)
509         (apply 'compile-file input-file allargs))
510        (t
511         (let* ((source-write-time (file-write-date input-file))
512                (output-file       (or (getf allargs :output-file)
513                                       (compile-file-pathname input-file)))
514                (target-write-time (and (probe-file output-file)
515                                        (file-write-date output-file))))
516           (if (or (null target-write-time)
517                   (<= target-write-time source-write-time))
518               (apply 'compile-file input-file allargs)
519               output-file)))))
520
521(provide 'compile-file)
Note: See TracBrowser for help on using the repository browser.