about summary refs log tree commit diff stats
path: root/archive/1.vm.arc/mu.arc.t
blob: 6c0464f9915d944bcaf77f28758fe7ef750e0ac7 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
; Mu: An exploration on making the global structure of programs more accessible.
;
;   "Is it a language, or an operating system, or a virtual machine? Mu."
;   (with apologies to Robert Pirsig: http://en.wikipedia.org/wiki/Mu_%28negative%29#In_popular_culture)
;
;; Motivation
;
; I want to live in a world where I can have an itch to tweak a program, clone
; its open-source repository, orient myself on how it's organized, and make
; the simple change I envisioned, all in an afternoon. This codebase tries to
; make this possible for its readers. (More details: http://akkartik.name/about)
;
; What helps comprehend the global structure of programs? For starters, let's
; enumerate what doesn't: idiomatic code, adherence to a style guide or naming
; convention, consistent indentation, API documentation for each class, etc.
; These conventional considerations improve matters in the small, but don't
; help understand global organization. They help existing programmers manage
; day-to-day operations, but they can't turn outsider programmers into
; insiders. (Elaboration: http://akkartik.name/post/readable-bad)
;
; In my experience, two things have improved matters so far: version control
; and automated tests. Version control lets me rewind back to earlier, simpler
; times when the codebase was simpler, when its core skeleton was easier to
; ascertain. Indeed, arguably what came first is by definition the skeleton of
; a program, modulo major rewrites. Once you understand the skeleton, it
; becomes tractable to 'play back' later major features one by one. (Previous
; project that fleshed out this idea: http://akkartik.name/post/wart-layers)
;
; The second and biggest boost to comprehension comes from tests. Tests are
; good for writers for well-understood reasons: they avoid regressions, and
; they can influence code to be more decoupled and easier to change. In
; addition, tests are also good for the outsider reader because they permit
; active reading. If you can't build a program and run its tests it can't help
; you understand it. It hangs limp at best, and might even be actively
; misleading. If you can run its tests, however, it comes alive. You can step
; through scenarios in a debugger. You can add logging and scan logs to make
; sense of them. You can run what-if scenarios: "why is this line not written
; like this?" Make a change, rerun tests: "Oh, that's why." (Elaboration:
; http://akkartik.name/post/literate-programming)
;
; However, tests are only useful to the extent that they exist. Think back to
; your most recent codebase. Do you feel comfortable releasing a new version
; just because the tests pass? I'm not aware of any such project. There's just
; too many situations envisaged by the authors that were never encoded in a
; test. Even disciplined authors can't test for performance or race conditions
; or fault tolerance. If a line is phrased just so because of some subtle
; performance consideration, it's hard to communicate to newcomers.
;
; This isn't an arcane problem, and it isn't just a matter of altruism. As
; more and more such implicit considerations proliferate, and as the original
; authors are replaced by latecomers for day-to-day operations, knowledge is
; actively forgotten and lost. The once-pristine codebase turns into legacy
; code that is hard to modify without expensive and stress-inducing
; regressions.
;
; How to write tests for performance, fault tolerance, race conditions, etc.?
; How can we state and verify that a codepath doesn't ever perform memory
; allocation, or write to disk? It requires better, more observable primitives
; than we currently have. Modern operating systems have their roots in the
; 70s. Their interfaces were not designed to be testable. They provide no way
; to simulate a full disk, or a specific sequence of writes from different
; threads. We need something better.
;
; This project tries to move, groping, towards that 'something better', a
; platform that is both thoroughly tested and allows programs written for it
; to be thoroughly tested. It tries to answer the question:
;
;   If Denis Ritchie and Ken Thompson were to set out today to co-design unix
;   and C, knowing what we know about automated tests, what would they do
;   differently?
;
; To try to impose *some* constraints on this gigantic yak-shave, we'll try to
; keep both language and OS as simple as possible, focused entirely on
; permitting more kinds of tests, on first *collecting* all the information
; about implicit considerations in some form so that readers and tools can
; have at least some hope of making sense of it.
;
; The initial language will be just assembly. We'll try to make it convenient
; to program in with some simple localized rewrite rules inspired by lisp
; macros and literate programming. Programmers will have to do their own
; memory management and register allocation, but we'll provide libraries to
; help with them.
;
; The initial OS will provide just memory management and concurrency
; primitives. No users or permissions (we don't live on mainframes anymore),
; no kernel- vs user-mode, no virtual memory or process abstraction, all
; threads sharing a single address space (use VMs for security and
; sandboxing). The only use case we care about is getting a test harness to
; run some code, feed it data through blocking channels, stop it and observe
; its internals. The code under test is expected to cooperate in such testing,
; by logging important events for the test harness to observe. (More info:
; http://akkartik.name/post/tracing-tests)
;
; The common thread here is elimination of abstractions, and it's not an
; accident. Abstractions help insiders manage the evolution of a codebase, but
; they actively hinder outsiders in understanding it from scratch. This
; matters, because the funnel to turn outsiders into insiders is critical to
; the long-term life of a codebase. Perhaps authors should raise their
; estimation of the costs of abstraction, and go against their instincts for
; introducing it. That's what I'll be trying to do: question every abstraction
; before I introduce it. We'll see how it goes.

; ---

;; Getting started
;
; Mu is currently built atop Racket and Arc, but this is temporary and
; contingent. We want to keep our options open, whether to port to a different
; host language, and easy to rewrite to native code for any platform. So we'll
; try to avoid 'cheating': relying on the host platform for advanced
; functionality.
;
; Other than that, we'll say no more about the code, and focus in the rest of
; this file on the scenarios the code cares about.

(selective-load "mu.arc" section-level)
(ero "running tests in mu.ar.c.t (takes ~30s)")
;? (quit)

(set allow-raw-addresses*)

(section 20

; Our language is assembly-like in that functions consist of series of
; statements, and statements consist of an operation and its arguments (input
; and output).
;
;   oarg1, oarg2, ... <- op arg1, arg2, ...
;
; Args must be atomic, like an integer or a memory address, they can't be
; expressions doing arithmetic or function calls. But we can have any number
; of them.
;
; Since we're building on lisp, our code samples won't look quite like the
; idealized syntax above. For now they will look like this:
;
;   (function f [
;     (oarg1 oarg2 ... <- op arg1 arg2 ...)
;     ...
;     ...
;    ])
;
; Each arg/oarg can contain metadata separated by slashes and colons. In this
; first example below, the only metadata is types: 'integer' for a memory
; location containing an integer, and 'literal' for a value included directly
; in code. (Assembly languages traditionally call them 'immediate' operands.)
; In the future a simple tool will check that the types line up as expected in
; each op. A different tool might add types where they aren't provided.
; Instead of a monolithic compiler I want to build simple, lightweight tools
; that can be combined in various ways, say for using different typecheckers
; in different subsystems.
;
; In our tests we'll define such mu functions using a call to 'add-code', so
; look for it when reading the code examples. Everything outside 'add-code' is
; just test-harness details that can be skipped at first.

(reset)
;? (set dump-trace*)
(new-trace "literal")
(add-code
  '((function main [
      (1:integer <- copy 23:literal)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (~is memory*.1 23)
  (prn "F - 'copy' writes its lone 'arg' after the instruction name to its lone 'oarg' or output arg before the arrow. After this test, the value 23 is stored in memory address 1."))
;? (reset) ;? 2
;? (quit) ;? 2

; Our basic arithmetic ops can operate on memory locations or literals.
; (Ignore hardware details like registers for now.)

(reset)
(new-trace "add")
(add-code
  '((function main [
      (1:integer <- copy 1:literal)
      (2:integer <- copy 3:literal)
      (3:integer <- add 1:integer 2:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 1  2 3  3 4))
  (prn "F - 'add' operates on two addresses"))
;? (reset) ;? 1
;? (quit) ;? 1

(reset)
(new-trace "add-literal")
(add-code
  '((function main [
      (1:integer <- add 2:literal 3:literal)
     ])))
(run 'main)
(when (~is memory*.1 5)
  (prn "F - ops can take 'literal' operands (but not return them)"))

(reset)
(new-trace "sub-literal")
(add-code
  '((function main [
      (1:integer <- subtract 1:literal 3:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.1 -2)
  (prn "F - 'subtract'"))

(reset)
(new-trace "mul-literal")
(add-code
  '((function main [
      (1:integer <- multiply 2:literal 3:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.1 6)
  (prn "F - 'multiply'"))

(reset)
(new-trace "div-literal")
(add-code
  '((function main [
      (1:integer <- divide 8:literal 3:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.1 (/ real.8 3))
  (prn "F - 'divide'"))

(reset)
(new-trace "idiv-literal")
(add-code
  '((function main [
      (1:integer 2:integer <- divide-with-remainder 23:literal 6:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 3  2 5))
  (prn "F - 'divide-with-remainder' performs integer division"))

(reset)
(new-trace "dummy-oarg")
;? (set dump-trace*)
(add-code
  '((function main [
      (_ 2:integer <- divide-with-remainder 23:literal 6:literal)
     ])))
(run 'main)
(when (~iso memory* (obj 2 5))
  (prn "F - '_' oarg can ignore some results"))
;? (quit)

; Basic boolean operations: and, or, not
; There are easy ways to encode booleans in binary, but we'll skip past those
; details for now.

(reset)
(new-trace "and-literal")
(add-code
  '((function main [
      (1:boolean <- and t:literal nil:literal)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (~is memory*.1 nil)
  (prn "F - logical 'and' for booleans"))

; Basic comparison operations

(reset)
(new-trace "lt-literal")
(add-code
  '((function main [
      (1:boolean <- less-than 4:literal 3:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.1 nil)
  (prn "F - 'less-than' inequality operator"))

(reset)
(new-trace "le-literal-false")
(add-code
  '((function main [
      (1:boolean <- lesser-or-equal 4:literal 3:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.1 nil)
  (prn "F - 'lesser-or-equal'"))

(reset)
(new-trace "le-literal-true")
(add-code
  '((function main [
      (1:boolean <- lesser-or-equal 4:literal 4:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.1 t)
  (prn "F - 'lesser-or-equal' returns true for equal operands"))

(reset)
(new-trace "le-literal-true-2")
(add-code
  '((function main [
      (1:boolean <- lesser-or-equal 4:literal 5:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.1 t)
  (prn "F - 'lesser-or-equal' - 2"))

; Control flow operations: jump, jump-if, jump-unless
; These introduce a new type -- 'offset' -- for literals that refer to memory
; locations relative to the current location.

(reset)
(new-trace "jump-skip")
(add-code
  '((function main [
      (1:integer <- copy 8:literal)
      (jump 1:offset)
      (2:integer <- copy 3:literal)  ; should be skipped
      (reply)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 8))
  (prn "F - 'jump' skips some instructions"))
;? (quit)

(reset)
(new-trace "jump-target")
(add-code
  '((function main [
      (1:integer <- copy 8:literal)
      (jump 1:offset)
      (2:integer <- copy 3:literal)  ; should be skipped
      (reply)
      (3:integer <- copy 34:literal)
     ])))  ; never reached
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 8))
  (prn "F - 'jump' doesn't skip too many instructions"))
;? (quit)

(reset)
(new-trace "jump-if-skip")
(add-code
  '((function main [
      (2:integer <- copy 1:literal)
      (1:boolean <- equal 1:literal 2:integer)
      (jump-if 1:boolean 1:offset)
      (2:integer <- copy 3:literal)
      (reply)
      (3:integer <- copy 34:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 t  2 1))
  (prn "F - 'jump-if' is a conditional 'jump'"))

(reset)
(new-trace "jump-if-fallthrough")
(add-code
  '((function main [
      (1:boolean <- equal 1:literal 2:literal)
      (jump-if 3:boolean 1:offset)
      (2:integer <- copy 3:literal)
      (reply)
      (3:integer <- copy 34:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 nil  2 3))
  (prn "F - if 'jump-if's first arg is false, it doesn't skip any instructions"))

(reset)
(new-trace "jump-if-backward")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 1:literal)
      ; loop
      (2:integer <- add 2:integer 2:integer)
      (3:boolean <- equal 1:integer 2:integer)
      (jump-if 3:boolean -3:offset)  ; to loop
      (4:integer <- copy 3:literal)
      (reply)
      (3:integer <- copy 34:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 2  2 4  3 nil  4 3))
  (prn "F - 'jump-if' can take a negative offset to make backward jumps"))

(reset)
(new-trace "jump-label")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 1:literal)
      loop
      (2:integer <- add 2:integer 2:integer)
      (3:boolean <- equal 1:integer 2:integer)
      (jump-if 3:boolean loop:offset)
      (4:integer <- copy 3:literal)
      (reply)
      (3:integer <- copy 34:literal)
     ])))
;? (set dump-trace*)
;? (= dump-trace* (obj whitelist '("-")))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 2  2 4  3 nil  4 3))
  (prn "F - 'jump-if' can take a negative offset to make backward jumps"))
;? (quit)

; Data movement relies on addressing modes:
;   'direct' - refers to a memory location; default for most types.
;   'literal' - directly encoded in the code; implicit for some types like 'offset'.

(reset)
(new-trace "direct-addressing")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:integer <- copy 1:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 34  2 34))
  (prn "F - 'copy' performs direct addressing"))

; 'Indirect' addressing refers to an address stored in a memory location.
; Indicated by the metadata '/deref'. Usually requires an address type.
; In the test below, the memory location 1 contains '2', so an indirect read
; of location 1 returns the value of location 2.

(reset)
(new-trace "indirect-addressing")
(add-code
  '((function main [
      (1:integer-address <- copy 2:literal)  ; unsafe; can't do this in general
      (2:integer <- copy 34:literal)
      (3:integer <- copy 1:integer-address/deref)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 2  2 34  3 34))
  (prn "F - 'copy' performs indirect addressing"))

; Output args can use indirect addressing. In the test below the value is
; stored at the location stored in location 1 (i.e. location 2).

(reset)
(new-trace "indirect-addressing-oarg")
(add-code
  '((function main [
      (1:integer-address <- copy 2:literal)
      (2:integer <- copy 34:literal)
      (1:integer-address/deref <- add 2:integer 2:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 2  2 36))
  (prn "F - instructions can perform indirect addressing on output arg"))

;; Compound data types
;
; Until now we've dealt with scalar types like integers and booleans and
; addresses, where mu looks like other assembly languages. In addition, mu
; provides first-class support for compound types: arrays and and-records.
;
; 'get' accesses fields in and-records
; 'index' accesses indices in arrays
;
; Both operations require knowledge about the types being worked on, so all
; types used in mu programs are defined in a single global system-wide table
; (see type* in mu.arc for the complete list of types; we'll add to it over
; time).

; first a sanity check that the table of types is consistent
(reset)
(each (typ typeinfo) type*
  (when typeinfo!and-record
    (assert (is typeinfo!size (len typeinfo!elems)))
    (when typeinfo!fields
      (assert (is typeinfo!size (len typeinfo!fields))))))

(reset)
(new-trace "get-record")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:boolean <- copy nil:literal)
      (3:boolean <- get 1:integer-boolean-pair 1:offset)
      (4:integer <- get 1:integer-boolean-pair 0:offset)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 34  2 nil  3 nil  4 34))
  (prn "F - 'get' accesses fields of and-records"))
;? (quit)

(reset)
(new-trace "get-indirect")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:boolean <- copy nil:literal)
      (3:integer-boolean-pair-address <- copy 1:literal)
      (4:boolean <- get 3:integer-boolean-pair-address/deref 1:offset)
      (5:integer <- get 3:integer-boolean-pair-address/deref 0:offset)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 34  2 nil  3 1  4 nil  5 34))
  (prn "F - 'get' accesses fields of and-record address"))

(reset)
(new-trace "get-indirect-repeated")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:integer <- copy 35:literal)
      (3:integer <- copy 36:literal)
      (4:integer-point-pair-address <- copy 1:literal)  ; unsafe
      (5:integer-point-pair-address-address <- copy 4:literal)  ; unsafe
      (6:integer-integer-pair <- get 5:integer-point-pair-address-address/deref/deref 1:offset)
      (8:integer <- get 5:integer-point-pair-address-address/deref/deref 0:offset)
     ])))
(run 'main)
(when (~memory-contains 6 '(35 36 34))
  (prn "F - 'get' can deref multiple times"))
;? (quit)

(reset)
(new-trace "get-compound-field")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:integer <- copy 35:literal)
      (3:integer <- copy 36:literal)
      (4:integer-integer-pair <- get 1:integer-point-pair 1:offset)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 34  2 35  3 36  4 35  5 36))
  (prn "F - 'get' accesses fields spanning multiple locations"))

(reset)
(new-trace "get-address")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:boolean <- copy t:literal)
      (3:boolean-address <- get-address 1:integer-boolean-pair 1:offset)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 34  2 t  3 2))
  (prn "F - 'get-address' returns address of fields of and-records"))

(reset)
(new-trace "get-address-indirect")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:boolean <- copy t:literal)
      (3:integer-boolean-pair-address <- copy 1:literal)
      (4:boolean-address <- get-address 3:integer-boolean-pair-address/deref 1:offset)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 34  2 t  3 1  4 2))
  (prn "F - 'get-address' accesses fields of and-record address"))

(reset)
(new-trace "index-literal")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 23:literal)
      (3:boolean <- copy nil:literal)
      (4:integer <- copy 24:literal)
      (5:boolean <- copy t:literal)
      (6:integer-boolean-pair <- index 1:integer-boolean-pair-array 1:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 2  2 23 3 nil  4 24 5 t  6 24 7 t))
  (prn "F - 'index' accesses indices of arrays"))
;? (quit)

(reset)
(new-trace "index-direct")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 23:literal)
      (3:boolean <- copy nil:literal)
      (4:integer <- copy 24:literal)
      (5:boolean <- copy t:literal)
      (6:integer <- copy 1:literal)
      (7:integer-boolean-pair <- index 1:integer-boolean-pair-array 6:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 2  2 23 3 nil  4 24 5 t  6 1  7 24 8 t))
  (prn "F - 'index' accesses indices of arrays"))
;? (quit)

(reset)
(new-trace "index-indirect")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 23:literal)
      (3:boolean <- copy nil:literal)
      (4:integer <- copy 24:literal)
      (5:boolean <- copy t:literal)
      (6:integer <- copy 1:literal)
      (7:integer-boolean-pair-array-address <- copy 1:literal)
      (8:integer-boolean-pair <- index 7:integer-boolean-pair-array-address/deref 6:integer)
     ])))
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1")))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 2  2 23 3 nil  4 24 5 t  6 1  7 1  8 24 9 t))
  (prn "F - 'index' accesses indices of array address"))
;? (quit)

(reset)
(new-trace "index-indirect-multiple")
(add-code
  '((function main [
      (1:integer <- copy 4:literal)
      (2:integer <- copy 23:literal)
      (3:integer <- copy 24:literal)
      (4:integer <- copy 25:literal)
      (5:integer <- copy 26:literal)
      (6:integer-array-address <- copy 1:literal)  ; unsafe
      (7:integer-array-address-address <- copy 6:literal)  ; unsafe
      (8:integer <- index 7:integer-array-address-address/deref/deref 1:literal)
     ])))
