forked from armedbear/abcl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile-file.lisp
More file actions
1095 lines (1014 loc) · 48.4 KB
/
compile-file.lisp
File metadata and controls
1095 lines (1014 loc) · 48.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; compile-file.lisp
;;;
;;; Copyright (C) 2004-2006 Peter Graves
;;; $Id$
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation; either version 2
;;; of the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;;
;;; As a special exception, the copyright holders of this library give you
;;; permission to link this library with independent modules to produce an
;;; executable, regardless of the license terms of these independent
;;; modules, and to copy and distribute the resulting executable under
;;; terms of your choice, provided that you also meet, for each linked
;;; independent module, the terms and conditions of the license of that
;;; module. An independent module is a module which is not derived from
;;; or based on this library. If you modify this library, you may extend
;;; this exception to your version of the library, but you are not
;;; obligated to do so. If you do not wish to do so, delete this
;;; exception statement from your version.
(in-package #:system)
(require "COMPILER-PASS2")
(export 'compile-file-if-needed)
(defvar *fbound-names*)
(defvar *class-number*)
(defvar *output-file-pathname*)
(defvar *toplevel-functions*)
(defvar *toplevel-macros*)
(defvar *toplevel-exports*)
(defvar *toplevel-setf-expanders*)
(defvar *toplevel-setf-functions*)
(defun base-classname (&optional (output-file-pathname *output-file-pathname*))
(sanitize-class-name (pathname-name output-file-pathname)))
(defun fasl-loader-classname (&optional (output-file-pathname *output-file-pathname*))
(%format nil "~A_0" (base-classname output-file-pathname)))
(declaim (ftype (function (t) t) compute-classfile))
(defun compute-classfile (n &optional (output-file-pathname
*output-file-pathname*))
"Computes the pathname of the class file associated with number `n'."
(let ((name
(sanitize-class-name
(%format nil "~A_~D" (pathname-name output-file-pathname) n))))
(merge-pathnames (make-pathname :name name :type *compile-file-class-extension*)
output-file-pathname)))
(defun sanitize-class-name (name)
(let ((name (copy-seq name)))
(dotimes (i (length name))
(declare (type fixnum i))
(when (or (char= (char name i) #\-)
(char= (char name i) #\.)
(char= (char name i) #\Space))
(setf (char name i) #\_)))
name))
(declaim (ftype (function () t) next-classfile))
(defun next-classfile ()
(compute-classfile (incf *class-number*)))
(defmacro report-error (&rest forms)
`(handler-case (progn ,@forms)
(compiler-unsupported-feature-error (condition)
(fresh-line)
(%format t "; UNSUPPORTED-FEATURE: ~A~%" condition)
(values nil condition))))
;; Dummy function. Should never be called.
(defun dummy (&rest ignored)
(declare (ignore ignored))
(assert nil))
;;; ??? rename to something shorter?
(defparameter *compiler-diagnostic* nil
"The stream to emit compiler diagnostic messages to, or nil to muffle output.")
(export '*compiler-diagnostic*)
(defun diag (format &rest args)
(apply #'cl:format
*compiler-diagnostic*
(cl:concatenate 'string "~&SYSTEM::*COMPILER-DIAGNOSTIC* " format "~&")
(when args
args)))
(declaim (ftype (function (t) t) verify-load))
(defun verify-load (classfile &key (force nil))
"Return whether the file at the path denoted by CLASSFILE is a loadable JVM artifact."
(declare (ignore force))
(unless classfile
(diag "Nil classfile argument passed to verify-load.")
(return-from verify-load nil))
(with-open-file (cf classfile :direction :input)
(when
(= 0 (file-length cf))
;;; TODO hook into a real ABCL compiler condition hierarchy
(diag "Internal compiler error detected: Fasl contains ~
zero-length jvm classfile corresponding to ~A." classfile)
(return-from verify-load nil)))
;; ### FIXME
;; The section below can't work, because we have
;; circular references between classes of outer- and innerscoped
;; functions. We need the class loader to resolve these circular
;; references for us. Our FASL class loader does exactly that,
;; so we need a class loader here which knows how to find
;; all the .cls files related to the current scope being loaded.
#+nil
(when (or force (> *safety* *speed*))
(diag "Testing compiled bytecode by loading classfile into JVM.")
(let ((*load-truename* *output-file-pathname*))
;; load-compiled-function used to be wrapped via report-error
(return-from verify-load (load-compiled-function classfile))))
t)
(declaim (ftype (function (t) t) note-toplevel-form))
(defun note-toplevel-form (form)
(when *compile-print*
(fresh-line)
(princ "; ")
(let ((*print-length* 2)
(*print-level* 2)
(*print-pretty* nil))
(prin1 form))
(terpri)))
(defun output-form (form)
(if *binary-fasls*
(push form *forms-for-output*)
(%fasl-emit-toplevel-form form *fasl-stream*)))
(defun finalize-fasl-output ()
(when *binary-fasls*
(let ((*package* (find-package :keyword))
(*double-colon-package-separators* T))
(%fasl-emit-toplevel-form
(convert-toplevel-form (list* 'PROGN
(nreverse *forms-for-output*))
t)
*fasl-stream*))))
(declaim (ftype (function (t) t) simple-toplevel-form-p))
(defun simple-toplevel-form-p (form)
"Returns NIL if the form is too complex to become an
interpreted toplevel form, non-NIL if it is 'simple enough'."
(and (consp form)
(every #'(lambda (arg)
(or (and (atom arg)
(not (and (symbolp arg)
(symbol-macro-p arg))))
(and (consp arg)
(eq 'QUOTE (car arg)))))
(cdr form))))
(declaim (ftype (function (t t) t) convert-toplevel-form))
(defun convert-toplevel-form (form declare-inline)
(when (or (simple-toplevel-form-p form)
(and (eq (car form) 'SETQ)
;; for SETQ, look at the evaluated part
(simple-toplevel-form-p (third form))))
;; single form with simple or constant arguments
;; Without this exception, toplevel function calls
;; will be compiled into lambdas which get compiled to
;; compiled-functions. Those need to be loaded.
;; Conclusion: Top level interpreting the function call
;; and its arguments may be (and should be) more efficient.
(return-from convert-toplevel-form
(precompiler:precompile-form form nil *compile-file-environment*)))
(let* ((toplevel-form (third form))
(expr `(lambda () ,form))
(saved-class-number *class-number*)
(classfile (next-classfile))
(result
(with-open-file
(f classfile
:direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede)
(report-error (jvm:compile-defun nil
expr *compile-file-environment*
classfile f
declare-inline))))
(compiled-function (verify-load classfile)))
(declare (ignore toplevel-form result))
(progn
#+nil
(when (> *debug* 0)
;; TODO (annotate form toplevel-form classfile compiled-function fasl-class-number)
;;; ??? define an API by perhaps exporting these symbols?
(setf (getf form 'form-source)
toplevel-form
(getf form 'classfile)
classfile
(getf form 'compiled-function)
compiled-function
(getf form 'class-number)
saved-class-number))
(setf form
(if compiled-function
`(funcall (sys::get-fasl-function *fasl-loader*
,saved-class-number))
(precompiler:precompile-form form nil
*compile-file-environment*))))))
(declaim (ftype (function (t stream t) t) process-progn))
(defun process-progn (forms stream compile-time-too)
(dolist (form forms)
(process-toplevel-form form stream compile-time-too))
nil)
(declaim (ftype (function (t t t) t) process-toplevel-form))
(defun precompile-toplevel-form (form stream compile-time-too)
(declare (ignore stream))
(let ((form (precompiler:precompile-form form nil
*compile-file-environment*)))
(when compile-time-too
(eval form))
form))
(defun process-toplevel-macrolet (form stream compile-time-too)
(let ((*compile-file-environment*
(make-environment *compile-file-environment*)))
(dolist (definition (cadr form))
(environment-add-macro-definition *compile-file-environment*
(car definition)
(make-macro (car definition)
(make-macro-expander definition))))
(dolist (body-form (cddr form))
(process-toplevel-form body-form stream compile-time-too)))
nil)
(declaim (ftype (function (t t t) t) process-toplevel-defconstant))
(defun process-toplevel-defconstant (form stream compile-time-too)
(declare (ignore stream compile-time-too))
;; "If a DEFCONSTANT form appears as a top level form, the compiler
;; must recognize that [the] name names a constant variable. An
;; implementation may choose to evaluate the value-form at compile
;; time, load time, or both. Therefore, users must ensure that the
;; initial-value can be evaluated at compile time (regardless of
;; whether or not references to name appear in the file) and that
;; it always evaluates to the same value."
(note-toplevel-form form)
(eval form)
;;; emit make-array when initial-value is a specialized vector
(let ((initial-value (third form)))
(when (and (atom initial-value)
(arrayp initial-value)
(= (length (array-dimensions initial-value)) 1)
(not (eq (array-element-type initial-value) t)))
(setf (third form)
`(common-lisp:make-array
',(array-dimensions initial-value)
:element-type ',(array-element-type initial-value)
:initial-contents ',(coerce initial-value 'list)))))
`(progn
(sys:put ',(second form) 'sys::source
(cl:cons '(,(second form) ,(namestring *source*) ,*source-position*)
(cl:get ',(second form) 'sys::source nil)))
,form))
(declaim (ftype (function (t t t) t) process-toplevel-quote))
(defun process-toplevel-quote (form stream compile-time-too)
(declare (ignore stream))
(when compile-time-too
(eval form))
nil)
(declaim (ftype (function (t t t) t) process-toplevel-import))
(defun process-toplevel-import (form stream compile-time-too)
(declare (ignore stream))
(let ((form (precompiler:precompile-form form nil
*compile-file-environment*)))
(let ((*package* +keyword-package+))
(output-form form))
(when compile-time-too
(eval form)))
nil)
(declaim (ftype (function (t t t) t) process-toplevel-export))
(defun process-toplevel-export (form stream compile-time-too)
(when (and (listp (second form))
(eq (car (second form)) 'QUOTE)) ;; constant export list
(let ((sym-or-syms (second (second form))))
(setf *toplevel-exports*
(append *toplevel-exports* (if (listp sym-or-syms)
sym-or-syms
(list sym-or-syms))))))
(precompile-toplevel-form form stream compile-time-too))
(declaim (ftype (function (t t t) t) process-record-source-information))
(defun process-record-source-information (form stream compile-time-too)
(declare (ignore stream compile-time-too))
(let* ((name (second form))
(type (third form)))
(when (quoted-form-p name) (setq name (second name)))
(when (quoted-form-p type) (setq type (second type)))
(let ((sym (if (consp name) (second name) name)))
`(sys:put ',sym 'sys::source
(cl:cons '(,type ,(namestring *source*) ,*source-position*)
(cl:get ',sym 'sys::source nil))))))
(declaim (ftype (function (t t t) t) process-toplevel-mop.ensure-method))
(defun process-toplevel-mop.ensure-method (form stream compile-time-too)
(declare (ignore stream))
(flet ((convert-ensure-method (form key)
(let* ((tail (cddr form))
(function-form (getf tail key)))
(when (and function-form (consp function-form)
(eq (%car function-form) 'FUNCTION))
(let ((lambda-expression (cadr function-form)))
(jvm::with-saved-compiler-policy
(let* ((saved-class-number *class-number*)
(classfile (next-classfile))
(result
(with-open-file
(f classfile
:direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede)
(report-error
(jvm:compile-defun nil lambda-expression
*compile-file-environment*
classfile f nil))))
(compiled-function (verify-load classfile)))
(declare (ignore result))
(cond
(compiled-function
(setf (getf tail key)
`(sys::get-fasl-function *fasl-loader*
,saved-class-number)))
(t
;; FIXME This should be a warning or error of some sort...
(format *error-output* "; Unable to compile method~%"))))))))))
(when compile-time-too
(let* ((copy-form (copy-tree form))
;; ### Ideally, the precompiler would leave the forms alone
;; and copy them where required, instead of forcing us to
;; do a deep copy in advance
(precompiled-form (precompiler:precompile-form copy-form nil
*compile-file-environment*)))
(eval precompiled-form)))
(convert-ensure-method form :function)
(convert-ensure-method form :fast-function))
(precompiler:precompile-form form nil *compile-file-environment*))
(declaim (ftype (function (t t t) t) process-toplevel-defvar/defparameter))
(defun process-toplevel-defvar/defparameter (form stream compile-time-too)
(declare (ignore stream))
(note-toplevel-form form)
(if compile-time-too
(eval form)
;; "If a DEFVAR or DEFPARAMETER form appears as a top level form,
;; the compiler must recognize that the name has been proclaimed
;; special. However, it must neither evaluate the initial-value
;; form nor assign the dynamic variable named NAME at compile
;; time."
(let ((name (second form)))
(%defvar name)))
(let ((name (second form))
(initial-value (third form)))
;;; emit make-array when initial-value is a specialized vector
(when (and (atom initial-value)
(arrayp initial-value)
(= (length (array-dimensions initial-value)) 1)
(not (eq (array-element-type initial-value) t)))
(setf (third form)
`(common-lisp:make-array
',(array-dimensions initial-value)
:element-type ',(array-element-type initial-value)
:initial-contents ',(coerce initial-value 'list))))
`(progn
(sys:put ',name 'sys::source
(cl:cons
(list :variable ,(namestring *source*) ,*source-position*)
(cl:get ',name 'sys::source nil)))
,form)))
(declaim (ftype (function (t t t) t) process-toplevel-defpackage/in-package))
(defun process-toplevel-defpackage/in-package (form stream compile-time-too)
(declare (ignore stream compile-time-too))
(note-toplevel-form form)
(let ((defpackage-name (and (eq (car form) 'defpackage) (intern (string (second form)) :keyword))) )
(setf form
(precompiler:precompile-form form nil *compile-file-environment*))
(eval form)
;; Force package prefix to be used when dumping form.
(let ((*package* +keyword-package+))
(output-form form))
;; a bit ugly here. Since we precompile, and added
;; record-source-information we need to know where it is.
;; The defpackage is at top, so we know where the name is (though
;; it is a string by now) (if it is a defpackage)
(if defpackage-name
`(sys:put ,defpackage-name 'sys::source
(cl:cons '(:package ,(namestring *source*) ,*source-position*)
(cl:get ,defpackage-name 'sys::source nil)))
nil)))
(declaim (ftype (function (t t t) t) process-toplevel-declare))
(defun process-toplevel-declare (form stream compile-time-too)
(declare (ignore stream compile-time-too))
(compiler-style-warn "Misplaced declaration: ~S" form)
nil)
(declaim (ftype (function (t t t) t) process-toplevel-progn))
(defun process-toplevel-progn (form stream compile-time-too)
(process-progn (cdr form) stream compile-time-too)
nil)
(declaim (ftype (function (t t t) t) process-toplevel-deftype))
(defun process-toplevel-deftype (form stream compile-time-too)
(declare (ignore stream compile-time-too))
(note-toplevel-form form)
(eval form)
`(progn
(sys:put ',(second form) 'sys::source
(cl:cons '(,(second form) ,(namestring *source*) ,*source-position*)
(cl:get ',(second form) 'sys::source nil)))
,form))
(declaim (ftype (function (t t t) t) process-toplevel-eval-when))
(defun process-toplevel-eval-when (form stream compile-time-too)
(flet ((parse-eval-when-situations (situations)
"Parse an EVAL-WHEN situations list, returning three flags,
(VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
the types of situations present in the list."
; Adapted from SBCL.
(when (or (not (listp situations))
(set-difference situations
'(:compile-toplevel
compile
:load-toplevel
load
:execute
eval)))
(error "Bad EVAL-WHEN situation list: ~S." situations))
(values (intersection '(:compile-toplevel compile) situations)
(intersection '(:load-toplevel load) situations)
(intersection '(:execute eval) situations))))
(multiple-value-bind (ct lt e)
(parse-eval-when-situations (cadr form))
(let ((new-compile-time-too (or ct (and compile-time-too e)))
(body (cddr form)))
(if lt
(process-progn body stream new-compile-time-too)
(when new-compile-time-too
(eval `(progn ,@body)))))))
nil)
(declaim (ftype (function (t t t) t) process-toplevel-defmethod/defgeneric))
(defun process-toplevel-defmethod/defgeneric (form stream compile-time-too)
(note-toplevel-form form)
(note-name-defined (second form))
(push (second form) *toplevel-functions*)
(when (and (consp (second form))
(eq 'setf (first (second form))))
(push (second (second form))
*toplevel-setf-functions*))
(let ((*compile-print* nil))
(process-toplevel-form (macroexpand-1 form *compile-file-environment*)
stream compile-time-too))
(let* ((sym (if (consp (second form)) (second (second form)) (second form))))
(when (eq (car form) 'defgeneric)
`(progn
(sys:put ',sym 'sys::source
(cl:cons '((:generic-function ,(second form))
,(namestring *source*) ,*source-position*)
(cl:get ',sym 'sys::source nil)))
,@(loop for method-form in (cdddr form)
when (eq (car method-form) :method)
collect
(multiple-value-bind (function-name qualifiers lambda-list specializers documentation declarations body)
(mop::parse-defmethod `(,(second form) ,@(rest method-form)))
;;; FIXME: style points for refactoring double backquote to "normal" form
`(sys:put ',sym 'sys::source
(cl:cons `((:method ,',sym ,',qualifiers ,',specializers)
,,(namestring *source*) ,,*source-position*)
(cl:get ',sym 'sys::source nil)))))))))
(declaim (ftype (function (t t t) t) process-toplevel-locally))
(defun process-toplevel-locally (form stream compile-time-too)
(jvm::with-saved-compiler-policy
(multiple-value-bind (forms decls)
(parse-body (cdr form) nil)
(process-optimization-declarations decls)
(let* ((jvm::*visible-variables* jvm::*visible-variables*)
(specials (jvm::process-declarations-for-vars (cdr form)
nil nil)))
(dolist (special specials)
(push special jvm::*visible-variables*))
(process-progn forms stream compile-time-too))))
nil)
(declaim (ftype (function (t t t) t) process-toplevel-defmacro))
(defun process-toplevel-defmacro (form stream compile-time-too)
(declare (ignore stream compile-time-too))
(note-toplevel-form form)
(let ((name (second form)))
(eval form)
(push name *toplevel-macros*)
(let* ((expr (function-lambda-expression (macro-function name)))
(saved-class-number *class-number*)
(classfile (next-classfile)))
(with-open-file
(f classfile
:direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede)
(ignore-errors
(jvm:compile-defun nil expr *compile-file-environment*
classfile f nil)))
(when (null (verify-load classfile))
;; FIXME error or warning
(format *error-output* "; Unable to compile macro ~A~%" name)
(return-from process-toplevel-defmacro form))
(if (special-operator-p name)
`(sys:put ',name 'macroexpand-macro
(sys:make-macro ',name
(sys::get-fasl-function *fasl-loader*
,saved-class-number)))
`(progn
(sys:put ',name 'sys::source
(cl:cons '(:macro ,(namestring *source*) ,*source-position*)
(cl:get ',name 'sys::source nil)))
(sys:fset ',name
(sys:make-macro ',name
(sys::get-fasl-function *fasl-loader*
,saved-class-number))
,*source-position*
',(third form)
,(%documentation name 'cl:function)))))))
(declaim (ftype (function (t t t) t) process-toplevel-defun))
(defun process-toplevel-defun (form stream compile-time-too)
(declare (ignore stream))
(note-toplevel-form form)
(let* ((name (second form))
(block-name (fdefinition-block-name name))
(lambda-list (third form))
(body (nthcdr 3 form)))
(jvm::with-saved-compiler-policy
(multiple-value-bind (body decls doc)
(parse-body body)
(let* ((expr `(lambda ,lambda-list
,@decls (block ,block-name ,@body)))
(saved-class-number *class-number*)
(classfile (next-classfile))
(internal-compiler-errors nil)
(result (with-open-file
(f classfile
:direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede)
(handler-bind
((internal-compiler-error
#'(lambda (e)
(push e internal-compiler-errors)
(continue))))
(report-error
(jvm:compile-defun name expr *compile-file-environment*
classfile f nil)))))
(compiled-function (if (not internal-compiler-errors)
(verify-load classfile)
nil)))
(declare (ignore result))
(cond
((and (not internal-compiler-errors)
compiled-function)
(when compile-time-too
(eval form))
(let ((sym (if (consp name) (second name) name)))
(setf form
`(progn
(sys:put ',sym 'sys::source
(cl:cons '((:function ,name)
,(namestring *source*) ,*source-position*)
(cl:get ',sym 'sys::source nil)))
(sys:fset ',name
(sys::get-fasl-function *fasl-loader*
,saved-class-number)
,*source-position*
',lambda-list
,doc)))))
(t
(compiler-warn "Unable to compile function ~A. Using interpreted form instead.~%" name)
(when internal-compiler-errors
(dolist (e internal-compiler-errors)
(format *error-output*
"; ~A~%" e)))
(let ((precompiled-function
(precompiler:precompile-form expr nil
*compile-file-environment*)))
(setf form
`(sys:fset ',name
,precompiled-function
,*source-position*
',lambda-list
,doc)))
(when compile-time-too
(eval form)))))
(when (and (symbolp name) (eq (get name '%inline) 'INLINE))
;; FIXME Need to support SETF functions too!
(setf (inline-expansion name)
(jvm::generate-inline-expansion block-name
lambda-list
(append decls body)))
(output-form `(cl:setf (inline-expansion ',name)
',(inline-expansion name))))))
(push name jvm::*functions-defined-in-current-file*)
(note-name-defined name)
(push name *toplevel-functions*)
(when (and (consp name)
(or
(eq 'setf (first name))
(eq 'cl:setf (first name))))
(push (second name) *toplevel-setf-functions*))
;; If NAME is not fbound, provide a dummy definition so that
;; getSymbolFunctionOrDie() will succeed when we try to verify that
;; functions defined later in the same file can be loaded correctly.
(unless (fboundp name)
(setf (fdefinition name) #'dummy)
(push name *fbound-names*)))
form)
;; toplevel handlers
;; each toplevel handler takes a form and stream as input
(defun install-toplevel-handler (symbol handler)
(setf (get symbol 'toplevel-handler) handler))
(dolist (pair '((COMPILER-DEFSTRUCT precompile-toplevel-form)
(DECLARE process-toplevel-declare)
(DEFCONSTANT process-toplevel-defconstant)
(DEFGENERIC process-toplevel-defmethod/defgeneric)
(DEFMACRO process-toplevel-defmacro)
(DEFMETHOD process-toplevel-defmethod/defgeneric)
(DEFPACKAGE process-toplevel-defpackage/in-package)
(DEFPARAMETER process-toplevel-defvar/defparameter)
(DEFTYPE process-toplevel-deftype)
(DEFUN process-toplevel-defun)
(DEFVAR process-toplevel-defvar/defparameter)
(EVAL-WHEN process-toplevel-eval-when)
(EXPORT process-toplevel-export)
(IMPORT process-toplevel-import)
(IN-PACKAGE process-toplevel-defpackage/in-package)
(LOCALLY process-toplevel-locally)
(MACROLET process-toplevel-macrolet)
(PROCLAIM precompile-toplevel-form)
(PROGN process-toplevel-progn)
(PROVIDE precompile-toplevel-form)
(PUT precompile-toplevel-form)
(QUOTE process-toplevel-quote)
(REQUIRE precompile-toplevel-form)
(SHADOW precompile-toplevel-form)
(%SET-FDEFINITION precompile-toplevel-form)
(MOP::ENSURE-METHOD process-toplevel-mop.ensure-method)
(record-source-information-for-type process-record-source-information)))
(install-toplevel-handler (car pair) (cadr pair)))
(declaim (ftype (function (t stream t) t) process-toplevel-form))
(defun process-toplevel-form (form stream compile-time-too)
(unless (atom form)
(let* ((operator (%car form))
(handler (if (symbolp operator)
(get operator 'toplevel-handler))))
(when handler
(let ((out-form (funcall handler form stream compile-time-too)))
(when out-form
(output-form out-form)))
(return-from process-toplevel-form))
(when (and (symbolp operator)
(macro-function operator *compile-file-environment*))
(when (eq operator 'define-setf-expander)
(push (second form) *toplevel-setf-expanders*))
(when (and (eq operator 'defsetf)
(consp (third form))) ;; long form of DEFSETF
(push (second form) *toplevel-setf-expanders*))
(note-toplevel-form form)
;; Note that we want MACROEXPAND-1 and not MACROEXPAND here, in
;; case the form being expanded expands into something that needs
;; special handling by PROCESS-TOPLEVEL-FORM (e.g. DEFMACRO).
(let ((*compile-print* nil))
(process-toplevel-form (macroexpand-1 form *compile-file-environment*)
stream compile-time-too))
(return-from process-toplevel-form))
(cond
((and (symbolp operator)
(not (special-operator-p operator))
(null (cdr form)))
(setf form (precompiler:precompile-form form nil
*compile-file-environment*)))
(t
(note-toplevel-form form)
(setf form (convert-toplevel-form form nil)))))
(when (consp form)
(output-form form)))
;; Make sure the compiled-function loader knows where
;; to load the compiled functions. Note that this trickery
;; was already used in verify-load before I used it,
;; however, binding *load-truename* isn't fully compliant, I think.
(when compile-time-too
(let ((*load-truename* *output-file-pathname*)
(*fasl-loader* (make-fasl-class-loader
(concatenate 'string
"org.armedbear.lisp." (base-classname)))))
(eval form))))
(defun populate-zip-fasl (output-file)
(let* ((type ;; Don't use ".zip", it'll result in an extension with
;; a dot, which is rejected by NAMESTRING
(%format nil "~A~A" (pathname-type output-file) "-zip"))
(output-file (if (logical-pathname-p output-file)
(translate-logical-pathname output-file)
output-file))
(zipfile
(if (find :windows *features*)
(make-pathname :defaults output-file :type type)
(make-pathname :defaults output-file :type type
:device :unspecific)))
(pathnames nil)
(fasl-loader (make-pathname :defaults output-file
:name (fasl-loader-classname)
:type *compile-file-class-extension*)))
(when (probe-file fasl-loader)
(push fasl-loader pathnames))
(dotimes (i *class-number*)
(let ((truename (probe-file (compute-classfile (1+ i)))))
(when truename
(push truename pathnames)
;;; XXX it would be better to just use the recorded number
;;; of class constants, but probing for the first at least
;;; makes this subjectively bearable.
(when (probe-file
(make-pathname :name (format nil "~A_0"
(pathname-name truename))
:type "clc"
:defaults truename))
(dolist (resource (directory
(make-pathname :name (format nil "~A_*"
(pathname-name truename))
:type "clc"
:defaults truename)))
(push resource pathnames))))))
(setf pathnames (nreverse (remove nil pathnames)))
(let ((load-file (make-pathname :defaults output-file
:name "__loader__"
:type "_")))
(rename-file output-file load-file)
(push load-file pathnames))
(zip zipfile pathnames)
(dolist (pathname pathnames)
(ignore-errors (delete-file pathname)))
(rename-file zipfile output-file)))
(defun write-fasl-prologue (stream in-package)
"Write the forms that form the fasl to STREAM.
The last form will use IN-PACKAGE to set the *package* to its value when
COMPILE-FILE was invoked."
(let ((out stream)
(*package* (find-package :keyword)))
;; write header
(write "; -*- Mode: Lisp -*-" :escape nil :stream out)
(%stream-terpri out)
(write (list 'sys:init-fasl :version *fasl-version*) :stream out)
(%stream-terpri out)
(write (list 'cl:setq 'sys:*source* *compile-file-truename*) :stream out)
(%stream-terpri out)
;; Note: Beyond this point, you can't use DUMP-FORM,
;; because the list of uninterned symbols has been fixed now.
(when *fasl-uninterned-symbols*
(write (list 'cl:setq 'sys::*fasl-uninterned-symbols*
(coerce (mapcar #'car (nreverse *fasl-uninterned-symbols*))
'vector))
:stream out :length nil))
(%stream-terpri out)
(when (and (boundp '*fasl-instance-count*)
(plusp *fasl-instance-count*))
(write (list 'cl:setq 'sys::*fasl-instances*
(list 'cl:make-array *fasl-instance-count*))
:stream out)
(%stream-terpri out))
(when (> *class-number* 0)
(write (list 'cl:setq 'sys:*fasl-loader*
`(sys::make-fasl-class-loader
,(concatenate 'string "org.armedbear.lisp."
(base-classname))))
:stream out))
(%stream-terpri out)
(write `(in-package ,(package-name in-package))
:stream out)
(%stream-terpri out)))
(defvar *binary-fasls* nil)
(defvar *forms-for-output* nil)
(defvar *fasl-stream* nil)
(defun compile-from-stream (in output-file temp-file temp-file2
extract-toplevel-funcs-and-macros
functions-file macros-file exports-file
setf-functions-file setf-expanders-file)
(let* ((*compile-file-pathname* (make-pathname :defaults (pathname in)
:version nil))
(*compile-file-truename* (make-pathname :defaults (truename in)
:version nil))
(*source* *compile-file-truename*)
(*class-number* 0)
(namestring (namestring *compile-file-truename*))
(start (get-internal-real-time))
*fasl-uninterned-symbols*
(*fasl-instance-table* (make-hash-table :test 'eq))
(*fasl-instance-forms* (make-hash-table :test 'eq))
(*fasl-instance-refs* (make-hash-table :test 'eq))
(*fasl-instance-created-p* (make-hash-table :test 'eq))
(*fasl-instance-initialized-p* (make-hash-table :test 'eq))
(*fasl-instance-in-creation-p* (make-hash-table :test 'eq))
(*fasl-instance-in-init-p* (make-hash-table :test 'eq))
(*fasl-instance-count* 0)
(warnings-p nil)
(in-package *package*)
(failure-p nil))
(when *compile-verbose*
(format t "; Compiling ~A ...~%" namestring))
(with-compilation-unit ()
(with-open-file (out temp-file
:direction :output :if-exists :supersede
:external-format *fasl-external-format*)
(let ((*readtable* *readtable*)
(*read-default-float-format* *read-default-float-format*)
(*read-base* *read-base*)
(*package* *package*)
(jvm::*functions-defined-in-current-file* '())
(*fbound-names* '())
(*fasl-stream* out)
*forms-for-output*)
(jvm::with-saved-compiler-policy
(jvm::with-file-compilation
(handler-bind
((style-warning
#'(lambda (c)
(setf warnings-p t)
;; let outer handlers do their thing
(signal c)
;; prevent the next handler
;; from running: we're a
;; WARNING subclass
(continue)))
((or warning compiler-error)
#'(lambda (c)
(declare (ignore c))
(setf warnings-p t
failure-p t))))
(loop
(let* ((*source-position* (file-position in))
(jvm::*source-line-number* (stream-line-number in))
(form (read in nil in))
(*compiler-error-context* form))
(when (eq form in)
(return))
(handler-case (process-toplevel-form form out nil)
(compiler-bytecode-length-error ()
;; Following the solution propose here:
;; see https://github.com/armedbear/abcl/issues/246#issuecomment-698854437
;; just include the offending interpreted form in the loader
;; using it instead of the compiled representation
(diag "Falling back to interpreted version of top-level form longer ~
than 65535 bytes")
(write (ext:macroexpand-all form *compile-file-environment*)
:stream out)))
)))
(finalize-fasl-output)
(dolist (name *fbound-names*)
(fmakunbound name)))))))
(when extract-toplevel-funcs-and-macros
(setf *toplevel-functions*
(remove-if-not (lambda (func-name)
(if (symbolp func-name)
(symbol-package func-name)
T))
(remove-duplicates
*toplevel-functions*)))
(when *toplevel-functions*
(with-open-file (f-out functions-file
:direction :output
:if-does-not-exist :create
:if-exists :supersede)
(let ((*package* (find-package :keyword)))
(write *toplevel-functions* :stream f-out))))
(setf *toplevel-macros*
(remove-if-not (lambda (mac-name)
(if (symbolp mac-name)
(symbol-package mac-name)
T))
(remove-duplicates *toplevel-macros*)))
(when *toplevel-macros*
(with-open-file (m-out macros-file
:direction :output
:if-does-not-exist :create
:if-exists :supersede)
(let ((*package* (find-package :keyword)))
(write *toplevel-macros* :stream m-out))))
(setf *toplevel-exports*
(remove-if-not (lambda (sym)
(if (symbolp sym)
(symbol-package sym)
T))
(remove-duplicates *toplevel-exports*)))
(when *toplevel-exports*
(with-open-file (e-out exports-file
:direction :output
:if-does-not-exist :create
:if-exists :supersede)
(let ((*package* (find-package :keyword)))
(write *toplevel-exports* :stream e-out))))
(setf *toplevel-setf-functions*
(remove-if-not (lambda (sym)
(if (symbolp sym)
(symbol-package sym)
T))
(remove-duplicates *toplevel-setf-functions*)))
(when *toplevel-setf-functions*
(with-open-file (e-out setf-functions-file
:direction :output
:if-does-not-exist :create
:if-exists :supersede)
(let ((*package* (find-package :keyword)))
(write *toplevel-setf-functions* :stream e-out))))
(setf *toplevel-setf-expanders*
(remove-if-not (lambda (sym)
(if (symbolp sym)
(symbol-package sym)
T))
(remove-duplicates *toplevel-setf-expanders*)))
(when *toplevel-setf-expanders*
(with-open-file (e-out setf-expanders-file
:direction :output
:if-does-not-exist :create
:if-exists :supersede)
(let ((*package* (find-package :keyword)))
(write *toplevel-setf-expanders* :stream e-out)))))
(with-open-file (in temp-file :direction :input :external-format *fasl-external-format*)
(with-open-file (out temp-file2 :direction :output
:if-does-not-exist :create
:if-exists :supersede
:external-format *fasl-external-format*)
(let ((*package* (find-package :keyword))
(*print-fasl* t)
(*print-array* t)
(*print-base* 10)
(*print-case* :upcase)
(*print-circle* nil)
(*print-escape* t)
(*print-gensym* t)
(*print-length* nil)
(*print-level* nil)
(*print-lines* nil)
(*print-pretty* nil)
(*print-radix* nil)
(*print-readably* t)
(*print-right-margin* nil)
(*print-structure* t)