/
precompiler.lisp
1228 lines (1099 loc) · 45.4 KB
/
precompiler.lisp
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
;;; precompiler.lisp
;;;
;;; Copyright (C) 2003-2008 Peter Graves <peter@armedbear.org>
;;; $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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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")
(export '(process-optimization-declarations
inline-p notinline-p inline-expansion expand-inline
*defined-functions* *undefined-functions* note-name-defined))
(declaim (ftype (function (t) t) process-optimization-declarations))
(defun process-optimization-declarations (forms)
(dolist (form forms)
(unless (and (consp form) (eq (%car form) 'DECLARE))
(return))
(dolist (decl (%cdr form))
(case (car decl)
(OPTIMIZE
(dolist (spec (%cdr decl))
(let ((val 3)
(quality spec))
(when (consp spec)
(setf quality (%car spec)
val (cadr spec)))
(when (and (fixnump val)
(<= 0 val 3))
(case quality
(speed
(setf *speed* val))
(safety
(setf *safety* val))
(debug
(setf *debug* val))
(space
(setf *space* val))
(compilation-speed) ;; Ignored.
(t
(compiler-warn "Ignoring unknown optimization quality ~S in ~S." quality decl)))))))
((INLINE NOTINLINE)
(dolist (symbol (%cdr decl))
(push (cons symbol (%car decl)) *inline-declarations*)))
(:explain
(dolist (spec (%cdr decl))
(let ((val t)
(quality spec))
(when (consp spec)
(setf quality (%car spec))
(when (= (length spec) 2)
(setf val (%cadr spec))))
(if val
(pushnew quality *explain*)
(setf *explain* (remove quality *explain*)))))))))
t)
(declaim (ftype (function (t) t) inline-p))
(defun inline-p (name)
(declare (optimize speed))
(let ((entry (assoc name *inline-declarations* :test #'equal)))
(if entry
(eq (cdr entry) 'INLINE)
(and (symbolp name) (eq (get name '%inline) 'INLINE)))))
(declaim (ftype (function (t) t) notinline-p))
(defun notinline-p (name)
(declare (optimize speed))
(let ((entry (assoc name *inline-declarations* :test #'equal)))
(if entry
(eq (cdr entry) 'NOTINLINE)
(and (symbolp name) (eq (get name '%inline) 'NOTINLINE)))))
(defun expand-inline (form expansion)
;; (format t "expand-inline form = ~S~%" form)
;; (format t "expand-inline expansion = ~S~%" expansion)
(let* ((op (car form))
(proclaimed-ftype (proclaimed-ftype op))
(args (cdr form))
(vars (cadr expansion))
(varlist ())
new-form)
;; (format t "op = ~S proclaimed-ftype = ~S~%" op (proclaimed-ftype op))
(do ((vars vars (cdr vars))
(args args (cdr args)))
((null vars))
(push (list (car vars) (car args)) varlist))
(setf new-form (list* 'LET (nreverse varlist)
(copy-tree (cddr expansion))))
(when proclaimed-ftype
(let ((result-type (ftype-result-type proclaimed-ftype)))
(when (and result-type
(neq result-type t)
(neq result-type '*))
(setf new-form (list 'TRULY-THE result-type new-form)))))
;; (format t "expand-inline new form = ~S~%" new-form)
new-form))
(define-compiler-macro assoc (&whole form &rest args)
(cond ((and (= (length args) 4)
(eq (third args) :test)
(or (equal (fourth args) '(quote eq))
(equal (fourth args) '(function eq))))
`(assq ,(first args) ,(second args)))
((= (length args) 2)
`(assql ,(first args) ,(second args)))
(t form)))
(define-compiler-macro member (&whole form &rest args)
(let ((arg1 (first args))
(arg2 (second args)))
(case (length args)
(2
`(memql ,arg1 ,arg2))
(4
(let ((arg3 (third args))
(arg4 (fourth args)))
(cond ((and (eq arg3 :test)
(or (equal arg4 '(quote eq))
(equal arg4 '(function eq))))
`(memq ,arg1 ,arg2))
((and (eq arg3 :test)
(or (equal arg4 '(quote eql))
(equal arg4 '(function eql))
(equal arg4 '(quote char=))
(equal arg4 '(function char=))))
`(memql ,arg1 ,arg2))
(t
form))))
(t
form))))
(define-compiler-macro search (&whole form &rest args)
(if (= (length args) 2)
`(simple-search ,@args)
form))
(define-compiler-macro identity (&whole form &rest args)
(if (= (length args) 1)
`(progn ,(car args))
form))
(defun quoted-form-p (form)
(and (consp form) (eq (%car form) 'QUOTE) (= (length form) 2)))
(define-compiler-macro eql (&whole form &rest args)
(let ((first (car args))
(second (cadr args)))
(if (or (and (quoted-form-p first) (symbolp (cadr first)))
(and (quoted-form-p second) (symbolp (cadr second))))
`(eq ,first ,second)
form)))
(define-compiler-macro not (&whole form arg)
(if (atom arg)
form
(let ((op (case (car arg)
(>= '<)
(< '>=)
(<= '>)
(> '<=)
(t nil))))
(if (and op (= (length arg) 3))
(cons op (cdr arg))
form))))
(defun predicate-for-type (type)
(cdr (assq type '((ARRAY . arrayp)
(ATOM . atom)
(BIT-VECTOR . bit-vector-p)
(CHARACTER . characterp)
(COMPLEX . complexp)
(CONS . consp)
(FIXNUM . fixnump)
(FLOAT . floatp)
(FUNCTION . functionp)
(HASH-TABLE . hash-table-p)
(INTEGER . integerp)
(LIST . listp)
(NULL . null)
(NUMBER . numberp)
(NUMBER . numberp)
(PACKAGE . packagep)
(RATIONAL . rationalp)
(REAL . realp)
(SIMPLE-BIT-VECTOR . simple-bit-vector-p)
(SIMPLE-STRING . simple-string-p)
(SIMPLE-VECTOR . simple-vector-p)
(STREAM . streamp)
(STRING . stringp)
(SYMBOL . symbolp)))))
(define-compiler-macro typep (&whole form &rest args)
(if (= (length args) 2) ; no environment arg
(let* ((object (%car args))
(type-specifier (%cadr args))
(type (and (consp type-specifier)
(eq (%car type-specifier) 'QUOTE)
(%cadr type-specifier)))
(predicate (and type (predicate-for-type type))))
(if predicate
`(,predicate ,object)
`(%typep ,@args)))
form))
(define-compiler-macro subtypep (&whole form &rest args)
(if (= (length args) 2)
`(%subtypep ,@args)
form))
(define-compiler-macro funcall (&whole form
&environment env &rest args)
(let ((callee (car args)))
(if (and (>= *speed* *debug*)
(consp callee)
(eq (%car callee) 'function)
(symbolp (cadr callee))
(not (special-operator-p (cadr callee)))
(not (macro-function (cadr callee) env))
(memq (symbol-package (cadr callee))
(list (find-package "CL") (find-package "SYS"))))
`(,(cadr callee) ,@(cdr args))
form)))
(define-compiler-macro byte (size position)
`(cons ,size ,position))
(define-compiler-macro byte-size (bytespec)
`(car ,bytespec))
(define-compiler-macro byte-position (bytespec)
`(cdr ,bytespec))
(define-source-transform concatenate (&whole form result-type &rest sequences)
(if (equal result-type '(quote STRING))
`(sys::concatenate-to-string (list ,@sequences))
form))
(define-source-transform ldb (&whole form bytespec integer)
(if (and (consp bytespec)
(eq (%car bytespec) 'byte)
(= (length bytespec) 3))
(let ((size (%cadr bytespec))
(position (%caddr bytespec)))
`(%ldb ,size ,position ,integer))
form))
(define-source-transform find (&whole form item sequence &key from-end test test-not start end key)
(cond ((and (>= (length form) 3) (null start) (null end))
(cond ((and (stringp sequence)
(null from-end)
(member test '(#'eql #'char=) :test #'equal)
(null test-not)
(null key))
`(string-find ,item ,sequence))
(t
(let ((item-var (gensym))
(seq-var (gensym)))
`(let ((,item-var ,item)
(,seq-var ,sequence))
(if (listp ,seq-var)
(list-find* ,item-var ,seq-var ,from-end ,test ,test-not 0 (length ,seq-var) ,key)
(vector-find* ,item-var ,seq-var ,from-end ,test ,test-not 0 (length ,seq-var) ,key)))))))
(t
form)))
(define-source-transform adjoin (&whole form &rest args)
(if (= (length args) 2)
`(adjoin-eql ,(first args) ,(second args))
form))
(define-source-transform format (&whole form &rest args)
(if (stringp (second args))
`(format ,(pop args) (formatter ,(pop args)) ,@args)
form))
(define-compiler-macro catch (&whole form tag &rest args)
(declare (ignore tag))
(if (and (null (cdr args))
(constantp (car args)))
(car args)
form))
(define-compiler-macro string= (&whole form &rest args)
(if (= (length args) 2)
`(sys::%%string= ,@args)
form))
(define-compiler-macro <= (&whole form &rest args)
(cond ((and (= (length args) 3)
(numberp (first args))
(numberp (third args))
(= (first args) (third args)))
`(= ,(second args) ,(first args)))
(t
form)))
(in-package "PRECOMPILER")
(export '(precompile-form precompile))
;; No source-transforms and inlining in precompile-function-call
;; No macro expansion in precompile-dolist and precompile-dotimes
;; No macro expansion in precompile-do/do*
;; No macro expansion in precompile-defun
;; Special precompilation in precompile-case and precompile-cond
;; Special precompilation in precompile-when and precompile-unless
;; No precompilation in precompile-nth-value
;; Special precompilation in precompile-return
;;
;; if *in-jvm-compile* is false
(defvar *in-jvm-compile* nil)
(defvar *precompile-env* nil)
(declaim (inline expand-macro))
(defun expand-macro (form)
(macroexpand-1 form *precompile-env*))
(declaim (ftype (function (t) t) precompile1))
(defun precompile1 (form)
(cond ((symbolp form)
(multiple-value-bind
(expansion expanded)
(expand-macro form)
(if expanded
(precompile1 expansion)
form)))
((atom form)
form)
(t
(let ((op (%car form))
handler)
(when (symbolp op)
(cond ((setf handler (get op 'precompile-handler))
(return-from precompile1 (funcall handler form)))
((macro-function op *precompile-env*)
(return-from precompile1 (precompile1 (expand-macro form))))
((special-operator-p op)
(error "PRECOMPILE1: unsupported special operator ~S." op))))
(precompile-function-call form)))))
(defun precompile-identity (form)
(declare (optimize speed))
form)
(declaim (ftype (function (t) cons) precompile-cons))
(defun precompile-cons (form)
(cons (car form) (mapcar #'precompile1 (cdr form))))
(declaim (ftype (function (t t) t) precompile-function-call))
(defun precompile-function-call (form)
(let ((op (car form)))
(when (and (consp op) (eq (%car op) 'LAMBDA))
(return-from precompile-function-call
(or (precompile-function-position-lambda op (cdr form))
(cons (precompile-lambda op)
(mapcar #'precompile1 (cdr form))))))
(when (or (not *in-jvm-compile*) (notinline-p op))
(return-from precompile-function-call (precompile-cons form)))
(when (source-transform op)
(let ((new-form (expand-source-transform form)))
(when (neq new-form form)
(return-from precompile-function-call (precompile1 new-form)))))
(when *enable-inline-expansion*
(let ((expansion (inline-expansion op)))
(when expansion
(let ((explain *explain*))
(when (and explain (memq :calls explain))
(format t "; inlining call to ~S~%" op)))
(return-from precompile-function-call (precompile1 (expand-inline form expansion))))))
(cons op (mapcar #'precompile1 (cdr form)))))
(defun precompile-function-position-lambda (lambda args)
(let* ((arglist (second lambda))
(body (cddr lambda))
(simple-arglist? (not (or (memq '&KEY arglist) (memq '&OPTIONAL arglist) (memq '&REST arglist)))))
(or
;;give a chance for someone to transform single-form function bodies
(and (= (length body) 1) (consp (car body)) (get (caar body) 'sys::function-position-lambda-transform)
(funcall (get (caar body) 'sys::function-position-lambda-transform) (caar body) (car body) (mapcar #'precompile1 args)))
(and simple-arglist?
(let ((arglist-length (if (memq '&aux arglist) (position '&aux arglist) (length arglist))))
(if (= (length args) arglist-length)
;; simplest case - we have a simple arglist with as many
;; arguments as call args. Transform to let.
(return-from precompile-function-position-lambda
`(let* ,(append
(loop for arg-name in arglist
for arg in (mapcar #'precompile1 args)
until (eq arg-name '&aux)
collect (list arg-name arg))
(subseq arglist (1+ arglist-length)))
,@body))
(error "Argument mismatch for lambda in function position: ~a applied to ~a" `(lambda ,arglist body) args)))))))
(defmacro define-function-position-lambda-transform (body-function-name (arglist form args) &body body)
`(put ',body-function-name 'sys::function-position-lambda-transform
#'(lambda(,arglist ,form ,args)
,@body)))
(defun precompile-locally (form)
(let ((*inline-declarations* *inline-declarations*))
(process-optimization-declarations (cdr form))
(cons 'LOCALLY (mapcar #'precompile1 (cdr form)))))
(defun precompile-block (form)
(let ((args (cdr form)))
(if (null (cdr args))
nil
(list* 'BLOCK (car args) (mapcar #'precompile1 (cdr args))))))
(defun precompile-dolist (form)
(if *in-jvm-compile*
(precompile1 (macroexpand form *precompile-env*))
(cons 'DOLIST (cons (mapcar #'precompile1 (cadr form))
(mapcar #'precompile1 (cddr form))))))
(defun precompile-dotimes (form)
(if *in-jvm-compile*
(precompile1 (macroexpand form *precompile-env*))
(cons 'DOTIMES (cons (mapcar #'precompile1 (cadr form))
(mapcar #'precompile1 (cddr form))))))
(defun precompile-do/do*-vars (varlist)
(let ((result nil))
(dolist (varspec varlist)
(if (atom varspec)
(push varspec result)
(case (length varspec)
(1
(push (%car varspec) result))
(2
(let* ((var (%car varspec))
(init-form (%cadr varspec)))
(unless (symbolp var)
(error 'type-error))
(push (list var (precompile1 init-form))
result)))
(3
(let* ((var (%car varspec))
(init-form (%cadr varspec))
(step-form (%caddr varspec)))
(unless (symbolp var)
(error 'type-error))
(push (list var (precompile1 init-form) (precompile1 step-form))
result))))))
(nreverse result)))
(defun precompile-do/do*-end-form (end-form)
(let ((end-test-form (car end-form))
(result-forms (cdr end-form)))
(list* (precompile1 end-test-form) (mapcar #'precompile1 result-forms))))
(defun precompile-do/do* (form)
(if *in-jvm-compile*
(precompile1 (macroexpand form *precompile-env*))
(list* (car form)
(precompile-do/do*-vars (cadr form))
(precompile-do/do*-end-form (caddr form))
(mapcar #'precompile1 (cdddr form)))))
(defun precompile-do-symbols (form)
(list* (car form) (cadr form) (mapcar #'precompile1 (cddr form))))
(defun precompile-load-time-value (form)
form)
(defun precompile-progn (form)
(let ((body (cdr form)))
(if (eql (length body) 1)
(let ((res (precompile1 (%car body))))
;; If the result turns out to be a bare symbol, leave it wrapped
;; with PROGN so it won't be mistaken for a tag in an enclosing
;; TAGBODY.
(if (symbolp res)
(list 'progn res)
res))
(cons 'PROGN (mapcar #'precompile1 body)))))
(defun precompile-threads-synchronized-on (form)
(cons 'threads:synchronized-on (mapcar #'precompile1 (cdr form))))
(defun precompile-progv (form)
(if (< (length form) 3)
(error "Not enough arguments for ~S." 'progv)
(list* 'PROGV (mapcar #'precompile1 (%cdr form)))))
(defun precompile-setf (form)
(let ((place (second form)))
(cond ((and (consp place)
(eq (%car place) 'VALUES))
(setf form
(list* 'SETF
(list* 'VALUES
(mapcar #'precompile1 (%cdr place)))
(cddr form)))
(precompile1 (expand-macro form)))
((symbolp place)
(multiple-value-bind
(expansion expanded)
;; Expand once in case the form expands
;; into something that needs special
;; SETF treatment
(macroexpand-1 place *precompile-env*)
(if expanded
(precompile1 (list* 'SETF expansion
(cddr form)))
(precompile1 (expand-macro form)))))
(t
(precompile1 (expand-macro form))))))
(defun precompile-setq (form)
(let* ((args (cdr form))
(len (length args)))
(when (oddp len)
(error 'simple-program-error
:format-control "Odd number of arguments to SETQ."))
(if (= len 2)
(let* ((sym (%car args))
(val (%cadr args)))
(multiple-value-bind
(expansion expanded)
;; Expand once in case the form expands
;; into something that needs special
;; SETF treatment
(macroexpand-1 sym *precompile-env*)
(if expanded
(precompile1 (list 'SETF expansion val))
(list 'SETQ sym (precompile1 val)))))
(let ((result ()))
(loop
(when (null args)
(return))
(push (precompile-setq (list 'SETQ (car args) (cadr args))) result)
(setq args (cddr args)))
(setq result (nreverse result))
(push 'PROGN result)
result))))
(defun precompile-psetf (form)
(setf form
(list* 'PSETF
(mapcar #'precompile1 (cdr form))))
(precompile1 (expand-macro form)))
(defun precompile-psetq (form)
;; Make sure all the vars are symbols.
(do* ((rest (cdr form) (cddr rest))
(var (car rest)))
((null rest))
(unless (symbolp var)
(error 'simple-error
:format-control "~S is not a symbol."
:format-arguments (list var))))
;; Delegate to PRECOMPILE-PSETF so symbol macros are handled correctly.
(precompile-psetf form))
(defun precompile-lambda-list (form)
(let (new aux-tail)
(dolist (arg form (nreverse new))
(if (or (atom arg) (> 2 (length arg)))
(progn
(when (eq arg '&aux)
(setf aux-tail t))
(push arg new))
;; must be a cons of more than 1 cell
(let ((new-arg (copy-list arg)))
(unless (<= 1 (length arg) (if aux-tail 2 3))
;; the aux-vars have a maximum length of 2 conses
;; optional and key vars may have 3
(error 'program-error
:format-control
"The ~A binding specification ~S is invalid."
:format-arguments (list (if aux-tail "&AUX"
"&OPTIONAL/&KEY") arg)))
(setf (second new-arg)
(precompile1 (second arg)))
(push new-arg new))))))
(defun extract-lambda-vars (lambda-list)
(let ((state :required)
vars)
(dolist (var/key lambda-list vars)
(cond
((eq '&aux var/key) (setf state :aux))
((eq '&key var/key) (setf state :key))
((eq '&optional var/key) (setf state :optional))
((eq '&rest var/key) (setf state :rest))
((symbolp var/key) (unless (eq var/key '&allow-other-keys)
(push var/key vars)))
((and (consp var/key)
(member state '(:optional :key)))
(setf var/key (car var/key))
(when (and (consp var/key) (eq state :key))
(setf var/key (second var/key)))
(if (symbolp var/key)
(push var/key vars)
(error 'program-error
:format-control
"Unexpected ~A variable specifier ~A."
:format-arguments (list state var/key))))
((and (consp var/key) (eq state :aux))
(if (symbolp (car var/key))
(push (car var/key) vars)
(error 'program-error
:format-control "Unexpected &AUX format for ~A."
:format-arguments (list var/key))))
(t
(error 'program-error
:format-control "Unexpected lambda-list format: ~A."
:format-arguments (list lambda-list)))))))
(defun precompile-lambda (form)
(let ((body (cddr form))
(precompiled-lambda-list
(precompile-lambda-list (cadr form)))
(*inline-declarations* *inline-declarations*)
(*precompile-env* (make-environment *precompile-env*)))
(process-optimization-declarations body)
(dolist (var (extract-lambda-vars precompiled-lambda-list))
(environment-add-symbol-binding *precompile-env* var nil))
(list* 'LAMBDA precompiled-lambda-list
(mapcar #'precompile1 body))))
(defun precompile-named-lambda (form)
(let ((lambda-form (list* 'LAMBDA (caddr form) (cdddr form))))
(let ((body (cddr lambda-form))
(precompiled-lambda-list
(precompile-lambda-list (cadr lambda-form)))
(*inline-declarations* *inline-declarations*)
(*precompile-env* (make-environment *precompile-env*)))
(process-optimization-declarations body)
(dolist (var (extract-lambda-vars precompiled-lambda-list))
(environment-add-symbol-binding *precompile-env* var nil))
(list* 'NAMED-LAMBDA (cadr form) precompiled-lambda-list
(mapcar #'precompile1 body)))))
(defun precompile-defun (form)
(if *in-jvm-compile*
(precompile1 (expand-macro form))
form))
(defun precompile-macrolet (form)
(let ((*precompile-env* (make-environment *precompile-env*)))
(dolist (definition (cadr form))
(environment-add-macro-definition
*precompile-env*
(car definition)
(make-macro (car definition)
(make-closure
(make-macro-expander definition)
NIL))))
(multiple-value-bind (body decls)
(parse-body (cddr form) nil)
`(locally ,@decls ,@(mapcar #'precompile1 body)))))
(defun precompile-symbol-macrolet (form)
(let ((*precompile-env* (make-environment *precompile-env*))
(defs (cadr form)))
(dolist (def defs)
(let ((sym (car def))
(expansion (cadr def)))
(when (special-variable-p sym)
(error 'program-error
:format-control
"Attempt to bind the special variable ~S with SYMBOL-MACROLET."
:format-arguments (list sym)))
(environment-add-symbol-binding *precompile-env*
sym
(sys::make-symbol-macro expansion))))
(multiple-value-bind (body decls)
(parse-body (cddr form) nil)
(when decls
(let ((specials ()))
(dolist (decl decls)
(when (eq (car decl) 'DECLARE)
(dolist (declspec (cdr decl))
(when (eq (car declspec) 'SPECIAL)
(setf specials (append specials (cdr declspec)))))))
(when specials
(let ((syms (mapcar #'car (cadr form))))
(dolist (special specials)
(when (memq special syms)
(error 'program-error
:format-control
"~S is a symbol-macro and may not be declared special."
:format-arguments (list special))))))))
`(locally ,@decls ,@(mapcar #'precompile1 body)))))
(defun precompile-the (form)
(list 'THE
(second form)
(precompile1 (third form))))
(defun precompile-truly-the (form)
(list 'TRULY-THE
(second form)
(precompile1 (third form))))
(defun precompile-let/let*-vars (vars)
(let ((result nil))
(dolist (var vars)
(cond ((consp var)
(unless (<= 1 (length var) 2)
(error 'program-error
:format-control
"The LET/LET* binding specification ~S is invalid."
:format-arguments (list var)))
(let ((v (%car var))
(expr (cadr var)))
(unless (symbolp v)
(error 'simple-type-error
:format-control "The variable ~S is not a symbol."
:format-arguments (list v)))
(push (list v (precompile1 expr)) result)
(environment-add-symbol-binding *precompile-env* v nil)))
;; any value will do: we just need to shadow any symbol macros
(t
(push var result)
(environment-add-symbol-binding *precompile-env* var nil))))
(nreverse result)))
(defun precompile-let (form)
(let ((*precompile-env* (make-environment *precompile-env*)))
(list* 'LET
(precompile-let/let*-vars (cadr form))
(mapcar #'precompile1 (cddr form)))))
;; (LET* ((X 1)) (LET* ((Y 2)) (LET* ((Z 3)) (+ X Y Z)))) =>
;; (LET* ((X 1) (Y 2) (Z 3)) (+ X Y Z))
(defun maybe-fold-let* (form)
(if (and (= (length form) 3)
(consp (%caddr form))
(eq (%car (%caddr form)) 'LET*))
(let ((third (maybe-fold-let* (%caddr form))))
(list* 'LET* (append (%cadr form) (cadr third)) (cddr third)))
form))
(defun precompile-let* (form)
(setf form (maybe-fold-let* form))
(let ((*precompile-env* (make-environment *precompile-env*)))
(list* 'LET*
(precompile-let/let*-vars (cadr form))
(mapcar #'precompile1 (cddr form)))))
(defun precompile-case (form)
(if *in-jvm-compile*
(precompile1 (macroexpand form *precompile-env*))
(let* ((keyform (cadr form))
(clauses (cddr form))
(result (list (precompile1 keyform))))
(dolist (clause clauses)
(push (precompile-case-clause clause) result))
(cons (car form) (nreverse result)))))
(defun precompile-case-clause (clause)
(let ((keys (car clause))
(forms (cdr clause)))
(cons keys (mapcar #'precompile1 forms))))
(defun precompile-cond (form)
(if *in-jvm-compile*
(precompile1 (macroexpand form *precompile-env*))
(let ((clauses (cdr form))
(result nil))
(dolist (clause clauses)
(push (precompile-cond-clause clause) result))
(cons 'COND (nreverse result)))))
(defun precompile-cond-clause (clause)
(let ((test (car clause))
(forms (cdr clause)))
(cons (precompile1 test) (mapcar #'precompile1 forms))))
(defun precompile-local-function-def (def)
(let ((name (car def))
(body (cddr def)))
;; Macro names are shadowed by local functions.
(environment-add-function-definition *precompile-env* name body)
(cdr (precompile-named-lambda (list* 'NAMED-LAMBDA def)))))
(defun precompile-local-functions (defs)
(let ((result nil))
(dolist (def defs (nreverse result))
(push (precompile-local-function-def def) result))))
(defun find-use (name expression)
(cond ((atom expression)
nil)
((eq (%car expression) name)
t)
((consp name)
t) ;; FIXME Recognize use of SETF functions!
(t
(or (find-use name (%car expression))
(find-use name (%cdr expression))))))
(defun precompile-flet/labels (form)
(let* ((*precompile-env* (make-environment *precompile-env*))
(operator (car form))
(locals (cadr form))
precompiled-locals
applicable-locals
body)
(when (eq operator 'FLET)
;; FLET functions *don't* shadow within their own FLET form
(setf precompiled-locals
(precompile-local-functions locals))
(setf applicable-locals precompiled-locals))
;; augment the environment with the newly-defined local functions
;; to shadow preexisting macro definitions with the same names
(dolist (local locals)
;; we can use the non-precompiled locals, because the function body isn't used
(environment-add-function-definition *precompile-env*
(car local) (cddr local)))
(when (eq operator 'LABELS)
;; LABELS functions *do* shadow within their own LABELS form
(setf precompiled-locals
(precompile-local-functions locals))
(setf applicable-locals precompiled-locals))
;; then precompile (thus macro-expand) the body before inspecting it
;; for the use of our locals and eliminating dead code
(setq body (mapcar #'precompile1 (cddr form)))
(dolist (local precompiled-locals)
(let* ((name (car local))
(used-p (find-use name body)))
(unless used-p
(when (eq operator 'LABELS)
(dolist (local precompiled-locals)
(when (neq name (car local))
(when (find-use name (cddr local))
(setf used-p t)
(return))
;; Scope of defined function names includes
;; &OPTIONAL, &KEY and &AUX parameters
;; (LABELS.7B, LABELS.7C and LABELS.7D).
(let ((vars (or
(cdr (memq '&optional (cadr local)))
(cdr (memq '&key (cadr local)))
(cdr (memq '&aux (cadr local))))))
(when (and vars (find-use name vars))
(setf used-p t)
(return)))
))))
(unless used-p
(compiler-style-warn "; Note: deleting unused local function ~A ~S~%"
operator name)
(setf applicable-locals (remove local applicable-locals)))))
(if applicable-locals
(list* operator applicable-locals body)
(list* 'LOCALLY body))))
(defun precompile-function (form)
(if (and (consp (cadr form)) (eq (caadr form) 'LAMBDA))
(list 'FUNCTION (precompile-lambda (%cadr form)))
form))
(defun precompile-if (form)
(let ((args (cdr form)))
(case (length args)
(2
(let ((test (precompile1 (%car args))))
(cond ((null test)
nil)
(;;(constantp test)
(eq test t)
(precompile1 (%cadr args)))
(t
(list 'IF
test
(precompile1 (%cadr args)))))))
(3
(let ((test (precompile1 (%car args))))
(cond ((null test)
(precompile1 (%caddr args)))
(;;(constantp test)
(eq test t)
(precompile1 (%cadr args)))
(t
(list 'IF
test
(precompile1 (%cadr args))
(precompile1 (%caddr args)))))))
(t
(error "wrong number of arguments for IF")))))
(defun precompile-when (form)
(if *in-jvm-compile*
(precompile1 (macroexpand form *precompile-env*))
(precompile-cons form)))
(defun precompile-unless (form)
(if *in-jvm-compile*
(precompile1 (macroexpand form *precompile-env*))
(precompile-cons form)))
;; MULTIPLE-VALUE-BIND is handled explicitly by the JVM compiler.
(defun precompile-multiple-value-bind (form)
(let ((vars (cadr form))
(values-form (caddr form))
(body (cdddr form))
(*precompile-env* (make-environment *precompile-env*)))
(dolist (var vars)
(environment-add-symbol-binding *precompile-env* var nil))
(list* 'MULTIPLE-VALUE-BIND
vars
(precompile1 values-form)
(mapcar #'precompile1 body))))
;; MULTIPLE-VALUE-LIST is handled explicitly by the JVM compiler.
(defun precompile-multiple-value-list (form)
(list 'MULTIPLE-VALUE-LIST (precompile1 (cadr form))))
(defun precompile-nth-value (form)
(if *in-jvm-compile*
(precompile1 (macroexpand form *precompile-env*))
form))
(defun precompile-return (form)
(if *in-jvm-compile*
(precompile1 (macroexpand form *precompile-env*))
(list 'RETURN (precompile1 (cadr form)))))
(defun precompile-return-from (form)
(list 'RETURN-FROM (cadr form) (precompile1 (caddr form))))
(defun precompile-tagbody (form)
(do ((body (cdr form) (cdr body))
(result ()))
((null body) (cons 'TAGBODY (nreverse result)))
(if (atom (car body))
(push (car body) result)
(push (let* ((first-form (car body))
(expanded (precompile1 first-form)))
(if (and (symbolp expanded)
(neq expanded first-form))
;; Workaround:
;; Since our expansion/compilation order
;; is out of sync with the definition of
;; TAGBODY (which requires the compiler
;; to look for tags before expanding),
;; we need to disguise anything which might
;; look like a tag. We do this by wrapping
;; it in a PROGN form.
(list 'PROGN expanded)
expanded)) result))))
(defun precompile-eval-when (form)
(list* 'EVAL-WHEN (cadr form) (mapcar #'precompile1 (cddr form))))
(defun precompile-unwind-protect (form)
(list* 'UNWIND-PROTECT
(precompile1 (cadr form))
(mapcar #'precompile1 (cddr form))))
(declaim (ftype (function (t t) t) precompile-form))
(defun precompile-form (form in-jvm-compile
&optional precompile-env)
(let ((*in-jvm-compile* in-jvm-compile)
(*inline-declarations* *inline-declarations*)
(pre::*precompile-env* precompile-env))
(precompile1 form)))
(defun install-handler (symbol &optional handler)
(declare (type symbol symbol))
(let ((handler (or handler
(find-symbol (sys::%format nil "PRECOMPILE-~A"
(symbol-name symbol))
'precompiler))))