(run 'main)
(when (~is memory*.8 24)
  (prn "F - 'index' can deref multiple times"))

(reset)
(new-trace "index-address")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 23:literal)
      (3:boolean <- copy nil:literal)
      (4:integer <- copy 24:literal)
      (5:boolean <- copy t:literal)
      (6:integer <- copy 1:literal)
      (7:integer-boolean-pair-address <- index-address 1:integer-boolean-pair-array 6:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 2  2 23 3 nil  4 24 5 t  6 1  7 4))
  (prn "F - 'index-address' returns addresses of indices of arrays"))

(reset)
(new-trace "index-address-indirect")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 23:literal)
      (3:boolean <- copy nil:literal)
      (4:integer <- copy 24:literal)
      (5:boolean <- copy t:literal)
      (6:integer <- copy 1:literal)
      (7:integer-boolean-pair-array-address <- copy 1:literal)
      (8:integer-boolean-pair-address <- index-address 7:integer-boolean-pair-array-address/deref 6:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 2  2 23 3 nil  4 24 5 t  6 1  7 1  8 4))
  (prn "F - 'index-address' returns addresses of indices of array addresses"))

; Array values know their length. Record lengths are saved in the types table.

(reset)
(new-trace "len-array")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 23:literal)
      (3:boolean <- copy nil:literal)
      (4:integer <- copy 24:literal)
      (5:boolean <- copy t:literal)
      (6:integer <- length 1:integer-boolean-pair-array)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.6 2)
  (prn "F - 'length' of array"))

(reset)
(new-trace "len-array-indirect")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 23:literal)
      (3:boolean <- copy nil:literal)
      (4:integer <- copy 24:literal)
      (5:boolean <- copy t:literal)
      (6:integer-address <- copy 1:literal)
      (7:integer <- length 6:integer-boolean-pair-array-address/deref)
     ])))
;? (set dump-trace*)
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1")))
(run 'main)
;? (prn memory*)
(when (~is memory*.7 2)
  (prn "F - 'length' of array address"))

; 'sizeof' is a helper to determine the amount of memory required by a type.
; Only for non-arrays.

(reset)
(new-trace "sizeof-record")
(add-code
  '((function main [
      (1:integer <- sizeof integer-boolean-pair:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.1 2)
  (prn "F - 'sizeof' returns space required by arg"))

(reset)
(new-trace "sizeof-record-not-len")
(add-code
  '((function main [
      (1:integer <- sizeof integer-point-pair:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (is memory*.1 2)
  (prn "F - 'sizeof' is different from number of elems"))

; Regardless of a type's length, you can move it around just like a primitive.

(reset)
(new-trace "copy-record")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:boolean <- copy nil:literal)
      (4:boolean <- copy t:literal)
      (3:integer-boolean-pair <- copy 1:integer-boolean-pair)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 34  2 nil  3 34  4 nil))
  (prn "F - ops can operate on records spanning multiple locations"))

(reset)
(new-trace "copy-record2")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:integer <- copy 35:literal)
      (3:integer <- copy 36:literal)
      (4:integer <- copy 0:literal)
      (5:integer <- copy 0:literal)
      (6:integer <- copy 0:literal)
      (4:integer-point-pair <- copy 1:integer-point-pair)
     ])))
;? (= dump-trace* (obj whitelist '("run" "sizeof")))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 34  2 35  3 36
                         ; result
                         4 34  5 35  6 36))
  (prn "F - ops can operate on records with fields spanning multiple locations"))

)  ; section 20

(section 100

; A special kind of record is the 'tagged type'. It lets us represent
; dynamically typed values, which save type information in memory rather than
; in the code to use them. This will let us do things like create heterogenous
; lists containing both integers and strings. Tagged values admit two
; operations:
;
;   'save-type' - turns a regular value into a tagged-value of the appropriate type
;   'maybe-coerce' - turns a tagged value into a regular value if the type matches
;
; The payload of a tagged value must occupy just one location. Save pointers
; to records.

(reset)
(new-trace "tagged-value")
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1")))
(add-code
  '((function main [
      (1:type <- copy integer:literal)
      (2:integer <- copy 34:literal)
      (3:integer 4:boolean <- maybe-coerce 1:tagged-value integer:literal)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn completed-routines*)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
;? (prn memory*)
(when (or (~is memory*.3 34)
          (~is memory*.4 t))
  (prn "F - 'maybe-coerce' copies value only if type tag matches"))
;? (quit)

(reset)
(new-trace "tagged-value-2")
;? (set dump-trace*)
(add-code
  '((function main [
      (1:type <- copy integer-address:literal)
      (2:integer <- copy 34:literal)
      (3:boolean 4:boolean <- maybe-coerce 1:tagged-value boolean:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (or (~is memory*.3 0)
          (~is memory*.4 nil))
  (prn "F - 'maybe-coerce' doesn't copy value when type tag doesn't match"))

(reset)
(new-trace "save-type")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:tagged-value <- save-type 1:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj  1 34  2 'integer  3 34))
  (prn "F - 'save-type' saves the type of a value at runtime, turning it into a tagged-value"))

(reset)
(new-trace "init-tagged-value")
(add-code
  '((function main [
      (1:integer <- copy 34:literal)
      (2:tagged-value-address <- init-tagged-value integer:literal 1:integer)
      (3:integer 4:boolean <- maybe-coerce 2:tagged-value-address/deref integer:literal)
     ])))
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1" "sizeof")))
(run 'main)
;? (prn memory*)
(when (or (~is memory*.3 34)
          (~is memory*.4 t))
  (prn "F - 'init-tagged-value' is the converse of 'maybe-coerce'"))
;? (quit)

; Now that we can package values together with their types, we can construct a
; dynamically typed list.

(reset)
(new-trace "list")
;? (set dump-trace*)
(add-code
  '((function main [
      ; 1 points at first node: tagged-value (int 34)
      (1:list-address <- new list:literal)
      (2:tagged-value-address <- list-value-address 1:list-address)
      (3:type-address <- get-address 2:tagged-value-address/deref type:offset)
      (3:type-address/deref <- copy integer:literal)
      (4:location <- get-address 2:tagged-value-address/deref payload:offset)
      (4:location/deref <- copy 34:literal)
      (5:list-address-address <- get-address 1:list-address/deref cdr:offset)
      (5:list-address-address/deref <- new list:literal)
      ; 6 points at second node: tagged-value (boolean t)
      (6:list-address <- copy 5:list-address-address/deref)
      (7:tagged-value-address <- list-value-address 6:list-address)
      (8:type-address <- get-address 7:tagged-value-address/deref type:offset)
      (8:type-address/deref <- copy boolean:literal)
      (9:location <- get-address 7:tagged-value-address/deref payload:offset)
      (9:location/deref <- copy t:literal)
      (10:list-address <- get 6:list-address/deref 1:offset)
     ])))
(let routine make-routine!main
  (enq routine running-routines*)
  (let first rep.routine!alloc
;?     (= dump-trace* (obj whitelist '("run")))
;?     (set dump-trace*)
    (run)
;?     (prn memory*)
    (each routine completed-routines*
      (aif rep.routine!error (prn "error - " it)))
    (when (or (~all first (map memory* '(1 2 3)))
              (~is memory*.first  'integer)
              (~is memory*.4 (+ first 1))
              (~is (memory* (+ first 1))  34)
              (~is memory*.5 (+ first 2))
              (let second memory*.6
                (or
                  (~is (memory* (+ first 2)) second)
                  (~all second (map memory* '(6 7 8)))
                  (~is memory*.second 'boolean)
                  (~is memory*.9 (+ second 1))
                  (~is (memory* (+ second 1)) t)
                  (~is memory*.10 nil))))
      (prn "F - lists can contain elements of different types"))))
(run-code test2
  (10:list-address <- list-next 1:list-address))
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~is memory*.10 memory*.6)
  (prn "F - 'list-next can move a list pointer to the next node"))
;? (quit)

; 'init-list' takes a variable number of args and constructs a list containing
; them. Just integers for now.

(reset)
(new-trace "init-list")
(add-code
  '((function main [
      (1:integer <- init-list 3:literal 4:literal 5:literal)
     ])))
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1" "sizeof")))
(run 'main)
;? (prn memory*)
(let first memory*.1
;?   (prn first)
  (when (or (~is memory*.first  'integer)
            (~is (memory* (+ first 1))  3)
            (let second (memory* (+ first 2))
;?               (prn second)
              (or (~is memory*.second 'integer)
                  (~is (memory* (+ second 1)) 4)
                  (let third (memory* (+ second 2))
;?                     (prn third)
                    (or (~is memory*.third 'integer)
                        (~is (memory* (+ third 1)) 5)
                        (~is (memory* (+ third 2) nil)))))))
    (prn "F - 'init-list' can construct a list of integers")))

)  ; section 100

(section 20

;; Functions
;
; Just like the table of types is centralized, functions are conceptualized as
; a centralized table of operations just like the "primitives" we've seen so
; far. If you create a function you can call it like any other op.

(reset)
(new-trace "new-fn")
(add-code
  '((function test1 [
      (3:integer <- add 1:integer 2:integer)
     ])
    (function main [
      (1:integer <- copy 1:literal)
      (2:integer <- copy 3:literal)
      (test1)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 1  2 3  3 4))
  (prn "F - calling a user-defined function runs its instructions"))
;? (quit)

(reset)
(new-trace "new-fn-once")
(add-code
  '((function test1 [
      (1:integer <- copy 1:literal)
     ])
    (function main [
      (test1)
     ])))
;? (= dump-trace* (obj whitelist '("run")))
(run 'main)
(when (~is 2 curr-cycle*)
  (prn "F - calling a user-defined function runs its instructions exactly once " curr-cycle*))
;? (quit)

; User-defined functions communicate with their callers through two
; primitives:
;
;   'arg' - to access inputs
;   'reply' - to return outputs

(reset)
(new-trace "new-fn-reply")
(add-code
  '((function test1 [
      (3:integer <- add 1:integer 2:integer)
      (reply)
      (4:integer <- copy 34:literal)
     ])
    (function main [
      (1:integer <- copy 1:literal)
      (2:integer <- copy 3:literal)
      (test1)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 1  2 3  3 4))
  (prn "F - 'reply' stops executing the current function"))
;? (quit)

(reset)
(new-trace "new-fn-reply-nested")
(add-code
  '((function test1 [
      (3:integer <- test2)
     ])
    (function test2 [
      (reply 2:integer)
     ])
    (function main [
      (2:integer <- copy 34:literal)
      (test1)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 2 34  3 34))
  (prn "F - 'reply' stops executing any callers as necessary"))
;? (quit)

(reset)
(new-trace "new-fn-reply-once")
(add-code
  '((function test1 [
      (3:integer <- add 1:integer 2:integer)
      (reply)
      (4:integer <- copy 34:literal)
     ])
    (function main [
      (1:integer <- copy 1:literal)
      (2:integer <- copy 3:literal)
      (test1)
     ])))
;? (= dump-trace* (obj whitelist '("run")))
(run 'main)
(when (~is 5 curr-cycle*)
  (prn "F - 'reply' executes instructions exactly once " curr-cycle*))
;? (quit)

(reset)
(new-trace "reply-increments-caller-pc")
(add-code
  '((function callee [
      (reply)
     ])
    (function caller [
      (1:integer <- copy 0:literal)
      (2:integer <- copy 0:literal)
     ])))
(freeze function*)
(= routine* (make-routine 'caller))
(assert (is 0 pc.routine*))
(push-stack routine* 'callee)  ; pretend call was at first instruction of caller
(run-for-time-slice 1)
(when (~is 1 pc.routine*)
  (prn "F - 'reply' increments pc in caller (to move past calling instruction)"))

(reset)
(new-trace "new-fn-arg-sequential")
(add-code
  '((function test1 [
      (4:integer <- next-input)
      (5:integer <- next-input)
      (3:integer <- add 4:integer 5:integer)
      (reply)
      (4:integer <- copy 34:literal)
     ])
    (function main [
      (1:integer <- copy 1:literal)
      (2:integer <- copy 3:literal)
      (test1 1:integer 2:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 1  2 3  3 4
                         ; test1's temporaries
                         4 1  5 3))
  (prn "F - 'arg' accesses in order the operands of the most recent function call (the caller)"))
;? (quit)

(reset)
(new-trace "new-fn-arg-random-access")
;? (set dump-trace*)
(add-code
  '((function test1 [
      (5:integer <- input 1:literal)
      (4:integer <- input 0:literal)
      (3:integer <- add 4:integer 5:integer)
      (reply)
      (4:integer <- copy 34:literal)  ; should never run
     ])
    (function main [
      (1:integer <- copy 1:literal)
      (2:integer <- copy 3:literal)
      (test1 1:integer 2:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 1  2 3  3 4
                         ; test's temporaries
                         4 1  5 3))
  (prn "F - 'arg' with index can access function call arguments out of order"))
;? (quit)

(reset)
(new-trace "new-fn-arg-random-then-sequential")
;? (set dump-trace*)
(add-code
  '((function test1 [
      (_ <- input 1:literal)
      (1:integer <- next-input)  ; takes next arg after index 1
     ])  ; should never run
    (function main [
      (test1 1:literal 2:literal 3:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 3))
  (prn "F - 'arg' with index resets index for later calls"))
;? (quit)

(reset)
(new-trace "new-fn-arg-status")
(add-code
  '((function test1 [
      (4:integer 5:boolean <- next-input)
     ])
    (function main [
      (test1 1:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 4 1  5 t))
  (prn "F - 'arg' sets a second oarg when arg exists"))
;? (quit)

(reset)
(new-trace "new-fn-arg-missing")
(add-code
  '((function test1 [
      (4:integer <- next-input)
      (5:integer <- next-input)
     ])
    (function main [
      (test1 1:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 4 1))
  (prn "F - missing 'arg' doesn't cause error"))
;? (quit)

(reset)
(new-trace "new-fn-arg-missing-2")
(add-code
  '((function test1 [
      (4:integer <- next-input)
      (5:integer 6:boolean <- next-input)
     ])
    (function main [
      (test1 1:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 4 1  6 nil))
  (prn "F - missing 'arg' wipes second oarg when provided"))
;? (quit)

(reset)
(new-trace "new-fn-arg-missing-3")
(add-code
  '((function test1 [
      (4:integer <- next-input)
      (5:integer <- copy 34:literal)
      (5:integer 6:boolean <- next-input)
    ])
    (function main [
      (test1 1:literal)
    ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 4 1  6 nil))
  (prn "F - missing 'arg' consistently wipes its oarg"))
;? (quit)

(reset)
(new-trace "new-fn-arg-missing-4")
(add-code
  '((function test1 [
      ; if given two args, adds them; if given one arg, increments
      (4:integer <- next-input)
      (5:integer 6:boolean <- next-input)
      { begin
        (break-if 6:boolean)
        (5:integer <- copy 1:literal)
      }
      (7:integer <- add 4:integer 5:integer)
     ])
    (function main [
      (test1 34:literal)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 4 34  5 1  6 nil  7 35))
  (prn "F - function with optional second arg"))
;? (quit)

(reset)
(new-trace "new-fn-arg-by-value")
(add-code
  '((function test1 [
      (1:integer <- copy 0:literal)  ; overwrite caller memory
      (2:integer <- next-input)
     ])  ; arg not clobbered
    (function main [
      (1:integer <- copy 34:literal)
      (test1 1:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 0  2 34))
  (prn "F - 'arg' passes by value"))

(reset)
(new-trace "arg-record")
(add-code
  '((function test1 [
      (4:integer-boolean-pair <- next-input)
     ])
    (function main [
      (1:integer <- copy 34:literal)
      (2:boolean <- copy nil:literal)
      (test1 1:integer-boolean-pair)
     ])))
(run 'main)
(when (~iso memory* (obj 1 34  2 nil  4 34  5 nil))
  (prn "F - 'arg' can copy records spanning multiple locations"))

(reset)
(new-trace "arg-record-indirect")
;? (set dump-trace*)
(add-code
  '((function test1 [
      (4:integer-boolean-pair <- next-input)
     ])
    (function main [
      (1:integer <- copy 34:literal)
      (2:boolean <- copy nil:literal)
      (3:integer-boolean-pair-address <- copy 1:literal)
      (test1 3:integer-boolean-pair-address/deref)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 34  2 nil  3 1  4 34  5 nil))
  (prn "F - 'arg' can copy records spanning multiple locations in indirect mode"))

(reset)
(new-trace "new-fn-reply-oarg")
(add-code
  '((function test1 [
      (4:integer <- next-input)
      (5:integer <- next-input)
      (6:integer <- add 4:integer 5:integer)
      (reply 6:integer)
      (4:integer <- copy 34:literal)
     ])
    (function main [
      (1:integer <- copy 1:literal)
      (2:integer <- copy 3:literal)
      (3:integer <- test1 1:integer 2:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 1  2 3  3 4
                         ; test1's temporaries
                         4 1  5 3  6 4))
  (prn "F - 'reply' can take aguments that are returned, or written back into output args of caller"))

(reset)
(new-trace "new-fn-reply-oarg-multiple")
(add-code
  '((function test1 [
      (4:integer <- next-input)
      (5:integer <- next-input)
      (6:integer <- add 4:integer 5:integer)
      (reply 6:integer 5:integer)
      (4:integer <- copy 34:literal)
     ])
    (function main [
      (1:integer <- copy 1:literal)
      (2:integer <- copy 3:literal)
      (3:integer 7:integer <- test1 1:integer 2:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 1  2 3  3 4    7 3
                         ; test1's temporaries
                         4 1  5 3  6 4))
  (prn "F - 'reply' permits a function to return multiple values at once"))

; 'prepare-reply' is useful for doing cleanup before exiting a function
(reset)
(new-trace "new-fn-prepare-reply")
(add-code
  '((function test1 [
      (4:integer <- next-input)
      (5:integer <- next-input)
      (6:integer <- add 4:integer 5:integer)
      (prepare-reply 6:integer 5:integer)
      (reply)
      (4:integer <- copy 34:literal)
     ])
    (function main [
      (1:integer <- copy 1:literal)
      (2:integer <- copy 3:literal)
      (3:integer 7:integer <- test1 1:integer 2:integer)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory* (obj 1 1  2 3  3 4    7 3
                         ; test1's temporaries
                         4 1  5 3  6 4))
  (prn "F - without args, 'reply' returns values from previous 'prepare-reply'."))

; When you have arguments that are both read from and written to, include them
; redundantly in both ingredients and results. That'll help tools track what
; changed.

; To enforce that the result and ingredient must always match, use the
; 'same-as-arg' property. Results with 'same-as-arg' properties should only be
; copied to a caller output arg identical to the specified caller arg.
(reset)
(new-trace "new-fn-same-as-arg")
(add-code
  '((function test1 [
      ; increment the contents of an address
      (default-space:space-address <- new space:literal 2:literal)
      (x:integer-address <- next-input)
      (x:integer-address/deref <- add x:integer-address/deref 1:literal)
      (reply x:integer-address/same-as-arg:0)
    ])
    (function main [
      (2:integer-address <- new integer:literal)
      (2:integer-address/deref <- copy 0:literal)
      (3:integer-address <- test1 2:integer-address)
    ])))
