-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcomp.l
1548 lines (1500 loc) · 52.4 KB
/
comp.l
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
;************************************************************************
;; E U S compiler
;; vers 1.0
;; 1986-SEP-15
;; 1986-Oct-27 compilation of functions and special forms
;; 1987-Mar-18 optimization for integer functions and conditional branch
;; 1987-Mar-31 compilation of special binding variables
;; 1987-Oct-19 keyword parameters
;; 1994-Apr Solaris-2
;;
;; author: T.MATSUI, automatic control division, Electrotechnical Lab.
;************************************************************************
(in-package "COMPILER")
(eval-when (load eval)
(export '(compile compile-file comfile identifier
compile-file-if-src-newer))
(export '(*safety* *space* *verbose* *optimize* *speed* *cc*)) )
(defun compiled-code-p (x) (derivedp x compiled-code))
(eval-when (load eval)
(defparameter *coptflags* "")
(defparameter *cflags* "")
(defparameter *defun-list* nil)
(defparameter *verbose* nil)
(defparameter *optimize* 2)
(defparameter *safety* 1)
(defparameter *space* 0)
(defparameter *do-cc* t)
(defparameter *pic* t)
(defparameter *kernel* nil)
;(defparameter *cc* (if (member :gcc *features*) "gcc" "cc"))
(defparameter *cc*
(cond ((member :sh4 *features*)
"sh4-linux-gcc -g")
((member :ia32 *features*)
"gcc -g -m32")
((member :gcc *features*)
"gcc -g")
((member :alpha *features*) "cc -ieee_with_inexact" )
(t "cc")))
(defvar comp nil "$Id$")
(defvar trans)
(defvar *multipass-optimize* t)
(defun ovafp (form) (and (cdr form) (symbolp (cdr form))))
(defun class-symbolp (x)
(and (symbolp x) (boundp x) (classp (symbol-value x))))
(defun quoted-symbolp (x)
(and (consp x) (eq (car x) 'quote) (symbolp (cadr x))))
(defun proclaimed-special-p (var)
(declare (type symbol var))
(or (>= (var . vtype) 3)
; (and (boundp var) (classp (symbol-value var)))
)
)
(defun proclaimed-global-p (var)
(declare (type symbol var))
(= (var . vtype) 2)
)
(defun object-variable-names (klass-name)
(let (klass varvec i r)
(if (symbolp klass-name)
(setq klass (symbol-value klass-name))
(setq klass klass-name))
(when (classp klass)
(setq varvec (klass . vars)
i (length varvec))
(while (>= (dec i) 0)
(setq r (cons (svref varvec i) r))) )
r))
(defun object-variable-type (klass var)
(declare (type metaclass klass))
(when (not (classp klass))
(if (class-symbolp klass)
(setq klass (symbol-value klass))
(error "class expected for type check")))
(if (numberp var)
(setq var (svref (klass . types) var))
(let ((index (position var (klass . vars))))
(if (null index)
(send comp :error "no such obj var" var)
(setq var (svref (klass . types) index)))))
(if (consp var) (car var) var))
(defun coerce-type (tp)
(cond ((memq tp '(integer :integer fixnum :fixnum)) 'integer)
((memq tp '(float :float)) 'float)
(t tp)))
(defun check-arg (req n &optional (fn "car/cdr"))
(if (null (= req n))
(warn "mismatch arg for " fn)))
(defun def-function-type (type funcs)
(dolist (f funcs) (putprop f type 'function-result-type)))
(def-function-type 'integer
'(ash logand logior logxor 1+ 1-
length
floor ceiling truncate round))
(def-function-type 'float
'(sin cos tan asin acos atan
sqrt exp log v. v.* norm distance))
(def-function-type float-vector
'(scale v+ v- v* transform))
) ;eval-when
;;
;; identifier management
;;
; identifier descriptor
;
(eval-when (load compile eval)
(defclass identifier :super object
:slots (name type binding level offset))
)
;; binding = (constant, local, global, special)
(eval-when (load eval)
(defmethod identifier
(:type (&optional (tp type)) (setq type (coerce-type tp)))
(:offset (&optional (off offset)) (setq offset off))
(:binding (&optional (bin binding)) (setq binding bin))
(:init (sym bin lev off)
(setq name sym
type t
binding bin
level lev
offset off)
self))
)
; identifier table
;
(eval-when (load compile eval)
(defclass identifier-table :super object
:slots (frames level maxlevel))
)
(eval-when (load eval)
(defmethod identifier-table
(:find (id &optional (lev level)) ;search only in the scope
(cdr (assq id (svref frames lev))))
(:get (id &optional (lev level)) ;search in nested scopes
(let (r)
(while (>= lev 0)
(setq r (assq id (svref frames lev)))
(if r (return-from :get (cdr r)) (dec lev)))))
(:enter (id &optional (lev level))
(svset frames lev (cons (cons (id . name) id) (svref frames lev)))
id)
(:enter-special (id) (send self :enter id 0))
(:create-frame () (inc level))
(:pop-frame () (svset frames level nil) (dec level))
(:clear-frame () (svset frames level nil))
(:level (&optional (lvl level)) (setq level lvl)) ;ask or set cur level
(:frame (&optional (n level)) (svref frames n))
(:init-frames ()
(dotimes (n maxlevel) (svset frames n nil))
(setq level 0)
self)
(:init (&optional (maxlev 20))
(setq maxlevel maxlev
frames (make-array maxlevel)
level 0)
self)
)
)
(eval-when (load compile eval)
(defclass stack-frame :super object
:slots (offset specials locals))
)
(eval-when (load eval)
(defmethod stack-frame
(:offset (&optional (off nil)) (if off (setq offset off)) offset)
(:special (&optional (i 0)) (setq specials (+ specials i)))
(:local (&optional (i 0)) (setq locals (+ locals i)))
(:init () (setq offset nil specials 0 locals 0) self))
)
;****************************************************************
;* EusLisp compiler class
;****************************************************************
(eval-when (load compile eval)
(defclass compiler :super object
:slots
(idtable ;identifier-table
closure-level ;currunt closure level
scope ;current variable scope (for sequential let)
frames ;list of the number of special bindings
blocks ;block labels
tags ;tagbody go labels
function-closures ; (function ...) list
initcodes ;initialize codes in "eusmain"
flets
unwind-frames ;frames need to unwound when jumps
)))
(eval-when (load eval)
(defmethod compiler
(:genlabel (head &optional (suffix ""))
(concatenate string (string (gensym head)) suffix))
(:gencname-tail
(&rest lnames)
;; NOTE: Deprecated. Use lisp::gencname-tail instead. (2017/10/19 furushchev)
(apply #'lisp::gencname-tail lnames))
(:progn (bodies)
(if bodies
(while bodies
(send self :eval (pop bodies))
(and bodies (send trans :discard 1)))
(send trans :load-nil)))
(:eval (form)
(if (and (symbolp form) (constantp form))
(let ((s (symbol-value form)))
(cond
((numberp s) (setq form s))
((memq s '(nil t)) (setq form s))
((symbolp s) (setq form (list 'quote s)))
((stringp s) (setq form s)))))
(cond ((eq form t) (send trans :load-t) 'symbol)
((eq form nil) (send trans :load-nil) nil)
((keywordp form) (send trans :load-quote form) 'symbol)
((symbolp form) (send self :load-var form))
((integerp form) (send trans :load-int form) 'integer)
((floatp form) (send trans :load-float form) 'float)
((atom form) (send trans :load-quote form) t)
((ovafp form) (send self :load-ovaf (car form) (cdr form)))
((listp (car form))
(send self :eval `(apply (function ,(car form)) ,(cdr form))))
(t
(let* ((fdef (send self :get-function (car form)))
(ftype (second fdef)))
(case ftype
(lambda (send self :funcall (car form) (cdr form)))
(special (send self :special-form (car form) (cdr form)))
(macro (send self :eval (macroexpand form)))
(closure (send self :call-closure fdef (cdr form)))
(t (send self :error "unknown func type" form))) ))))
(:get-function (fn)
(if (not (symbolp fn)) (send self :error "symbol expected for a func. name"))
(let ((fdef (assq fn flets)))
(if fdef
fdef
(cond ((fboundp fn)
(setq fdef (symbol-function fn))
(if (compiled-function-p fdef)
(list fn (cdr (assq (compiled-code-type fdef)
'((0 . lambda) (1 . macro) (2 . special))))
fdef)
(cons fn fdef)))
(t (list fn 'lambda))))))
(:call-closure (fdef args)
(if *debug* (print (list :call-closure fdef args)))
(dolist (a args) (send self :eval a))
(send trans :load-local (third fdef) (- closure-level (fourth fdef)))
(send trans :call-closure (fifth fdef) (length args)))
(:variable (var) ;var must be a symbol, ovaf is not allowed here.
(let ((result (send idtable :get var scope)))
(if result (return-from :variable result))
(setq result (instance identifier :init var 'special 0 0))
(cond
((proclaimed-special-p var)
(send idtable :enter-special result) )
((proclaimed-global-p var)
(send idtable :enter-special result)) ;Jan, 96
(t
(format *error-output* "; ~C[34;7m~S~C[0;34m is assumed to be global~C[0m~%"
#x1b var #x1b #x1b)
(send idtable :enter result)) )
result))
(:var-binding (var)
(if (consp var)
'ovaf
(send (send self :variable var) :binding)))
(:special-variable-p (id)
(let ((v (send idtable :find id)))
(if (null v)
(or (proclaimed-special-p id) (proclaimed-global-p id))
(eq (send v :binding) 'special))))
(:enter-variable (id)
;make id descripter and enter it in idtable
(let ((v (send idtable :find id))) ;found in current scope?
(when (null v)
(setq v (instance identifier :init
id 'unknown closure-level nil))
(send idtable :enter v)
(when (or (proclaimed-special-p id) (proclaimed-global-p id))
(if (constantp id) (send self :error "not variable" id))
(send v :binding 'special)
))
v))
(:bind (id binding offset &optional (keyvarp nil))
(unless (symbolp id)
(error "symbol expected for function argument" id))
(let ((v (send self :enter-variable id)))
(declare (type identifier v))
(cond
((eq (v . binding) 'special)
(send (car frames) :special 1)
(case binding
(local (if keyvarp (send (car frames) :local 1)))
(arg (send trans :load-arg offset 0))
(t (send self :error "illegal binding")))
(push offset unwind-frames)
(send trans :bind-special id))
(t
(if (eq binding 'local) (send (car frames) :local 1))
(setq (v . binding) binding
(v . offset) offset)))
v))
(:create-frame ()
(push (instance stack-frame :init) frames)
(send idtable :create-frame)
(setq scope (send idtable :level)))
(:delete-frame (flag)
(let* ((f (pop frames)) (nospecials (f . specials)))
(declare (type stack-frame f))
(if (> nospecials 0)
(if flag
(send trans :unbind-special (f . specials))
(send self :warning "no special object variables allowed")))
(if flag (send trans :del-frame (f . specials) (f . locals)))
(send idtable :pop-frame)
(setq scope (send idtable :level))))
(:load-ovaf (form var)
(let ((form-type (send self :eval form)) (index 0))
(if (null (memq form-type '(integer fixnum float number t nil)))
(setq form-type (symbol-value form-type))) ;class object
(cond
((classp form-type)
(setq index (position var (object-variable-names form-type)))
(if (numberp index)
(send trans :load-indexed-ov index)
(send self :error "no such object variable" form var))
(object-variable-type form-type index))
(t (send trans :load-ovaf var)
t)) ))
(:load-var (var) ;returns type
(when (consp var)
(send self :load-ovaf (car var) (cdr var))
(return-from :load-var t))
(setq var (send self :variable var))
(case (var . binding)
((special) (send trans :load-global (var . name)))
((local let lambda)
(send trans :load-local (var . offset) (- closure-level (var . level))))
((param arg)
(send trans :load-arg (var . offset) (- closure-level (var . level))))
((object)
(send trans :load-obj (var . offset) (- closure-level (var . level))))
(unknown
(send self :error "declared but unknown variable" (var . name)))
)
(send var :type))
(:store-ovaf (form varname)
(let ((form-type (send self :eval form)))
(if (null (memq form-type '(integer fixnum float number t nil)))
(setq form-type (symbol-value form-type)))
(if (classp form-type)
(progn
(send trans :store-indexed-ov
(position varname
(object-variable-names form-type)))
t)
(send trans :store-ovaf varname))))
(:store-var (var)
(setq var (send self :variable var))
(case (var . binding)
((special) (send trans :store-global (var . name)))
((local let lambda)
(send trans :store-local (var . offset) (- closure-level (var . level))))
((param arg) (send trans :store-arg (var . offset)
(- closure-level (var . level))))
((object) (send trans :store-obj (var . offset)
(- closure-level (var . level)))))))
) ;eval-when
(eval-when (load eval)
(defmethod compiler
(:funcall (sym args)
(let ((argcount (length args)) (arg-type-list nil) (entry))
(declare (type integer argcount))
(if (null (memq sym '(slot setslot)))
(setq arg-type-list
(mapcar #'(lambda (x) (send self :eval x)) args)))
(case sym
((null not) (check-arg 1 argcount 'null)
(send trans :nullx) 'symbol)
((eq) (check-arg 2 argcount 'eq) (send trans :eqx) 'symbol)
((memq) (check-arg 2 argcount 'memq) (send trans :memqx) t)
((1+) (check-arg 1 argcount '1+)
(cond ((integerp (car args))
(send trans :1+) 'integer)
((floatp (car args))
(send trans :call sym argcount) 'float)
(t
(send trans :call sym argcount) 'symbol)))
((1-) (check-arg 1 argcount '1-)
(cond ((integerp (car args))
(send trans :1-) 'integer)
((floatp (car args))
(send trans :call sym argcount) 'float)
(t
(send trans :call sym argcount) 'symbol)))
((car first) (check-arg 1 argcount sym) (send trans :car) t)
((cdr rest) (check-arg 1 argcount sym) (send trans :cdr) t)
((cadr second) (check-arg 1 argcount sym) (send trans :cadr) t)
((caar) (check-arg 1 argcount sym) (send trans :caar) t)
((cdar) (check-arg 1 argcount sym) (send trans :cdar) t)
((cddr) (check-arg 1 argcount sym) (send trans :cddr) t)
((caddr third) (check-arg 1 argcount sym) (send trans :caddr) t)
((cons) (check-arg 2 argcount 'cons) (send trans :cons) t)
((symbolp consp stringp integerp numberp floatp)
(check-arg 1 argcount sym)
(send trans :type-check-predicate sym) t)
((svref) (check-arg 2 argcount 'svref) (send trans :svref) t)
((svset) (check-arg 3 argcount 'svset) (send trans :svset) t)
((char schar) (check-arg 2 argcount sym) (send trans :char))
((setchar) (check-arg 3 argcount sym) (send trans :setchar))
((bit sbit) (check-arg 2 argcount sym) (send trans :bit))
((setbit) (check-arg 3 argcount sym) (send trans :setbit))
((aref aset)
(if (> argcount (cdr (assq sym '((aref . 2) (aset . 3)))))
(send trans :call sym argcount)
(send self :vector-op sym argcount arg-type-list)))
((+ logand logior * -)
(send self :arithmetic sym argcount arg-type-list))
((slot) (check-arg 3 argcount 'slot)
(send self :slot (first args) (second args) (third args)))
((setslot) (check-arg 4 argcount 'setslot)
(send self :setslot (first args) (second args)
(third args) (fourth args)))
((abs) (check-arg 1 argcount 'abs)
(cond ((eql (coerce-type (car arg-type-list)) 'integer)
(send trans :int-abs)
'integer)
((eql (coerce-type (car arg-type-list)) 'float)
(send trans :flt-abs)
'float)
(t (send trans :call sym 1) t)) )
(t
(send trans :call sym argcount)
(if (symbolp sym) (get sym 'function-result-type) t))
)))
) ; defmethod
) ; eval-when
(eval-when (load eval)
(defmethod compiler
(:vector-op (func argcount arg-type-list)
(let ((vectype (car arg-type-list))
(meth (cdr (assq func '((aref . :vref) (aset . :vset)) ))))
(if (and (symbolp vectype)
(boundp vectype)
(classp (setq vectype (symbol-value vectype))))
(cond
((subclassp vectype integer-vector)
(send trans meth 'integer)
'integer)
((subclassp vectype string)
(send trans meth 'character)
'integer)
((subclassp vectype float-vector)
(send trans meth 'float)
'float)
((subclassp vectype bit-vector)
(send trans :call func argcount)
'integer)
((subclassp vectype vector)
(send trans meth 'pointer)
t)
(t (send trans :call func argcount) t))
(send trans :call func argcount))) )
(:slot (obj klass index)
(let (i)
(send self :eval obj)
(when (and (null (numberp index))
(class-symbolp klass)
(quoted-symbolp index)
(setq i (position (cadr index) ((symbol-value klass) . vars))))
(setq index i))
(cond ((numberp index)
(send trans :load-indexed-ov index)
(object-variable-type klass index))
(t (send self :eval klass)
(send self :eval index)
(send trans :call 'slot 3)
t))))
(:setslot (obj klass index newval)
(let (i)
(when (and (null (numberp index))
(class-symbolp klass)
(quoted-symbolp index)
(setq i (position (cadr index) ((symbol-value klass) . vars))))
(setq index i))
(cond ((numberp index)
(send self :eval newval)
(send trans :dupe)
(send self :eval obj)
(send trans :store-indexed-ov index)
(object-variable-type klass index))
(t
(send self :eval obj)
(send self :eval klass)
(send self :eval index)
(send self :eval newval)
(send trans :call 'setslot 4)
t))))
) ;defmethod
) ;eval-when
(eval-when (load eval)
(defmethod compiler
(:arithmetic (op argcount arg-type-list)
(if (>= *safety* 2)
(progn (send trans :call op argcount) t)
(cond ((eq op '-)
(cond
((every #'(lambda (x) (memq x '(:integer integer fixnum :fixnum)))
arg-type-list)
(case argcount
((1) (send trans :int-neg))
((2) (send trans :int-op2 '-))
(t (while (> (dec argcount) 1) (send trans :int-op2 '+))
(send trans :int-neg)
(send trans :int-op2 '+)))
'integer)
((every #'(lambda (x) (memq x '(:float float))) arg-type-list)
(case argcount
((1) (send trans :flt-neg))
((2) (send trans :flt-op2 '-))
(t (while (> (dec argcount) 1) (send trans :flt-op2 '+))
(send trans :flt-neg)
(send trans :flt-op2 '+)))
'float)
(t (send trans :call '- argcount) t)))
(t
(cond
((every #'(lambda (x) (memq x '(:integer integer fixnum :fixnum)))
arg-type-list)
(while (> (dec argcount) 0) (send trans :int-op2 op))
'integer)
((every #'(lambda (x) (memq x '(:float float))) arg-type-list)
(while (> (dec argcount) 0) (send trans :flt-op2 op))
'float)
(t (send trans :call op argcount) t))) ) ) )
) ; defmethod
) ; eval-when
(eval-when (load eval)
(defmethod compiler
(:special-form (fn args)
(case fn
((quote) (send trans :load-quote (car args)) t)
((setq) (send self :setq args))
((if) (send self :if args) t)
((let) (send self :let args) t)
((let*) (send self :let* args) t)
((cond) (send self :cond args) t)
((while) (send self :while args) t)
((and) (send self :and args) t)
((or) (send self :or args) t)
((block) (send self :block (car args) (cdr args)) t)
((return-from) (send self :return-from (car args) (cadr args)) t)
((catch) (send self :catch (car args) (cdr args)) t)
((throw) (send self :throw (car args) (cadr args)) t)
((tagbody) (send self :tagbody args) t)
((go) (send self :go (car args)) t)
((flet) (send self :flet (car args) (cdr args) nil) t)
((labels) (send self :flet (car args) (cdr args) t) t)
((unwind-protect)
(send self :unwind-protect (car args) (cdr args)) t)
((progn) (send self :progn args) t)
((function) (send self :function (car args)) t)
((the) (send self :eval (cadr args)) (coerce-type (car args)))
((defun) (send self :warning "defun must be at toplevel") t)
((defmacro) (send self :warning "defmacro must be at toplevel") t)
((defmethod) (send self :warning "defmethod must be at toplevel") t)
((eval-when) (send self :warning
"eval-when must appear at toplevel, ignored") t)
(t (send self :error "Compiling method is not yet implemented." fn))))
) ;defmethod
) ;eval-when
(eval-when (load eval)
(defmethod compiler
(:conditional-jump (t-label nil-label)
(if nil-label
(send trans :if-nil nil-label)
(send trans :if-t t-label)))
(:evcon (condition t-label nil-label)
(let* (op args arg-types optype (label nil-label))
(if (listp condition) (setq op (car condition) args (cdr condition)))
(cond ((or (atom condition) (<= (length condition) 1))
(send self :eval condition)
(send self :conditional-jump t-label nil-label))
((eq op 'eq)
(check-arg 2 (length args) op)
(send self :eval (car args))
(send self :eval (cadr args))
(if nil-label
(send trans :if-neq nil-label)
(send trans :if-eq t-label)))
((memq op '(null not))
(check-arg 1 (length args) op)
(send self :evcon (car args) nil-label t-label))
((eq op 'and)
(if (null nil-label)
(setq label (send self :genlabel "AND")))
(while args
(send self :evcon (pop args) nil label))
(when (null nil-label)
(send trans :jump t-label)
(send trans :label label)))
((eq op 'or)
(if (null t-label)
(setq label (send self :genlabel "OR"))
(setq label t-label))
(while args
(send self :evcon (pop args) label nil))
(when (null t-label)
(send trans :jump nil-label)
(send trans :label label)))
((memq op '(symbolp numberp integerp floatp atom consp stringp))
(check-arg 1 (length args) op)
(send self :eval (car args))
(if nil-label
(send trans :if-not-type op nil-label)
(send trans :if-type op t-label)))
((and (= (length args) 2) (memq op '(< > = <= >=)))
(setq arg-types (mapcar #'(lambda (x) (send self :eval x)) args))
(cond
((and (member (car arg-types) '(integer fixnum float))
(eql (car arg-types) (cadr arg-types)))
(setq optype
(cdr (assq (car arg-types)
'((integer . int) (fixnum . int) (float . float)))))
(cond
(t-label (setq label t-label))
(t (setq op (cdr (assq op
'((< . >=) (> . <=) (<= . >)
(>= . <) (= . <>) (<> . =)))))))
(send trans :compare optype op label))
(t ;2 args, types unknown or mixed
(send trans :call op (length args))
(send self :conditional-jump t-label nil-label)) ) )
(t
(send self :eval condition)
(send self :conditional-jump t-label nil-label))) ))
) ;defmethod
) ;eval-when
(eval-when (load eval)
(defmethod compiler
(:if (forms)
(let ((elselab (send self :genlabel "IF"))
(exitlab (send self :genlabel "IF")))
(send self :evcon (car forms) nil elselab)
(send self :eval (cadr forms))
(send trans :jump exitlab)
(send trans :adjust 1)
(send trans :label elselab)
(send self :eval (caddr forms))
(send trans :label exitlab)))
(:setq (var-val)
(unless var-val
(send trans :load-nil)
(return-from :setq t))
(let ((var nil) (val nil) (duped nil) (result-type))
(while var-val
(setq var (car var-val)
val (cadr var-val)
var-val (cddr var-val)
duped nil)
(setq result-type (send self :eval val))
(when
(and (null var-val) ;last setq pair
(memq (send self :var-binding var) '(special ovaf)))
(setq duped t)
(send trans :dupe))
(if (symbolp var)
(send self :store-var var)
(progn ;ovaf?
(if (not (symbolp (cdr var)))
(send self :error "illegal ovaf" var))
(send self :store-ovaf (car var) (cdr var))))
(and (null var-val) (null duped) (send self :load-var var)))
result-type))
(:let* (bodies) ;sequential let
(let ((local-list (pop bodies)) (unwind-save unwind-frames))
(send self :create-frame)
(while (and bodies (consp (car bodies)) (eq (caar bodies) 'declare))
(send self :declare (cdr (pop bodies))))
(dolist (init-form local-list)
(send self :eval (if (listp init-form) (cadr init-form) nil))
(send self :bind
(if (listp init-form) (car init-form) init-form)
'local (1- (send trans :offset-from-fp))) )
(send self :progn bodies)
(setq unwind-frames unwind-save)
(send self :delete-frame t)))
(:let (bodies) ;parallel let
(let ((local-list (pop bodies)) var special-list offset
(unwind-save unwind-frames))
(send self :create-frame)
(while (and bodies (consp (car bodies)) (eq (caar bodies) 'declare))
(send self :declare (cdr (pop bodies))))
(dolist (init-form local-list)
(setq scope (1- (send idtable :level)))
(setq var (if (listp init-form) (pop init-form) init-form))
(send self :eval (if (listp init-form) (car init-form) nil))
(setq offset (1- (send trans :offset-from-fp)))
(cond
((send self :special-variable-p var)
(send (car frames) :local 1) ;a trick
(push (list var offset) special-list))
(t (send self :bind var 'local offset))))
;all the evaluation have finished, bind specials
(dolist (spe special-list)
(setq offset (send trans :offset-from-fp))
(send trans :load-local (cadr spe) 0)
(send self :bind (car spe) 'local offset))
(setq scope (send idtable :level))
(send self :progn bodies)
(setq unwind-frames unwind-save)
(send self :delete-frame t)))
(:cond (clauses)
(let (clause pred next t-found (exit (send self :genlabel "CON")))
(while clauses
(setq clause (pop clauses)
pred (car clause)
next (send self :genlabel "CON"))
(cond
((null (cdr clause))
(send self :eval pred)
(send trans :dupe)
(send trans :if-t exit)
(send trans :discard 1))
((eq pred t) (setq t-found t))
(t (send self :evcon pred nil next)))
(when (cdr clause)
(send self :progn (cdr clause))
(send trans :jump exit)
(send trans :adjust 1))
(send trans :label next))
;(if t-found (send trans :adjust -1)
(send trans :load-nil) ;)
(send trans :label exit)))
(:while (bodies)
(let ((cond (car bodies)) (bodies (cdr bodies))
(whilent (send self :genlabel "WHL"))
(whilexit (send self :genlabel "WHX"))
(whileblock (send self :enter-block nil)))
(send trans :label whilent)
(send self :evcon cond nil whilexit) ;evaluate condition
(send self :progn bodies)
(send trans :discard 1)
(send trans :jump whilent)
(send trans :label whilexit)
(send trans :load-nil) ;result=NIL
(send trans :label whileblock)
(send self :leave-block)
))
(:and (bodies)
(let ((andexit (send self :genlabel "AND")))
(while (cdr bodies)
(send self :eval (pop bodies))
(send trans :dupe)
(send trans :if-nil andexit)
(send trans :discard 1)
)
(send self :eval (car bodies))
(send trans :label andexit)))
(:or (bodies)
(let ((orexit (send self :genlabel "OR")))
(while (cdr bodies)
(send self :eval (pop bodies))
(send trans :dupe)
(send trans :if-t orexit)
(send trans :discard 1))
(send self :eval (car bodies))
(send trans :label orexit)))
(:catch (label bodies)
(let ((exlab (send self :genlabel "CAT")))
(push (send trans :offset-from-fp) unwind-frames)
(send self :eval label)
(send trans :entercatch exlab)
(send self :progn bodies)
(pop unwind-frames)
(send trans :exitcatch exlab)))
(:throw (lab val)
(send self :eval lab)
(send self :eval val)
(send trans :throw))
(:unwind-protect (prot cleanup)
(let ((cleaner (send self :genlabel "UWP"))
(newcomp))
(push (send trans :offset-from-fp) unwind-frames)
(send trans :closure cleaner) ;make cleanup closure
(setq newcomp (send self :copy-compiler))
(send self :add-closure
(list cleaner
(cons 'lambda (cons nil cleanup))
newcomp))
(send trans :bind-cleaner)
(send self :eval prot)
(send trans :call-cleaner cleaner)
(pop unwind-frames)
))
) ;defmethod
) ;eval-when
(eval-when (load eval)
(defmethod compiler
(:enter-block (lab)
(let ((spsave (send trans :offset-from-fp))
(blklab (send self :genlabel "BLK")))
(if (null (symbolp lab)) (send self :warning "no symbolic block label"))
(push (list lab blklab closure-level spsave) blocks)
blklab))
(:leave-block () (setq blocks (cdr blocks)))
(:block (lab bodies)
(setq lab (send self :enter-block lab))
(send self :progn bodies)
(send trans :label lab)
(send self :leave-block))
(:return-from (lab val)
(let ((blklab (assq lab blocks)) (need-unwind) (offset))
(if (null blklab) (send self :error "no such block" lab))
(send self :eval val)
(if (> closure-level (third blklab))
(send self :error "return-from in a closure cannot be compiled"))
(setq offset (fourth blklab))
(setq need-unwind (and unwind-frames (>= (first unwind-frames) offset)))
(send trans :return-from offset need-unwind)
(send trans :jump (cadr blklab))))
(:tagbody (bodies)
(let ((oldtags tags)
(fp (send trans :offset-from-fp)) (p bodies) golab clab form)
(while p
(when (atom (setq golab (car p)))
(push (list golab (send self :genlabel "TAG") fp) tags))
(setq p (cdr p)))
(while bodies
(setq form (pop bodies))
(cond ((atom form)
(send trans :label (cadr (assq form tags))))
(t (send self :eval form)
(send trans :discard 1)))) ;only the side effect is needed
(send trans :load-nil) ;result is nil
(setq tags oldtags)))
(:go (lab)
(let ((tag (assq lab tags)) (offset) (need-unwind))
(if (null tag) (send self :error "no such tag to go" lab))
(setq offset (third tag))
(setq need-unwind (and unwind-frames (>= (first unwind-frames) offset)))
(send trans :go-tag offset need-unwind)
(send trans :jump (elt tag 1))))
(:function (fn)
(if (symbolp fn) ; #'FUNC
(let ((flet-def (send self :get-function fn)))
(if (eq (second flet-def) 'closure)
(send trans :load-local (third flet-def)
(- closure-level (fourth flet-def)))
(send trans :getfunc fn)))
(let ((entry (send self :genlabel "CLO")) ; #'(lambda (...) ...)
(newcomp))
(send trans :closure entry)
(setq newcomp (send self :copy-compiler))
(send self :add-closure (list entry fn newcomp))
)))
(:flet (funcs bodies recursive-scope &aux (flets-save flets))
;recursive-scope==T for labels, NIL for flet
(let (entry newcomp newcomps flets-exchange)
(dolist (fn funcs)
(setq entry (send self :genlabel "FLET"))
(send trans :closure entry) ;makeclosure
(push (list (car fn) ;func name
'closure ;func bind type
(1- (send trans :offset-from-fp))
closure-level
entry)
flets)
(setq newcomp (send self :copy-compiler))
(push newcomp newcomps)
(send self :add-closure (list entry (cons 'lambda (cdr fn))
newcomp)))
(if recursive-scope
(send-all newcomps :change-flets flets))
(send self :progn bodies)
(setq flets (nthcdr (length funcs) flets))
(send trans :del-frame 0 (length funcs))))
(:change-flets (newflets) (setq flets newflets))
(:declare (args)
(let (v)
(declare (type identifier v))
(dolist (decl args)
(case (car decl)
(special
(dolist (id (cdr decl))
(setq v (send self :enter-variable id))
(setq (v . binding) 'special)))
(type
(dolist (id (cddr decl))
(setq v (send self :enter-variable id))
(send v :type (cadr decl))))
(ftype
(def-function-type (cadr decl) (cddr decl)))
((integer :integer fixnum :fixnum :float float)
(dolist (id (cdr decl))
(setq v (send self :enter-variable id))
(send v :type (car decl))))
(optimize (setq *optimize* (cadr decl)))
(safety (setq *safety* (cadr decl)))
(space (setq *space* (cadr decl)))
(inline )
(not-inline)
(t (cond ((class-symbolp (car decl))
(dolist (id (cdr decl))
(setq v (send self :enter-variable id))
(send v :type (car decl))))
(t (send self :warning "unknown declaration" decl))))
))))
(:lambda (param forms)
(let ((labels nil)
(i 0)
(reqn 0)
(optn 0)
(keyn 0)
(opt-vars (memq '&optional param))
(rest-var (memq '&rest param))
(key-forms (memq '&key param))
(aux-vars (memq '&aux param))
(key-vars)
(req-vars nil)
(opt-forms nil)
(key-names nil)
(key-inits nil)
(key-base 0)
(specially-bound nil)
(allowotherkeys (memq '&allow-other-keys param))
(unwind-save unwind-frames)
(vname nil)
(label2))
(setq req-vars
(reverse
(nthcdr (length (cond (opt-vars)
(rest-var)
(key-forms)
(aux-vars)))
(reverse param))))
(setq opt-vars (cdr (reverse (nthcdr (length (cond (rest-var)
(key-forms)
(aux-vars)))
(reverse opt-vars))))
rest-var (cond (rest-var (cadr rest-var)))
reqn (length req-vars)
optn (length opt-vars)
opt-forms (mapcar #'(lambda (x) (if (listp x) (cadr x)))
opt-vars)
key-forms (cdr key-forms)
keyn (length key-forms))
(when allowotherkeys
(setq key-forms (subseq key-forms 0 (dec keyn))))
;; prepare labels for the init-forms of optional variables
(if opt-vars
(while (<= i optn) ;optn+1 labels
(push (send self :genlabel "ENT") labels)
(inc i))
(if rest-var (setq labels (list (send self :genlabel "RST")))))
;; extract optional variable names
(setq opt-vars (mapcar #'(lambda (x) (if (listp x) (car x) x)) opt-vars))
(send self :create-frame)
;; parse keyword variables
(if key-forms
(let (init key var)
(dolist (k key-forms)
(cond ((listp k)
(setq init (cadr k) k (car k))
(cond ((listp k)
(setq key (car k) var (cadr k))