-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.lisp
8021 lines (6926 loc) · 314 KB
/
run.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
(in-package :common-tones)
;;; Apr-14 -- clm-5 (remove framples and mixers)
;;; Feb-08 -- clm-4 (new gens, etc)
;;; Jul-04 -- clm-3 (use clm.c)
;;; Mar-00 -- sndlib internal sample representation is now configurable, so clm has to reflect that.
;;; Jul-99 -- C side uses doubles throughout, Lisp side uses CLOS
;;; Jun-99 -- clm-2 name changes, run-time types -- everything changed!
;;; Jul-98 -- serious bug in readin forced me to change its layout, rippling through several other gens.
;;; Jun-98 -- _c names removed from cmus0.lisp, and Mac calloc/free replaced by NewPtr/DisposePtr
;;; Apr-98 -- many MCL changes
;;; Mar-98 -- higher precision clm_env causes layout changes
;;; Jul-97 -- no-rld linker support removed -- many prototype changes
;;; Oct-95 -- many changes for n-channel IO and run-block connections to src et al
;;; Jul-95 -- array allocation changed for SGI port
;;; Apr-95 -- boolean handling completely revised.
;;; Oct-94 -- parallel instrument scheduling option.
;;; Mar-94 -- full C output, rather than heavy use of foreign function interface
;;; Nov-93 -- array version
;;; Mar-93 -- struct version
;;; 1990 -- initial clm implementation from mus10/sambox
(defvar *clm-report-untyped-vars* nil)
(defconstant +setf+ 0)
(defconstant +incf+ 1)
(defconstant +decf+ 2)
(defconstant +as-needed-input+ 0)
(defconstant +as-needed-edit+ 1)
(defconstant +as-needed-analyze+ 2)
(defconstant +as-needed-synthesize+ 3)
(defvar new-prog nil)
(defvar loop-var nil)
(defmacro wf (x)
`(walk-form ,x #+clisp walker:*toplevel-environment* #-clisp nil 'lisp->c))
(defmacro fully-expand (x)
`(walk-form ,x #+clisp walker:*toplevel-environment* #-clisp nil #'(lambda (x y z) x)))
(defvar unhappy nil)
(defvar old-prog nil)
(defvar loop-beg nil)
(defvar loop-end nil)
(defvar loop-label-stack nil)
(defvar return-stack nil)
(defvar clm-functions nil)
(defvar vars nil)
(defvar function-ctr 0)
(defvar lambda-args nil)
(defvar *gen-type* nil)
(defvar *as-needed* nil)
(defvar *as-needed-functions* nil)
(defvar *as-needed-function-types* nil)
(defvar *global* nil)
(defstruct varinfo name (depth 0) (max-depth 1)
refd parallel type iloc rloc initialize temp shadowed
gen-type arr-gen-type
global ctr loaded
ref-chain)
(defun clm-function (x) (gethash x clm-functions))
(defun def-clm-fun (x y)
(if (null clm-functions) (setf clm-functions (make-hash-table)))
(setf (gethash x clm-functions) y))
(defmacro run (run-time-code)
`(macroexpand-1 (run-1 ,run-time-code)))
(defvar outer-environment nil)
(defun make-var (var &optional type)
(if (and var (not (eq var t)))
(when (symbolp var)
(if (null vars) (setf vars (make-hash-table)))
(let ((lst (gethash var vars)))
(if (not lst)
(setf (gethash var vars)
(make-varinfo :name var :type type :temp t :global *global*)) ; if as-needed, we're declared globally
(if type
(setf (varinfo-type lst) type))))))
var)
(defun make-user-var (var &optional type)
;; (if type (format t "make-user-var ~A: ~A~%" var type))
(let ((lv (and lambda-args (find var lambda-args :key #'first))))
(if lv
(list 'lambda-arg (second lv))
(progn
(if (null vars) (setf vars (make-hash-table)))
(if (not (symbolp var)) (error "setf ~A?" var))
(if (not (gethash var vars))
(setf (gethash var vars) (make-varinfo :name var :type type :initialize t :global *global*)))
var))))
(defun make-local-var (var &optional parallel type)
(let ((lst (gethash var vars)))
(if (not lst)
(setf (gethash var vars) (make-varinfo :name var :parallel parallel :type type :global *global*))
(progn
(incf (varinfo-depth lst))
(setf (varinfo-shadowed lst) t)
(if (>= (varinfo-depth lst) (varinfo-max-depth lst))
(setf (varinfo-max-depth lst) (1+ (varinfo-depth lst))))
(setf (varinfo-parallel lst) parallel))))
var)
(defun current-var (var &optional do-init)
(if (and var (not (eq var t)))
(if (symbolp var)
(let ((lst (gethash var vars)))
(if (not lst)
(error "reference to undefined variable: ~A" var)
(if (< (varinfo-depth lst) 0)
(format t "reference to undefined variable ~A: ~A" var lst)
(progn
(setf (varinfo-refd lst) t)
(when (and *global*
(not (varinfo-global lst))
(not (varinfo-temp lst)))
(setf (varinfo-global lst) *global*))
(if (and (varinfo-parallel lst) (not (eq do-init var)))
(progn
(if (<= (varinfo-depth lst) 0) (warn "reference to undefined variable ~A" var))
(list :var var (1- (varinfo-depth lst))))
(progn
(if (< (varinfo-depth lst) 0) (warn "reference to undefined variable ~A" var))
(list :var var (varinfo-depth lst))))))))
var)
var))
(defun provisional-var (var)
(if (and var (not (eq var t)))
(if (symbolp var)
(let ((lst (gethash var vars)))
(if (not lst)
(error "reference to undefined variable: ~A" var)
(if (or (< (varinfo-depth lst) 0)
(and (varinfo-parallel lst) (= (varinfo-depth lst) 0)))
(format t "reference to undefined variable ~A: ~A" var lst)
(list :var var (varinfo-depth lst)))))
var)
var))
(defun chained-var (var ref)
(if (and var (not (eq var t)))
(if (symbolp var)
(let ((lst (gethash var vars)))
(if (not lst)
(error "reference to undefined variable: ~A" var)
(if (or (< (varinfo-depth lst) 0)
(and (varinfo-parallel lst)
(= (varinfo-depth lst) 0)))
(format t "reference to undefined variable ~A: ~A" var lst)
(let ((info (gethash ref vars)))
(when info
(setf (varinfo-ref-chain lst) (cons info (varinfo-ref-chain lst))))
(list :var var (varinfo-depth lst))))))
var)
var))
(defun make-compiler-temp (&optional type) (make-var (gensym "_clm_") type))
(defun new-label (&optional name) (if name name (gensym "L-")))
(defmacro run-1 (run-time-code &environment env)
;;Lisp expands this twice unless we take other action (i.e. run and run-1 two-step)
(setf new-prog nil
unhappy nil
loop-beg nil
loop-end nil
loop-var nil
loop-label-stack nil
vars nil
function-ctr 0
lambda-args nil
return-stack nil
*safety* *clm-safety*
*debug* *clm-debug*
*gen-type* nil
*as-needed* nil
*global* nil
*as-needed-functions* nil
*as-needed-function-types* nil
outer-environment env) ;needed by macrolet?
(wf run-time-code) ; builds new-prog
(if *clm-debug*
(progn
(pprint (append (list 'progn) (reverse new-prog)))
(terpri)
(print-hash vars)
(terpri)
(pprint (append (list 'progn) (reverse *as-needed-functions*)))))
(if unhappy ;we hit something we can't deal with
run-time-code
#-openmcl
`(progn
(<start> ,loop-end)
,@(nreverse new-prog)
(<end-1>)
,@(or (nreverse *as-needed-functions*) '())
(<end-2> ,(or loop-beg 0) ,(or loop-end 0) ,loop-end)
)
#+openmcl
`(progn
(<start> ,loop-end)
,@(let* ((lst (nreverse new-prog))
(len (length lst)))
(loop for i from 0 by 50 below len
collect (append (list 'progn) (subseq lst i (min len (+ i 50))))))
(<end-1>)
,@(or (nreverse *as-needed-functions*) '())
(<end-2> ,(or loop-beg 0) ,(or loop-end 0) ,loop-end)
)
))
(defun give-up (x)
(setf unhappy t)
(error "the run macro can't handle ~S in ~S" (if (listp x) (car x) x) x))
(defun arith-type (args &optional (restyp :clm-integer))
;; nil result type unpredictable, :integer known to be int result, :real known to be float
(if (not args)
restyp
(let ((arg (car args)))
(if (listp arg)
(let ((argv (gethash (second arg) vars)))
(if argv
(if (member (varinfo-type argv) '(:integer :clm-integer :clm-boolean :boolean))
(arith-type (cdr args) restyp)
(if (member (varinfo-type argv) '(:real :clm-real))
:clm-real
(arith-type (cdr args) nil)))
(if (member arg '(pi *srate*))
:clm-real
(arith-type (cdr args) nil))))
(if (integerp arg)
(arith-type (cdr args) restyp)
:clm-real)))))
(defun package-op (clm-name var x &optional type-func spec provisional-result arg-func)
(let* ((argnum 0)
(args (mapcar #'(lambda (arg)
(let ((val (current-var (wf arg))))
(setf *gen-type* nil)
(if arg-func
(progn
(funcall arg-func val argnum) ; gad CL doesn't have closures!
(incf argnum)))
val))
(cdr x))))
(let ((result-type (if type-func
(if (member type-func '(:clm-integer :clm-real :clm-boolean :real :integer :boolean
:string :float-array :double-array :integer-array))
type-func
(funcall type-func args)))))
(if clm-name
(if var
(if (or (not (numberp var)) (not (zerop var)))
(progn
(make-var var result-type)
(if provisional-result
(if spec
(push `(,clm-name ,spec ,(provisional-var var) ,@args) new-prog)
(push `(,clm-name ,(provisional-var var) ,@args) new-prog))
(if spec
(push `(,clm-name ,spec ,(current-var var) ,@args) new-prog)
(push `(,clm-name ,(current-var var) ,@args) new-prog))))
(push `(,clm-name 0 ,@args) new-prog))
(if spec
(push `(,clm-name ,spec ,@args) new-prog)
(push `(,clm-name ,@args) new-prog))))
(if clm-name var args))))
(defun set-gen-type (gen gen-type)
(when (and gen
(not (varinfo-temp gen)))
(if (or (not (varinfo-gen-type gen))
(eq (varinfo-gen-type gen) :mus-any))
(setf (varinfo-gen-type gen) gen-type)
;; var's gen-type must already be set
(if (and (not (eq (varinfo-gen-type gen) gen-type))
(not (eq gen-type :mus-any)))
;; var has several possible gen types, current type might be another
(if (symbolp (varinfo-gen-type gen))
(setf (varinfo-gen-type gen) (list (varinfo-gen-type gen) gen-type))
(if (not (member gen-type (varinfo-gen-type gen)))
(setf (varinfo-gen-type gen) (append (varinfo-gen-type gen) (list gen-type)))))))))
(defun gen-package-op (clm-name var x &optional type-func gen-type arg-func)
(setf *gen-type* gen-type)
(let ((result (package-op clm-name var x type-func nil nil arg-func)))
(setf *gen-type* nil)
(let ((gen (gethash (cadr x) vars)))
(if gen
(set-gen-type gen gen-type)))
result))
(defun gen2-package-op (clm-name var x &optional type-func gen-type gen2-type)
(setf *gen-type* gen-type)
(let ((result (package-op clm-name var x type-func)))
(setf *gen-type* nil)
(let ((gen (gethash (cadr x) vars)))
(set-gen-type gen gen-type))
(let ((gen (gethash (caddr x) vars)))
(set-gen-type gen gen2-type))
result))
(defun bank-package-op (clm-name var x &optional type-func gen-type)
(let ((result (package-op clm-name var x type-func)))
;(format t "look for ~A~%" (caddr x))
(let ((gen (gethash (caddr x) vars)))
(if gen
(progn
;(format t "set ~A fields to ~A~%" gen gen-type)
(setf (varinfo-arr-gen-type gen) gen-type)
(setf (varinfo-gen-type gen) :mus-any-array))))
result))
(defun find-loop-control-info (var x)
(declare (ignore var))
;; We save the name of the loop control variable and check it against the OUTA or OUTB call, if any.
;;
;; 29-Mar-01
;; this is no longer needed as an optimization for speed. If the run macro sees
;; (progn (loop for i fixnum from ...))
;; the code it generates is just as fast as
;; (loop for i from...)
;;
(if (and (not loop-end)
(not new-prog)
(not (string-equal (cadr x) "WHILE"))
(not (string-equal (cadr x) "UNTIL")))
(do* ((i 1 (1+ i))
(loop-e nil)
(loop-b 0)
(loop-v nil)
(all-done nil)
(val (nth i x) (nth i x)))
((or all-done
(= i (length x)))
(if (and (not loop-e) (not loop-v))
(error "Can't find control variable or end point of outer loop")
(progn
;;; (setf loop-end (or loop-e (list '+ loop-b 1234567890)))
(setf loop-end loop-e)
(setf loop-beg loop-b)
(setf loop-var loop-v)
(make-var loop-v)
`(progn ,@(nthcdr i x)))))
(if (symbolp val)
(if (or (string-equal val "DO")
(string-equal val "AND"))
(setf all-done (string-equal val "DO"))
(if (or (string-equal val "FOR")
(string-equal val "AS"))
(setf loop-v (nth (+ i 1) x))
(if (or (string-equal val "FROM")
(string-equal val "UPFROM")
(string-equal val "="))
(setf loop-b (nth (+ i 1) x))
(if (or (string-equal val "TO")
(string-equal val "UPTO"))
(setf loop-e (nth (+ i 1) x))
(if (string-equal val "BELOW")
(setf loop-e (list '- (nth (+ i 1) x) 1)))))))))
(let ((last-var nil))
;; the problem here is that we might have nested loops within the outer loop, and all of
;; them have the identical label names LOOP::NEXT-LOOP and LOOP::END-LOOP. It is only
;; at this level that we have block structure info, so we can't put off the label disambiguation
;; until later -- END-LOOPs are all at the end, but references within the I-code triples
;; to END-LOOP have no way of telling which END-LOOP. So we expand the loop block, then
;; run through it, making sure all occurences of END-LOOP and NEXT-LOOP are fixed up so
;; no confusion can happen later. The next problem is that in ACL 4.1, which has loop built-in,
;; these labels have different names (EXCL::END-LOOP).
(push (gentemp "L_") loop-label-stack)
(setf last-var (wf (macroexpand x)))
(pop loop-label-stack)
(if last-var (make-var last-var)))))
(defun lisp->c (x y env)
(declare (ignore y env)) ;env is the local environment that the tree walker keeps track of
(block sbcl-stupidity
(if (and x (listp x))
(if (eq (car x) :var)
x
(let ((var (make-compiler-temp)))
#+sbcl (if (and (symbolp (car x))
(string-equal "SRC" (symbol-name (car x)))) ; sbcl apparently exports SRC but it's in about 40 packages...
(return-from sbcl-stupidity (apply (clm-function 'COMMON-TONES::SRC) (list var x))))
(if (clm-function (car x))
(apply (clm-function (car x)) (list var x))
(if (and (listp (car x)) (eq (caar x) 'lambda))
(lambda-branch var x)
(let ((xx (macroexpand-1 x outer-environment))) ;else full expansion => trouble in struct field setfs (system::rplaca-nthcdr)
(if (eq x xx)
(give-up x)
xx))))))
(if (symbolp x)
(if #-clisp (constantp x) ;PI is not a constant in clisp!
#+clisp (or (constantp x) (eq x 'pi))
(if (eq x nil)
nil
(if (eq x t)
t
(eval x)))
(make-user-var x))
x))))
;;; return has to thread its way through block lists and get its value returned
;;; from the correct place, so it's not enough to just dump the value in A and
;;; jump to the end. The return stack values is a list of lists, pushed anew
;;; as blocks are opened, and popped as they close. The inner list is of the
;;; form (block-name block-variable block-end-label 0) -- the block-variable is
;;; what we return (either via return or by falling through the bottom), block-label
;;; is the location of the end of the block, and the block-name is either nil
;;; for an unnamed block or the name provided by the caller.
(defun return-branch (var x) ;return [result]
(declare (ignore var))
(if return-stack
(let ((our-block-data (find nil return-stack :key #'first)))
(if (not our-block-data)
(error "return not inside a block named NIL")
(let ((block-lab (third our-block-data))
(block-var (second our-block-data)))
(incf (fourth our-block-data))
(push `(<comment> return ,(cadr x)) new-prog)
(if (cdr x)
(let ((val (wf (cadr x))))
(push `(<setf> ,+setf+ ,(provisional-var block-var) ,(chained-var val block-var)) new-prog))
(push `(<setf> ,+setf+ ,(provisional-var block-var) nil) new-prog))
(push `(<jump> ,block-lab) new-prog)
nil)))
(error "return outside any block")))
(defun return-from-branch (var x)
(declare (ignore var))
(if return-stack
(let ((our-block-data (find (cadr x) return-stack :key #'first)))
(if (not our-block-data)
(error "return-from ~S not inside the specified block" (cadr x))
(let ((block-lab (third our-block-data))
(block-var (second our-block-data)))
(incf (fourth our-block-data))
(push `(<comment> return-from ,(cadr x)) new-prog)
(if (cddr x)
(let ((val (wf (caddr x))))
(push `(<setf> ,+setf+ ,(provisional-var block-var) ,(chained-var val block-var)) new-prog))
(push `(<setf> ,+setf+ ,(provisional-var block-var) nil) new-prog))
(push `(<jump> ,block-lab) new-prog)
nil)))
(error "return-from outside any block")))
(defun block-branch (var x)
(let ((end-label (new-label))
(last-var nil))
(if (not new-prog) (push :block new-prog))
(push (list (cadr x) (make-var var) end-label 0) return-stack)
(loop for i in (cddr x) do (setf last-var (wf i)))
(if (zerop (fourth (first return-stack)))
(setf end-label nil))
;; zero=no return so block var need not be saved if we're at top level
(push `(<comment> block ,(cadr x)) new-prog)
(when last-var
(push `(<setf> ,+setf+ ,(provisional-var var) ,(chained-var last-var var)) new-prog))
(if end-label (push `(<label> ,end-label) new-prog))
(pop return-stack)
var))
(defun tagbody-branch (var x)
;; need check for duplicate tag within one scope, and shadowing
;; 17-May-95 -- make sure tagbody returns nil
(declare (ignore var))
(let ((inner-labs nil))
(if (not new-prog) (push :tagbody new-prog)) ; (see progn)
(push `(<comment> tagbody) new-prog)
(loop for i in (cdr x) do
(if (listp i)
(wf i)
(let ((lab (if (and loop-label-stack
#-(or openmcl cmu sbcl clisp (and excl cltl2)) (member i '(loop::next-loop loop::end-loop))
#+(and excl cltl2) (member i '(excl::next-loop excl::end-loop))
#+(or openmcl cmu) (member i '(ansi-loop::next-loop ansi-loop::end-loop))
#+sbcl (member i '(sb-loop::next-loop sb-loop::end-loop))
#+clisp (member i '(system::next-loop system::end-loop))
)
(intern (concatenate 'string (symbol-name i) "-" (symbol-name (first loop-label-stack))))
i)))
(push lab inner-labs)
(push `(<label> ,lab) new-prog))))
nil))
(defun if-branch (var x)
(let* ((flabel (new-label))
(false-too (cadddr x))
(end-label (new-label))
(ifvar nil))
(make-var var)
(push `(<comment> if ,(cadr x)) new-prog)
(setf ifvar (wf (cadr x)))
(make-var ifvar)
(if (and ifvar (symbolp ifvar) (not (eq ifvar t)))
(let ((lst (gethash ifvar vars)))
(if (not lst)
(format t "~A has no info?~%" ifvar))
(when (and lst
(not (varinfo-type lst))
(varinfo-temp lst))
(if *clm-debug* (format t "if set ~A to boolean~%" ifvar))
(setf (varinfo-type lst) :clm-boolean))))
(push `(<jump-if-not> ,(current-var ifvar) ,flabel) new-prog)
(if (caddr x)
(let ((val (wf (caddr x))))
(push `(<setf> ,+setf+ ,(provisional-var var) ,(chained-var val var)) new-prog)
(push `(<jump> ,end-label) new-prog)
(push `(<label> ,flabel) new-prog))
;; actually a malformed if statement if no caddr
(progn
(push `(<setf> ,+setf+ ,(provisional-var var) nil) new-prog)
(push `(<jump> ,end-label) new-prog)
(push `(<label> ,flabel) new-prog)))
(if false-too
(let ((val (wf (cadddr x))))
(push `(<setf> ,+setf+ ,(provisional-var var) ,(chained-var val var)) new-prog)
(push `(<label> ,end-label) new-prog))
(progn
(push `(<setf> ,+setf+ ,(provisional-var var) nil) new-prog)
(push `(<label> ,end-label) new-prog)))
var))
(defun cond-branch (var x)
(let ((flabel nil)
(cond-last-var nil)
(end-label (new-label))
(ifvar nil))
(push `(<comment> cond) new-prog)
(make-var var)
(loop for i in (cdr x) do
(setf flabel (new-label))
(setf ifvar (wf (car i)))
(make-var ifvar)
(if (and ifvar (symbolp ifvar) (not (eq ifvar t)))
(let ((lst (gethash ifvar vars)))
(if (not lst)
(format t "~A has no info?~%" ifvar))
(when (and lst
(not (varinfo-type lst))
(varinfo-temp lst))
(if *clm-debug* (format t "cond set ~A to boolean~%" ifvar))
(setf (varinfo-type lst) :clm-boolean))))
(push `(<setf> ,+setf+ ,(provisional-var var) ,(chained-var ifvar var)) new-prog)
(if (not (eq ifvar t))
(push `(<jump-if-not> ,(current-var ifvar) ,flabel) new-prog))
(if (cdr i)
(progn
(loop for j in (cdr i) do
(setf cond-last-var (wf j)))
(make-var cond-last-var)
(push `(<setf> ,+setf+ ,(provisional-var var) ,(chained-var cond-last-var var)) new-prog)))
(push `(<jump> ,end-label) new-prog)
(push `(<label> ,flabel) new-prog))
(push `(<setf> ,+setf+ ,(provisional-var var) nil) new-prog)
(push `(<label> ,end-label) new-prog)
var))
(defun case-branch (var x)
(declare (ignore var))
;; CASE is of this form:
;; eval index expr => t
;; goto test
;; L1: code for S1
;; goto next
;; L2 ...
;; ...
;; test: case V1 L1 (V1=index of that branch)
;; case V2 L2 (i.e. if t=V2 goto L2 etc)
;; ...
;; goto Ln (i.e. case-else branch, if any)
;; next:
(push `(<comment> case ,(cadr x)) new-prog)
(let ((test-lab (new-label))
(next-lab (new-label))
(nvar (make-compiler-temp))
(selectors nil)
(labels nil)
(last-var nil)
(v-lab nil)
(var1 (wf (cadr x))))
;; can this return an integer constant? (case 1 ...)
(make-var var1)
(push `(<jump> ,test-lab) new-prog)
(loop for i in (cddr x) do
(setf v-lab (new-label))
(push `(<label> ,v-lab) new-prog)
(push (car i) selectors)
(push v-lab labels)
(setf last-var nil)
(loop for j in (cdr i) do
(setf last-var (wf j)))
(make-var last-var)
(if last-var (push `(<setf> ,+setf+ ,(provisional-var nvar) ,(chained-var last-var nvar)) new-prog))
(push `(<jump> ,next-lab) new-prog))
(push `(<label> ,test-lab) new-prog)
(push `(<case> ,(current-var var1) ,(nreverse selectors) ,(nreverse labels)) new-prog)
(push `(<setf> ,+setf+ ,(provisional-var nvar) nil) new-prog)
(push `(<label> ,next-lab) new-prog)
nvar))
(defun dotimes-branch (var x)
(declare (ignore var))
(let ((cmp-lab (new-label))
; (res-lab (new-label))
(body-lab (new-label))
(end-lab (new-label))
(end-var (make-compiler-temp :clm-integer))
(done (make-compiler-temp :clm-boolean))
(res-var (make-compiler-temp))
(inner-labs nil)
(endnum (second (second x)))
(do-var (make-local-var (first (second x)) nil :clm-integer)))
(if (and (symbolp endnum) (or (not vars) (not (gethash endnum vars)))) (make-user-var endnum :integer))
(push `(<comment> dotimes ,(cadr x)) new-prog)
(push (list nil res-var end-lab) return-stack)
(push `(<setf> ,+setf+ ,(current-var do-var) 0) new-prog)
(push `(<setf> ,+setf+ ,(provisional-var end-var) ,(chained-var (wf endnum) end-var)) new-prog)
(push `(<label> ,cmp-lab) new-prog)
(push `(<compare> ">" ,(current-var done) ,(current-var end-var) ,(current-var do-var)) new-prog)
(push `(<jump-if> ,(current-var done) ,body-lab) new-prog) ;i.e. if leq drop through into result block
; (push `(<label> ,res-lab) new-prog)
(if (third (second x))
(let ((val (wf (third (second x)))))
(push `(<setf> ,+setf+ ,(provisional-var res-var) ,(chained-var val res-var)) new-prog))
(push `(<setf> ,+setf+ ,(provisional-var res-var) nil) new-prog))
(push `(<jump> ,end-lab) new-prog)
(push `(<label> ,body-lab) new-prog)
(push (list (cadr x) end-lab) return-stack)
(loop for i in (cddr x) do
(if (listp i)
(wf i)
(progn
(push i inner-labs)
(push `(<label> ,i) new-prog))))
(push `(<setf> ,+incf+ ,(current-var do-var) 1) new-prog)
(push `(<jump> ,cmp-lab) new-prog)
(pop return-stack) ;cadr x?
(pop return-stack) ;res-var?
(push `(<label> ,end-lab) new-prog)
(let ((lst (gethash do-var vars)))
(decf (varinfo-depth lst)))
res-var))
(defun do-branch (var x starred)
(declare (ignore var))
(let* ((body-lab (new-label)) ;start of body (i.e. tagbody)
(test-lab (new-label)) ;start of end test expr
; (res-lab (new-label)) ;result expr
(step-lab nil) ;start of stepping exprs (are threaded through init code) (keep original intact)
(next-step-lab (new-label))
(next-init-lab nil) ;these two are for threading through initialization and stepping segments
(end-lab (new-label)) ;end of statement (for return, etc)
(end-var (make-compiler-temp)) ;result (if any) of do
(lvars (cadr x)) ;list of (var init step) lists
(end-test (caaddr x)) ;car of caddr I hope
(result (cadr (caddr x))) ;cadr of caddr?
(body (cdddr x))
(inner-labs nil)) ;keep track of labels created on the fly
(push `(<comment> do) new-prog)
(let ((need-next-init nil)
(need-next-step nil))
;; variables (list of (var init step) lists)
(loop for j on lvars do
(let* ((i (car j))
(var-i (make-local-var (car i) (not starred))))
;; I knew there was something really peculiar about do -- the local variable is not declared
;; within the block of variables, yet it is declared within the step statement in its declaration!!!
(when next-init-lab
(push next-init-lab inner-labs)
(push `(<label> ,next-init-lab) new-prog))
(setf next-init-lab (new-label))
(setf need-next-init nil)
(if (cdr i)
(push `(<setf> ,+setf+ ,(current-var var-i var-i) ,(current-var (wf (cadr i)))) new-prog)
(push `(<setf> ,+setf+ ,(current-var var-i var-i) nil) new-prog))
(when (cddr i) ;is there a step expr?
(push `(<jump> ,next-init-lab) new-prog)
(setf need-next-init t)
(if (null step-lab) (setf step-lab next-step-lab))
(push `(<label> ,next-step-lab) new-prog)
(push next-step-lab inner-labs)
(setf next-step-lab (new-label))
(setf need-next-step nil)
;; now make sure we get the current (not-yet-defined) variable if it is referenced in step expression
(let ((lst (gethash var-i vars)))
(if lst
(if (not starred)
(progn
(incf (varinfo-depth lst))
(setf (varinfo-shadowed lst) t)))
(warn "step expr of ~A is screwed up" var-i))
(let ((val (current-var (wf (caddr i)))))
(if (and lst (not starred)) (decf (varinfo-depth lst)))
(push `(<setf> ,+setf+ ,(current-var var-i var-i) ,val) new-prog)))
(when (cdr j)
(setf need-next-step t)
(push `(<jump> ,next-step-lab) new-prog)))))
(if (not starred)
(loop for j on lvars do
(let ((lst (gethash (caar j) vars)))
(if lst
(setf (varinfo-parallel lst) nil)
(warn "failed to find ~A" j)))))
(if (null step-lab) ;no step exprs found
(progn
(setf step-lab (new-label))
(push step-lab inner-labs)
(push `(<label> ,step-lab) new-prog))
(when need-next-step
(push next-step-lab inner-labs)
(push `(<label> ,next-step-lab) new-prog)))
(when need-next-init
(push next-init-lab inner-labs)
(push `(<label> ,next-init-lab) new-prog))
;; end test
(push `(<label> ,test-lab) new-prog)
(if end-test
(push `(<jump-if-not> ,(current-var (wf end-test)) ,body-lab) new-prog)
(if result (push `(<jump> ,body-lab) new-prog)))
;; result
; (push `(<label> ,res-lab) new-prog)
(if result
(push `(<setf> ,+setf+ ,(provisional-var end-var) ,(chained-var (wf result) end-var)) new-prog)
(push `(<setf> ,+setf+ ,(provisional-var end-var) nil) new-prog))
(push `(<jump> ,end-lab) new-prog)
(push `(<label> ,body-lab) new-prog)
;; DO body (a tagbody)
(push (list nil end-var end-lab) return-stack)
(loop for i in body do
(if (listp i)
(wf i)
(progn
(push i inner-labs)
(push `(<label> ,i) new-prog))))
(pop return-stack)
(push `(<jump> ,step-lab) new-prog)
(push `(<label> ,end-lab) new-prog)
(loop for j on lvars do
(let ((lst (gethash (caar j) vars)))
(if lst
(decf (varinfo-depth lst))
(warn "can't find ~A" j))))
end-var)))
(defvar setf-functions (list (list 'mus-frequency '<mus-set-frequency> nil :mus-any)
;; :mus-any as fourth declares that arg is a generator, (nil as third is aref constant for def-clm-struct)
;; if fifth exists, it overrides :mus-any as particular gen type
(list 'mus-phase '<mus-set-phase> nil :mus-any)
(list 'mus-scaler '<mus-set-scaler> nil :mus-any)
(list 'mus-increment '<mus-set-increment> nil :mus-any)
(list 'mus-a0 '<mus-set-a0> nil :mus-any)
(list 'mus-a1 '<mus-set-a1> nil :mus-any)
(list 'mus-a2 '<mus-set-a2> nil :mus-any)
(list 'mus-b1 '<mus-set-b1> nil :mus-any)
(list 'mus-b2 '<mus-set-b2> nil :mus-any)
(list 'aref '<setf-aref> nil)
(list 'mus-feedforward '<mus-set-feedforward> nil :mus-any)
(list 'mus-feedback '<mus-set-feedback> nil :mus-any)
(list 'mus-hop '<mus-set-hop> nil :mus-any)
(list 'mus-ramp '<mus-set-ramp> nil :mus-any)
(list 'mus-location '<mus-set-location> nil :mus-any)
(list 'mus-length '<mus-set-length> nil :mus-any)
(list 'mus-width '<mus-set-width> nil :mus-any)
(list 'mus-xcoeff '<mus-set-xcoeff> nil :mus-any)
(list 'mus-ycoeff '<mus-set-ycoeff> nil :mus-any)
(list 'locsig-ref '<locsig-set!> nil :mus-any 'locsig)
(list 'locsig-reverb-ref '<locsig-reverb-set!> nil :mus-any 'locsig)
;(list 'frame-ref '<frame-set!> nil :mus-any 'frame)
;(list 'mixer-ref '<mixer-set!> nil :mus-any 'mixer)
;(list 'mus-name '<mus-set-name> nil :mus-any)
))
(defun setf-branch (var x &optional (set-type +setf+))
(declare (ignore var))
(when (> (length x) 1) ;i.e. (setq) which should return nil
(let ((last-var nil))
(if (evenp (length x))
(if (or (= set-type +setf+) (> (length x) 2))
(error "~D args to ~A in ~A?" (1- (length x)) (car x) x)
;; here we have (incf <place>) etc
(setf x (append x (list 1)))))
(loop for svar in (cdr x) by #'cddr and
sval in (cddr x) by #'cddr do
(let ((setf-data (and (listp svar) (find (car svar) setf-functions :key #'first)))
(evalled-sval (setf last-var (current-var (wf sval)))))
(if *clm-debug* (format t "~A ~A: set ~A~%" svar sval setf-data))
(if setf-data
;; create <setf...> (var adr ...) val
;; svar is either a symbol or a list (<accessor> name [args...])
(let* ((setf-name (second setf-data))
(setf-added-args (third setf-data)) ; aref index for def-clm-struct
(args (cdr svar))
(evalled-args (and args (loop for arg in args collect (current-var (wf arg)))))
(aref-type (gethash (second (first evalled-args)) vars)))
(if *clm-debug* (format t "evalled: ~A~%" evalled-args))
(if (and (fourth setf-data)
(= (length evalled-args) 1)
(listp (first evalled-args)))
(let ((info (gethash (second (first evalled-args)) vars)))
(when (and info
(not (varinfo-type info))
(not (varinfo-gen-type info)))
(if *clm-debug* (format t "set ~A to :mus-any~%" (second (first evalled-args))))
(if (eq (fourth setf-data) :mus-any)
(if (= (length setf-data) 5)
(setf (varinfo-gen-type info) (fifth setf-data)) ; a particular gen type ('locsig etc)
(setf (varinfo-gen-type info) :mus-any))
(setf (varinfo-type info) (fourth setf-data))))))
(if *clm-debug* (format t "aref-type: ~A, setf-added-args: ~A, data: ~A~%" aref-type setf-added-args setf-data))
(if (and aref-type
(member (varinfo-type aref-type) '(:double-array :integer-array :float-array))
(or (eq (first setf-data) 'aref) ; trying to handle def-clm-float-struct
(not (third setf-data))))
(if (eq (varinfo-type aref-type) :float-array)
(push `(<setf-float-aref> ,set-type ,(first evalled-args) ,(second evalled-args) ,evalled-sval) new-prog)
(if (eq (varinfo-type aref-type) :double-array)
(push `(<setf-double-aref> ,set-type ,@evalled-args ,evalled-sval) new-prog)
(if (eq (varinfo-type aref-type) :integer-array)
(push `(<setf-integer-aref> ,set-type ,@evalled-args ,evalled-sval) new-prog))))
(if setf-added-args
(push `(,setf-name ,set-type ,@(append evalled-args setf-added-args) ,evalled-sval) new-prog)
(push `(,setf-name ,set-type ,@evalled-args ,evalled-sval) new-prog))))
(if (listp svar)
(error "setf: unknown accessor: ~A in ~A" (car svar) svar)
(push `(<setf> ,set-type ,(current-var (make-user-var svar)) ,evalled-sval) new-prog)))
))
last-var)))
(defun aref-branch (var x)
(make-var var)
(let ((saved-gen-type *gen-type*))
(let ((args (loop for form in (cdr x) by #'cdr collect (current-var (wf form)))))
(setf *gen-type* saved-gen-type)
(if *clm-debug* (format t "aref: ~A~%" *gen-type*))
(let ((arrvar (gethash (cadr (car args)) vars))
(lst (gethash var vars)))
(if *gen-type*
(progn
(if (and lst
(not (varinfo-gen-type lst)))
(setf (varinfo-gen-type lst) :mus-any))
(if (and arrvar
(or (not (varinfo-gen-type arrvar))
(not (varinfo-arr-gen-type arrvar))))
(progn
(setf (varinfo-gen-type arrvar) :mus-any-array)
(if (not (eq *gen-type* :mus-any))
(setf (varinfo-arr-gen-type arrvar) *gen-type*)))))
#|
(if (and (not (varinfo-type arrvar))
(not (varinfo-gen-type arrvar)))
(progn
(if (and lst
(not (varinfo-gen-type lst)))
(setf (varinfo-type lst) :clm-real))
(if arrvar
(setf (varinfo-type arrvar) :float-array))))
|#
)
(if (and lst
(not (varinfo-gen-type lst))
(not (varinfo-type lst)))
(if (eq (varinfo-gen-type arrvar) :mus-any-array)
(setf (varinfo-gen-type lst) :mus-any)
(if (member (varinfo-type lst) '(:float-array :double-array))
(setf (varinfo-type lst) :clm-real)
(if (eq (varinfo-type lst) :integer-array)
(setf (varinfo-type lst) :clm-integer)))))
(if *clm-debug* (format t "aref: ~A is ~A~%" (cadr (car args)) (or (varinfo-gen-type arrvar) (varinfo-type arrvar) :unknown)))
(loop for arg in (cdr args) do
(let ((arg-info (and (listp arg)
(gethash (second arg) vars))))
(if (and arg-info
(not (varinfo-type arg-info)))
(if (varinfo-temp arg-info)
(setf (varinfo-type arg-info) :clm-integer)
(setf (varinfo-type arg-info) :integer)))))
(if arrvar
(if (eq (varinfo-gen-type arrvar) :mus-any-array)
(push `(<gen-aref> ,(current-var var) ,@args) new-prog)
(if (eq (varinfo-type arrvar) :float-array)
(progn
(setf (varinfo-type lst) :clm-real)
(push `(<float-aref> ,(current-var var) ,@args) new-prog))
(if (eq (varinfo-type arrvar) :double-array)
(progn
(setf (varinfo-type lst) :clm-real)
(push `(<double-aref> ,(current-var var) ,@args) new-prog))
(if (eq (varinfo-type arrvar) :integer-array)
(progn
(setf (varinfo-type lst) :clm-integer)
(push `(<integer-aref> ,(current-var var) ,@args) new-prog))
(push `(<aref> ,(current-var var) ,@args) new-prog)))))
(push `(<aref> ,(current-var var) ,@args) new-prog)))))
var)
(defun let-branch (var x starred)
(let ((locals nil)
(last-var nil))
(make-var var)
(when (cadr x) ;(LET NIL ...) ?
(loop for svar in (cadr x) by #'cdr do
;; the following is legal lisp: (let* ((hi 3) (hi (+ hi 1))) ...) so we have to walk the expression first
(if (listp svar) ;(LET ((A 1) ...))
(let* ((form (current-var (wf (cadr svar))))
(sig (make-local-var (car svar)
(not starred)
(if (floatp form)
:clm-real
(let ((info (and (listp form)
(gethash (second form) vars))))
(and info
(or (varinfo-type info)
(and (varinfo-gen-type info)
:mus-any))))))))
(push sig locals)
(push `(<setf> ,+setf+ ,(current-var sig sig) ,form) new-prog))
(let ((sig (make-local-var svar (not starred))))
(push sig locals)))))
(if (and locals (not starred))
(loop for loc in locals do
(let ((lst (gethash loc vars)))
(setf (varinfo-parallel lst) nil))))
(loop for i in (cddr x) do (setf last-var (wf i)))
(push `(<setf> ,+setf+ ,(provisional-var var) ,(chained-var last-var var)) new-prog)
(if locals
(loop for loc in locals do
(let ((lst (gethash loc vars)))
(decf (varinfo-depth lst)))))
var))
(defun lambda-branch (var x)
;; x is the outer (enclosing) list ((LAMBDA (args) body) args)
(declare (ignore var))
(let* ((passed-args (cdr x))
(lambda-args (second (first x)))
(lambda-body (cddr (first x))))
(if (eq (car (car lambda-body)) 'declare) (setf lambda-body (cdr lambda-body)))