(run 'main)
(let routine (car completed-routines*)
;?   (prn rep.routine!error) ;? 1
  (when (no rep.routine!error)
    (prn "F - 'same-as-arg' results must be identical to a given input")))
;? (quit) ;? 2

)  ; section 20

(section 11

;; Structured programming
;
; Our jump operators are quite inconvenient to use, so mu provides a
; lightweight tool called 'convert-braces' to work in a slightly more
; convenient format with nested braces:
;
;   {
;     some instructions
;     {
;       more instructions
;     }
;   }
;
; Braces are like labels in assembly language, they require no special
; parsing. The operations 'loop' and 'break' jump to just after the enclosing
; '{' and '}' respectively.
;
; Conditional and unconditional 'loop' and 'break' should give us 80% of the
; benefits of the control-flow primitives we're used to in other languages,
; like 'if', 'while', 'for', etc.
;
; Compare 'unquoted blocks' using {} with 'quoted blocks' using [] that we've
; gotten used to seeing. Quoted blocks are used by top-level instructions to
; provide code without running it.

(reset)
(new-trace "convert-braces")
(= traces* (queue))
;? (= dump-trace* (obj whitelist '("c{0" "c{1")))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                (((2 integer)) <- ((copy)) ((0 literal)))
                (((3 integer)) <- ((copy)) ((0 literal)))
                { begin  ; 'begin' is just a hack because racket turns braces into parens
                  (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
                  (((break-if)) ((4 boolean)))
                  (((5 integer)) <- ((copy)) ((0 literal)))
                }
                (((reply)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
              (((jump-if)) ((4 boolean)) ((1 offset)))
              (((5 integer)) <- ((copy)) ((0 literal)))
              (((reply)))))
  (prn "F - convert-braces replaces break-if with a jump-if to after the next close-brace"))
;? (quit)

(reset)
(new-trace "convert-braces-empty-block")
(= traces* (queue))
;? (= dump-trace* (obj whitelist '("c{0" "c{1")))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                (((2 integer)) <- ((copy)) ((0 literal)))
                (((3 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((break)))
                }
                (((reply)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((0 offset)))
              (((reply)))))
  (prn "F - convert-braces works for degenerate blocks"))
;? (quit)

(reset)
(new-trace "convert-braces-nested-break")
(= traces* (queue))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                (((2 integer)) <- ((copy)) ((0 literal)))
                (((3 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
                  (((break-if)) ((4 boolean)))
                  { begin
                    (((5 integer)) <- ((copy)) ((0 literal)))
                  }
                }
                (((reply)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
              (((jump-if)) ((4 boolean)) ((1 offset)))
              (((5 integer)) <- ((copy)) ((0 literal)))
              (((reply)))))
  (prn "F - convert-braces balances braces when converting break"))

(reset)
(new-trace "convert-braces-repeated-jump")
(= traces* (queue))
;? (= dump-trace* (obj whitelist '("c{0" "c{1")))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((break)))
                  (((2 integer)) <- ((copy)) ((0 literal)))
                }
                { begin
                  (((break)))
                  (((3 integer)) <- ((copy)) ((0 literal)))
                }
                (((4 integer)) <- ((copy)) ((0 literal)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((1 offset)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((1 offset)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((4 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-braces handles jumps on jumps"))
;? (quit)

(reset)
(new-trace "convert-braces-nested-loop")
(= traces* (queue))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                (((2 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((3 integer)) <- ((copy)) ((0 literal)))
                  { begin
                    (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
                  }
                  (((loop-if)) ((4 boolean)))
                  (((5 integer)) <- ((copy)) ((0 literal)))
                }
                (((reply)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
              (((jump-if)) ((4 boolean)) ((-3 offset)))
              (((5 integer)) <- ((copy)) ((0 literal)))
              (((reply)))))
  (prn "F - convert-braces balances braces when converting 'loop'"))

(reset)
(new-trace "convert-braces-label")
(= traces* (queue))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                foo
                (((2 integer)) <- ((copy)) ((0 literal)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              foo
              (((2 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-braces skips past labels"))
;? (quit)

(reset)
(new-trace "convert-braces-label-increments-offset")
(= traces* (queue))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((break)))
                  foo
                }
                (((2 integer)) <- ((copy)) ((0 literal)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((1 offset)))
              foo
              (((2 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-braces treats labels as instructions"))
;? (quit)

(reset)
(new-trace "convert-braces-label-increments-offset2")
(= traces* (queue))
;? (= dump-trace* (obj whitelist '("c{0" "c{1")))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((break)))
                  foo
                }
                (((2 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((break)))
                  (((3 integer)) <- ((copy)) ((0 literal)))
                }
                (((4 integer)) <- ((copy)) ((0 literal)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((1 offset)))
              foo
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((1 offset)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((4 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-braces treats labels as instructions - 2"))
;? (quit)

(reset)
(new-trace "break-multiple")
(= traces* (queue))
;? (= dump-trace* (obj whitelist '("-")))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                { begin
                  { begin
                    (((break)) ((2 blocks)))
                  }
                  (((2 integer)) <- ((copy)) ((0 literal)))
                  (((3 integer)) <- ((copy)) ((0 literal)))
                  (((4 integer)) <- ((copy)) ((0 literal)))
                  (((5 integer)) <- ((copy)) ((0 literal)))
                }))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((4 offset)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((4 integer)) <- ((copy)) ((0 literal)))
              (((5 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - 'break' can take an extra arg with number of nested blocks to exit"))
;? (quit)

(reset)
(new-trace "loop")
;? (set dump-trace*)
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                (((2 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((3 integer)) <- ((copy)) ((0 literal)))
                  (((loop)))
                }))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((-2 offset)))))
  (prn "F - 'loop' jumps to start of containing block"))
;? (quit)

; todo: fuzz-test invariant: convert-braces offsets should be robust to any
; number of inner blocks inside but not around the loop block.

(reset)
(new-trace "loop-nested")
;? (set dump-trace*)
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                (((2 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((3 integer)) <- ((copy)) ((0 literal)))
                  { begin
                    (((4 integer)) <- ((copy)) ((0 literal)))
                  }
                  (((loop)))
                }))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((4 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((-3 offset)))))
  (prn "F - 'loop' correctly jumps back past nested braces"))

(reset)
(new-trace "loop-multiple")
(= traces* (queue))
;? (= dump-trace* (obj whitelist '("-")))
(when (~iso (convert-braces
              '((((1 integer)) <- ((copy)) ((0 literal)))
                { begin
                  (((2 integer)) <- ((copy)) ((0 literal)))
                  (((3 integer)) <- ((copy)) ((0 literal)))
                  { begin
                    (((loop)) ((2 blocks)))
                  }
                }))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))
              (((jump)) ((-3 offset)))))
  (prn "F - 'loop' can take an extra arg with number of nested blocks to exit"))
;? (quit)

(reset)
(new-trace "convert-labels")
(= traces* (queue))
(when (~iso (convert-labels
              '(loop
                (((jump)) ((loop offset)))))
            '(loop
              (((jump)) ((-2 offset)))))
  (prn "F - 'convert-labels' rewrites jumps to labels"))

;; Variables
;
; A big convenience high-level languages provide is the ability to name memory
; locations. In mu, a lightweight tool called 'convert-names' provides this
; convenience.

(reset)
(new-trace "convert-names")
(= traces* (queue))
;? (set dump-trace*)
(when (~iso (convert-names
              '((((x integer)) <- ((copy)) ((0 literal)))
                (((y integer)) <- ((copy)) ((0 literal)))
                (((z integer)) <- ((copy)) ((0 literal)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-names renames symbolic names to integer locations"))

(reset)
(new-trace "convert-names-compound")
(= traces* (queue))
(when (~iso (convert-names
              ; copying 0 into pair is meaningless; just for testing
              '((((x integer-boolean-pair)) <- ((copy)) ((0 literal)))
                (((y integer)) <- ((copy)) ((0 literal)))))
            '((((1 integer-boolean-pair)) <- ((copy)) ((0 literal)))
              (((3 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-names increments integer locations by the size of the type of the previous var"))

(reset)
(new-trace "convert-names-nil")
(= traces* (queue))
;? (set dump-trace*)
(when (~iso (convert-names
              '((((x integer)) <- ((copy)) ((0 literal)))
                (((y integer)) <- ((copy)) ((0 literal)))
                ; nil location is meaningless; just for testing
                (((nil integer)) <- ((copy)) ((0 literal)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((nil integer)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-names never renames nil"))

(reset)
(new-trace "convert-names-string")
;? (set dump-trace*)
(when (~iso (convert-names
              '((((1 integer-address)) <- ((new)) "foo")))
            '((((1 integer-address)) <- ((new)) "foo")))
  (prn "convert-names passes through raw strings (just a convenience arg for 'new')"))

(reset)
(new-trace "convert-names-raw")
(= traces* (queue))
(when (~iso (convert-names
              '((((x integer)) <- ((copy)) ((0 literal)))
                (((y integer) (raw)) <- ((copy)) ((0 literal)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((y integer) (raw)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-names never renames raw operands"))

(reset)
(new-trace "convert-names-literal")
(= traces* (queue))
(when (~iso (convert-names
              ; meaningless; just for testing
              '((((x literal)) <- ((copy)) ((0 literal)))))
            '((((x literal)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-names never renames literals"))

(reset)
(new-trace "convert-names-literal-2")
(= traces* (queue))
(when (~iso (convert-names
              '((((x boolean)) <- ((copy)) ((x literal)))))
            '((((1 boolean)) <- ((copy)) ((x literal)))))
  (prn "F - convert-names never renames literals, even when the name matches a variable"))

; kludgy support for 'fork' below
(reset)
(new-trace "convert-names-functions")
(= traces* (queue))
(when (~iso (convert-names
              '((((x integer)) <- ((copy)) ((0 literal)))
                (((y integer)) <- ((copy)) ((0 literal)))
                ; meaningless; just for testing
                (((z fn)) <- ((copy)) ((0 literal)))))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              (((2 integer)) <- ((copy)) ((0 literal)))
              (((z fn)) <- ((copy)) ((0 literal)))))
  (prn "F - convert-names never renames fns"))

(reset)
(new-trace "convert-names-record-fields")
(= traces* (queue))
;? (= dump-trace* (obj whitelist '("cn0")))
(when (~iso (convert-names
              '((((x integer)) <- ((get)) ((34 integer-boolean-pair)) ((bool offset)))))
            '((((1 integer)) <- ((get)) ((34 integer-boolean-pair)) ((1 offset)))))
  (prn "F - convert-names replaces record field offsets"))

(reset)
(new-trace "convert-names-record-fields-ambiguous")
(= traces* (queue))
(when (errsafe (convert-names
                 '((((bool boolean)) <- ((copy)) ((t literal)))
                   (((x integer)) <- ((get)) ((34 integer-boolean-pair)) ((bool offset))))))
  (prn "F - convert-names doesn't allow offsets and variables with the same name in a function"))

(reset)
(new-trace "convert-names-record-fields-ambiguous-2")
(= traces* (queue))
(when (errsafe (convert-names
                 '((((x integer)) <- ((get)) ((34 integer-boolean-pair)) ((bool offset)))
                   (((bool boolean)) <- ((copy)) ((t literal))))))
  (prn "F - convert-names doesn't allow offsets and variables with the same name in a function - 2"))

(reset)
(new-trace "convert-names-record-fields-indirect")
(= traces* (queue))
;? (= dump-trace* (obj whitelist '("cn0")))
(when (~iso (convert-names
              '((((x integer)) <- ((get)) ((34 integer-boolean-pair-address) (deref)) ((bool offset)))))
            '((((1 integer)) <- ((get)) ((34 integer-boolean-pair-address) (deref)) ((1 offset)))))
  (prn "F - convert-names replaces field offsets for record addresses"))
;? (quit)

(reset)
(new-trace "convert-names-record-fields-multiple")
(= traces* (queue))
(when (~iso (convert-names
              '((((2 boolean)) <- ((get)) ((1 integer-boolean-pair)) ((bool offset)))
                (((3 boolean)) <- ((get)) ((1 integer-boolean-pair)) ((bool offset)))))
            '((((2 boolean)) <- ((get)) ((1 integer-boolean-pair)) ((1 offset)))
              (((3 boolean)) <- ((get)) ((1 integer-boolean-pair)) ((1 offset)))))
  (prn "F - convert-names replaces field offsets with multiple mentions"))
;? (quit)

(reset)
(new-trace "convert-names-label")
(= traces* (queue))
(when (~iso (convert-names
              '((((1 integer)) <- ((copy)) ((0 literal)))
                foo))
            '((((1 integer)) <- ((copy)) ((0 literal)))
              foo))
  (prn "F - convert-names skips past labels"))
;? (quit)

)  ; section 11

(section 20

; A rudimentary memory allocator. Eventually we want to write this in mu.
;
; No deallocation yet; let's see how much code we can build in mu before we
; feel the need for it.

(reset)
(new-trace "new-primitive")
(add-code
  '((function main [
      (1:integer-address <- new integer:literal)
     ])))
(let routine make-routine!main
  (enq routine running-routines*)
  (let before rep.routine!alloc
;?     (set dump-trace*)
    (run)
;?     (prn memory*)
    (when (~iso memory*.1 before)
      (prn "F - 'new' returns current high-water mark"))
    (when (~iso rep.routine!alloc (+ before 1))
      (prn "F - 'new' on primitive types increments high-water mark by their size"))))
;? (quit)

(reset)
(new-trace "new-array-literal")
(add-code
  '((function main [
      (1:type-array-address <- new type-array:literal 5:literal)
     ])))
(let routine make-routine!main
  (enq routine running-routines*)
  (let before rep.routine!alloc
    (run)
;?     (prn memory*)
    (when (~iso memory*.1 before)
      (prn "F - 'new' on array with literal size returns current high-water mark"))
    (when (~iso rep.routine!alloc (+ before 6))
      (prn "F - 'new' on primitive arrays increments high-water mark by their size"))))

(reset)
(new-trace "new-array-direct")
(add-code
  '((function main [
      (1:integer <- copy 5:literal)
      (2:type-array-address <- new type-array:literal 1:integer)
     ])))
(let routine make-routine!main
  (enq routine running-routines*)
  (let before rep.routine!alloc
    (run)
;?     (prn memory*)
    (when (~iso memory*.2 before)
      (prn "F - 'new' on array with variable size returns current high-water mark"))
    (when (~iso rep.routine!alloc (+ before 6))
      (prn "F - 'new' on primitive arrays increments high-water mark by their (variable) size"))))

(reset)
(new-trace "new-allocation-chunk")
(add-code
  '((function main [
      (1:integer-address <- new integer:literal)
     ])))
; start allocating from address 30, in chunks of 10 locations each
(= Memory-allocated-until 30
   Allocation-chunk 10)
(let routine make-routine!main
  (assert:is rep.routine!alloc 30)
  (assert:is rep.routine!alloc-max 40)
  ; pretend the current chunk is full
  (= rep.routine!alloc 40)
  (enq routine running-routines*)
  (run)
  (each routine completed-routines*
    (aif rep.routine!error (prn "error - " it)))
  (when (~is rep.routine!alloc 41)
    (prn "F - 'new' can allocate past initial routine memory"))
  (when (~is rep.routine!alloc-max 50)
    (prn "F - 'new' updates upper bound for routine memory @rep.routine!alloc-max")))

(reset)
(new-trace "new-skip")
(add-code
  '((function main [
      (1:integer-boolean-pair-address <- new integer-boolean-pair:literal)
     ])))
; start allocating from address 30, in chunks of 10 locations each
(= Memory-allocated-until 30
   Allocation-chunk 10)
(let routine make-routine!main
  (assert:is rep.routine!alloc 30)
  (assert:is rep.routine!alloc-max 40)
  ; pretend the current chunk has just one location left
  (= rep.routine!alloc 39)
  (enq routine running-routines*)
  ; request 2 locations
  (run)
  (each routine completed-routines*
    (aif rep.routine!error (prn "error - " it)))
  (when (or (~is memory*.1 40)
            (~is rep.routine!alloc 42)
            (~is rep.routine!alloc-max 50)
            (~is Memory-allocated-until 50))
    (prn "F - 'new' skips past current chunk if insufficient space")))

(reset)
(new-trace "new-skip-noncontiguous")
(add-code
  '((function main [
      (1:integer-boolean-pair-address <- new integer-boolean-pair:literal)
     ])))
; start allocating from address 30, in chunks of 10 locations each
(= Memory-allocated-until 30
   Allocation-chunk 10)
(let routine make-routine!main
  (assert:is rep.routine!alloc 30)
  (assert:is rep.routine!alloc-max 40)
  ; pretend the current chunk has just one location left
  (= rep.routine!alloc 39)
  ; pretend we allocated more memory since we created the routine
  (= Memory-allocated-until 90)
  (enq routine running-routines*)
  ; request 2 locations
  (run)
  (each routine completed-routines*
    (aif rep.routine!error (prn "error - " it)))
  (when (or (~is memory*.1 90)
            (~is rep.routine!alloc 92)
            (~is rep.routine!alloc-max 100)
            (~is Memory-allocated-until 100))
    (prn "F - 'new' allocates a new chunk if insufficient space")))

(reset)
(new-trace "new-array-skip-noncontiguous")
(add-code
  '((function main [
      (1:integer-array-address <- new integer-array:literal 4:literal)
     ])))
; start allocating from address 30, in chunks of 10 locations each
(= Memory-allocated-until 30
   Allocation-chunk 10)
(let routine make-routine!main
  (assert:is rep.routine!alloc 30)
  (assert:is rep.routine!alloc-max 40)
  ; pretend the current chunk has just one location left
  (= rep.routine!alloc 39)
  ; pretend we allocated more memory since we created the routine
  (= Memory-allocated-until 90)
  (enq routine running-routines*)
  ; request 4 locations
  (run)
  (each routine completed-routines*
    (aif rep.routine!error (prn "error - " it)))
;?   (prn memory*.1) ;? 1
;?   (prn rep.routine) ;? 1
;?   (prn Memory-allocated-until) ;? 1
  (when (or (~is memory*.1 90)
            (~is rep.routine!alloc 95)
            (~is rep.routine!alloc-max 100)
            (~is Memory-allocated-until 100))
    (prn "F - 'new-array' allocates a new chunk if insufficient space")))

;? (quit) ;? 1

; Even though our memory locations can now have names, the names are all
; globals, accessible from any function. To isolate functions from their
; callers we need local variables, and mu provides them using a special
; variable called default-space. When you initialize such a variable (likely
; with a call to our just-defined memory allocator) mu interprets memory
; locations as offsets from its value. If default-space is set to 1000, for
; example, reads and writes to memory location 1 will really go to 1001.
;
; 'default-space' is itself hard-coded to be function-local; it's nil in a new
; function, and it's restored when functions return to their callers. But the
; actual space allocation is independent. So you can define closures, or do
; even more funky things like share locals between two coroutines.

(reset)
(new-trace "set-default-space")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 2:literal)
      (1:integer <- copy 23:literal)
     ])))
(let routine make-routine!main
  (enq routine running-routines*)
  (let before rep.routine!alloc
;?     (set dump-trace*)
    (run)
;?     (prn memory*)
    (when (~and (~is 23 memory*.1)
                (is 23 (memory* (+ before 2))))
      (prn "F - default-space implicitly modifies variable locations"))))
;? (quit)

(reset)
(new-trace "set-default-space-skips-offset")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 2:literal)
      (1:integer <- copy 23:offset)
     ])))
(let routine make-routine!main
  (enq routine running-routines*)
  (let before rep.routine!alloc
;?     (set dump-trace*)
    (run)
;?     (prn memory*)
    (when (~and (~is 23 memory*.1)
                (is 23 (memory* (+ before 2))))
      (prn "F - default-space skips 'offset' types just like literals"))))

(reset)
(new-trace "default-space-bounds-check")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 2:literal)
      (2:integer <- copy 23:literal)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(let routine (car completed-routines*)
  (when (no rep.routine!error)
    (prn "F - default-space checks bounds")))

(reset)
(new-trace "default-space-and-get-indirect")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 5:literal)
      (1:integer-boolean-pair-address <- new integer-boolean-pair:literal)
      (2:integer-address <- get-address 1:integer-boolean-pair-address/deref 0:offset)
      (2:integer-address/deref <- copy 34:literal)
      (3:integer/raw <- get 1:integer-boolean-pair-address/deref 0:offset)
     ])))
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1")))
(run 'main)
;? (prn memory*)
;? (prn completed-routines*)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~is 34 memory*.3)
  (prn "F - indirect 'get' works in the presence of default-space"))
;? (quit)

(reset)
(new-trace "default-space-and-index-indirect")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 5:literal)
      (1:integer-array-address <- new integer-array:literal 4:literal)
      (2:integer-address <- index-address 1:integer-array-address/deref 2:offset)
      (2:integer-address/deref <- copy 34:literal)
      (3:integer/raw <- index 1:integer-array-address/deref 2:offset)
     ])))
;? (= dump-trace* (obj whitelist '("run" "array-info")))
(run 'main)
;? (prn memory*)
;? (prn completed-routines*)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~is 34 memory*.3)
  (prn "F - indirect 'index' works in the presence of default-space"))
;? (quit)

(reset)
(new-trace "convert-names-default-space")
(= traces* (queue))
(when (~iso (convert-names
              '((((x integer)) <- ((copy)) ((4 literal)))
                (((y integer)) <- ((copy)) ((2 literal)))
                ; unsafe in general; don't write random values to 'default-space'
                (((default-space integer)) <- ((add)) ((x integer)) ((y integer)))))
            '((((1 integer)) <- ((copy)) ((4 literal)))
              (((2 integer)) <- ((copy)) ((2 literal)))
              (((default-space integer)) <- ((add)) ((1 integer)) ((2 integer)))))
  (prn "F - convert-names never renames default-space"))

