-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.scm
1667 lines (1453 loc) · 69.3 KB
/
codegen.scm
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
(define nll "\n")
;; based on SRV:send-reply by Oleg Kiselyov
(define (print-fragments b out)
(let loop ((fragments b) (result #f))
(cond
((null? fragments) result)
((not (car fragments)) (loop (cdr fragments) result))
((null? (car fragments)) (loop (cdr fragments) result))
((eq? #t (car fragments)) (loop (cdr fragments) #t))
((pair? (car fragments))
(loop (cdr fragments) (loop (car fragments) result)))
((procedure? (car fragments))
((car fragments))
(loop (cdr fragments) #t))
(else
(display (car fragments) out)
(loop (cdr fragments) #t)))))
(define-datatype value value?
(V:C (v (lambda (x) (or (symbol? x) (number? x) (boolean? x) ))))
(V:Var (name symbol?))
(V:Sub (v value?) (index (lambda (x) (and (integer? x) (or (zero? x) (positive? x))))) )
(V:Fn (args list?) (body stmt?))
(V:Op (name symbol?) (args (lambda (x) (every value? x))))
(V:Ifv (test value?) (ift value?) (iff value?))
(V:Rcon (args (lambda (x) (every (lambda (p) (and (pair? p) (symbol? (car p)) (value? (cdr p)))) x))))
(V:Rsel (v value?) (fld symbol?))
)
(define-record-printer (value x out)
(fprintf out "~A"
(cases value x
(V:C (v) (sprintf "(V:C ~A)" v))
(V:Var (n) (sprintf "(V:Var ~A)" n))
(V:Sub (v i) (sprintf "(V:Sub ~A ~A)" v i))
(V:Ifv (test tv fv) (sprintf "(V:Ifv ~A ~A ~A)" test tv fv))
(V:Fn (args body) (sprintf "(V:Fn ~A = ~A)" args body))
(V:Op (name args) (sprintf "(V:Op ~A ~A)" name args))
(V:Rcon (args) (sprintf "(V:Rcon ~A)" args))
(V:Rsel (v fld) (sprintf "(V:Rsel ~A ~A)" v fld))
))
)
(define (symbol-or-pair? v)
(or (symbol? v) (and (pair? v) (symbol? (car v)))))
(define-datatype binding binding?
(B:Val (name symbol-or-pair?) (v value?))
)
(define-record-printer (binding x out)
(fprintf out "~A"
(cases binding x
(B:Val (name v) (sprintf "B:Val ~A = ~A" name v))
)))
(define-datatype stmt stmt?
(E:Ife (test value?) (ift stmt?) (iff stmt?))
(E:Let (bnds (lambda (x) (every binding? x))) (body stmt?))
(E:Begin (stmts (lambda (x) (every stmt? x))))
(E:Set (v value?) (i integer?) (x value?))
(E:Ret (v value?))
(E:Noop)
)
(define-record-printer (stmt x out)
(fprintf out "~A"
(cases stmt x
(E:Ife (test ift iff) (sprintf "E:Ife ~A ~A ~A" test ift iff))
(E:Let (bnds body) (sprintf "E:Let ( ~A ) ~A" bnds body))
(E:Begin (stmts) (sprintf "E:Begin ~A" stmts))
(E:Set (v i x) (sprintf "(E:Set ~A ~A ~A)" v i x))
(E:Ret (v) (sprintf "E:Ret ~A" v))
(E:Noop () (sprintf "E:Noop"))
)))
(define (filter-assoc key alst)
(filter-map (lambda (x) (and (equal? key (car x)) x)) alst))
(define (codegen-primop op args)
(case op
((signal.mul)
(match args
((($ salt#value 'V:C 1.0) right) right)
((left ($ salt#value 'V:C 1.0)) left)
(else (V:Op op args))))
((signal.div)
(match args
((left ($ salt#value 'V:C 1.0)) left)
(else (V:Op op args))))
(else (V:Op op args))
))
(define (codegen-expr ode-inds ode-order invcindexmap invextindexmap)
(lambda (expr)
(let recur ((expr expr))
(match expr
((? symbol?)
(V:Var expr))
((? boolean?)
(V:C expr))
(('signal.primop op . args)
(codegen-primop op (map recur args)))
(('getindex vect index)
(case vect
((y)
(if (member index ode-inds)
(let ((ode-index-assoc (assv index ode-order)))
(V:Sub (recur vect) (cdr ode-index-assoc)))
(V:Var (cdr (assv index invcindexmap)))))
((yext)
(V:Var (cdr (assv index invextindexmap))))
(else (V:Sub (recur vect) index))))
;(('ext index t)
; (recur `(getindex ext ,index)))
;(('extev index t)
; (recur `(signal.primop signal.sub ,t (getindex extev ,index))))
(($ salt#constant 'number val unit)
(V:C val))
(('signal.if test ift iff)
(let ((test1 (recur test))
(ift1 (recur ift))
(iff1 (recur iff)))
(let ((ret (V:Ifv test1 ift1 iff1)))
ret)))
(('signal.reinit test dflt upd)
(V:Ifv (V:Op 'signal.gte (list (recur test) (V:C 0.0)))
(recur upd)
(recur dflt)))
(('prioq.findMinDflt q dflt)
(V:Op 'prioq.findMinDflt (list (recur q) (recur dflt))))
(('prioq.insert key val q)
(V:Op 'prioq.insert (list (recur key) (recur val) (recur q))))
(('prioq.empty)
(V:Op 'prioq.empty '()))
(else expr))
)))
(define (codegen-const-expr expr)
(let recur ((expr expr))
(match expr
((? symbol?)
(V:Var expr))
((? boolean?)
(V:C expr))
(('signal.primop op . args)
(codegen-primop op (map recur args)))
(('getindex vect index)
(error 'codegen-const-expr "not a const expr" expr))
(($ salt#constant 'number val unit)
(V:C val))
(('signal.if test ift iff)
(let ((test1 (recur test))
(ift1 (recur ift))
(iff1 (recur iff)))
(let ((ret (V:Ifv test1 ift1 iff1)))
ret)))
(('signal.reinit test dflt upd)
(error 'codegen-const-expr "not a const expr" expr))
(else expr))
))
(define (codegen-ODE sim #!key (libs '()))
(define (update-bucket k v alst)
(let recur ((alst alst)
(res '()))
(cond ((null? alst)
(cons (list k v) res))
((equal? k (car (car alst)))
(append (cons (cons k (cons v (cdr (car alst)))) res)
(cdr alst)))
(else
(recur (cdr alst) (cons (car alst) res)))
))
)
(define (bucket f lst)
(let recur ((lst lst) (res '()))
(if (null? lst) res
(let ((k (f (car lst))))
(recur (cdr lst) (if k (update-bucket k (car lst) res) res)))
))
)
(define (update-edge e dag)
(update-bucket (car e) (cdr e) dag))
(define (fold-reinits lst)
(let recur ((lst (cdr lst)) (expr (car lst)))
(if (null? lst) expr
(match (car lst)
(('signal.reinit ev lhs rhs)
(recur (cdr lst) `(signal.reinit ,ev ,expr ,rhs)))
(else (error 'fold-reinits "unknown reinit equation" (car lst)))))
))
(define (fold-reinit-blocks ode-inds blocks)
(sort
(filter-map
(lambda (x)
(let ((reinits
(filter-map
(match-lambda
[('setindex 'y_out index val) (and (member index ode-inds) val)]
[('setindex out index val) val]
[else #f])
(cdr x))))
(and (pair? reinits) (cons (car x) (fold-reinits reinits)))))
blocks)
(lambda (x y) (< (car x) (car y))))
)
(define (fold-reinits/priority lst dflt)
(let recur ((lst (cdr lst))
(expr (match (car lst)
(('signal.reinitPriority ev rhs)
`(prioq.insert ,ev ,rhs prioq.empty))
(else (error 'fold-reinits "unknown reinit equation" (car lst))))))
(if (null? lst)
`(prioq.findMinDflt ,expr ,dflt)
(match (car lst)
(('signal.reinitPriority ev rhs)
(recur (cdr lst) `(prioq.insert ,ev ,rhs ,expr)))
(else (error 'fold-reinits "unknown reinit equation" (car lst)))))
))
(define (fold-reinit-blocks/priority ode-inds blocks dflt)
(sort
(filter-map
(lambda (x)
(let ((reinits
(filter-map
(match-lambda
[('setindex 'y_out index val) (and (member index ode-inds) val)]
[('setindex out index val) val]
[else #f])
(cdr x))))
(and (pair? reinits) (cons (car x) (fold-reinits/priority reinits dflt)))))
blocks)
(lambda (x y) (< (car x) (car y))))
)
(define (fold-regime-reinit-blocks blocks)
(sort
(filter-map
(lambda (x)
(let ((reinits
(filter-map
(match-lambda
[('setindex 'r_out index val) val]
[else #f])
(cdr x))))
(and (pair? reinits) (cons (car x) (fold-reinits reinits)))))
blocks)
(lambda (x y) (< (car x) (car y))))
)
(define (fold-asgns asgn-idxs expr)
(let recur ((expr expr) (ax '()))
(match expr
((? symbol?) ax)
((? boolean?) ax)
(('signal.primop op . args)
(fold recur ax args))
(('getindex 'y index)
(if (member `(y ,index) asgn-idxs)
(cons `(y ,index) ax) ax))
(('getindex 'yext index)
(cons `(ext ,index) ax))
(($ salt#constant 'number val unit) ax)
(('signal.if test ift iff)
(recur iff (recur ift (recur test ax))))
(('signal.reinit test dflt upd)
(recur test (recur dflt (recur upd ax))))
(else ax))
))
(define (fold-used-asgns blocks asgn-idxs asgn-defs asgns asgn-dag ext-defs)
(define (fold-eqids myeqid myindex eq-node-map expr)
(let recur ((expr expr) (ax '()))
(match expr
((? symbol?) ax)
((? boolean?) ax)
(('signal.primop op . args)
(fold recur ax args))
(('getindex 'y index)
(if (and (member index asgn-idxs)
(not (equal? `(y ,index) myindex)))
(append (map
(match-lambda ((index . eqid) eqid))
(filter-assoc `(y ,index) eq-node-map)) ax)
ax))
(('getindex 'yext index)
(if (not (equal? `(ext ,index) myindex))
(append (map
(match-lambda ((index . eqid) eqid))
(filter-assoc `(ext ,index) eq-node-map)) ax)
ax))
(($ salt#constant 'number val unit) ax)
(('signal.if test ift iff)
(recur iff (recur ift (recur test ax))))
(('signal.reinit test dflt upd)
(recur test (recur dflt (recur upd ax))))
(else ax))
))
(let* (
(order
(let recur ((order (delete-duplicates
(fold-right
(lambda (expr ax)
(append (reverse (fold-asgns asgn-idxs expr)) ax))
'() blocks))))
(let ((order1 (if (null? order) '()
(concatenate
(map
(lambda (e)
(if (member (last e) order)
(filter-map (lambda (v) (and (not (member v order)) v)) (drop-right e 1))
'()))
asgn-dag)))))
(if (null? order1)
order
(recur (append order1 order)))
))
)
(eq-nodes
(cadr
(fold
(match-lambda*
[((and index ('y _)) (n lst))
(let ((asgns1 (filter-assoc index asgns)))
(let ((ns-asgns1 (list-tabulate (length asgns1) (lambda (i) (+ i n)))))
(list (+ n (length asgns1)) (append (map cons ns-asgns1 asgns1) lst))))]
[((and index ('ext _)) (n lst))
(let ((asgns1 (append (filter-assoc index asgns)
(filter-assoc index ext-defs))))
(let ((ns-asgns1 (list-tabulate (length asgns1) (lambda (i) (+ i n)))))
(list (+ n (length asgns1)) (append (map cons ns-asgns1 asgns1) lst))))]
)
'(0 ())
order
)))
(eq-node-map
(map (match-lambda
((eqid index name rhs)
(cons index eqid)))
(sort
eq-nodes
(lambda (x y) (< (car x) (car y))))
))
(eq-dag
(map
(match-lambda
((eqid index name rhs)
(append (fold-eqids eqid index eq-node-map rhs) (list eqid))))
eq-nodes))
(eq-order (topological-sort eq-dag eq?))
)
(delete-duplicates
(append
(filter-map (lambda (kv) (or (assoc (car kv) asgn-defs) (assoc (car kv) ext-defs))) eq-node-map)
(map (lambda (eqid) (cdr (assoc eqid eq-nodes))) eq-order)))
))
(define (codegen-set-stmts codegen-expr exprs out)
(let recur ((i 0) (exprs exprs) (stmts '()))
(if (null? exprs)
(append stmts (list (E:Ret (V:Var out))))
(recur (+ i 1) (cdr exprs)
(cons (E:Set (V:Var out) i (codegen-expr (car exprs))) stmts))
))
)
(define (codegen-set-stmts/index codegen-expr exprs is out)
(let recur ((is is) (exprs exprs) (stmts '()))
(if (null? exprs)
(append stmts (list (E:Ret (V:Var out))))
(recur (cdr is) (cdr exprs)
(cons (E:Set (V:Var out) (car is) (codegen-expr (car exprs))) stmts))
))
)
(define (codegen-set-stmts* codegen-expr exprs out)
(let recur ((i 0) (exprs exprs) (stmts '()))
(if (null? exprs)
stmts
(recur (+ i 1) (cdr exprs)
(cons (E:Set (V:Var out) i (codegen-expr (car exprs))) stmts))
))
)
(define (ode-def? cindexmap ode-inds)
(lambda (def)
(let* ((name (car def))
(index (cdr (assv name cindexmap))))
(member index ode-inds))
))
(define (random-decls body libs)
(if (member 'random libs)
(E:Let `(,(B:Val 'random_uniform (V:Op 'mk_random_uniform (list (V:Var 'rs))))
,(B:Val 'random_unifrange (V:Op 'mk_random_unifrange (list (V:Var 'rs))))
,(B:Val 'random_normal (V:Op 'mk_random_normal (list (V:Var 'rs) (V:Var 'rszt))))
,(B:Val 'random_exponential (V:Op 'mk_random_exponential (list (V:Var 'rs) (V:Var 'rszt))))
,(B:Val 'random_poisson (V:Op 'mk_random_poisson (list (V:Var 'rs) (V:Var 'rszt)))))
body)
body))
(define (random-args arg-list libs)
(if (member 'random libs) (append arg-list '(rs rszt)) arg-list))
(define (random-cargs arg-list libs)
(if (member 'random libs)
(append arg-list '(((%pointer int) . rs)
((%pointer uint64_t) . ki)
((%pointer uint64_t) . ke)
((%pointer double) . wi)
((%pointer double) . fi)
((%pointer double) . we)
((%pointer double) . fe)
)) arg-list))
(define (random-vargs arg-list libs)
(if (member 'random libs)
(append arg-list `((rs . ,(V:Var 'rs))
(rszt . ,(V:Var 'rszt))))
arg-list))
(let* (
(cindexmap (simruntime-cindexmap sim))
(invcindexmap (map (lambda (x) (cons (cdr x) (car x))) (env->list cindexmap)))
(dindexmap (simruntime-dindexmap sim))
(rindexmap (simruntime-rindexmap sim))
(extindexmap (simruntime-extindexmap sim))
(invextindexmap (map (lambda (x) (cons (cdr x) (car x))) (env->list extindexmap)))
(defs (simruntime-definitions sim))
(discrete-defs (simruntime-discrete-definitions sim))
(params (simruntime-parameters sim))
(fields (simruntime-fields sim))
(externals (simruntime-external-definitions sim))
(externalevs (simruntime-externalev-definitions sim))
(extevlinks (simruntime-externalev-links sim))
(eqblock (simruntime-eqblock sim))
(trstmt (trace 'codegen-ODE "eqblock: ~A~%" eqblock))
(trstmt (trace 'codegen-ODE "externals: ~A~%" externals))
(ode-inds
(sort
(filter-map
(match-lambda [('setindex 'dy_out index val) index]
[('setindex 'y_out index val) #f]
[('reduceindex 'y_out index val) #f]
[('reduceindex 'yext_out index val) #f]
[eq (error 'codegen "unknown equation type" eq)])
eqblock) <))
(ode-order
(cadr (fold (match-lambda* [(index (order lst))
(list (+ 1 order) (cons (cons index order) lst))])
(list 0 '()) ode-inds)))
(ode-n (length ode-order))
(condblock (let ((sorted-eqs
(sort (simruntime-condblock sim)
(match-lambda*
[(('setindex vect1 index1 val1)
('setindex vect2 index2 val2))
(< index1 index2)]))))
(map (match-lambda [('setindex vect index val) val]
[eq (error 'codegen "unknown condition type" eq)]
)
sorted-eqs)))
(trstmt (trace 'codegen-ODE "condblock: ~A~%" condblock))
(cond-n (length condblock))
(dposblocks (let ((bucket-eqs
(bucket
(match-lambda*
[((and eq ('setindex 'd_out index ('signal.reinit ev y rhs))))
index]
[else #f])
(simruntime-posresp sim))))
bucket-eqs))
(trstmt (trace 'codegen-ODE "dposblocks: ~A~%" dposblocks))
(dsc-n (length dposblocks))
(posblocks (let ((bucket-eqs
(bucket
(match-lambda*
[((and eq ('setindex 'y_out index ('signal.reinit ev y rhs))))
index]
[else #f])
(simruntime-posresp sim))))
bucket-eqs))
(posblocks (list-tabulate
(length eqblock)
(lambda (i)
(let ((posblock-assoc (assoc i posblocks)))
(if (not posblock-assoc)
(list i `(setindex y_out ,i (getindex y ,i)))
posblock-assoc)))))
(trstmt (trace 'codegen-ODE "posblocks: ~A~%" posblocks))
(negblocks (let ((bucket-eqs
(bucket
(match-lambda*
[((and eq ('setindex 'y_out index ('signal.reinit ev y rhs))))
index]
[else #f])
(simruntime-negresp sim))))
bucket-eqs))
(negblocks (list-tabulate
(length eqblock)
(lambda (i)
(let ((negblock-assoc (assoc i negblocks)))
(if (not negblock-assoc)
(list i `(setindex y_out ,i (getindex y ,i)))
negblock-assoc)))))
(trstmt (trace 'codegen-ODE "negblocks: ~A~%" negblocks))
(regblocks (let ((bucket-eqs
(bucket
(match-lambda*
[((and eq ('setindex 'r_out index ('signal.reinit ev y rhs))))
index]
[_ #f])
(simruntime-posresp sim))))
bucket-eqs))
(trstmt (trace 'codegen-ODE "regblocks: ~A~%" regblocks))
(extevlink-blocks (let ((bucket-eqs
(bucket
(match-lambda*
[((and eq ('setindex 'ext_out index ('signal.reinitPriority ev rhs))))
index]
[else #f])
extevlinks)))
bucket-eqs))
(regime-n (length regblocks))
(ext-n (length externals))
(has-conds? (> cond-n 0))
(has-regimes? (> regime-n 0))
(has-fields? (> (length fields) 0))
(has-params? (> (length params) 0))
(has-ext? (> ext-n 0))
)
(let* (
(codegen-expr1 (codegen-expr ode-inds ode-order invcindexmap invextindexmap))
(ode-defs (filter-map (lambda (index)
(and (member index ode-inds)
(list-ref defs index)))
(list-tabulate (length invcindexmap)
(lambda (x) x))))
(trstmt (trace 'codegen-ODE "ode-defs: ~A~%" ode-defs))
(asgn-defs (filter-map (lambda (index)
(and (not (member index ode-inds))
(let ((invassoc (assv index invcindexmap)))
(if (not invassoc)
(error 'codegen "unknown continuous quantity index"
index invcindexmap)
(list `(y ,index)
(cdr invassoc)
(list-ref defs index))))))
(list-tabulate (length invcindexmap)
(lambda (x) x))))
(trstmt (trace 'codegen-ODE "asgn-defs: ~A~%" asgn-defs))
(ext-defs (map (match-lambda [(index . name)
`((ext ,index) ,name (getindex ext ,index))])
invextindexmap))
(trstmt (trace 'codegen-ODE "ext-defs: ~A~%" ext-defs))
(asgn-idxs (delete-duplicates
(append
(map car asgn-defs)
(filter-map
(match-lambda [('setindex 'dy_out index val) #f]
[(or ('setindex 'y_out index val)
('reduceindex 'y_out index val)) `(y ,index)]
[('reduceindex 'yext_out index val) #f]
[eq (error 'codegen "unknown equation type" eq)])
eqblock))
))
(trstmt (trace 'codegen-ODE "asgn-idxs: ~A~%" asgn-idxs))
(asgn-dag (fold (match-lambda*
[(('setindex 'dy_out index val) dag) dag]
[(('setindex 'y_out index val) dag)
(let ((srcs (fold-asgns asgn-idxs val)))
(if (null? srcs) (cons `((y ,index)) dag)
(fold (lambda (src dag) (update-edge (cons src `(y ,index)) dag))
dag srcs)))]
[(('reduceindex 'y_out index val) dag)
(let ((edges (filter-map (match-lambda
[(and src ('y srcindex))
(and (not (= srcindex index)) (cons src `(y ,index)))]
[(and src ('ext srcindex))
(cons src `(y ,index))]
[src (error 'codegen "unknown node type" src)])
(fold-asgns asgn-idxs val))))
(trace 'codegen-ODE "asgn-dag: reduceindex y_out ~A: val = ~A asgns: ~A~%"
index val (fold-asgns asgn-idxs val))
(if (null? edges) (cons `((y ,index)) dag)
(fold (lambda (e dag) (update-edge e dag))
dag edges)))]
[(('reduceindex 'yext_out index val) dag)
(let ((edges (filter-map (match-lambda
[(and src ('ext srcindex))
(and (not (= srcindex index)) (cons src `(ext ,index)))]
[(and src ('y srcindex))
(cons src `(ext ,index))]
[src (error 'codegen "unknown node type" src)])
(fold-asgns asgn-idxs val))))
(if (null? edges) (cons `((ext ,index)) dag)
(fold (lambda (e dag) (update-edge e dag))
dag edges)))]
[eq (error 'codegen "unknown equation type" eq)])
'() eqblock))
(trstmt (trace 'codegen-ODE "asgn-dag: ~A~%" asgn-dag))
(asgn-order (topological-sort asgn-dag eq?))
(trstmt (trace 'codegen-ODE "asgn-order: ~A~%" asgn-order))
(asgns
(delete-duplicates
(fold-right
(lambda (ordtindex ax)
(append
(reverse
(filter-map
(match ordtindex
[('y ordindex)
(match-lambda [('setindex 'dy_out index val) #f]
[(or ('setindex 'y_out index val)
('reduceindex 'y_out index val))
(and (= index ordindex)
(list ordtindex (cdr (assv index invcindexmap)) val))]
[('reduceindex 'yext_out index val) #f]
[eq (error 'codegen "unknown equation type" eq)])]
[('ext ordindex)
(match-lambda [('setindex 'dy_out index val) #f]
[(or ('setindex 'y_out index val)
('reduceindex 'y_out index val)) #f]
[('reduceindex 'yext_out index val)
(and (= index ordindex)
(list ordtindex (cdr (assv index invextindexmap)) val))]
[eq (error 'codegen "unknown equation type" eq)])]
[else (error 'codegen "unknown index type" ordtindex)])
eqblock)) ax))
'() asgn-order)
equal?))
(trstmt (trace 'codegen-ODE "asgns: ~A~%" asgns))
(odeblock
(sort
(filter-map
(match-lambda [(and ode ('setindex 'dy_out index val)) ode]
[('setindex 'y_out index val) #f]
[('reduceindex 'y_out index val) #f]
[('reduceindex 'yext_out index val) #f]
[eq (error 'codegen "unknown equation type" eq)])
eqblock)
(match-lambda*
[(('setindex vect1 index1 val1)
('setindex vect2 index2 val2))
(< index1 index2)])))
(paramfun (if has-params?
(V:Op 'make_real_initial
(list (V:C (length params))
(V:Fn '(p_out) (E:Begin (codegen-set-stmts codegen-expr1 params 'p_out)))))
(V:Fn '() (E:Ret (V:C 'empty_real_initial)))))
(fieldfun (if has-fields?
(V:Op 'make_real_initial
(list (V:C (length fields))
(V:Fn '(fld_out) (E:Begin (codegen-set-stmts codegen-expr1 fields 'fld_out)))))
(V:Fn '() (E:Ret (V:C 'empty_real_initial)))))
(initfun
(let ((stmts (codegen-set-stmts codegen-expr1 ode-defs 'y_out)))
(V:Fn (let ((args0 '(p fld)))
(random-args args0 libs))
(random-decls
(E:Ret (V:Fn '(y_out)
(if (null? asgn-defs)
(E:Begin stmts)
(E:Let (map (match-lambda ((_ name rhs) (B:Val name (codegen-expr1 rhs))))
asgn-defs)
(E:Begin stmts)))))
libs))
))
(dinitfun
(let ((stmts (codegen-set-stmts codegen-expr1 discrete-defs 'd_out)))
(if (null? discrete-defs)
(V:C 'NONE)
(V:Op 'SOME
(list
(V:Fn (let ((args0 '(p fld)))
(random-args args0 libs))
(random-decls
(E:Ret (V:Fn '(d_out)
(if (null? asgn-defs)
(E:Begin stmts)
(E:Let (map (match-lambda ((_ name rhs) (B:Val name (codegen-expr1 rhs))))
asgn-defs)
(E:Begin stmts)))))
libs))
))
))
)
(initextfun
(let ((stmts (codegen-set-stmts codegen-expr1 externals 'ext_out)))
(V:Fn '(p fld)
(E:Ret (V:Op 'make_ext (list (V:C (length externals))
(V:Fn '(ext_out)
(if (null? asgn-defs)
(E:Begin stmts)
(E:Let (map (match-lambda ((_ name rhs) (B:Val name (codegen-expr1 rhs))))
asgn-defs)
(E:Begin stmts))))))
))
))
(initextevfun
(let ((stmts (codegen-set-stmts codegen-expr1 externalevs 'extev_out)))
(V:Fn '(p fld)
(E:Ret (V:Op 'make_ext (list (V:C (length externalevs))
(V:Fn '(extev_out)
(if (null? asgn-defs)
(E:Begin stmts)
(E:Let (map (match-lambda ((_ name rhs) (B:Val name (codegen-expr1 rhs))))
asgn-defs)
(E:Begin stmts))))))
))
))
(linkextevfun
(let* ((dflt (make-constant 'number +inf.0 'unitbottom))
(blocks (fold-reinit-blocks/priority ode-inds extevlink-blocks dflt))
(stmts (codegen-set-stmts/index (compose codegen-expr1 cdr) blocks (map car blocks) 'extev_out)))
(V:Fn '(extev extev_out)
(E:Begin stmts)
))
)
(rhsfun
(let ((rhs-args '((double . t) ((%pointer double) . y) ((%pointer double) . dy_out) ((%pointer (%pointer void)) . clos))))
(V:Fn rhs-args
(let* ((clos-args
(let ((args0
(if has-regimes?
`(((%pointer double) . p) ((%pointer double) . fld)
((%pointer double) . d) ((%pointer int) . r)
((%pointer double) . ext) ((%pointer double) . extev))
`(((%pointer double) . p) ((%pointer double) . fld)
((%pointer double) . ext) ((%pointer double) . extev)))))
(random-cargs args0 libs)))
(odes (map (match-lambda [('setindex 'dy_out index val) val]) odeblock))
(used-asgns (fold-used-asgns odes asgn-idxs asgn-defs asgns asgn-dag ext-defs))
(stmts (codegen-set-stmts* codegen-expr1 odes 'dy_out)))
(E:Let (map (lambda (arg index) (B:Val (cons (cdr arg) (car arg)) (V:Sub (V:Var 'clos) index)))
clos-args (list-tabulate (length clos-args) (lambda (i) i)))
(if (null? used-asgns)
(E:Begin stmts)
(E:Let (map (match-lambda ((_ name rhs) (B:Val name (codegen-expr1 rhs)))) used-asgns)
(E:Begin stmts)))
))
))
)
(stepfun
(let* ((odes (map (match-lambda [('setindex 'dy_out index val) val]) odeblock))
(used-asgns (fold-used-asgns odes asgn-idxs asgn-defs asgns asgn-dag ext-defs))
(clos-args (let ((args0 (if has-regimes? '(p fld d r ext extev) '(p fld ext extev))))
(random-args args0 libs)))
(rhsfun
(V:Fn '(clos)
(E:Let
(filter-map (lambda (arg)
(let ((dflt (B:Val arg (V:Rsel (V:Var 'clos) arg))))
(case arg
((p) (if has-params? dflt #f))
((fld) (if has-fields? dflt #f))
((ext) (if has-ext? dflt #f))
((extev) (if has-ext? dflt #f))
(else dflt))))
clos-args )
(random-decls
(E:Ret
(V:Fn '(t y dy_out)
(let ((stmts (codegen-set-stmts codegen-expr1 odes 'dy_out)))
(if (null? used-asgns)
(E:Begin stmts)
(E:Let (map (match-lambda ((_ name rhs) (B:Val name (codegen-expr1 rhs)))) used-asgns)
(E:Begin stmts))
))
))
libs))
))
)
(if has-regimes?
(V:Op 'make_regime_stepper (list rhsfun))
(V:Op 'make_stepper (list rhsfun))))
)
(odefun
(V:Fn (random-args '(p fld) libs)
(E:Ret
(cond
(has-regimes?
(V:Op 'RegimeStepper
(list (V:Fn '(d r ext extev h abstol reltol x y yout)
(E:Ret (V:Op 'stepfun
(list (V:Rcon
(random-vargs
`((p . ,(V:Var 'p))
(fld . ,(V:Var 'fld))
(d . ,(V:Var 'd))
(r . ,(V:Var 'r))
(ext . ,(V:Var 'ext))
(extev . ,(V:Var 'extev)))
libs))
(V:Var 'h)
(V:Var 'abstol)
(V:Var 'reltol)
(V:Var 'x)
(V:Var 'y)
(V:Var 'yout)
))
))
))
)
(has-conds?
(V:Op 'EventStepper
(list (V:Fn '(ext extev h abstol reltol x y yout)
(E:Ret (V:Op 'stepfun
(list (V:Rcon
(random-vargs
`((p . ,(V:Var 'p))
(fld . ,(V:Var 'fld))
(ext . ,(V:Var 'ext))
(extev . ,(V:Var 'extev)))
libs))
(V:Var 'h)
(V:Var 'abstol)
(V:Var 'reltol)
(V:Var 'x)
(V:Var 'y)
(V:Var 'yout))
))
))
))
(else
(V:Op 'ContStepper
(list (V:Fn '(ext extev h abstol reltol x y yout)
(E:Ret (V:Op 'stepfun
(list (V:Rcon
(random-vargs
`((p . ,(V:Var 'p))
(fld . ,(V:Var 'fld))
(ext . ,(V:Var 'ext))
(extev . ,(V:Var 'extev)))
libs))
(V:Var 'h)
(V:Var 'abstol)
(V:Var 'reltol)
(V:Var 'x)
(V:Var 'y)
(V:Var 'yout)))
))
))
))
))
)
(initcondfun
(if (null? condblock)
(V:C 'NONE)
(V:Op 'SOME
(list
(V:Fn '(c_out)
(E:Begin
(append
(list-tabulate
cond-n
(lambda (i) (E:Set (V:Var 'c_out) i (V:C 'negInf))))
(list (E:Ret (V:Var 'c_out)))
)
))
))
))
(condfun
(let ((used-asgns (fold-used-asgns condblock asgn-idxs asgn-defs asgns asgn-dag ext-defs)))
(if (null? condblock)
(V:C 'NONE)
(let ((fnval (V:Fn (if has-regimes?
'(t y c d r ext extev c_out)
'(t y c ext extev c_out))
(let ((stmts (codegen-set-stmts codegen-expr1 condblock 'c_out)))
(if (null? used-asgns)
(E:Begin stmts)
(E:Let (map (match-lambda ((_ name rhs) (B:Val name (codegen-expr1 rhs))))
used-asgns)
(E:Begin stmts)))))))
(V:Op 'SOME
(list
(V:Fn '(p fld)
(E:Ret (if has-regimes?
(V:Op 'RegimeCondition (list (V:Op 'make_regime_cond (list (V:Var 'p) (V:Var 'fld) fnval))))
(V:Op 'SCondition (list (V:Op 'make_cond (list (V:Var 'p) (V:Var 'fld) fnval))))
)
))
))
))
))
(condrhsfun
(let ((used-asgns (fold-used-asgns condblock asgn-idxs asgn-defs asgns asgn-dag ext-defs))
(rhs-args (let ((args0 (if has-regimes?
'((double . t) ((%pointer double) . p) ((%pointer double) . fld)
((%pointer double) . y) ((%pointer double) . c)
((%pointer double) . d) ((%pointer int) . r)
((%pointer double) . ext) ((%pointer double) . extev)
((%pointer double) . c_out))
'((double . t) ((%pointer double) . p) ((%pointer double) . fld)
((%pointer double) . y) ((%pointer double) . c)
((%pointer double) . ext) ((%pointer double) . extev)