(reset)
(new-trace "suppress-default-space")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 2:literal)
      (1:integer/raw <- copy 23:literal)
     ])))
(let routine make-routine!main
  (enq routine running-routines*)
  (let before rep.routine!alloc
;?     (set dump-trace*)
    (run)
;?     (prn memory*)
    (when (~and (is 23 memory*.1)
                (~is 23 (memory* (+ before 1))))
      (prn "F - default-space skipped for locations with metadata 'raw'"))))
;? (quit)

(reset)
(new-trace "array-copy-indirect-scoped")
(add-code
  '((function main [
      (10:integer <- copy 30:literal)  ; pretend allocation
      (default-space:space-address <- copy 10:literal)  ; unsafe
      (1:integer <- copy 2:literal)  ; raw location 12
      (2:integer <- copy 23:literal)
      (3:boolean <- copy nil:literal)
      (4:integer <- copy 24:literal)
      (5:boolean <- copy t:literal)
      (6:integer-boolean-pair-array-address <- copy 12:literal)  ; unsafe
      (7:integer-boolean-pair-array <- copy 6:integer-boolean-pair-array-address/deref)
     ])))
;? (set dump-trace*)
;? (= dump-trace* (obj whitelist '("run" "mem" "sizeof")))
(run 'main)
;? (prn memory*)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~iso memory*.18 2)  ; variable 7
  (prn "F - indirect array copy in the presence of 'default-space'"))
;? (quit)

(reset)
(new-trace "len-array-indirect-scoped")
(add-code
  '((function main [
      (10:integer <- copy 30:literal)  ; pretend allocation
      (default-space:space-address <- copy 10:literal)  ; unsafe
      (1:integer <- copy 2:literal)  ; raw location 12
      (2:integer <- copy 23:literal)
      (3:boolean <- copy nil:literal)
      (4:integer <- copy 24:literal)
      (5:boolean <- copy t:literal)
      (6:integer-address <- copy 12:literal)  ; unsafe
      (7:integer <- length 6:integer-boolean-pair-array-address/deref)
     ])))
;? (= dump-trace* (obj whitelist '("run" "addr" "sz" "array-len")))
(run 'main)
;? (prn memory*)
(when (~iso memory*.18 2)
  (prn "F - 'len' accesses length of array address"))
;? (quit)

(reset)
(new-trace "default-space-shared")
(add-code
  '((function init-counter [
      (default-space:space-address <- new space:literal 30:literal)
      (1:integer <- copy 3:literal)  ; initialize to 3
      (reply default-space:space-address)
     ])
    (function increment-counter [
      (default-space:space-address <- next-input)
      (1:integer <- add 1:integer 1:literal)  ; increment
      (reply 1:integer)
     ])
    (function main [
      (1:space-address <- init-counter)
      (2:integer <- increment-counter 1:space-address)
      (3:integer <- increment-counter 1:space-address)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
;? (prn memory*)
(when (or (~is memory*.2 4)
          (~is memory*.3 5))
  (prn "F - multiple calls to a function can share locals"))
;? (quit)

(reset)
(new-trace "default-space-closure")
(add-code
  '((function init-counter [
      (default-space:space-address <- new space:literal 30:literal)
      (1:integer <- copy 3:literal)  ; initialize to 3
      (reply default-space:space-address)
     ])
    (function increment-counter [
      (default-space:space-address <- new space:literal 30:literal)
      (0:space-address <- next-input)  ; share outer space
      (1:integer/space:1 <- add 1:integer/space:1 1:literal)  ; increment
      (1:integer <- copy 34:literal)  ; dummy
      (reply 1:integer/space:1)
     ])
    (function main [
      (1:space-address <- init-counter)
      (2:integer <- increment-counter 1:space-address)
      (3:integer <- increment-counter 1:space-address)
     ])))
;? (set dump-trace*)
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
;? (prn memory*)
(when (or (~is memory*.2 4)
          (~is memory*.3 5))
  (prn "F - closures using /space metadata"))
;? (quit)

(reset)
(new-trace "default-space-closure-with-names")
(add-code
  '((function init-counter [
      (default-space:space-address <- new space:literal 30:literal)
      (x:integer <- copy 23:literal)
      (y:integer <- copy 3:literal)  ; correct copy of y
      (reply default-space:space-address)
     ])
    (function increment-counter [
      (default-space:space-address <- new space:literal 30:literal)
      (0:space-address/names:init-counter <- next-input)  ; outer space must be created by 'init-counter' above
      (y:integer/space:1 <- add y:integer/space:1 1:literal)  ; increment
      (y:integer <- copy 34:literal)  ; dummy
      (reply y:integer/space:1)
     ])
    (function main [
      (1:space-address/names:init-counter <- init-counter)
      (2:integer <- increment-counter 1:space-address/names:init-counter)
      (3:integer <- increment-counter 1:space-address/names:init-counter)
     ])))
;? (set dump-trace*)
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
;? (prn memory*)
(when (or (~is memory*.2 4)
          (~is memory*.3 5))
  (prn "F - /names to name variables in outer spaces"))
;? (quit)

(reset)
(new-trace "default-space-shared-with-names")
(add-code
  '((function f [
      (default-space:space-address <- new space:literal 30:literal)
      (x:integer <- copy 3:literal)
      (y:integer <- copy 4:literal)
      (reply default-space:space-address)
     ])
    (function g [
      (default-space:space-address/names:f <- next-input)
      (y:integer <- add y:integer 1:literal)
      (x:integer <- add x:integer 2:literal)
      (reply x:integer y:integer)
     ])
    (function main [
      (1:space-address <- f)
      (2:integer 3:integer <- g 1:space-address)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (or (~is memory*.2 5)
          (~is memory*.3 5))
  (prn "F - override names for the default space"))

(reset)
(new-trace "default-space-shared-with-extra-names")
(add-code
  '((function f [
      (default-space:space-address <- new space:literal 30:literal)
      (x:integer <- copy 3:literal)
      (y:integer <- copy 4:literal)
      (reply default-space:space-address)
     ])
    (function g [
      (default-space:space-address/names:f <- next-input)
      (y:integer <- add y:integer 1:literal)
      (x:integer <- add x:integer 2:literal)
      (z:integer <- add x:integer y:integer)
      (reply z:integer)
     ])
    (function main [
      (1:space-address <- f)
      (2:integer <- g 1:space-address)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~is memory*.2 10)
  (prn "F - shared spaces can add new names"))

(reset)
(new-trace "default-space-shared-extra-names-dont-overlap-bindings")
(add-code
  '((function f [
      (default-space:space-address <- new space:literal 30:literal)
      (x:integer <- copy 3:literal)
      (y:integer <- copy 4:literal)
      (reply default-space:space-address)
     ])
    (function g [
      (default-space:space-address/names:f <- next-input)
      (y:integer <- add y:integer 1:literal)
      (x:integer <- add x:integer 2:literal)
      (z:integer <- copy 2:literal)
      (reply x:integer y:integer)
     ])
    (function main [
      (1:space-address <- f)
      (2:integer 3:integer <- g 1:space-address)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
;? (prn memory*) ;? 1
(when (or (~is memory*.2 5)
          (~is memory*.3 5))
  (prn "F - new names in shared spaces don't override old ones"))
;? (quit) ;? 1

)  ; section 20

(section 100

;; Dynamic dispatch
;
; Putting it all together, here's how you define generic functions that run
; different code based on the types of their args.

(reset)
(new-trace "dispatch-clause")
;? (set dump-trace*)
(add-code
  '((function test1 [
      ; doesn't matter too much how many locals you allocate space for (here 20)
      ; if it's slightly too many -- memory is plentiful
      ; if it's too few -- mu will raise an error
      (default-space:space-address <- new space:literal 20:literal)
      (first-arg-box:tagged-value-address <- next-input)
      ; if given integers, add them
      { begin
        (first-arg:integer match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref integer:literal)
        (break-unless match?:boolean)
        (second-arg-box:tagged-value-address <- next-input)
        (second-arg:integer <- maybe-coerce second-arg-box:tagged-value-address/deref integer:literal)
        (result:integer <- add first-arg:integer second-arg:integer)
        (reply result:integer)
      }
      (reply nil:literal)
     ])
    (function main [
      (1:tagged-value-address <- init-tagged-value integer:literal 34:literal)
      (2:tagged-value-address <- init-tagged-value integer:literal 3:literal)
      (3:integer <- test1 1:tagged-value-address 2:tagged-value-address)
     ])))
(run 'main)
;? (prn memory*)
(when (~is memory*.3 37)
  (prn "F - an example function that checks that its oarg is an integer"))
;? (quit)

(reset)
(new-trace "dispatch-multiple-clauses")
;? (set dump-trace*)
(add-code
  '((function test1 [
      (default-space:space-address <- new space:literal 20:literal)
      (first-arg-box:tagged-value-address <- next-input)
      ; if given integers, add them
      { begin
        (first-arg:integer match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref integer:literal)
        (break-unless match?:boolean)
        (second-arg-box:tagged-value-address <- next-input)
        (second-arg:integer <- maybe-coerce second-arg-box:tagged-value-address/deref integer:literal)
        (result:integer <- add first-arg:integer second-arg:integer)
        (reply result:integer)
      }
      ; if given booleans, or them (it's a silly kind of generic function)
      { begin
        (first-arg:boolean match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref boolean:literal)
        (break-unless match?:boolean)
        (second-arg-box:tagged-value-address <- next-input)
        (second-arg:boolean <- maybe-coerce second-arg-box:tagged-value-address/deref boolean:literal)
        (result:boolean <- or first-arg:boolean second-arg:boolean)
        (reply result:integer)
      }
      (reply nil:literal)
     ])
    (function main [
      (1:tagged-value-address <- init-tagged-value boolean:literal t:literal)
      (2:tagged-value-address <- init-tagged-value boolean:literal nil:literal)
      (3:boolean <- test1 1:tagged-value-address 2:tagged-value-address)
     ])))
;? (each stmt function*!test-fn
;?   (prn "  " stmt))
(run 'main)
;? (wipe dump-trace*)
;? (prn memory*)
(when (~is memory*.3 t)
  (prn "F - an example function that can do different things (dispatch) based on the type of its args or oargs"))
;? (quit)

(reset)
(new-trace "dispatch-multiple-calls")
(add-code
  '((function test1 [
      (default-space:space-address <- new space:literal 20:literal)
      (first-arg-box:tagged-value-address <- next-input)
      ; if given integers, add them
      { begin
        (first-arg:integer match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref integer:literal)
        (break-unless match?:boolean)
        (second-arg-box:tagged-value-address <- next-input)
        (second-arg:integer <- maybe-coerce second-arg-box:tagged-value-address/deref integer:literal)
        (result:integer <- add first-arg:integer second-arg:integer)
        (reply result:integer)
      }
      ; if given booleans, or them (it's a silly kind of generic function)
      { begin
        (first-arg:boolean match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref boolean:literal)
        (break-unless match?:boolean)
        (second-arg-box:tagged-value-address <- next-input)
        (second-arg:boolean <- maybe-coerce second-arg-box:tagged-value-address/deref boolean:literal)
        (result:boolean <- or first-arg:boolean second-arg:boolean)
        (reply result:integer)
      }
      (reply nil:literal)
     ])
    (function main [
      (1:tagged-value-address <- init-tagged-value boolean:literal t:literal)
      (2:tagged-value-address <- init-tagged-value boolean:literal nil:literal)
      (3:boolean <- test1 1:tagged-value-address 2:tagged-value-address)
      (10:tagged-value-address <- init-tagged-value integer:literal 34:literal)
      (11:tagged-value-address <- init-tagged-value integer:literal 3:literal)
      (12:integer <- test1 10:tagged-value-address 11:tagged-value-address)
     ])))
(run 'main)
;? (prn memory*)
(when (~and (is memory*.3 t) (is memory*.12 37))
  (prn "F - different calls can exercise different clauses of the same function"))

; We can also dispatch based on the type of the operands or results at the
; caller.

(reset)
(new-trace "dispatch-otype")
(add-code
  '((function test1 [
      (4:type <- otype 0:offset)
      { begin
        (5:boolean <- equal 4:type integer:literal)
        (break-unless 5:boolean)
        (6:integer <- next-input)
        (7:integer <- next-input)
        (8:integer <- add 6:integer 7:integer)
      }
      (reply 8:integer)
     ])
    (function main [
      (1:integer <- test1 1:literal 3:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~iso memory*.1 4)
  (prn "F - an example function that checks that its oarg is an integer"))
;? (quit)

(reset)
(new-trace "dispatch-otype-multiple-clauses")
;? (set dump-trace*)
(add-code
  '((function test1 [
      (4:type <- otype 0:offset)
      { begin
        ; integer needed? add args
        (5:boolean <- equal 4:type integer:literal)
        (break-unless 5:boolean)
        (6:integer <- next-input)
        (7:integer <- next-input)
        (8:integer <- add 6:integer 7:integer)
        (reply 8:integer)
      }
      { begin
        ; boolean needed? 'or' args
        (5:boolean <- equal 4:type boolean:literal)
        (break-unless 5:boolean 4:offset)
        (6:boolean <- next-input)
        (7:boolean <- next-input)
        (8:boolean <- or 6:boolean 7:boolean)
        (reply 8:boolean)
      }])
    (function main [
      (1:boolean <- test1 t:literal t:literal)
     ])))
;? (each stmt function*!test1
;?   (prn "  " stmt))
(run 'main)
;? (wipe dump-trace*)
;? (prn memory*)
(when (~is memory*.1 t)
  (prn "F - an example function that can do different things (dispatch) based on the type of its args or oargs"))
;? (quit)

(reset)
(new-trace "dispatch-otype-multiple-calls")
(add-code
  '((function test1 [
      (4:type <- otype 0:offset)
      { begin
        (5:boolean <- equal 4:type integer:literal)
        (break-unless 5:boolean)
        (6:integer <- next-input)
        (7:integer <- next-input)
        (8:integer <- add 6:integer 7:integer)
        (reply 8:integer)
      }
      { begin
        (5:boolean <- equal 4:type boolean:literal)
        (break-unless 5:boolean)
        (6:boolean <- next-input)
        (7:boolean <- next-input)
        (8:boolean <- or 6:boolean 7:boolean)
        (reply 8:boolean)
      }])
    (function main [
      (1:boolean <- test1 t:literal t:literal)
      (2:integer <- test1 3:literal 4:literal)
     ])))
(run 'main)
;? (prn memory*)
(when (~and (is memory*.1 t) (is memory*.2 7))
  (prn "F - different calls can exercise different clauses of the same function"))

)  ; section 100

(section 20

;; Concurrency
;
; A rudimentary process scheduler. You can 'run' multiple functions at once,
; and they share the virtual processor.
;
; There's also a 'fork' primitive to let functions create new threads of
; execution (we call them routines).
;
; Eventually we want to allow callers to influence how much of their CPU they
; give to their 'children', or to rescind a child's running privileges.

(reset)
(new-trace "scheduler")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 3:literal)
     ])
    (function f2 [
      (2:integer <- copy 4:literal)
     ])))
(run 'f1 'f2)
(when (~iso 2 curr-cycle*)
  (prn "F - scheduler didn't run the right number of instructions: " curr-cycle*))
(when (~iso memory* (obj 1 3  2 4))
  (prn "F - scheduler runs multiple functions: " memory*))
(check-trace-contents "scheduler orders functions correctly"
  '(("schedule" "f1")
    ("schedule" "f2")
  ))
(check-trace-contents "scheduler orders schedule and run events correctly"
  '(("schedule" "f1")
    ("run" "f1 0")
    ("schedule" "f2")
    ("run" "f2 0")
  ))

(reset)
(new-trace "scheduler-alternate")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
      (1:integer <- copy 0:literal)
     ])
    (function f2 [
      (2:integer <- copy 0:literal)
      (2:integer <- copy 0:literal)
     ])))
;? (= dump-trace* (obj whitelist '("schedule")))
(= scheduling-interval* 1)
(run 'f1 'f2)
(check-trace-contents "scheduler alternates between routines"
  '(("run" "f1 0")
    ("run" "f2 0")
    ("run" "f1 1")
    ("run" "f2 1")
  ))

(reset)
(new-trace "scheduler-sleep")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])
    (function f2 [
      (2:integer <- copy 0:literal)
     ])))
; add one baseline routine to run (empty running-routines* handled below)
(enq make-routine!f1 running-routines*)
(assert (is 1 len.running-routines*))
; sleeping routine
(let routine make-routine!f2
  (= rep.routine!sleep '(until 23))
  (set sleeping-routines*.routine))
; not yet time for it to wake up
(= curr-cycle* 23)
;? (set dump-trace*)
;? (= dump-trace* (obj whitelist '("run" "schedule")))
(update-scheduler-state)
(when (~is 1 len.running-routines*)
  (prn "F - scheduler lets routines sleep"))

(reset)
(new-trace "scheduler-wakeup")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])
    (function f2 [
      (2:integer <- copy 0:literal)
     ])))
; add one baseline routine to run (empty running-routines* handled below)
(enq make-routine!f1 running-routines*)
(assert (is 1 len.running-routines*))
; sleeping routine
(let routine make-routine!f2
  (= rep.routine!sleep '(until 23))
  (set sleeping-routines*.routine))
; time for it to wake up
(= curr-cycle* 24)
(update-scheduler-state)
(when (~is 2 len.running-routines*)
  (prn "F - scheduler wakes up sleeping routines at the right time"))

(reset)
(new-trace "scheduler-sleep-location")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])
    (function f2 [
      (2:integer <- copy 0:literal)
     ])))
; add one baseline routine to run (empty running-routines* handled below)
(enq make-routine!f1 running-routines*)
(assert (is 1 len.running-routines*))
; blocked routine waiting for location 23 to change
(let routine make-routine!f2
  (= rep.routine!sleep '(until-location-changes 23 0))
  (set sleeping-routines*.routine))
; leave memory location 23 unchanged
(= memory*.23 0)
;? (prn memory*)
;? (prn running-routines*)
;? (prn sleeping-routines*)
;? (set dump-trace*)
;? (= dump-trace* (obj whitelist '("run" "schedule")))
(update-scheduler-state)
;? (prn running-routines*)
;? (prn sleeping-routines*)
; routine remains blocked
(when (~is 1 len.running-routines*)
  (prn "F - scheduler lets routines block on locations"))
;? (quit)

(reset)
(new-trace "scheduler-wakeup-location")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])
    (function f2 [
      (2:integer <- copy 0:literal)
     ])))
; add one baseline routine to run (empty running-routines* handled below)
(enq make-routine!f1 running-routines*)
(assert (is 1 len.running-routines*))
; blocked routine waiting for location 23 to change
(let routine make-routine!f2
  (= rep.routine!sleep '(until-location-changes 23 0))
  (set sleeping-routines*.routine))
; change memory location 23
(= memory*.23 1)
(update-scheduler-state)
; routine unblocked
(when (~is 2 len.running-routines*)
  (prn "F - scheduler unblocks routines blocked on locations"))

(reset)
(new-trace "scheduler-skip")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])))
; running-routines* is empty
(assert (empty running-routines*))
; sleeping routine
(let routine make-routine!f1
  (= rep.routine!sleep '(until 34))
  (set sleeping-routines*.routine))
; long time left for it to wake up
(= curr-cycle* 0)
(update-scheduler-state)
;? (prn curr-cycle*)
(assert (is curr-cycle* 35))
(when (~is 1 len.running-routines*)
  (prn "F - scheduler skips ahead to earliest sleeping routines when nothing to run"))

(reset)
(new-trace "scheduler-deadlock")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])))
(assert (empty running-routines*))
(assert (empty completed-routines*))
; blocked routine
(let routine make-routine!f1
  (= rep.routine!sleep '(until-location-changes 23 0))
  (set sleeping-routines*.routine))
; location it's waiting on is 'unchanged'
(= memory*.23 0)
(update-scheduler-state)
(assert (~empty completed-routines*))
;? (prn completed-routines*)
(let routine completed-routines*.0
  (when (~posmatch "deadlock" rep.routine!error)
    (prn "F - scheduler detects deadlock")))
;? (quit)

(reset)
(new-trace "scheduler-deadlock2")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])))
; running-routines* is empty
(assert (empty running-routines*))
; blocked routine
(let routine make-routine!f1
  (= rep.routine!sleep '(until-location-changes 23 0))
  (set sleeping-routines*.routine))
; but is about to become ready
(= memory*.23 1)
(update-scheduler-state)
(when (~empty completed-routines*)
  (prn "F - scheduler ignores sleeping but ready threads when detecting deadlock"))

; Helper routines are just to sidestep the deadlock test; they stop running
; when there's no non-helper routines left to run.
;
; Be careful not to overuse them. In particular, the component under test
; should never run in a helper routine; that'll make interrupting and
; restarting it very brittle.
(reset)
(new-trace "scheduler-helper")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])))
; just a helper routine
(= routine* make-routine!f1)
(set rep.routine*!helper)
;? (= dump-trace* (obj whitelist '("schedule")))
(update-scheduler-state)
(when (or (~empty running-routines*) (~empty sleeping-routines*))
  (prn "F - scheduler stops when there's only helper routines left"))

(reset)
(new-trace "scheduler-helper-sleeping")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])))
; just a helper routine
(let routine make-routine!f1
  (set rep.routine!helper)
  (= rep.routine!sleep '(until-location-changes 23 nil))
  (set sleeping-routines*.routine))
;? (= dump-trace* (obj whitelist '("schedule")))
;? (prn "1 " running-routines*)
;? (prn sleeping-routines*)
(update-scheduler-state)
;? (prn "2 " running-routines*)
;? (prn sleeping-routines*)
(when (or (~empty running-routines*) (~empty sleeping-routines*))
  (prn "F - scheduler stops when there's only sleeping helper routines left"))

(reset)
(new-trace "scheduler-termination")
(= traces* (queue))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])))
; all routines done
(update-scheduler-state)
(check-trace-doesnt-contain "scheduler helper check shouldn't trigger unless necessary"
  '(("schedule" "just helpers left")))

; both running and sleeping helpers
; running helper and sleeping non-helper
; sleeping helper and running non-helper

(reset)
(new-trace "scheduler-account-slice")
; function running an infinite loop
(add-code
  '((function f1 [
      { begin
        (1:integer <- copy 0:literal)
        (loop)
      }
     ])))
(let routine make-routine!f1
  (= rep.routine!limit 10)
  (enq routine running-routines*))
(= scheduling-interval* 20)
(run)
(when (or (empty completed-routines*)
          (~is -10 ((rep completed-routines*.0) 'limit)))
  (prn "F - when given a low cycle limit, a routine runs to end of time slice"))

(reset)
(new-trace "scheduler-account-slice-multiple")
; function running an infinite loop
(add-code
  '((function f1 [
      { begin
        (1:integer <- copy 0:literal)
        (loop)
      }
     ])))
(let routine make-routine!f1
  (= rep.routine!limit 100)
  (enq routine running-routines*))
(= scheduling-interval* 20)
(run)
(when (or (empty completed-routines*)
          (~is -0 ((rep completed-routines*.0) 'limit)))
  (prn "F - when given a high limit, a routine successfully stops after multiple time slices"))

(reset)
(new-trace "scheduler-account-run-while-asleep")
(add-code
    ; f1 needs 4 cycles of sleep time, 4 cycles of work
  '((function f1 [
      (sleep for-some-cycles:literal 4:literal)
      (i:integer <- copy 0:literal)
      (i:integer <- copy 0:literal)
      (i:integer <- copy 0:literal)
      (i:integer <- copy 0:literal)
     ])))
(let routine make-routine!f1
  (= rep.routine!limit 6)  ; enough time excluding sleep
  (enq routine running-routines*))
(= scheduling-interval* 1)
;? (= dump-trace* (obj whitelist '("schedule")))
(run)
; if time slept counts against limit, routine doesn't have time to complete
(when (ran-to-completion 'f1)
  (prn "F - time slept counts against a routine's cycle limit"))
;? (quit)

(reset)
(new-trace "scheduler-account-stop-on-preempt")
(add-code
  '((function baseline [
      (i:integer <- copy 0:literal)
      { begin
        (done?:boolean <- greater-or-equal i:integer 10:literal)
        (break-if done?:boolean)
        (1:integer <- add i:integer 1:literal)
        (loop)
      }
     ])
    (function f1 [
      (i:integer <- copy 0:literal)
      { begin
        (done?:boolean <- greater-or-equal i:integer 6:literal)
        (break-if done?:boolean)
        (1:integer <- add i:integer 1:literal)
        (loop)
      }
     ])))
(let routine make-routine!baseline
  (enq routine running-routines*))
; now add the routine we care about
(let routine make-routine!f1
  (= rep.routine!limit 40)  ; less than 2x time f1 needs to complete
  (enq routine running-routines*))
(= scheduling-interval* 1)
; if baseline's time were to count against f1's limit, it wouldn't be able to
; complete.
(when (~ran-to-completion 'f1)
  (prn "F - preempted time doesn't count against a routine's limit"))
;? (quit)

(reset)
(new-trace "scheduler-sleep-timeout")
(add-code
  '((function baseline [
      (i:integer <- copy 0:literal)
      { begin
        (done?:boolean <- greater-or-equal i:integer 10:literal)
        (break-if done?:boolean)
        (1:integer <- add i:integer 1:literal)
        (loop)
      }
     ])
    (function f1 [
      (sleep for-some-cycles:literal 10:literal)  ; less time than baseline would take to run
     ])))
; add baseline routine to prevent cycle-skipping
(let routine make-routine!baseline
  (enq routine running-routines*))
; now add the routine we care about
(let routine make-routine!f1
  (= rep.routine!limit 4)  ; less time than f1 would take to run
  (enq routine running-routines*))
(= scheduling-interval* 1)
;? (= dump-trace* (obj whitelist '("schedule")))
(run)
(when (ran-to-completion 'f1)
  (prn "F - sleeping routines can time out"))
;? (quit)

(reset)
(new-trace "sleep")
(add-code
  '((function f1 [
      (sleep for-some-cycles:literal 1:literal)
      (1:integer <- copy 0:literal)
      (1:integer <- copy 0:literal)
     ])
    (function f2 [
      (2:integer <- copy 0:literal)
      (2:integer <- copy 0:literal)
     ])))
;? (= dump-trace* (obj whitelist '("run" "schedule")))
(run 'f1 'f2)
(check-trace-contents "scheduler handles sleeping routines"
  '(("run" "f1 0")
    ("run" "sleeping until 2")
    ("schedule" "pushing f1 to sleep queue")
    ("run" "f2 0")
    ("run" "f2 1")
    ("schedule" "waking up f1")
    ("run" "f1 1")
    ("run" "f1 2")
  ))

(reset)
(new-trace "sleep-long")
(add-code
  '((function f1 [
      (sleep for-some-cycles:literal 20:literal)
      (1:integer <- copy 0:literal)
      (1:integer <- copy 0:literal)
     ])
    (function f2 [
      (2:integer <- copy 0:literal)
      (2:integer <- copy 0:literal)
     ])))
;? (= dump-trace* (obj whitelist '("run" "schedule")))
(run 'f1 'f2)
(check-trace-contents "scheduler progresses sleeping routines when there are no routines left to run"
  '(("run" "f1 0")
    ("run" "sleeping until 21")
    ("schedule" "pushing f1 to sleep queue")
    ("run" "f2 0")
    ("run" "f2 1")
    ("schedule" "waking up f1")
    ("run" "f1 1")
    ("run" "f1 2")
  ))

(reset)
(new-trace "sleep-location")
(add-code
  '((function f1 [
      ; waits for memory location 1 to be set, before computing its successor
      (1:integer <- copy 0:literal)
      (sleep until-location-changes:literal 1:integer)
      (2:integer <- add 1:integer 1:literal)
     ])
    (function f2 [
      (sleep for-some-cycles:literal 30:literal)
      (1:integer <- copy 3:literal)  ; set to value
     ])))
;? (= dump-trace* (obj whitelist '("run" "schedule")))
;? (set dump-trace*)
(run 'f1 'f2)
;? (prn int-canon.memory*)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~is memory*.2 4)  ; successor of value
  (prn "F - sleep can block on a memory location"))
;? (quit)

(reset)
(new-trace "sleep-scoped-location")
(add-code
  '((function f1 [
      ; waits for memory location 1 to be changed, before computing its successor
      (10:integer <- copy 5:literal)  ; array of locals
      (default-space:space-address <- copy 10:literal)
      (1:integer <- copy 23:literal)  ; really location 12
      (sleep until-location-changes:literal 1:integer)
      (2:integer <- add 1:integer 1:literal)
     ])
    (function f2 [
      (sleep for-some-cycles:literal 30:literal)
      (12:integer <- copy 3:literal)  ; set to value
     ])))
;? (= dump-trace* (obj whitelist '("run" "schedule")))
(run 'f1 'f2)
(when (~is memory*.13 4)  ; successor of value
  (prn "F - sleep can block on a scoped memory location"))
;? (quit)

(reset)
(new-trace "fork")
(add-code
  '((function f1 [
      (1:integer <- copy 4:literal)
     ])
    (function main [
      (fork f1:fn)
     ])))
(run 'main)
(when (~iso memory*.1 4)
  (prn "F - fork works"))

(reset)
(new-trace "fork-returns-id")
(add-code
  '((function f1 [
      (1:integer <- copy 4:literal)
     ])
    (function main [
      (2:integer <- fork f1:fn)
     ])))
(run 'main)
;? (prn memory*)
(when (no memory*.2)
  (prn "F - fork returns a pid for the new routine"))

(reset)
(new-trace "fork-returns-unique-id")
(add-code
  '((function f1 [
      (1:integer <- copy 4:literal)
     ])
    (function main [
      (2:integer <- fork f1:fn)
      (3:integer <- fork f1:fn)
     ])))
(run 'main)
(when (or (no memory*.2)
          (no memory*.3)
          (is memory*.2 memory*.3))
  (prn "F - fork returns a unique pid everytime"))

(reset)
(new-trace "fork-with-args")
(add-code
  '((function f1 [
      (2:integer <- next-input)
     ])
    (function main [
      (fork f1:fn nil:literal/globals nil:literal/limit 4:literal)
     ])))
(run 'main)
(when (~iso memory*.2 4)
  (prn "F - fork can pass args"))

(reset)
(new-trace "fork-copies-args")
(add-code
  '((function f1 [
      (2:integer <- next-input)
     ])
    (function main [
      (default-space:space-address <- new space:literal 5:literal)
      (x:integer <- copy 4:literal)
      (fork f1:fn nil:literal/globals nil:literal/limit x:integer)
      (x:integer <- copy 0:literal)  ; should be ignored
     ])))
(run 'main)
(when (~iso memory*.2 4)
  (prn "F - fork passes args by value"))

(reset)
(new-trace "fork-global")
(add-code
  '((function f1 [
      (1:integer/raw <- copy 2:integer/space:global)
     ])
    (function main [
      (default-space:space-address <- new space:literal 5:literal)
      (2:integer <- copy 4:literal)
      (fork f1:fn default-space:space-address/globals nil:literal/limit)
     ])))
(run 'main)
(each routine completed-routines*
  (awhen rep.routine!error (prn "error - " it)))
(when (~iso memory*.1 4)
  (prn "F - fork can take a space of global variables to access"))

(reset)
(new-trace "fork-limit")
(add-code
  '((function f1 [
      { begin
        (loop)
      }
     ])
    (function main [
      (fork f1:fn nil:literal/globals 30:literal/limit)
     ])))
(= scheduling-interval* 5)
(run 'main)
(each routine completed-routines*
  (awhen rep.routine!error (prn "error - " it)))
(when (ran-to-completion 'f1)
  (prn "F - fork can specify a maximum cycle limit"))

(reset)
(new-trace "fork-then-wait")
(add-code
  '((function f1 [
      { begin
        (loop)
      }
     ])
    (function main [
      (1:integer/routine-id <- fork f1:fn nil:literal/globals 30:literal/limit)
      (sleep until-routine-done:literal 1:integer/routine-id)
      (2:integer <- copy 34:literal)
     ])))
(= scheduling-interval* 5)
;? (= dump-trace* (obj whitelist '("schedule")))
(run 'main)
(each routine completed-routines*
  (awhen rep.routine!error (prn "error - " it)))
(check-trace-contents "scheduler orders functions correctly"
  '(("schedule" "pushing main to sleep queue")
    ("schedule" "scheduling f1")
    ("schedule" "ran out of time")
    ("schedule" "waking up main")
  ))
;? (quit)

; todo: Haven't yet written several tests
;   that restarting a routine works
;     when it died
;     when it timed out
;     when it completed
;   running multiple routines in tandem
; first example using these features: read-move-incomplete in chessboard-cursor.arc.t

; The scheduler needs to keep track of the call stack for each routine.
; Eventually we'll want to save this information in mu's address space itself,
; along with the types array, the magic buffers for args and oargs, and so on.
;
; Eventually we want the right stack-management primitives to build delimited
; continuations in mu.

; Routines can throw errors.
(reset)
(new-trace "array-bounds-check")
(add-code
  '((function main [
      (1:integer <- copy 2:literal)
      (2:integer <- copy 23:literal)
      (3:integer <- copy 24:literal)
      (4:integer <- index 1:integer-array 2:literal)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(let routine (car completed-routines*)
  (when (no rep.routine!error)
    (prn "F - 'index' throws an error if out of bounds")))

)  ; section 20

(section 100

;; Synchronization
;
; Mu synchronizes using channels rather than locks, like Erlang and Go.
;
; The two ends of a channel will usually belong to different routines, but
; each end should only be used by a single one. Don't try to read from or
; write to it from multiple routines at once.
;
; To avoid locking, writer and reader will never write to the same location.
; So channels will include fields in pairs, one for the writer and one for the
; reader.

; The core circular buffer contains values at index 'first-full' up to (but
; not including) index 'first-empty'. The reader always modifies it at
; first-full, while the writer always modifies it at first-empty.
(reset)
(new-trace "channel-new")
(add-code
  '((function main [
      (1:channel-address <- init-channel 3:literal)
      (2:integer <- get 1:channel-address/deref first-full:offset)
      (3:integer <- get 1:channel-address/deref first-free:offset)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (or (~is 0 memory*.2)
          (~is 0 memory*.3))
  (prn "F - 'init-channel' initializes 'first-full and 'first-free to 0"))

(reset)
(new-trace "channel-write")
(add-code
  '((function main [
      (1:channel-address <- init-channel 3:literal)
      (2:integer <- copy 34:literal)
      (3:tagged-value <- save-type 2:integer)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      (5:integer <- get 1:channel-address/deref first-full:offset)
      (6:integer <- get 1:channel-address/deref first-free:offset)
     ])))
;? (prn function*!write)
;? (set dump-trace*)
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "array-len" "cvt0" "cvt1")))
;? (= dump-trace* (obj whitelist '("jump")))
;? (= dump-trace* (obj whitelist '("run" "reply")))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
;? (prn canon.memory*)
(when (or (~is 0 memory*.5)
          (~is 1 memory*.6))
  (prn "F - 'write' enqueues item to channel"))
;? (quit)

(reset)
(new-trace "channel-read")
(add-code
  '((function main [
      (1:channel-address <- init-channel 3:literal)
      (2:integer <- copy 34:literal)
      (3:tagged-value <- save-type 2:integer)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      (5:tagged-value 1:channel-address/deref <- read 1:channel-address)
      (7:integer <- maybe-coerce 5:tagged-value integer:literal)
      (8:integer <- get 1:channel-address/deref first-full:offset)
      (9:integer <- get 1:channel-address/deref first-free:offset)
     ])))
;? (set dump-trace*)
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "array-len" "cvt0" "cvt1")))
(run 'main)
;? (prn int-canon.memory*)
(when (~is memory*.7 34)
  (prn "F - 'read' returns written value"))
(when (or (~is 1 memory*.8)
          (~is 1 memory*.9))
  (prn "F - 'read' dequeues item from channel"))

(reset)
(new-trace "channel-write-wrap")
(add-code
  '((function main [
      ; channel with 1 slot
      (1:channel-address <- init-channel 1:literal)
      ; write a value
      (2:integer <- copy 34:literal)
      (3:tagged-value <- save-type 2:integer)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      ; first-free will now be 1
      (5:integer <- get 1:channel-address/deref first-free:offset)
      ; read one value
      (_ 1:channel-address/deref <- read 1:channel-address)
      ; write a second value; verify that first-free wraps around to 0.
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      (6:integer <- get 1:channel-address/deref first-free:offset)
     ])))
;? (set dump-trace*)
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "array-len" "cvt0" "cvt1")))
(run 'main)
;? (prn canon.memory*)
(when (or (~is 1 memory*.5)
          (~is 0 memory*.6))
  (prn "F - 'write' can wrap pointer back to start"))

(reset)
(new-trace "channel-read-wrap")
(add-code
  '((function main [
      ; channel with 1 slot
      (1:channel-address <- init-channel 1:literal)
      ; write a value
      (2:integer <- copy 34:literal)
      (3:tagged-value <- save-type 2:integer)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      ; read one value
      (_ 1:channel-address/deref <- read 1:channel-address)
      ; first-full will now be 1
      (5:integer <- get 1:channel-address/deref first-full:offset)
      ; write a second value
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      ; read second value; verify that first-full wraps around to 0.
      (_ 1:channel-address/deref <- read 1:channel-address)
      (6:integer <- get 1:channel-address/deref first-full:offset)
     ])))
;? (set dump-trace*)
;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "array-len" "cvt0" "cvt1")))
(run 'main)
;? (prn canon.memory*)
(when (or (~is 1 memory*.5)
          (~is 0 memory*.6))
  (prn "F - 'read' can wrap pointer back to start"))

(reset)
(new-trace "channel-new-empty-not-full")
(add-code
  '((function main [
      (1:channel-address <- init-channel 3:literal)
      (2:boolean <- empty? 1:channel-address/deref)
      (3:boolean <- full? 1:channel-address/deref)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (or (~is t memory*.2)
          (~is nil memory*.3))
  (prn "F - a new channel is always empty, never full"))

(reset)
(new-trace "channel-write-not-empty")
(add-code
  '((function main [
      (1:channel-address <- init-channel 3:literal)
      (2:integer <- copy 34:literal)
      (3:tagged-value <- save-type 2:integer)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      (5:boolean <- empty? 1:channel-address/deref)
      (6:boolean <- full? 1:channel-address/deref)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (or (~is nil memory*.5)
          (~is nil memory*.6))
  (prn "F - a channel after writing is never empty"))

(reset)
(new-trace "channel-write-full")
(add-code
  '((function main [
      (1:channel-address <- init-channel 1:literal)
      (2:integer <- copy 34:literal)
      (3:tagged-value <- save-type 2:integer)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      (5:boolean <- empty? 1:channel-address/deref)
      (6:boolean <- full? 1:channel-address/deref)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (or (~is nil memory*.5)
          (~is t memory*.6))
  (prn "F - a channel after writing may be full"))

(reset)
(new-trace "channel-read-not-full")
(add-code
  '((function main [
      (1:channel-address <- init-channel 3:literal)
      (2:integer <- copy 34:literal)
      (3:tagged-value <- save-type 2:integer)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      (_ 1:channel-address/deref <- read 1:channel-address)
      (5:boolean <- empty? 1:channel-address/deref)
      (6:boolean <- full? 1:channel-address/deref)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (or (~is nil memory*.5)
          (~is nil memory*.6))
  (prn "F - a channel after reading is never full"))

(reset)
(new-trace "channel-read-empty")
(add-code
  '((function main [
      (1:channel-address <- init-channel 3:literal)
      (2:integer <- copy 34:literal)
      (3:tagged-value <- save-type 2:integer)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      (_ 1:channel-address/deref <- read 1:channel-address)
      (5:boolean <- empty? 1:channel-address/deref)
      (6:boolean <- full? 1:channel-address/deref)
     ])))
;? (set dump-trace*)
(run 'main)
;? (prn memory*)
(when (or (~is t memory*.5)
          (~is nil memory*.6))
  (prn "F - a channel after reading may be empty"))

; The key property of channels; writing to a full channel blocks the current
; routine until it creates space. Ditto reading from an empty channel.

(reset)
(new-trace "channel-read-block")
(add-code
  '((function main [
      (1:channel-address <- init-channel 3:literal)
      ; channel is empty, but receives a read
      (2:tagged-value 1:channel-address/deref <- read 1:channel-address)
     ])))
;? (set dump-trace*)
;? (= dump-trace* (obj whitelist '("run" "schedule")))
(run 'main)
;? (prn int-canon.memory*)
;? (prn sleeping-routines*)
;? (prn completed-routines*)
; read should cause the routine to sleep, and
; the sole sleeping routine should trigger the deadlock detector
(let routine (car completed-routines*)
  (when (or (no routine)
            (no rep.routine!error)
            (~posmatch "deadlock" rep.routine!error))
    (prn "F - 'read' on empty channel blocks (puts the routine to sleep until the channel gets data)")))
;? (quit)

(reset)
(new-trace "channel-write-block")
(add-code
  '((function main [
      (1:channel-address <- init-channel 1:literal)
      (2:integer <- copy 34:literal)
      (3:tagged-value <- save-type 2:integer)
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
      ; channel has capacity 1, but receives a second write
      (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
     ])))
;? (set dump-trace*)
;? (= dump-trace* (obj whitelist '("run" "schedule" "addr")))
(run 'main)
;? (prn int-canon.memory*)
;? (prn running-routines*)
;? (prn sleeping-routines*)
;? (prn completed-routines*)
; second write should cause the routine to sleep, and
; the sole sleeping routine should trigger the deadlock detector
(let routine (car completed-routines*)
  (when (or (no routine)
            (no rep.routine!error)
            (~posmatch "deadlock" rep.routine!error))
    (prn "F - 'write' on full channel blocks (puts the routine to sleep until the channel gets data)")))
;? (quit)

(reset)
(new-trace "channel-handoff")
(add-code
  '((function consumer [
      (default-space:space-address <- new space:literal 30:literal)
      (chan:channel-address <- init-channel 3:literal)  ; create a channel
      (fork producer:fn nil:literal/globals nil:literal/limit chan:channel-address)  ; fork a routine to produce a value in it
      (1:tagged-value/raw <- read chan:channel-address)  ; wait for input on channel
     ])
    (function producer [
      (default-space:space-address <- new space:literal 30:literal)
      (n:integer <- copy 24:literal)
      (ochan:channel-address <- next-input)
      (x:tagged-value <- save-type n:integer)
      (ochan:channel-address/deref <- write ochan:channel-address x:tagged-value)
     ])))
;? (set dump-trace*)
;? (= dump-trace* (obj whitelist '("schedule" "run" "addr")))
;? (= dump-trace* (obj whitelist '("-")))
(run 'consumer)
;? (prn memory*)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~is 24 memory*.2)  ; location 1 contains tagged-value x above
  (prn "F - channels are meant to be shared between routines"))
;? (quit)

(reset)
(new-trace "channel-handoff-routine")
(add-code
  '((function consumer [
      (default-space:space-address <- new space:literal 30:literal)
      (1:channel-address <- init-channel 3:literal)  ; create a channel
      (fork producer:fn default-space:space-address/globals nil:literal/limit)  ; pass it as a global to another routine
      (1:tagged-value/raw <- read 1:channel-address)  ; wait for input on channel
     ])
    (function producer [
      (default-space:space-address <- new space:literal 30:literal)
      (n:integer <- copy 24:literal)
      (x:tagged-value <- save-type n:integer)
      (1:channel-address/space:global/deref <- write 1:channel-address/space:global x:tagged-value)
     ])))
(run 'consumer)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~is 24 memory*.2)  ; location 1 contains tagged-value x above
  (prn "F - channels are meant to be shared between routines"))

)  ; section 100

(section 10

;; Separating concerns
;
; Lightweight tools can also operate on quoted lists of statements surrounded
; by square brackets. In the example below, we mimic Go's 'defer' keyword
; using 'convert-quotes'. It lets us write code anywhere in a function, but
; have it run just before the function exits. Great for keeping code to
; reclaim memory or other resources close to the code to allocate it. (C++
; programmers know this as RAII.) We'll use 'defer' when we build a memory
; deallocation routine like C's 'free'.
;
; More powerful reorderings are also possible like in Literate Programming or
; Aspect-Oriented Programming; one advantage of prohibiting arbitrarily nested
; code is that we can naturally name 'join points' wherever we want.

(reset)
(new-trace "convert-quotes-defer")
(= traces* (queue))
(when (~iso (convert-quotes
              '((1:integer <- copy 4:literal)
                (defer [
                         (3:integer <- copy 6:literal)
                       ])
                (2:integer <- copy 5:literal)))
            '((1:integer <- copy 4:literal)
              (2:integer <- copy 5:literal)
              (3:integer <- copy 6:literal)))
  (prn "F - convert-quotes can handle 'defer'"))

(reset)
(new-trace "convert-quotes-defer-reply")
(= traces* (queue))
(when (~iso (convert-quotes
              '((1:integer <- copy 0:literal)
                (defer [
                         (5:integer <- copy 0:literal)
                       ])
                (2:integer <- copy 0:literal)
                (reply)
                (3:integer <- copy 0:literal)
                (4:integer <- copy 0:literal)))
            '((1:integer <- copy 0:literal)
              (2:integer <- copy 0:literal)
              (5:integer <- copy 0:literal)
              (reply)
              (3:integer <- copy 0:literal)
              (4:integer <- copy 0:literal)
              (5:integer <- copy 0:literal)))
  (prn "F - convert-quotes inserts code at early exits"))

(reset)
(new-trace "convert-quotes-defer-reply-arg")
(= traces* (queue))
(when (~iso (convert-quotes
              '((1:integer <- copy 0:literal)
                (defer [
                         (5:integer <- copy 0:literal)
                       ])
                (2:integer <- copy 0:literal)
                (reply 2:literal)
                (3:integer <- copy 0:literal)
                (4:integer <- copy 0:literal)))
            '((1:integer <- copy 0:literal)
              (2:integer <- copy 0:literal)
              (prepare-reply 2:literal)
              (5:integer <- copy 0:literal)
              (reply)
              (3:integer <- copy 0:literal)
              (4:integer <- copy 0:literal)
              (5:integer <- copy 0:literal)))
  (prn "F - convert-quotes inserts code at early exits"))

(reset)
(new-trace "convert-quotes-label")
(= traces* (queue))
(when (~iso (convert-quotes
              '((1:integer <- copy 4:literal)
                foo
                (2:integer <- copy 5:literal)))
            '((1:integer <- copy 4:literal)
              foo
              (2:integer <- copy 5:literal)))
  (prn "F - convert-quotes can handle labels"))

(reset)
(new-trace "before")
(= traces* (queue))
(add-code
  '((before label1 [
     (2:integer <- copy 0:literal)
    ])))
(when (~iso (as cons before*!label1)
            '(; fragment
              (
                (2:integer <- copy 0:literal))))
  (prn "F - 'before' records fragments of code to insert before labels"))

(when (~iso (insert-code
              '((1:integer <- copy 0:literal)
                label1
                (3:integer <- copy 0:literal)))
            '((1:integer <- copy 0:literal)
              (2:integer <- copy 0:literal)
              label1
              (3:integer <- copy 0:literal)))
  (prn "F - 'insert-code' can insert fragments before labels"))

(reset)
(new-trace "before-multiple")
(= traces* (queue))
(add-code
  '((before label1 [
      (2:integer <- copy 0:literal)
     ])
    (before label1 [
      (3:integer <- copy 0:literal)
     ])))
(when (~iso (as cons before*!label1)
            '(; fragment
              (
                (2:integer <- copy 0:literal))
              (
                (3:integer <- copy 0:literal))))
  (prn "F - 'before' records fragments in order"))

(when (~iso (insert-code
              '((1:integer <- copy 0:literal)
                label1
                (4:integer <- copy 0:literal)))
            '((1:integer <- copy 0:literal)
              (2:integer <- copy 0:literal)
              (3:integer <- copy 0:literal)
              label1
              (4:integer <- copy 0:literal)))
  (prn "F - 'insert-code' can insert multiple fragments in order before label"))

(reset)
(new-trace "before-scoped")
(= traces* (queue))
(add-code
  '((before f/label1 [  ; label1 only inside function f
     (2:integer <- copy 0:literal)
    ])))
(when (~iso (insert-code
              '((1:integer <- copy 0:literal)
                label1
                (3:integer <- copy 0:literal))
              'f)
            '((1:integer <- copy 0:literal)
              (2:integer <- copy 0:literal)
              label1
              (3:integer <- copy 0:literal)))
  (prn "F - 'insert-code' can insert fragments before labels just in specified functions"))

(reset)
(new-trace "before-scoped2")
(= traces* (queue))
(add-code
  '((before f/label1 [  ; label1 only inside function f
      (2:integer <- copy 0:literal)
     ])))
(when (~iso (insert-code
              '((1:integer <- copy 0:literal)
                label1
                (3:integer <- copy 0:literal)))
            '((1:integer <- copy 0:literal)
              label1
              (3:integer <- copy 0:literal)))
  (prn "F - 'insert-code' ignores labels not in specified functions"))

(reset)
(new-trace "after")
(= traces* (queue))
(add-code
  '((after label1 [
      (2:integer <- copy 0:literal)
     ])))
(when (~iso (as cons after*!label1)
            '(; fragment
              (
                (2:integer <- copy 0:literal))))
  (prn "F - 'after' records fragments of code to insert after labels"))

(when (~iso (insert-code
              '((1:integer <- copy 0:literal)
                label1
                (3:integer <- copy 0:literal)))
            '((1:integer <- copy 0:literal)
              label1
              (2:integer <- copy 0:literal)
              (3:integer <- copy 0:literal)))
  (prn "F - 'insert-code' can insert fragments after labels"))

(reset)
(new-trace "after-multiple")
(= traces* (queue))
(add-code
  '((after label1 [
      (2:integer <- copy 0:literal)
     ])
    (after label1 [
      (3:integer <- copy 0:literal)
     ])))
(when (~iso (as cons after*!label1)
            '(; fragment
              (
                (3:integer <- copy 0:literal))
              (
                (2:integer <- copy 0:literal))))
  (prn "F - 'after' records fragments in *reverse* order"))

(when (~iso (insert-code
              '((1:integer <- copy 0:literal)
                label1
                (4:integer <- copy 0:literal)))
            '((1:integer <- copy 0:literal)
              label1
              (3:integer <- copy 0:literal)
              (2:integer <- copy 0:literal)
              (4:integer <- copy 0:literal)))
  (prn "F - 'insert-code' can insert multiple fragments in order after label"))

(reset)
(new-trace "before-after")
(= traces* (queue))
(add-code
  '((before label1 [
      (2:integer <- copy 0:literal)
     ])
    (after label1 [
      (3:integer <- copy 0:literal)
     ])))
(when (and (~iso (as cons before*!label1)
                 '(; fragment
                   (
                     (2:integer <- copy 0:literal))))
           (~iso (as cons after*!label1)
                 '(; fragment
                   (
                     (3:integer <- copy 0:literal)))))
  (prn "F - 'before' and 'after' fragments work together"))

(when (~iso (insert-code
              '((1:integer <- copy 0:literal)
                label1
                (4:integer <- copy 0:literal)))
            '((1:integer <- copy 0:literal)
              (2:integer <- copy 0:literal)
              label1
              (3:integer <- copy 0:literal)
              (4:integer <- copy 0:literal)))
  (prn "F - 'insert-code' can insert multiple fragments around label"))

(reset)
(new-trace "before-after-multiple")
(= traces* (queue))
(add-code
  '((before label1 [
      (2:integer <- copy 0:literal)
      (3:integer <- copy 0:literal)
     ])
    (after label1 [
      (4:integer <- copy 0:literal)
     ])
    (before label1 [
      (5:integer <- copy 0:literal)
     ])
    (after label1 [
      (6:integer <- copy 0:literal)
      (7:integer <- copy 0:literal)
     ])))
(when (or (~iso (as cons before*!label1)
                '(; fragment
                  (
                    (2:integer <- copy 0:literal)
                    (3:integer <- copy 0:literal))
                  (
                    (5:integer <- copy 0:literal))))
          (~iso (as cons after*!label1)
                '(; fragment
                  (
                    (6:integer <- copy 0:literal)
                    (7:integer <- copy 0:literal))
                  (
                    (4:integer <- copy 0:literal)))))
  (prn "F - multiple 'before' and 'after' fragments at once"))

(when (~iso (insert-code
              '((1:integer <- copy 0:literal)
                label1
                (8:integer <- copy 0:literal)))
            '((1:integer <- copy 0:literal)
              (2:integer <- copy 0:literal)
              (3:integer <- copy 0:literal)
              (5:integer <- copy 0:literal)
              label1
              (6:integer <- copy 0:literal)
              (7:integer <- copy 0:literal)
              (4:integer <- copy 0:literal)
              (8:integer <- copy 0:literal)))
  (prn "F - 'insert-code' can insert multiple fragments around label - 2"))

(reset)
(new-trace "before-after-independent")
(= traces* (queue))
(when (~iso (do
              (reset)
              (add-code
                '((before label1 [
                    (2:integer <- copy 0:literal)
                   ])
                  (after label1 [
                    (3:integer <- copy 0:literal)
                   ])
                  (before label1 [
                    (4:integer <- copy 0:literal)
                   ])
                  (after label1 [
                    (5:integer <- copy 0:literal)
                   ])))
              (list before*!label1 after*!label1))
            (do
              (reset)
              (add-code
                '((before label1 [
                    (2:integer <- copy 0:literal)
                   ])
                  (before label1 [
                    (4:integer <- copy 0:literal)
                   ])
                  (after label1 [
                    (3:integer <- copy 0:literal)
                   ])
                  (after label1 [
                    (5:integer <- copy 0:literal)
                   ])))
              (list before*!label1 after*!label1)))
  (prn "F - order matters between 'before' and between 'after' fragments, but not *across* 'before' and 'after' fragments"))

(reset)
(new-trace "before-after-braces")
(= traces* (queue))
(= function* (table))
(add-code
  '((after label1 [
      (1:integer <- copy 0:literal)
     ])
    (function f1 [
      { begin
        label1
      }
     ])))
;? (= dump-trace* (obj whitelist '("cn0")))
(freeze function*)
(when (~iso function*!f1
            '(label1
              (((1 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - before/after works inside blocks"))

(reset)
(new-trace "before-after-any-order")
(= traces* (queue))
(= function* (table))
(add-code
  '((function f1 [
      { begin
        label1
      }
     ])
    (after label1 [
       (1:integer <- copy 0:literal)
     ])))
(freeze function*)
(when (~iso function*!f1
            '(label1
              (((1 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - before/after can come after the function they need to modify"))
;? (quit)

(reset)
(new-trace "multiple-defs")
(= traces* (queue))
(= function* (table))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])
    (function f1 [
      (2:integer <- copy 0:literal)
     ])))
(freeze function*)
(when (~iso function*!f1
            '((((2 integer)) <- ((copy)) ((0 literal)))
              (((1 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - multiple 'def' of the same function add clauses"))

(reset)
(new-trace "def!")
(= traces* (queue))
(= function* (table))
(add-code
  '((function f1 [
      (1:integer <- copy 0:literal)
     ])
    (function! f1 [
      (2:integer <- copy 0:literal)
     ])))
(freeze function*)
(when (~iso function*!f1
            '((((2 integer)) <- ((copy)) ((0 literal)))))
  (prn "F - 'def!' clears all previous clauses"))

)  ; section 10

;; ---

(section 100

; String utilities

(reset)
(new-trace "string-new")
(add-code
  '((function main [
      (1:string-address <- new string:literal 5:literal)
     ])))
(let routine make-routine!main
  (enq routine running-routines*)
  (let before rep.routine!alloc
    (run)
    (when (~iso rep.routine!alloc (+ before 5 1))
      (prn "F - 'new' allocates arrays of bytes for strings"))))

; Convenience: initialize strings using string literals
(reset)
(new-trace "string-literal")
(add-code
  '((function main [
      (1:string-address <- new "hello")
     ])))
(let routine make-routine!main
  (enq routine running-routines*)
  (let before rep.routine!alloc
;?     (set dump-trace*)
;?     (= dump-trace* (obj whitelist '("schedule" "run" "addr")))
    (run)
    (when (~iso rep.routine!alloc (+ before 5 1))
      (prn "F - 'new' allocates arrays of bytes for string literals"))
    (when (~memory-contains-array before "hello")
      (prn "F - 'new' initializes allocated memory to string literal"))))

(reset)
(new-trace "string-equal")
(add-code
  '((function main [
      (1:string-address <- new "hello")
      (2:string-address <- new "hello")
      (3:boolean <- string-equal 1:string-address 2:string-address)
     ])))
(run 'main)
(when (~iso memory*.3 t)
  (prn "F - 'string-equal'"))

(reset)
(new-trace "string-equal-empty")
(add-code
  '((function main [
      (1:string-address <- new "")
      (2:string-address <- new "")
      (3:boolean <- string-equal 1:string-address 2:string-address)
     ])))
(run 'main)
(when (~iso memory*.3 t)
  (prn "F - 'string-equal' works on empty strings"))

(reset)
(new-trace "string-equal-compare-with-empty")
(add-code
  '((function main [
      (1:string-address <- new "a")
      (2:string-address <- new "")
      (3:boolean <- string-equal 1:string-address 2:string-address)
     ])))
(run 'main)
(when (~iso memory*.3 nil)
  (prn "F - 'string-equal' compares correctly with empty strings"))

(reset)
(new-trace "string-equal-compares-length")
(add-code
  '((function main [
      (1:string-address <- new "a")
      (2:string-address <- new "ab")
      (3:boolean <- string-equal 1:string-address 2:string-address)
     ])))
(run 'main)
(when (~iso memory*.3 nil)
  (prn "F - 'string-equal' handles differing lengths"))

(reset)
(new-trace "string-equal-compares-initial-element")
(add-code
  '((function main [
      (1:string-address <- new "aa")
      (2:string-address <- new "ba")
      (3:boolean <- string-equal 1:string-address 2:string-address)
     ])))
(run 'main)
(when (~iso memory*.3 nil)
  (prn "F - 'string-equal' handles inequal final byte"))

(reset)
(new-trace "string-equal-compares-final-element")
(add-code
  '((function main [
      (1:string-address <- new "ab")
      (2:string-address <- new "aa")
      (3:boolean <- string-equal 1:string-address 2:string-address)
     ])))
(run 'main)
(when (~iso memory*.3 nil)
  (prn "F - 'string-equal' handles inequal final byte"))

(reset)
(new-trace "string-equal-reflexive")
(add-code
  '((function main [
      (1:string-address <- new "ab")
      (3:boolean <- string-equal 1:string-address 1:string-address)
     ])))
(run 'main)
(when (~iso memory*.3 t)
  (prn "F - 'string-equal' handles identical pointer"))

(reset)
(new-trace "strcat")
(add-code
  '((function main [
      (1:string-address <- new "hello,")
      (2:string-address <- new " world!")
      (3:string-address <- strcat 1:string-address 2:string-address)
     ])))
;? (= dump-trace* (obj whitelist '("run"))) ;? 1
(run 'main)
(when (~memory-contains-array memory*.3 "hello, world!")
  (prn "F - 'strcat' concatenates strings"))
;? (quit) ;? 1

(reset)
(new-trace "interpolate")
(add-code
  '((function main [
      (1:string-address <- new "hello, _!")
      (2:string-address <- new "abc")
      (3:string-address <- interpolate 1:string-address 2:string-address)
     ])))
;? (= dump-trace* (obj whitelist '("run")))
(run 'main)
(when (~memory-contains-array memory*.3 "hello, abc!")
  (prn "F - 'interpolate' splices strings"))

(reset)
(new-trace "interpolate-empty")
(add-code
  '((function main [
      (1:string-address <- new "hello!")
      (2:string-address <- new "abc")
      (3:string-address <- interpolate 1:string-address 2:string-address)
     ])))
;? (= dump-trace* (obj whitelist '("run")))
(run 'main)
(when (~memory-contains-array memory*.3 "hello!")
  (prn "F - 'interpolate' without underscore returns template"))

(reset)
(new-trace "interpolate-at-start")
(add-code
  '((function main [
      (1:string-address <- new "_, hello!")
      (2:string-address <- new "abc")
      (3:string-address <- interpolate 1:string-address 2:string-address)
     ])))
;? (= dump-trace* (obj whitelist '("run")))
(run 'main)
(when (~memory-contains-array memory*.3 "abc, hello")
  (prn "F - 'interpolate' splices strings at start"))

(reset)
(new-trace "interpolate-at-end")
(add-code
  '((function main [
      (1:string-address <- new "hello, _")
      (2:string-address <- new "abc")
      (3:string-address <- interpolate 1:string-address 2:string-address)
     ])))
;? (= dump-trace* (obj whitelist '("run")))
(run 'main)
(when (~memory-contains-array memory*.3 "hello, abc")
  (prn "F - 'interpolate' splices strings at start"))

(reset)
(new-trace "interpolate-varargs")
(add-code
  '((function main [
      (1:string-address <- new "hello, _, _, and _!")
      (2:string-address <- new "abc")
      (3:string-address <- new "def")
      (4:string-address <- new "ghi")
      (5:string-address <- interpolate 1:string-address 2:string-address 3:string-address 4:string-address)
     ])))
;? (= dump-trace* (obj whitelist '("run")))
;? (= dump-trace* (obj whitelist '("run" "array-info")))
;? (set dump-trace*)
(run 'main)
;? (quit)
;? (up i 1 (+ 1 (memory* memory*.5))
;?   (prn (memory* (+ memory*.5 i))))
(when (~memory-contains-array memory*.5 "hello, abc, def, and ghi!")
  (prn "F - 'interpolate' splices in any number of strings"))

(reset)
(new-trace "string-find-next")
(add-code
  '((function main [
      (1:string-address <- new "a/b")
      (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
     ])))
(run 'main)
(when (~is memory*.2 1)
  (prn "F - 'find-next' finds first location of a character"))

(reset)
(new-trace "string-find-next-empty")
(add-code
  '((function main [
      (1:string-address <- new "")
      (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~is memory*.2 0)
  (prn "F - 'find-next' finds first location of a character"))

(reset)
(new-trace "string-find-next-initial")
(add-code
  '((function main [
      (1:string-address <- new "/abc")
      (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
     ])))
(run 'main)
(when (~is memory*.2 0)
  (prn "F - 'find-next' handles prefix match"))

(reset)
(new-trace "string-find-next-final")
(add-code
  '((function main [
      (1:string-address <- new "abc/")
      (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
     ])))
(run 'main)
;? (prn memory*.2)
(when (~is memory*.2 3)
  (prn "F - 'find-next' handles suffix match"))

(reset)
(new-trace "string-find-next-missing")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
     ])))
(run 'main)
;? (prn memory*.2)
(when (~is memory*.2 3)
  (prn "F - 'find-next' handles no match"))

(reset)
(new-trace "string-find-next-invalid-index")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:integer <- find-next 1:string-address ((#\/ literal)) 4:literal)
     ])))
;? (= dump-trace* (obj whitelist '("run")))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
;? (prn memory*.2)
(when (~is memory*.2 4)
  (prn "F - 'find-next' skips invalid index (past end of string)"))

(reset)
(new-trace "string-find-next-first")
(add-code
  '((function main [
      (1:string-address <- new "ab/c/")
      (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
     ])))
(run 'main)
(when (~is memory*.2 2)
  (prn "F - 'find-next' finds first of multiple options"))

(reset)
(new-trace "string-find-next-second")
(add-code
  '((function main [
      (1:string-address <- new "ab/c/")
      (2:integer <- find-next 1:string-address ((#\/ literal)) 3:literal)
     ])))
(run 'main)
(when (~is memory*.2 4)
  (prn "F - 'find-next' finds second of multiple options"))

(reset)
(new-trace "match-at")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- new "ab")
      (3:boolean <- match-at 1:string-address 2:string-address 0:literal)
     ])))
(run 'main)
(when (~is memory*.3 t)
  (prn "F - 'match-at' matches substring at given index"))

(reset)
(new-trace "match-at-reflexive")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (3:boolean <- match-at 1:string-address 1:string-address 0:literal)
     ])))
(run 'main)
(when (~is memory*.3 t)
  (prn "F - 'match-at' always matches a string at itself at index 0"))

(reset)
(new-trace "match-at-outside-bounds")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- new "a")
      (3:boolean <- match-at 1:string-address 2:string-address 4:literal)
     ])))
(run 'main)
(when (~is memory*.3 nil)
  (prn "F - 'match-at' always fails to match outside the bounds of the text"))

(reset)
(new-trace "match-at-empty-pattern")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- new "")
      (3:boolean <- match-at 1:string-address 2:string-address 0:literal)
     ])))
(run 'main)
(when (~is memory*.3 t)
  (prn "F - 'match-at' always matches empty pattern"))

(reset)
(new-trace "match-at-empty-pattern-outside-bounds")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- new "")
      (3:boolean <- match-at 1:string-address 2:string-address 4:literal)
     ])))
(run 'main)
(when (~is memory*.3 nil)
  (prn "F - 'match-at' matches empty pattern -- unless index is out of bounds"))

(reset)
(new-trace "match-at-empty-text")
(add-code
  '((function main [
      (1:string-address <- new "")
      (2:string-address <- new "abc")
      (3:boolean <- match-at 1:string-address 2:string-address 0:literal)
     ])))
(run 'main)
(when (~is memory*.3 nil)
  (prn "F - 'match-at' never matches empty text"))

(reset)
(new-trace "match-at-empty-against-empty")
(add-code
  '((function main [
      (1:string-address <- new "")
      (3:boolean <- match-at 1:string-address 1:string-address 0:literal)
     ])))
(run 'main)
(when (~is memory*.3 t)
  (prn "F - 'match-at' never matches empty text -- unless pattern is also empty"))

(reset)
(new-trace "match-at-inside-bounds")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- new "bc")
      (3:boolean <- match-at 1:string-address 2:string-address 1:literal)
     ])))
(run 'main)
(when (~is memory*.3 t)
  (prn "F - 'match-at' matches inner substring"))

(reset)
(new-trace "match-at-inside-bounds-2")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- new "bc")
      (3:boolean <- match-at 1:string-address 2:string-address 0:literal)
     ])))
(run 'main)
(when (~is memory*.3 nil)
  (prn "F - 'match-at' matches inner substring - 2"))

(reset)
(new-trace "find-substring")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- new "bc")
      (3:integer <- find-substring 1:string-address 2:string-address 0:literal)
     ])))
(run 'main)
;? (prn memory*.3) ;? 1
(when (~is memory*.3 1)
  (prn "F - 'find-substring' returns index of match"))

(reset)
(new-trace "find-substring-2")
(add-code
  '((function main [
      (1:string-address <- new "abcd")
      (2:string-address <- new "bc")
      (3:integer <- find-substring 1:string-address 2:string-address 1:literal)
     ])))
(run 'main)
(when (~is memory*.3 1)
  (prn "F - 'find-substring' returns provided index if it matches"))

(reset)
(new-trace "find-substring-no-match")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- new "bd")
      (3:integer <- find-substring 1:string-address 2:string-address 0:literal)
     ])))
(run 'main)
(when (~is memory*.3 3)
  (prn "F - 'find-substring' returns out-of-bounds index on no-match"))

(reset)
(new-trace "find-substring-suffix-match")
(add-code
  '((function main [
      (1:string-address <- new "abcd")
      (2:string-address <- new "cd")
      (3:integer <- find-substring 1:string-address 2:string-address 0:literal)
     ])))
(run 'main)
(when (~is memory*.3 2)
  (prn "F - 'find-substring' returns provided index if it matches"))

(reset)
(new-trace "find-substring-suffix-match-2")
(add-code
  '((function main [
      (1:string-address <- new "abcd")
      (2:string-address <- new "cde")
      (3:integer <- find-substring 1:string-address 2:string-address 0:literal)
     ])))
(run 'main)
(when (~is memory*.3 4)
  (prn "F - 'find-substring' returns provided index if it matches"))

;? (quit) ;? 1

(reset)
(new-trace "string-split")
(add-code
  '((function main [
      (1:string-address <- new "a/b")
      (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
     ])))
;? (set dump-trace*)
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(let base memory*.2
;?   (prn base " " memory*.base)
  (when (or (~is memory*.base 2)
;?             (do1 nil prn.111)
            (~memory-contains-array (memory* (+ base 1)) "a")
;?             (do1 nil prn.111)
            (~memory-contains-array (memory* (+ base 2)) "b"))
    (prn "F - 'split' cuts string at delimiter")))

(reset)
(new-trace "string-split2")
(add-code
  '((function main [
      (1:string-address <- new "a/b/c")
      (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
     ])))
;? (set dump-trace*)
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(let base memory*.2
;?   (prn base " " memory*.base)
  (when (or (~is memory*.base 3)
;?             (do1 nil prn.111)
            (~memory-contains-array (memory* (+ base 1)) "a")
;?             (do1 nil prn.111)
            (~memory-contains-array (memory* (+ base 2)) "b")
;?             (do1 nil prn.111)
            (~memory-contains-array (memory* (+ base 3)) "c"))
    (prn "F - 'split' cuts string at two delimiters")))

(reset)
(new-trace "string-split-missing")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(let base memory*.2
  (when (or (~is memory*.base 1)
            (~memory-contains-array (memory* (+ base 1)) "abc"))
    (prn "F - 'split' handles missing delimiter")))

(reset)
(new-trace "string-split-empty")
(add-code
  '((function main [
      (1:string-address <- new "")
      (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
     ])))
;? (= dump-trace* (obj whitelist '("run")))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(let base memory*.2
;?   (prn base " " memory*.base)
  (when (~is memory*.base 0)
    (prn "F - 'split' handles empty string")))

(reset)
(new-trace "string-split-empty-piece")
(add-code
  '((function main [
      (1:string-address <- new "a/b//c")
      (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(let base memory*.2
  (when (or (~is memory*.base 4)
            (~memory-contains-array (memory* (+ base 1)) "a")
            (~memory-contains-array (memory* (+ base 2)) "b")
            (~memory-contains-array (memory* (+ base 3)) "")
            (~memory-contains-array (memory* (+ base 4)) "c"))
    (prn "F - 'split' cuts string at two delimiters")))
;? (quit) ;? 1

(reset)
(new-trace "string-split-first")
(add-code
  '((function main [
      (1:string-address <- new "a/b")
      (2:string-address 3:string-address <- split-first 1:string-address ((#\/ literal)))
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (or (~memory-contains-array memory*.2 "a")
          (~memory-contains-array memory*.3 "b"))
  (prn "F - 'split-first' cuts string at first occurrence of delimiter"))

(reset)
(new-trace "string-split-first-at-substring")
(add-code
  '((function main [
      (1:string-address <- new "a//b")
      (2:string-address <- new "//")
      (3:string-address 4:string-address <- split-first-at-substring 1:string-address 2:string-address)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
;? (prn int-canon.memory*) ;? 1
(when (or (~memory-contains-array memory*.3 "a")
          (~memory-contains-array memory*.4 "b"))
  (prn "F - 'split-first-at-substring' is like split-first but with a string delimiter"))

(reset)
(new-trace "string-copy")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- string-copy 1:string-address 1:literal 3:literal)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~memory-contains-array memory*.2 "bc")
  (prn "F - 'string-copy' returns a copy of a substring"))

(reset)
(new-trace "string-copy-out-of-bounds")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- string-copy 1:string-address 2:literal 4:literal)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~memory-contains-array memory*.2 "c")
  (prn "F - 'string-copy' stops at bounds"))

(reset)
(new-trace "string-copy-out-of-bounds-2")
(add-code
  '((function main [
      (1:string-address <- new "abc")
      (2:string-address <- string-copy 1:string-address 3:literal 3:literal)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
(when (~memory-contains-array memory*.2 "")
  (prn "F - 'string-copy' returns empty string when range is out of bounds"))

(reset)
(new-trace "min")
(add-code
  '((function main [
      (1:integer <- min 3:literal 4:literal)
     ])))
(run 'main)
(each routine completed-routines*
  (aif rep.routine!error (prn "error - " it)))
;? (prn int-canon.memory*) ;? 1
(when (~is memory*.1 3)
  (prn "F - 'min' returns smaller of two numbers"))

;? (quit) ;? 2

(reset)
(new-trace "integer-to-decimal-string")
(add-code
  '((function main [
      (1:string-address/raw <- integer-to-decimal-string 34:literal)
    ])))
;? (set dump-trace*)
;? (= dump-trace* (obj whitelist '("run")))
(run 'main)
(let base memory*.1
  (when (~memory-contains-array base "34")
    (prn "F - converting integer to decimal string")))

(reset)
(new-trace "integer-to-decimal-string-zero")
(add-code
  '((function main [
      (1:string-address/raw <- integer-to-decimal-string 0:literal)
    ])))
(run 'main)
(let base memory*.1
  (when (~memory-contains-array base "0")
    (prn "F - converting zero to decimal string")))

(reset)
(new-trace "integer-to-decimal-string-negative")
(add-code
  '((function main [
      (1:string-address/raw <- integer-to-decimal-string -237:literal)
    ])))
(run 'main)
(let base memory*.1
  (when (~memory-contains-array base "-237")
    (prn "F - converting negative integer to decimal string")))

; fake screen for tests; prints go to a string
(reset)
(new-trace "fake-screen-empty")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 30:literal/capacity)
      (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
      (5:string-address/raw <- get screen:terminal-address/deref data:offset)
     ])))
(run 'main)
(each routine completed-routines*
  (awhen rep.routine!error
    (prn "error - " it)))
(when (~memory-contains-array memory*.5
          (+ "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "))
  (prn "F - fake screen starts out with all spaces"))

; fake keyboard for tests; must initialize keys in advance
(reset)
(new-trace "fake-keyboard")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 30:literal)
      (s:string-address <- new "foo")
      (x:keyboard-address <- init-keyboard s:string-address)
      (1:character-address/raw <- read-key x:keyboard-address)
     ])))
(run 'main)
(when (~is memory*.1 #\f)
  (prn "F - 'read-key' reads character from provided 'fake keyboard' string"))

; fake keyboard for tests; must initialize keys in advance
(reset)
(new-trace "fake-keyboard2")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 30:literal)
      (s:string-address <- new "foo")
      (x:keyboard-address <- init-keyboard s:string-address)
      (1:character-address/raw <- read-key x:keyboard-address)
      (1:character-address/raw <- read-key x:keyboard-address)
     ])))
(run 'main)
(when (~is memory*.1 #\o)
  (prn "F - 'read-key' advances cursor in provided string"))

; to receive input line by line, run send-keys-buffered-to-stdin
(reset)
(new-trace "buffer-stdin-until-newline")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 30:literal)
      (s:string-address <- new "foo")
      (k:keyboard-address <- init-keyboard s:string-address)
      (stdin:channel-address <- init-channel 1:literal)
      (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
      (buffered-stdin:channel-address <- init-channel 1:literal)
      (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
      (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
      (5:string-address/raw <- get screen:terminal-address/deref data:offset)
      (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
      (sleep until-routine-done:literal r:integer/routine)
    ])))
;? (set dump-trace*) ;? 3
;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 0
(run 'main)
;? (prn int-canon.memory*) ;? 0
(each routine completed-routines*
  (awhen rep.routine!error
    (prn "error - " it)))
(when (~memory-contains-array memory*.5
          (+ "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "))
  (prn "F - 'buffer-lines' prints nothing until newline is encountered"))
;? (quit) ;? 3

(reset)
(new-trace "print-buffered-contents-on-newline")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 30:literal)
      (s:string-address <- new "foo\nline2")
      (k:keyboard-address <- init-keyboard s:string-address)
      (stdin:channel-address <- init-channel 1:literal)
      (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
      (buffered-stdin:channel-address <- init-channel 1:literal)
      (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
      (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
      (5:string-address/raw <- get screen:terminal-address/deref data:offset)
      (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
      (sleep until-routine-done:literal r:integer/routine)
    ])))
;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 1
(run 'main)
(each routine completed-routines*
  (awhen rep.routine!error
    (prn "error - " it)))
(when (~memory-contains-array memory*.5
          (+ "foo\n                "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "))
  (prn "F - 'buffer-lines' prints lines to screen"))

(reset)
(new-trace "print-buffered-contents-right-at-newline")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 30:literal)
      (s:string-address <- new "foo\n")
      (k:keyboard-address <- init-keyboard s:string-address)
      (stdin:channel-address <- init-channel 1:literal)
      (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
      (buffered-stdin:channel-address <- init-channel 1:literal)
      (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
      (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
      (5:string-address/raw <- get screen:terminal-address/deref data:offset)
      (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
      (sleep until-routine-done:literal r:integer/routine)
      ; hack: give helper some time to finish printing
      (sleep for-some-cycles:literal 500:literal)
    ])))
;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 1
(run 'main)
(each routine completed-routines*
  (awhen rep.routine!error
    (prn "error - " it)))
(when (~memory-contains-array memory*.5
          (+ "foo\n                "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "))
  (prn "F - 'buffer-lines' prints lines to screen immediately on newline"))

(reset)
(new-trace "buffered-contents-skip-backspace")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 30:literal)
      (s:string-address <- new "fooa\b\nline2")
      (k:keyboard-address <- init-keyboard s:string-address)
      (stdin:channel-address <- init-channel 1:literal)
      (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
      (buffered-stdin:channel-address <- init-channel 1:literal)
      (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
      (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
      (5:string-address/raw <- get screen:terminal-address/deref data:offset)
      (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
      (sleep until-routine-done:literal r:integer/routine)
    ])))
;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 1
(run 'main)
(each routine completed-routines*
  (awhen rep.routine!error
    (prn "error - " it)))
(when (~memory-contains-array memory*.5
          (+ "foo\n                "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "))
  (prn "F - 'buffer-lines' handles backspace"))

(reset)
(new-trace "buffered-contents-ignore-excess-backspace")
(add-code
  '((function main [
      (default-space:space-address <- new space:literal 30:literal)
      (s:string-address <- new "a\b\bfoo\n")
      (k:keyboard-address <- init-keyboard s:string-address)
      (stdin:channel-address <- init-channel 1:literal)
      (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
      (buffered-stdin:channel-address <- init-channel 1:literal)
      (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
      (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
      (5:string-address/raw <- get screen:terminal-address/deref data:offset)
      (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
      (sleep until-routine-done:literal r:integer/routine)
      ; hack: give helper some time to finish printing
      (sleep for-some-cycles:literal 500:literal)
    ])))
;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 1
(run 'main)
(each routine completed-routines*
  (awhen rep.routine!error
    (prn "error - " it)))
;? (prn memory*.5) ;? 1
(when (~memory-contains-array memory*.5
          (+ "foo\n                "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "
             "                    "))
  (prn "F - 'buffer-lines' ignores backspace when there's nothing to backspace over"))

)  ; section 100

(reset)
(new-trace "parse-and-record")
(add-code
  '((and-record foo [
      x:string
      y:integer
      z:boolean
     ])))
(when (~iso type*!foo (obj size 3  and-record t  elems '((string) (integer) (boolean))  fields '(x y z)))
  (prn "F - 'add-code' can add new and-records"))

;; unit tests for various helpers

; tokenize-args
(prn "== tokenize-args")
(assert:iso '((a b) (c d))
            (tokenize-arg 'a:b/c:d))
; numbers are not symbols
(assert:iso '((a b) (1 d))
            (tokenize-arg 'a:b/1:d))
; special symbols are skipped
(assert:iso '<-
            (tokenize-arg '<-))
(assert:iso '_
            (tokenize-arg '_))

; idempotent
(assert:iso (tokenize-arg:tokenize-arg 'a:b/c:d)
            (tokenize-arg              'a:b/c:d))

; support labels
(assert:iso '((((default-space space-address)) <- ((new)) ((space literal)) ((30 literal)))
              foo)
            (tokenize-args
              '((default-space:space-address <- new space:literal 30:literal)
                foo)))

; support braces
(assert:iso '((((default-space space-address)) <- ((new)) ((space literal)) ((30 literal)))
              foo
              { begin
                bar
                (((a b)) <- ((op)) ((c d)) ((e f)))
              })
            (tokenize-args
              '((default-space:space-address <- new space:literal 30:literal)
                foo
                { begin
                  bar
                  (a:b <- op c:d e:f)
                })))

; space
(prn "== space")
(reset)
(when (~iso 0 (space '((4 integer))))
  (prn "F - 'space' is 0 by default"))
(when (~iso 1 (space '((4 integer) (space 1))))
  (prn "F - 'space' picks up space when available"))
(when (~iso 'global (space '((4 integer) (space global))))
  (prn "F - 'space' understands routine-global space"))

; absolutize
(prn "== absolutize")
(reset)
(when (~iso '((4 integer)) (absolutize '((4 integer))))
  (prn "F - 'absolutize' works without routine"))
(= routine* make-routine!foo)
(when (~iso '((4 integer)) (absolutize '((4 integer))))
  (prn "F - 'absolutize' works without default-space"))
(= rep.routine*!call-stack.0!default-space 10)
(= memory*.10 5)  ; bounds check for default-space
(when (~iso '((15 integer) (raw))
            (absolutize '((4 integer))))
  (prn "F - 'absolutize' works with default-space"))
(absolutize '((5 integer)))
(when (~posmatch "no room" rep.routine*!error)
  (prn "F - 'absolutize' checks against default-space bounds"))
(when (~iso '((_ integer)) (absolutize '((_ integer))))
  (prn "F - 'absolutize' passes dummy args right through"))
(when (~iso '((default-space integer)) (absolutize '((default-space integer))))
  (prn "F - 'absolutize' passes 'default-space' right through"))

(= memory*.20 5)  ; pretend array
(= rep.routine*!globals 20)  ; provide it to routine global
(when (~iso '((22 integer) (raw))
            (absolutize '((1 integer) (space global))))
  (prn "F - 'absolutize' handles variables in the global space"))

; deref
(prn "== deref")
(reset)
(= memory*.3 4)
(when (~iso '((4 integer))
            (deref '((3 integer-address)
                     (deref))))
  (prn "F - 'deref' handles simple addresses"))
(when (~iso '((4 integer) (deref))
            (deref '((3 integer-address)
                     (deref)
                     (deref))))
  (prn "F - 'deref' deletes just one deref"))
(= memory*.4 5)
(when (~iso '((5 integer))
            (deref:deref '((3 integer-address-address)
                           (deref)
                           (deref))))
  (prn "F - 'deref' can be chained"))
(when (~iso '((5 integer) (foo))
            (deref:deref '((3 integer-address-address)
                           (deref)
                           (foo)
                           (deref))))
  (prn "F - 'deref' skips junk"))

; addr
(prn "== addr")
(reset)
(= routine* nil)
;? (prn 111)
(when (~is 4 (addr '((4 integer))))
  (prn "F - directly addressed operands are their own address"))
;? (quit)
(when (~is 4 (addr '((4 integer-address))))
  (prn "F - directly addressed operands are their own address - 2"))
(when (~is 4 (addr '((4 literal))))
  (prn "F - 'addr' doesn't understand literals"))
;? (prn 201)
(= memory*.4 23)
;? (prn 202)
(when (~is 23 (addr '((4 integer-address) (deref))))
  (prn "F - 'addr' works with indirectly-addressed 'deref'"))
;? (quit)
(= memory*.3 4)
(when (~is 23 (addr '((3 integer-address-address) (deref) (deref))))
  (prn "F - 'addr' works with multiple 'deref'"))

(= routine* make-routine!foo)
(when (~is 4 (addr '((4 integer))))
  (prn "F - directly addressed operands are their own address inside routines"))
(when (~is 4 (addr '((4 integer-address))))
  (prn "F - directly addressed operands are their own address inside routines - 2"))
(when (~is 4 (addr '((4 literal))))
  (prn "F - 'addr' doesn't understand literals inside routines"))
(= memory*.4 23)
(when (~is 23 (addr '((4 integer-address) (deref))))
  (prn "F - 'addr' works with indirectly-addressed 'deref' inside routines"))

;? (prn 301)
(= rep.routine*!call-stack.0!default-space 10)
;? (prn 302)
(= memory*.10 5)  ; bounds check for default-space
;? (prn 303)
(when (~is 15 (addr '((4 integer))))
  (prn "F - directly addressed operands in routines add default-space"))
;? (quit)
(when (~is 15 (addr '((4 integer-address))))
  (prn "F - directly addressed operands in routines add default-space - 2"))
(when (~is 15 (addr '((4 literal))))
  (prn "F - 'addr' doesn't understand literals"))
(= memory*.15 23)
(when (~is 23 (addr '((4 integer-address) (deref))))
  (prn "F - 'addr' adds default-space before 'deref', not after"))
;? (quit)

; array-len
(prn "== array-len")
(reset)
(= memory*.35 4)
(when (~is 4 (array-len '((35 integer-boolean-pair-array))))
  (prn "F - 'array-len'"))
(= memory*.34 35)
(when (~is 4 (array-len '((34 integer-boolean-pair-array-address) (deref))))
  (prn "F - 'array-len'"))
;? (quit)

; sizeof
(prn "== sizeof")
(reset)
;? (set dump-trace*)
;? (prn 401)
(when (~is 1 (sizeof '((_ integer))))
  (prn "F - 'sizeof' works on primitives"))
(when (~is 1 (sizeof '((_ integer-address))))
  (prn "F - 'sizeof' works on addresses"))
(when (~is 2 (sizeof '((_ integer-boolean-pair))))
  (prn "F - 'sizeof' works on and-records"))
(when (~is 3 (sizeof '((_ integer-point-pair))))
  (prn "F - 'sizeof' works on and-records with and-record fields"))

;? (prn 410)
(when (~is 1 (sizeof '((34 integer))))
  (prn "F - 'sizeof' works on primitive operands"))
(when (~is 1 (sizeof '((34 integer-address))))
  (prn "F - 'sizeof' works on address operands"))
(when (~is 2 (sizeof '((34 integer-boolean-pair))))
  (prn "F - 'sizeof' works on and-record operands"))
(when (~is 3 (sizeof '((34 integer-point-pair))))
  (prn "F - 'sizeof' works on and-record operands with and-record fields"))
(when (~is 2 (sizeof '((34 integer-boolean-pair-address) (deref))))
  (prn "F - 'sizeof' works on pointers to and-records"))
(= memory*.35 4)  ; size of array
(= memory*.34 35)
;? (= dump-trace* (obj whitelist '("sizeof" "array-len")))
(when (~is 9 (sizeof '((34 integer-boolean-pair-array-address) (deref))))
  (prn "F - 'sizeof' works on pointers to arrays"))
;? (quit)

;? (prn 420)
(= memory*.4 23)
(when (~is 24 (sizeof '((4 integer-array))))
  (prn "F - 'sizeof' reads array lengths from memory"))
(= memory*.3 4)
(when (~is 24 (sizeof '((3 integer-array-address) (deref))))
  (prn "F - 'sizeof' handles pointers to arrays"))
(= memory*.15 34)
(= routine* make-routine!foo)
(when (~is 24 (sizeof '((4 integer-array))))
  (prn "F - 'sizeof' reads array lengths from memory inside routines"))
(= rep.routine*!call-stack.0!default-space 10)
(= memory*.10 5)  ; bounds check for default-space
(when (~is 35 (sizeof '((4 integer-array))))
  (prn "F - 'sizeof' reads array lengths from memory using default-space"))
(= memory*.35 4)  ; size of array
(= memory*.15 35)
;? (= dump-trace* (obj whitelist '("sizeof")))
(aif rep.routine*!error (prn "error - " it))
(when (~is 9 (sizeof '((4 integer-boolean-pair-array-address) (deref))))
  (prn "F - 'sizeof' works on pointers to arrays using default-space"))
;? (quit)

; m
(prn "== m")
(reset)
(when (~is 4 (m '((4 literal))))
  (prn "F - 'm' avoids reading memory for literals"))
(when (~is 4 (m '((4 offset))))
  (prn "F - 'm' avoids reading memory for offsets"))
(= memory*.4 34)
(when (~is 34 (m '((4 integer))))
  (prn "F - 'm' reads memory for simple types"))
(= memory*.3 4)
(when (~is 34 (m '((3 integer-address) (deref))))
  (prn "F - 'm' redirects addresses"))
(= memory*.2 3)
(when (~is 34 (m '((2 integer-address-address) (deref) (deref))))
  (prn "F - 'm' multiply redirects addresses"))
(when (~iso (annotate 'record '(34 nil)) (m '((4 integer-boolean-pair))))
  (prn "F - 'm' supports compound records"))
(= memory*.5 35)
(= memory*.6 36)
(when (~iso (annotate 'record '(34 35 36)) (m '((4 integer-point-pair))))
  (prn "F - 'm' supports records with compound fields"))
(when (~iso (annotate 'record '(34 35 36)) (m '((3 integer-point-pair-address) (deref))))
  (prn "F - 'm' supports indirect access to records"))
(= memory*.4 2)
(when (~iso (annotate 'record '(2 35 36)) (m '((4 integer-array))))
  (prn "F - 'm' supports access to arrays"))
(when (~iso (annotate 'record '(2 35 36)) (m '((3 integer-array-address) (deref))))
  (prn "F - 'm' supports indirect access to arrays"))

(= routine* make-routine!foo)
(= memory*.10 5)  ; fake array
(= memory*.12 34)
(= rep.routine*!globals 10)
(when (~iso 34 (m '((1 integer) (space global))))
  (prn "F - 'm' supports access to per-routine globals"))

; setm
(prn "== setm")
(reset)
(setm '((4 integer)) 34)
(when (~is 34 memory*.4)
  (prn "F - 'setm' writes primitives to memory"))
(setm '((3 integer-address)) 4)
(when (~is 4 memory*.3)
  (prn "F - 'setm' writes addresses to memory"))
(setm '((3 integer-address) (deref)) 35)
(when (~is 35 memory*.4)
  (prn "F - 'setm' redirects writes"))
(= memory*.2 3)
(setm '((2 integer-address-address) (deref) (deref)) 36)
(when (~is 36 memory*.4)
  (prn "F - 'setm' multiply redirects writes"))
;? (prn 505)
(setm '((4 integer-integer-pair)) (annotate 'record '(23 24)))
(when (~memory-contains 4 '(23 24))
  (prn "F - 'setm' writes compound records"))
(assert (is memory*.7 nil))
;? (prn 506)
(setm '((7 integer-point-pair)) (annotate 'record '(23 24 25)))
(when (~memory-contains 7 '(23 24 25))
  (prn "F - 'setm' writes records with compound fields"))
(= routine* make-routine!foo)
(setm '((4 integer-point-pair)) (annotate 'record '(33 34)))
(when (~posmatch "incorrect size" rep.routine*!error)
  (prn "F - 'setm' checks size of target"))
(wipe routine*)
(setm '((3 integer-point-pair-address) (deref)) (annotate 'record '(43 44 45)))
(when (~memory-contains 4 '(43 44 45))
  (prn "F - 'setm' supports indirect writes to records"))
(setm '((2 integer-point-pair-address-address) (deref) (deref)) (annotate 'record '(53 54 55)))
(when (~memory-contains 4 '(53 54 55))
  (prn "F - 'setm' supports multiply indirect writes to records"))
(setm '((4 integer-array)) (annotate 'record '(2 31 32)))
(when (~memory-contains 4 '(2 31 32))
  (prn "F - 'setm' writes arrays"))
(setm '((3 integer-array-address) (deref)) (annotate 'record '(2 41 42)))
(when (~memory-contains 4 '(2 41 42))
  (prn "F - 'setm' supports indirect writes to arrays"))
(= routine* make-routine!foo)
(setm '((4 integer-array)) (annotate 'record '(2 31 32 33)))
(when (~posmatch "invalid array" rep.routine*!error)
  (prn "F - 'setm' checks that array written is well-formed"))
(= routine* make-routine!foo)
;? (prn 111)
;? (= dump-trace* (obj whitelist '("sizeof" "mem")))
(setm '((4 integer-boolean-pair-array)) (annotate 'record '(2 31 nil 32 nil 33)))
(when (~posmatch "invalid array" rep.routine*!error)
  (prn "F - 'setm' checks that array of records is well-formed"))
(= routine* make-routine!foo)
;? (prn 222)
(setm '((4 integer-boolean-pair-array)) (annotate 'record '(2 31 nil 32 nil)))
(when (posmatch "invalid array" rep.routine*!error)
  (prn "F - 'setm' checks that array of records is well-formed - 2"))
(wipe routine*)

(reset)  ; end file with this to persist the trace for the final test